win32.c 8.0 KB

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