win32.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. address -> port != ENET_PORT_ANY &&
  116. listen (newSocket, SOMAXCONN) == SOCKET_ERROR))
  117. {
  118. closesocket (newSocket);
  119. return ENET_SOCKET_NULL;
  120. }
  121. return newSocket;
  122. }
  123. int
  124. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  125. {
  126. struct sockaddr_in sin;
  127. memset (& sin, 0, sizeof (struct sockaddr_in));
  128. sin.sin_family = AF_INET;
  129. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  130. sin.sin_addr.s_addr = address -> host;
  131. return connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  132. }
  133. ENetSocket
  134. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  135. {
  136. SOCKET result;
  137. struct sockaddr_in sin;
  138. int sinLength = sizeof (struct sockaddr_in);
  139. result = accept (socket,
  140. address != NULL ? (struct sockaddr *) & sin : NULL,
  141. address != NULL ? & sinLength : NULL);
  142. if (result == INVALID_SOCKET)
  143. return ENET_SOCKET_NULL;
  144. if (address != NULL)
  145. {
  146. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  147. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  148. }
  149. return result;
  150. }
  151. void
  152. enet_socket_destroy (ENetSocket socket)
  153. {
  154. closesocket (socket);
  155. }
  156. int
  157. enet_socket_send (ENetSocket socket,
  158. const ENetAddress * address,
  159. const ENetBuffer * buffers,
  160. size_t bufferCount)
  161. {
  162. struct sockaddr_in sin;
  163. DWORD sentLength;
  164. if (address != NULL)
  165. {
  166. sin.sin_family = AF_INET;
  167. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  168. sin.sin_addr.s_addr = address -> host;
  169. }
  170. if (WSASendTo (socket,
  171. (LPWSABUF) buffers,
  172. (DWORD) bufferCount,
  173. & sentLength,
  174. 0,
  175. address != NULL ? (struct sockaddr *) & sin : 0,
  176. address != NULL ? sizeof (struct sockaddr_in) : 0,
  177. NULL,
  178. NULL) == SOCKET_ERROR)
  179. {
  180. if (WSAGetLastError () == WSAEWOULDBLOCK)
  181. return 0;
  182. return -1;
  183. }
  184. return (int) sentLength;
  185. }
  186. int
  187. enet_socket_receive (ENetSocket socket,
  188. ENetAddress * address,
  189. ENetBuffer * buffers,
  190. size_t bufferCount)
  191. {
  192. INT sinLength = sizeof (struct sockaddr_in);
  193. DWORD flags = 0,
  194. recvLength;
  195. struct sockaddr_in sin;
  196. if (WSARecvFrom (socket,
  197. (LPWSABUF) buffers,
  198. (DWORD) bufferCount,
  199. & recvLength,
  200. & flags,
  201. address != NULL ? (struct sockaddr *) & sin : NULL,
  202. address != NULL ? & sinLength : NULL,
  203. NULL,
  204. NULL) == SOCKET_ERROR)
  205. {
  206. switch (WSAGetLastError ())
  207. {
  208. case WSAEWOULDBLOCK:
  209. case WSAECONNRESET:
  210. return 0;
  211. }
  212. return -1;
  213. }
  214. if (flags & MSG_PARTIAL)
  215. return -1;
  216. if (address != NULL)
  217. {
  218. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  219. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  220. }
  221. return (int) recvLength;
  222. }
  223. int
  224. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  225. {
  226. fd_set readSet, writeSet;
  227. struct timeval timeVal;
  228. int selectCount;
  229. timeVal.tv_sec = timeout / 1000;
  230. timeVal.tv_usec = (timeout % 1000) * 1000;
  231. FD_ZERO (& readSet);
  232. FD_ZERO (& writeSet);
  233. if (* condition & ENET_SOCKET_WAIT_SEND)
  234. FD_SET (socket, & writeSet);
  235. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  236. FD_SET (socket, & readSet);
  237. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  238. if (selectCount < 0)
  239. return -1;
  240. * condition = ENET_SOCKET_WAIT_NONE;
  241. if (selectCount == 0)
  242. return 0;
  243. if (FD_ISSET (socket, & writeSet))
  244. * condition |= ENET_SOCKET_WAIT_SEND;
  245. if (FD_ISSET (socket, & readSet))
  246. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  247. return 0;
  248. }
  249. #endif