#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>
#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;
{
static struct sockaddr_in in_addr;
static socklen_t in_addr_len;
+
int status;
int i;
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);
{
char* trb3_name = NULL;
struct hostent* host = NULL;
- struct timeval tv;
trb_errno = TRB_NONE;
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;