win32.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. name[nameLength - 1] = '\0';
  67. return 0;
  68. }
  69. int
  70. enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
  71. {
  72. struct in_addr in;
  73. struct hostent * hostEntry;
  74. in.s_addr = address -> host;
  75. hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
  76. if (hostEntry == NULL)
  77. return enet_address_get_host_ip (address, name, nameLength);
  78. strncpy (name, hostEntry -> h_name, nameLength);
  79. name[nameLength - 1] = '\0';
  80. return 0;
  81. }
  82. int
  83. enet_socket_bind (ENetSocket socket, const ENetAddress * address)
  84. {
  85. struct sockaddr_in sin;
  86. memset (& sin, 0, sizeof (struct sockaddr_in));
  87. sin.sin_family = AF_INET;
  88. if (address != NULL)
  89. {
  90. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  91. sin.sin_addr.s_addr = address -> host;
  92. }
  93. else
  94. {
  95. sin.sin_port = 0;
  96. sin.sin_addr.s_addr = INADDR_ANY;
  97. }
  98. return bind (socket,
  99. (struct sockaddr *) & sin,
  100. sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
  101. }
  102. int
  103. enet_socket_listen (ENetSocket socket, int backlog)
  104. {
  105. return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0;
  106. }
  107. ENetSocket
  108. enet_socket_create (ENetSocketType type)
  109. {
  110. return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  111. }
  112. int
  113. enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
  114. {
  115. int result = SOCKET_ERROR;
  116. switch (option)
  117. {
  118. case ENET_SOCKOPT_NONBLOCK:
  119. {
  120. u_long nonBlocking = (u_long) value;
  121. result = ioctlsocket (socket, FIONBIO, & nonBlocking);
  122. break;
  123. }
  124. case ENET_SOCKOPT_BROADCAST:
  125. result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
  126. break;
  127. case ENET_SOCKOPT_REUSEADDR:
  128. result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
  129. break;
  130. case ENET_SOCKOPT_RCVBUF:
  131. result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
  132. break;
  133. case ENET_SOCKOPT_SNDBUF:
  134. result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
  135. break;
  136. case ENET_SOCKOPT_RCVTIMEO:
  137. result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & value, sizeof (int));
  138. break;
  139. case ENET_SOCKOPT_SNDTIMEO:
  140. result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int));
  141. break;
  142. default:
  143. break;
  144. }
  145. return result == SOCKET_ERROR ? -1 : 0;
  146. }
  147. int
  148. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  149. {
  150. struct sockaddr_in sin;
  151. int result;
  152. memset (& sin, 0, sizeof (struct sockaddr_in));
  153. sin.sin_family = AF_INET;
  154. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  155. sin.sin_addr.s_addr = address -> host;
  156. result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  157. if (result == SOCKET_ERROR && WSAGetLastError () != WSAEWOULDBLOCK)
  158. return -1;
  159. return 0;
  160. }
  161. ENetSocket
  162. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  163. {
  164. SOCKET result;
  165. struct sockaddr_in sin;
  166. int sinLength = sizeof (struct sockaddr_in);
  167. result = accept (socket,
  168. address != NULL ? (struct sockaddr *) & sin : NULL,
  169. address != NULL ? & sinLength : NULL);
  170. if (result == INVALID_SOCKET)
  171. return ENET_SOCKET_NULL;
  172. if (address != NULL)
  173. {
  174. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  175. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  176. }
  177. return result;
  178. }
  179. int
  180. enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how)
  181. {
  182. return shutdown (socket, (int) how) == SOCKET_ERROR ? -1 : 0;
  183. }
  184. void
  185. enet_socket_destroy (ENetSocket socket)
  186. {
  187. if (socket != INVALID_SOCKET)
  188. closesocket (socket);
  189. }
  190. int
  191. enet_socket_send (ENetSocket socket,
  192. const ENetAddress * address,
  193. const ENetBuffer * buffers,
  194. size_t bufferCount)
  195. {
  196. struct sockaddr_in sin;
  197. DWORD sentLength;
  198. if (address != NULL)
  199. {
  200. memset (& sin, 0, sizeof (struct sockaddr_in));
  201. sin.sin_family = AF_INET;
  202. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  203. sin.sin_addr.s_addr = address -> host;
  204. }
  205. if (WSASendTo (socket,
  206. (LPWSABUF) buffers,
  207. (DWORD) bufferCount,
  208. & sentLength,
  209. 0,
  210. address != NULL ? (struct sockaddr *) & sin : NULL,
  211. address != NULL ? sizeof (struct sockaddr_in) : 0,
  212. NULL,
  213. NULL) == SOCKET_ERROR)
  214. {
  215. if (WSAGetLastError () == WSAEWOULDBLOCK)
  216. return 0;
  217. return -1;
  218. }
  219. return (int) sentLength;
  220. }
  221. int
  222. enet_socket_receive (ENetSocket socket,
  223. ENetAddress * address,
  224. ENetBuffer * buffers,
  225. size_t bufferCount)
  226. {
  227. INT sinLength = sizeof (struct sockaddr_in);
  228. DWORD flags = 0,
  229. recvLength;
  230. struct sockaddr_in sin;
  231. if (WSARecvFrom (socket,
  232. (LPWSABUF) buffers,
  233. (DWORD) bufferCount,
  234. & recvLength,
  235. & flags,
  236. address != NULL ? (struct sockaddr *) & sin : NULL,
  237. address != NULL ? & sinLength : NULL,
  238. NULL,
  239. NULL) == SOCKET_ERROR)
  240. {
  241. switch (WSAGetLastError ())
  242. {
  243. case WSAEWOULDBLOCK:
  244. case WSAECONNRESET:
  245. return 0;
  246. }
  247. return -1;
  248. }
  249. if (flags & MSG_PARTIAL)
  250. return -1;
  251. if (address != NULL)
  252. {
  253. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  254. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  255. }
  256. return (int) recvLength;
  257. }
  258. int
  259. enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
  260. {
  261. struct timeval timeVal;
  262. timeVal.tv_sec = timeout / 1000;
  263. timeVal.tv_usec = (timeout % 1000) * 1000;
  264. return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
  265. }
  266. int
  267. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  268. {
  269. fd_set readSet, writeSet;
  270. struct timeval timeVal;
  271. int selectCount;
  272. timeVal.tv_sec = timeout / 1000;
  273. timeVal.tv_usec = (timeout % 1000) * 1000;
  274. FD_ZERO (& readSet);
  275. FD_ZERO (& writeSet);
  276. if (* condition & ENET_SOCKET_WAIT_SEND)
  277. FD_SET (socket, & writeSet);
  278. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  279. FD_SET (socket, & readSet);
  280. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  281. if (selectCount < 0)
  282. return -1;
  283. * condition = ENET_SOCKET_WAIT_NONE;
  284. if (selectCount == 0)
  285. return 0;
  286. if (FD_ISSET (socket, & writeSet))
  287. * condition |= ENET_SOCKET_WAIT_SEND;
  288. if (FD_ISSET (socket, & readSet))
  289. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  290. return 0;
  291. }
  292. #endif