3 #include <sys/socket.h>
9 #include <sys/select.h>
13 #define CONNECT_TIMEOUT 10*1000
19 struct sockaddr_un addr;
23 int Net_int_Open(const char *Path);
26 tIf gaInterfaces[MAX_IFS];
29 int Net_Open(int IfNum, const char *Path)
31 if( IfNum >= MAX_IFS )
34 if(gaInterfaces[IfNum].FD != 0) return 1;
35 gaInterfaces[IfNum].addrlen = sizeof(gaInterfaces[IfNum].addr);
36 gaInterfaces[IfNum].FD = Net_int_Open(Path);
40 int Net_int_Open(const char *Path)
42 int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
43 struct sockaddr_un sa = {AF_UNIX, ""};
44 strcpy(sa.sun_path, Path);
46 if( bind(fd, (struct sockaddr*)&sa, sizeof(sa)) ) {
47 perror("NetTest_OpenUnix - bind");
55 void Net_Close(int IfNum)
57 assert(IfNum < MAX_IFS);
58 close(gaInterfaces[IfNum].FD);
59 gaInterfaces[IfNum].FD = 0;
62 bool WaitOnFD(int FD, bool Write, unsigned int Timeout)
67 struct timeval timeout;
68 timeout.tv_sec = Timeout/1000;
69 timeout.tv_usec = (Timeout%1000) * 1000;
72 select(FD+1, NULL, &fds, NULL, &timeout);
74 select(FD+1, &fds, NULL, NULL, &timeout);
75 return FD_ISSET(FD, &fds);
78 bool Net_int_EnsureConnected(int IfNum)
80 assert(IfNum < MAX_IFS);
82 if( gaInterface_Clients[IfNum] == 0 )
84 //if( !WaitOnFD(gaInterface_Servers[IfNum], CONNECT_TIMEOUT) )
86 // fprintf(stderr, "ERROR: Client has not connected");
89 gaInterface_Clients[IfNum] = accept(gaInterface_Servers[IfNum], NULL, NULL);
90 if( gaInterface_Clients[IfNum] < 0 ) {
91 perror("Net_int_EnsureConnected - accept");
99 size_t Net_Receive(int IfNum, size_t MaxLen, void *DestBuf, unsigned int Timeout)
101 assert(IfNum < MAX_IFS);
102 tIf *If = &gaInterfaces[IfNum];
104 if( Net_int_EnsureConnected(IfNum) && WaitOnFD(If->FD, false, Timeout) )
106 return recvfrom(If->FD, DestBuf, MaxLen, 0, &If->addr, &If->addrlen);
111 void Net_Send(int IfNum, size_t Length, const void *Buf)
113 assert(IfNum < MAX_IFS);
114 tIf *If = &gaInterfaces[IfNum];
116 if( !WaitOnFD(If->FD, true, CONNECT_TIMEOUT) )
118 int rv = sendto(If->FD, Buf, Length, 0, &If->addr, If->addrlen);
120 perror("Net_Send - send");