]> jspc29.x-matter.uni-frankfurt.de Git - trbnettools.git/commitdiff
UDP receiver timeout now handled by select call, MT, LM
authorhadaq <hadaq>
Wed, 18 Jul 2012 20:45:35 +0000 (20:45 +0000)
committerhadaq <hadaq>
Wed, 18 Jul 2012 20:45:35 +0000 (20:45 +0000)
libtrbnet/trbnet.c

index d2f8879124f62cd23a2ad72bfbc2941c1e77f3d3..0c6514570b0f5206bcf44066db1ce137c82aec4d 100644 (file)
@@ -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 <stdlib.h>
@@ -44,12 +44,18 @@ int pexor_dma = 1;
 #include <sys/wait.h>
 #include <netinet/in.h>
 #include <netdb.h>
+#include <sys/select.h>
 
 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;