Added reload of levels on F7 (Update levelpack) to ease the test of changes.
[enigmagame.git] / lib-src / enet / unix.c
blob702b814d613482cb5082c7b6b49dfe4519a6bdc4
1 /**
2 @file unix.c
3 @brief ENet Unix system specific functions
4 */
5 #ifndef WIN32
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <sys/ioctl.h>
10 #include <sys/time.h>
11 #include <sys/param.h>
12 #include <arpa/inet.h>
13 #include <netdb.h>
14 #include <unistd.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <time.h>
19 #define ENET_BUILDING_LIB 1
20 #include "enet/enet.h"
22 #ifdef HAS_FCNTL
23 #include <fcntl.h>
24 #endif
26 #ifdef __APPLE__
27 #undef HAS_POLL
28 #endif
30 #ifdef HAS_POLL
31 #include <sys/poll.h>
32 #endif
34 #ifndef HAS_SOCKLEN_T
35 typedef int socklen_t;
36 #endif
38 #ifndef MSG_NOSIGNAL
39 #define MSG_NOSIGNAL 0
40 #endif
42 static enet_uint32 timeBase = 0;
44 int
45 enet_initialize (void)
47 return 0;
50 void
51 enet_deinitialize (void)
55 enet_uint32
56 enet_time_get (void)
58 struct timeval timeVal;
60 gettimeofday (& timeVal, NULL);
62 return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase;
65 void
66 enet_time_set (enet_uint32 newTimeBase)
68 struct timeval timeVal;
70 gettimeofday (& timeVal, NULL);
72 timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
75 int
76 enet_address_set_host (ENetAddress * address, const char * name)
78 struct hostent * hostEntry = NULL;
79 #ifdef HAS_GETHOSTBYNAME_R
80 struct hostent hostData;
81 char buffer [2048];
82 int errnum;
84 #if defined(linux) || defined(__GLIBC__) || defined(__GNU__) || (defined(__DragonFly__) && __DragonFly_version >= 200202)
85 gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
86 #else
87 hostEntry = gethostbyname_r (name, & hostData, buffer, sizeof (buffer), & errnum);
88 #endif
89 #else
90 hostEntry = gethostbyname (name);
91 #endif
93 if (hostEntry == NULL ||
94 hostEntry -> h_addrtype != AF_INET)
96 #ifdef HAS_INET_PTON
97 if (! inet_pton (AF_INET, name, & address -> host))
98 #else
99 if (! inet_aton (name, (struct in_addr *) & address -> host))
100 #endif
101 return -1;
102 return 0;
105 address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
107 return 0;
111 enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
113 struct in_addr in;
114 struct hostent * hostEntry = NULL;
115 #ifdef HAS_GETHOSTBYADDR_R
116 struct hostent hostData;
117 char buffer [2048];
118 int errnum;
120 in.s_addr = address -> host;
122 #if defined(linux) || defined(__GLIBC__) || defined(__GNU__) || (defined(__DragonFly__) && __DragonFly_version >= 200202)
123 gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & hostEntry, & errnum);
124 #else
125 hostEntry = gethostbyaddr_r ((char *) & in, sizeof (struct in_addr), AF_INET, & hostData, buffer, sizeof (buffer), & errnum);
126 #endif
127 #else
128 in.s_addr = address -> host;
130 hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
131 #endif
133 if (hostEntry == NULL)
135 #ifdef HAS_INET_NTOP
136 if (inet_ntop (AF_INET, & address -> host, name, nameLength) == NULL)
137 #else
138 char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
139 if (addr != NULL)
140 strncpy (name, addr, nameLength);
141 else
142 #endif
143 return -1;
144 return 0;
147 strncpy (name, hostEntry -> h_name, nameLength);
149 return 0;
152 ENetSocket
153 enet_socket_create (ENetSocketType type, const ENetAddress * address)
155 ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
156 int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE,
157 allowBroadcasting = 1;
158 #ifndef HAS_FCNTL
159 int nonBlocking = 1;
160 #endif
161 struct sockaddr_in sin;
163 if (newSocket == ENET_SOCKET_NULL)
164 return ENET_SOCKET_NULL;
166 if (type == ENET_SOCKET_TYPE_DATAGRAM)
168 #ifdef HAS_FCNTL
169 fcntl (newSocket, F_SETFL, O_NONBLOCK | fcntl (newSocket, F_GETFL));
170 #else
171 ioctl (newSocket, FIONBIO, & nonBlocking);
172 #endif
174 setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) & receiveBufferSize, sizeof (int));
175 setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) & allowBroadcasting, sizeof (int));
178 if (address == NULL)
179 return newSocket;
181 memset (& sin, 0, sizeof (struct sockaddr_in));
183 sin.sin_family = AF_INET;
184 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
185 sin.sin_addr.s_addr = address -> host;
187 if (bind (newSocket,
188 (struct sockaddr *) & sin,
189 sizeof (struct sockaddr_in)) == -1 ||
190 (type == ENET_SOCKET_TYPE_STREAM &&
191 listen (newSocket, SOMAXCONN) == -1))
193 close (newSocket);
195 return ENET_SOCKET_NULL;
198 return newSocket;
202 enet_socket_connect (ENetSocket socket, const ENetAddress * address)
204 struct sockaddr_in sin;
206 memset (& sin, 0, sizeof (struct sockaddr_in));
208 sin.sin_family = AF_INET;
209 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
210 sin.sin_addr.s_addr = address -> host;
212 return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
215 ENetSocket
216 enet_socket_accept (ENetSocket socket, ENetAddress * address)
218 int result;
219 struct sockaddr_in sin;
220 socklen_t sinLength = sizeof (struct sockaddr_in);
222 result = accept (socket,
223 address != NULL ? (struct sockaddr *) & sin : NULL,
224 address != NULL ? & sinLength : NULL);
226 if (result == -1)
227 return ENET_SOCKET_NULL;
229 if (address != NULL)
231 address -> host = (enet_uint32) sin.sin_addr.s_addr;
232 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
235 return result;
238 void
239 enet_socket_destroy (ENetSocket socket)
241 close (socket);
245 enet_socket_send (ENetSocket socket,
246 const ENetAddress * address,
247 const ENetBuffer * buffers,
248 size_t bufferCount)
250 struct msghdr msgHdr;
251 struct sockaddr_in sin;
252 int sentLength;
254 memset (& msgHdr, 0, sizeof (struct msghdr));
256 if (address != NULL)
258 sin.sin_family = AF_INET;
259 sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
260 sin.sin_addr.s_addr = address -> host;
262 msgHdr.msg_name = & sin;
263 msgHdr.msg_namelen = sizeof (struct sockaddr_in);
266 msgHdr.msg_iov = (struct iovec *) buffers;
267 msgHdr.msg_iovlen = bufferCount;
269 sentLength = sendmsg (socket, & msgHdr, MSG_NOSIGNAL);
271 if (sentLength == -1)
273 if (errno == EWOULDBLOCK)
274 return 0;
276 return -1;
279 return sentLength;
283 enet_socket_receive (ENetSocket socket,
284 ENetAddress * address,
285 ENetBuffer * buffers,
286 size_t bufferCount)
288 struct msghdr msgHdr;
289 struct sockaddr_in sin;
290 int recvLength;
292 memset (& msgHdr, 0, sizeof (struct msghdr));
294 if (address != NULL)
296 msgHdr.msg_name = & sin;
297 msgHdr.msg_namelen = sizeof (struct sockaddr_in);
300 msgHdr.msg_iov = (struct iovec *) buffers;
301 msgHdr.msg_iovlen = bufferCount;
303 recvLength = recvmsg (socket, & msgHdr, MSG_NOSIGNAL);
305 if (recvLength == -1)
307 if (errno == EWOULDBLOCK)
308 return 0;
310 return -1;
313 #ifdef HAS_MSGHDR_FLAGS
314 if (msgHdr.msg_flags & MSG_TRUNC)
315 return -1;
316 #endif
318 if (address != NULL)
320 address -> host = (enet_uint32) sin.sin_addr.s_addr;
321 address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
324 return recvLength;
328 enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
330 #ifdef HAS_POLL
331 struct pollfd pollSocket;
332 int pollCount;
334 pollSocket.fd = socket;
335 pollSocket.events = 0;
337 if (* condition & ENET_SOCKET_WAIT_SEND)
338 pollSocket.events |= POLLOUT;
340 if (* condition & ENET_SOCKET_WAIT_RECEIVE)
341 pollSocket.events |= POLLIN;
343 pollCount = poll (& pollSocket, 1, timeout);
345 if (pollCount < 0)
346 return -1;
348 * condition = ENET_SOCKET_WAIT_NONE;
350 if (pollCount == 0)
351 return 0;
353 if (pollSocket.revents & POLLOUT)
354 * condition |= ENET_SOCKET_WAIT_SEND;
356 if (pollSocket.revents & POLLIN)
357 * condition |= ENET_SOCKET_WAIT_RECEIVE;
359 return 0;
360 #else
361 fd_set readSet, writeSet;
362 struct timeval timeVal;
363 int selectCount;
365 timeVal.tv_sec = timeout / 1000;
366 timeVal.tv_usec = (timeout % 1000) * 1000;
368 FD_ZERO (& readSet);
369 FD_ZERO (& writeSet);
371 if (* condition & ENET_SOCKET_WAIT_SEND)
372 FD_SET (socket, & writeSet);
374 if (* condition & ENET_SOCKET_WAIT_RECEIVE)
375 FD_SET (socket, & readSet);
377 selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
379 if (selectCount < 0)
380 return -1;
382 * condition = ENET_SOCKET_WAIT_NONE;
384 if (selectCount == 0)
385 return 0;
387 if (FD_ISSET (socket, & writeSet))
388 * condition |= ENET_SOCKET_WAIT_SEND;
390 if (FD_ISSET (socket, & readSet))
391 * condition |= ENET_SOCKET_WAIT_RECEIVE;
393 return 0;
394 #endif
397 #endif