win32.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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_host_random_seed (void)
  34. {
  35. return (enet_uint32) timeGetTime ();
  36. }
  37. enet_uint32
  38. enet_time_get (void)
  39. {
  40. return (enet_uint32) timeGetTime () - timeBase;
  41. }
  42. void
  43. enet_time_set (enet_uint32 newTimeBase)
  44. {
  45. timeBase = (enet_uint32) timeGetTime () - newTimeBase;
  46. }
  47. int
  48. enet_address_set_host (ENetAddress * address, const char * name)
  49. {
  50. struct hostent * hostEntry;
  51. hostEntry = gethostbyname (name);
  52. if (hostEntry == NULL ||
  53. hostEntry -> h_addrtype != AF_INET)
  54. {
  55. unsigned long host = inet_addr (name);
  56. if (host == INADDR_NONE)
  57. return -1;
  58. address -> host = host;
  59. return 0;
  60. }
  61. address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
  62. return 0;
  63. }
  64. int
  65. enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
  66. {
  67. char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
  68. if (addr == NULL)
  69. return -1;
  70. else
  71. {
  72. size_t addrLen = strlen(addr);
  73. if (addrLen >= nameLength)
  74. return -1;
  75. memcpy (name, addr, addrLen + 1);
  76. }
  77. return 0;
  78. }
  79. int
  80. enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
  81. {
  82. struct in_addr in;
  83. struct hostent * hostEntry;
  84. in.s_addr = address -> host;
  85. hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
  86. if (hostEntry == NULL)
  87. return enet_address_get_host_ip (address, name, nameLength);
  88. else
  89. {
  90. size_t hostLen = strlen (hostEntry -> h_name);
  91. if (hostLen >= nameLength)
  92. return -1;
  93. memcpy (name, hostEntry -> h_name, hostLen + 1);
  94. }
  95. return 0;
  96. }
  97. int
  98. enet_socket_bind (ENetSocket socket, const ENetAddress * address)
  99. {
  100. struct sockaddr_in sin;
  101. memset (& sin, 0, sizeof (struct sockaddr_in));
  102. sin.sin_family = AF_INET;
  103. if (address != NULL)
  104. {
  105. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  106. sin.sin_addr.s_addr = address -> host;
  107. }
  108. else
  109. {
  110. sin.sin_port = 0;
  111. sin.sin_addr.s_addr = INADDR_ANY;
  112. }
  113. return bind (socket,
  114. (struct sockaddr *) & sin,
  115. sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
  116. }
  117. int
  118. enet_socket_get_address (ENetSocket socket, ENetAddress * address)
  119. {
  120. struct sockaddr_in sin;
  121. int sinLength = sizeof (struct sockaddr_in);
  122. if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1)
  123. return -1;
  124. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  125. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  126. return 0;
  127. }
  128. int
  129. enet_socket_listen (ENetSocket socket, int backlog)
  130. {
  131. return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0;
  132. }
  133. ENetSocket
  134. enet_socket_create (ENetSocketType type)
  135. {
  136. return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
  137. }
  138. int
  139. enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
  140. {
  141. int result = SOCKET_ERROR;
  142. switch (option)
  143. {
  144. case ENET_SOCKOPT_NONBLOCK:
  145. {
  146. u_long nonBlocking = (u_long) value;
  147. result = ioctlsocket (socket, FIONBIO, & nonBlocking);
  148. break;
  149. }
  150. case ENET_SOCKOPT_BROADCAST:
  151. result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
  152. break;
  153. case ENET_SOCKOPT_REUSEADDR:
  154. result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
  155. break;
  156. case ENET_SOCKOPT_RCVBUF:
  157. result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
  158. break;
  159. case ENET_SOCKOPT_SNDBUF:
  160. result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
  161. break;
  162. case ENET_SOCKOPT_RCVTIMEO:
  163. result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & value, sizeof (int));
  164. break;
  165. case ENET_SOCKOPT_SNDTIMEO:
  166. result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int));
  167. break;
  168. case ENET_SOCKOPT_NODELAY:
  169. result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int));
  170. break;
  171. default:
  172. break;
  173. }
  174. return result == SOCKET_ERROR ? -1 : 0;
  175. }
  176. int
  177. enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value)
  178. {
  179. int result = SOCKET_ERROR, len;
  180. switch (option)
  181. {
  182. case ENET_SOCKOPT_ERROR:
  183. len = sizeof(int);
  184. result = getsockopt (socket, SOL_SOCKET, SO_ERROR, (char *) value, & len);
  185. break;
  186. default:
  187. break;
  188. }
  189. return result == SOCKET_ERROR ? -1 : 0;
  190. }
  191. int
  192. enet_socket_connect (ENetSocket socket, const ENetAddress * address)
  193. {
  194. struct sockaddr_in sin;
  195. int result;
  196. memset (& sin, 0, sizeof (struct sockaddr_in));
  197. sin.sin_family = AF_INET;
  198. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  199. sin.sin_addr.s_addr = address -> host;
  200. result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
  201. if (result == SOCKET_ERROR && WSAGetLastError () != WSAEWOULDBLOCK)
  202. return -1;
  203. return 0;
  204. }
  205. ENetSocket
  206. enet_socket_accept (ENetSocket socket, ENetAddress * address)
  207. {
  208. SOCKET result;
  209. struct sockaddr_in sin;
  210. int sinLength = sizeof (struct sockaddr_in);
  211. result = accept (socket,
  212. address != NULL ? (struct sockaddr *) & sin : NULL,
  213. address != NULL ? & sinLength : NULL);
  214. if (result == INVALID_SOCKET)
  215. return ENET_SOCKET_NULL;
  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 result;
  222. }
  223. int
  224. enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how)
  225. {
  226. return shutdown (socket, (int) how) == SOCKET_ERROR ? -1 : 0;
  227. }
  228. void
  229. enet_socket_destroy (ENetSocket socket)
  230. {
  231. if (socket != INVALID_SOCKET)
  232. closesocket (socket);
  233. }
  234. int
  235. enet_socket_send (ENetSocket socket,
  236. const ENetAddress * address,
  237. const ENetBuffer * buffers,
  238. size_t bufferCount)
  239. {
  240. struct sockaddr_in sin;
  241. DWORD sentLength;
  242. if (address != NULL)
  243. {
  244. memset (& sin, 0, sizeof (struct sockaddr_in));
  245. sin.sin_family = AF_INET;
  246. sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
  247. sin.sin_addr.s_addr = address -> host;
  248. }
  249. if (WSASendTo (socket,
  250. (LPWSABUF) buffers,
  251. (DWORD) bufferCount,
  252. & sentLength,
  253. 0,
  254. address != NULL ? (struct sockaddr *) & sin : NULL,
  255. address != NULL ? sizeof (struct sockaddr_in) : 0,
  256. NULL,
  257. NULL) == SOCKET_ERROR)
  258. {
  259. if (WSAGetLastError () == WSAEWOULDBLOCK)
  260. return 0;
  261. return -1;
  262. }
  263. return (int) sentLength;
  264. }
  265. int
  266. enet_socket_receive (ENetSocket socket,
  267. ENetAddress * address,
  268. ENetBuffer * buffers,
  269. size_t bufferCount)
  270. {
  271. INT sinLength = sizeof (struct sockaddr_in);
  272. DWORD flags = 0,
  273. recvLength;
  274. struct sockaddr_in sin;
  275. if (WSARecvFrom (socket,
  276. (LPWSABUF) buffers,
  277. (DWORD) bufferCount,
  278. & recvLength,
  279. & flags,
  280. address != NULL ? (struct sockaddr *) & sin : NULL,
  281. address != NULL ? & sinLength : NULL,
  282. NULL,
  283. NULL) == SOCKET_ERROR)
  284. {
  285. switch (WSAGetLastError ())
  286. {
  287. case WSAEWOULDBLOCK:
  288. case WSAECONNRESET:
  289. return 0;
  290. }
  291. return -1;
  292. }
  293. if (flags & MSG_PARTIAL)
  294. return -1;
  295. if (address != NULL)
  296. {
  297. address -> host = (enet_uint32) sin.sin_addr.s_addr;
  298. address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
  299. }
  300. return (int) recvLength;
  301. }
  302. int
  303. enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
  304. {
  305. struct timeval timeVal;
  306. timeVal.tv_sec = timeout / 1000;
  307. timeVal.tv_usec = (timeout % 1000) * 1000;
  308. return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
  309. }
  310. int
  311. enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
  312. {
  313. fd_set readSet, writeSet;
  314. struct timeval timeVal;
  315. int selectCount;
  316. timeVal.tv_sec = timeout / 1000;
  317. timeVal.tv_usec = (timeout % 1000) * 1000;
  318. FD_ZERO (& readSet);
  319. FD_ZERO (& writeSet);
  320. if (* condition & ENET_SOCKET_WAIT_SEND)
  321. FD_SET (socket, & writeSet);
  322. if (* condition & ENET_SOCKET_WAIT_RECEIVE)
  323. FD_SET (socket, & readSet);
  324. selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
  325. if (selectCount < 0)
  326. return -1;
  327. * condition = ENET_SOCKET_WAIT_NONE;
  328. if (selectCount == 0)
  329. return 0;
  330. if (FD_ISSET (socket, & writeSet))
  331. * condition |= ENET_SOCKET_WAIT_SEND;
  332. if (FD_ISSET (socket, & readSet))
  333. * condition |= ENET_SOCKET_WAIT_RECEIVE;
  334. return 0;
  335. }
  336. #endif