win32.c 9.8 KB

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