From c6fe2658ce1ad8b3b3f7c0efb62eb2a8d1596513 Mon Sep 17 00:00:00 2001 From: hadaq Date: Wed, 18 Jul 2012 20:45:35 +0000 Subject: [PATCH] UDP receiver timeout now handled by select call, MT, LM --- libtrbnet/trbnet.c | 59 ++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 26 deletions(-) diff --git a/libtrbnet/trbnet.c b/libtrbnet/trbnet.c index d2f8879..0c65145 100644 --- a/libtrbnet/trbnet.c +++ b/libtrbnet/trbnet.c @@ -1,11 +1,11 @@ #ifdef ETRAX -const char trbnet_version[] = "$Revision: 4.31 $ Local Etrax"; +const char trbnet_version[] = "$Revision: 4.32 $ Local Etrax"; #elif defined PEXOR -const char trbnet_version[] = "$Revision: 4.31 $ Local Pexor"; +const char trbnet_version[] = "$Revision: 4.32 $ Local Pexor"; #elif defined TRB3 -const char trbnet_version[] = "$Revision: 4.31 $ Local TRB3"; +const char trbnet_version[] = "$Revision: 4.32 $ Local TRB3"; #else -const char trbnet_version[] = "$Revision: 4.31 $ UNKNOWN, i.e. ERROR"; +const char trbnet_version[] = "$Revision: 4.32 $ UNKNOWN, i.e. ERROR"; #endif #include @@ -44,12 +44,18 @@ int pexor_dma = 1; #include #include #include +#include static uint16_t udpBuffer[32768]; /* One UDP Package */ static uint16_t dataBuffer[4096 * 1024]; static unsigned int dataBufferSize = 0; /* Size of dataBuffer in 16Bit words */ static int trb3_sockfd = -1; static struct sockaddr_in trb3_addr; +static fd_set fds; /* used by select call */ +static const struct timespec tv = { + .tv_sec = 3, /* UDP timeout for receive call */ + .tv_nsec = 0 +}; static uint16_t trb3_port = 25000; uint16_t sender_address = 0x5555; @@ -384,6 +390,7 @@ static int getUDPPackage() { static struct sockaddr_in in_addr; static socklen_t in_addr_len; + int status; int i; @@ -391,19 +398,32 @@ static int getUDPPackage() if (trb_debug > 2) { fprintf(stderr, "Wait UDP Data\n"); } + + /* Wait for data ready */ + status = pselect(trb3_sockfd, &fds, NULL, NULL, &tv, NULL); + if (status == -1) { + trb_errno = TRB_TRB3_SOCKET_ERROR; + return -1; + } + if (status == 0) { + trb_errno = TRB_TRB3_SOCKET_TIMEOUT; + return -1; + } + + /* Read Data */ in_addr_len = sizeof(struct sockaddr_in); status = recvfrom(trb3_sockfd, (void*)dataBuffer, 65536, - 0, + MSG_DONTWAIT, (struct sockaddr*)&in_addr, &in_addr_len); + if (status == -1) { - trb_errno = errno == EAGAIN - ? TRB_TRB3_SOCKET_TIMEOUT - : TRB_TRB3_SOCKET_ERROR; + trb_errno = TRB_TRB3_SOCKET_ERROR; return -1; - } + } + dataBufferSize = status / 2; if (trb_debug > 2) { fprintf(stderr, "udp received: %d\n", status); @@ -1582,7 +1602,6 @@ int init_ports() { char* trb3_name = NULL; struct hostent* host = NULL; - struct timeval tv; trb_errno = TRB_NONE; @@ -1614,22 +1633,10 @@ int init_ports() trb3_addr.sin_addr = *((struct in_addr*)host->h_addr); memset(&(trb3_addr.sin_zero), 0, 8); - /* Set Socket Timepout */ - tv.tv_sec = 3; /* 3 Secs Timeout */ - tv.tv_usec = 0; /* do not init this, it can cause strange errors */ - if (setsockopt(trb3_sockfd, - SOL_SOCKET, - SO_RCVTIMEO, - (char *)&tv, - sizeof(struct timeval)) == -1) { - trb_errno = TRB_TRB3_SOCKET_ERROR; - return -1; - } - - if (trb_debug > 0) { - fprintf(stderr, "init_ports: TRB3-Socket in opened = %d\n", trb3_sockfd); - } - + /* Set Socket Timeout */ + FD_ZERO(&fds); + FD_SET(trb3_sockfd, &fds); + /* Init semaphore and signal handling */ if (init_semaphore() == -1) return -1; -- 2.43.0