win32.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. @file win32.c
  3. @brief ENet Win32 system specific functions
  4. */
  5. #ifdef WIN32
  6. #include <time.h>
  7. #define ENET_BUILDING_LIB 1
  8. #include "enet/enet.h"
  9. static enet_uint32 timeBase = 0;
  10. int
  11. enet_initialize (void)
  12. {
  13. WORD versionRequested = MAKEWORD (1, 1);
  14. WSADATA wsaData;
  15. if (WSAStartup (versionRequested, & wsaData))
  16. return -1;
  17. if (LOBYTE (wsaData.wVersion) != 1||
  18. HIBYTE (wsaData.wVersion) != 1)
  19. {
  20. WSACleanup ();
  21. return -1;
  22. }
  23. timeBeginPeriod (1);
  24. return 0;
  25. }
  26. void
  27. enet_deinitialize (void)
  28. {
  29. timeEndPeriod (1);
  30. WSACleanup ();
  31. }
  32. enet_uint32
  33. enet_time_get (void)
  34. {
  35. return (enet_uint32) timeGetTime () - timeBase;
  36. }
  37. void
  38. enet_time_set (enet_uint32 newTimeBase)
  39. {
  40. timeBase = (enet_uint32) timeGetTime () - newTimeBase;
  41. }
  42. int
  43. enet_address_set_host (ENetAddress * address, const char * name)
  44. {
  45. struct hostent * hostEntry;
  46. hostEntry = gethostbyname (name);
  47. if (hostEntry == NULL ||
  48. hostEntry -> h_addrtype != AF_INET)
  49. {
  50. unsigned long host = inet_addr (name);
  51. if (host == INADDR_NONE)
  52. return -1;
  53. address -> host = host;
  54. return 0;
  55. }
  56. address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
  57. return 0;
  58. }
  59. int
  60. enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
  61. {
  62. char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
  63. if (addr == NULL)
  64. return -1;
  65. strncpy (name, addr, nameLength);
  66. return 0;
  67. }
  68. int
  69. enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
  70. {
  71. struct in_addr in;
  72. struct hostent * hostEntry;
  73. in.s_addr = address -> host;
  74. hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
  75. if (hostEntry == NULL)
  76. return enet_address_get_host_ip (address, name, nameLength);
  77. strncpy (name, hostEntry -> h_name, nameLength);
  78. return 0;
  79. }
  80. ENetSocket
  81. enet_socket_create (ENetSocketType type, const ENetAddress * address)
  82. {
  83. ENetSocket newSocket = socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  84. u_long nonBlocking = 1;
  85. int receiveBufferSize = ENET_HOST_RECEIVE_BUFFER_SIZE,
  86. sendBufferSize = ENET_HOST_SEND_BUFFER_SIZE,
  87. allowBroadcasting = 1;
  88. struct sockaddr_in sin;
  89. if (newSocket == ENET_SOCKET_NULL)
  90. return ENET_SOCKET_NULL;
  91. if (type == ENET_SOCKET_TYPE_DATAGRAM)
  92. {
  93. ioctlsocket (newSocket, FIONBIO, & nonBlocking);
  94. setsockopt (newSocket, SOL_SOCKET, SO_RCVBUF, (char *) & receiveBufferSize, sizeof (int));
  95. setsockopt (newSocket, SOL_SOCKET, SO_SNDBUF, (char *) & sendBufferSize, sizeof (int));
  96. setsockopt (newSocket, SOL_SOCKET, SO_BROADCAST, (char *) & allowBroadcasting, sizeof (int));
  97. }
  98. memset (& sin, 0, sizeof (struct sockaddr_in));
  99. sin.sin_family = AF_INET;
  100. if (address != NULL)
  101. {
  102. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  103. sin.sin_addr.s_addr = address -> host;
  104. }
  105. else
  106. {
  107. sin.sin_port = 0;
  108. sin.sin_addr.s_addr = INADDR_ANY;
  109. }
  110. if (bind (newSocket,
  111. (struct sockaddr *) & sin,
  112. sizeof (struct sockaddr_in)) == SOCKET_ERROR ||
  113. (type == ENET_SOCKET_TYPE_STREAM &&
  114. address != NULL &&
  115. listen (newSocket, SOMAXCONN) == SOCKET_ERROR))
  116. {
  117. closesocket (newSocket);
  118. return ENET_SOCKET_NULL;
  119. }
  120. return newSocket;
  121. }
  122. int
  123. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  124. {
  125. struct sockaddr_in sin;
  126. memset (& sin, 0, sizeof (struct sockaddr_in));
  127. sin.sin_family = AF_INET;
  128. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  129. sin.sin_addr.s_addr = address -> host;
  130. return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  131. }
  132. ENetSocket
  133. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  134. {
  135. SOCKET result;
  136. struct sockaddr_in sin;
  137. int sinLength = sizeof (struct sockaddr_in);
  138. result = accept (socket,
  139. address != NULL ? (struct sockaddr *) & sin : NULL,
  140. address != NULL ? & sinLength : NULL);
  141. if (result == INVALID_SOCKET)
  142. return ENET_SOCKET_NULL;
  143. if (address != NULL)
  144. {
  145. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  146. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  147. }
  148. return result;
  149. }
  150. void
  151. enet_socket_destroy (ENetSocket socket)
  152. {
  153. closesocket (socket);
  154. }
  155. int
  156. enet_socket_send (ENetSocket socket,
  157. const ENetAddress * address,
  158. const ENetBuffer * buffers,
  159. size_t bufferCount)
  160. {
  161. struct sockaddr_in sin;
  162. DWORD sentLength;
  163. if (address != NULL)
  164. {
  165. sin.sin_family = AF_INET;
  166. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  167. sin.sin_addr.s_addr = address -> host;
  168. }
  169. if (WSASendTo (socket,
  170. (LPWSABUF) buffers,
  171. (DWORD) bufferCount,
  172. & sentLength,
  173. 0,
  174. address != NULL ? (struct sockaddr *) & sin : 0,
  175. address != NULL ? sizeof (struct sockaddr_in) : 0,
  176. NULL,
  177. NULL) == SOCKET_ERROR)
  178. {
  179. if (WSAGetLastError () == WSAEWOULDBLOCK)
  180. return 0;
  181. return -1;
  182. }
  183. return (int) sentLength;
  184. }
  185. int
  186. enet_socket_receive (ENetSocket socket,
  187. ENetAddress * address,
  188. ENetBuffer * buffers,
  189. size_t bufferCount)
  190. {
  191. INT sinLength = sizeof (struct sockaddr_in);
  192. DWORD flags = 0,
  193. recvLength;
  194. struct sockaddr_in sin;
  195. if (WSARecvFrom (socket,
  196. (LPWSABUF) buffers,
  197. (DWORD) bufferCount,
  198. & recvLength,
  199. & flags,
  200. address != NULL ? (struct sockaddr *) & sin : NULL,
  201. address != NULL ? & sinLength : NULL,
  202. NULL,
  203. NULL) == SOCKET_ERROR)
  204. {
  205. switch (WSAGetLastError ())
  206. {
  207. case WSAEWOULDBLOCK:
  208. case WSAECONNRESET:
  209. return 0;
  210. }
  211. return -1;
  212. }
  213. if (flags & MSG_PARTIAL)
  214. return -1;
  215. if (address != NULL)
  216. {
  217. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  218. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  219. }
  220. return (int) recvLength;
  221. }
  222. int
  223. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  224. {
  225. fd_set readSet, writeSet;
  226. struct timeval timeVal;
  227. int selectCount;
  228. timeVal.tv_sec = timeout / 1000;
  229. timeVal.tv_usec = (timeout % 1000) * 1000;
  230. FD_ZERO (& readSet);
  231. FD_ZERO (& writeSet);
  232. if (* condition & ENET_SOCKET_WAIT_SEND)
  233. FD_SET (socket, & writeSet);
  234. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  235. FD_SET (socket, & readSet);
  236. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  237. if (selectCount < 0)
  238. return -1;
  239. * condition = ENET_SOCKET_WAIT_NONE;
  240. if (selectCount == 0)
  241. return 0;
  242. if (FD_ISSET (socket, & writeSet))
  243. * condition |= ENET_SOCKET_WAIT_SEND;
  244. if (FD_ISSET (socket, & readSet))
  245. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  246. return 0;
  247. }
  248. #endif