3 @brief ENet Win32 system specific functions
8 #define ENET_BUILDING_LIB 1
11 static enet_uint32 timeBase
= 0;
14 enet_initialize (void)
16 WORD versionRequested
= MAKEWORD (1, 1);
19 if (WSAStartup (versionRequested
, & wsaData
))
22 if (LOBYTE (wsaData
.wVersion
) != 1||
23 HIBYTE (wsaData
.wVersion
) != 1)
36 enet_deinitialize (void)
46 return (enet_uint32
) timeGetTime () - timeBase
;
50 enet_time_set (enet_uint32 newTimeBase
)
52 timeBase
= (enet_uint32
) timeGetTime () - newTimeBase
;
56 enet_address_set_host (ENetAddress
* address
, const char * name
)
58 struct hostent
* hostEntry
;
60 hostEntry
= gethostbyname (name
);
61 if (hostEntry
== NULL
||
62 hostEntry
-> h_addrtype
!= AF_INET
)
64 unsigned long host
= inet_addr (name
);
65 if (host
== INADDR_NONE
)
67 address
-> host
= host
;
71 address
-> host
= * (enet_uint32
*) hostEntry
-> h_addr_list
[0];
77 enet_address_get_host_ip (const ENetAddress
* address
, char * name
, size_t nameLength
)
79 char * addr
= inet_ntoa (* (struct in_addr
*) & address
-> host
);
82 strncpy (name
, addr
, nameLength
);
87 enet_address_get_host (const ENetAddress
* address
, char * name
, size_t nameLength
)
90 struct hostent
* hostEntry
;
92 in
.s_addr
= address
-> host
;
94 hostEntry
= gethostbyaddr ((char *) & in
, sizeof (struct in_addr
), AF_INET
);
95 if (hostEntry
== NULL
)
96 return enet_address_get_host_ip (address
, name
, nameLength
);
98 strncpy (name
, hostEntry
-> h_name
, nameLength
);
104 enet_socket_create (ENetSocketType type
, const ENetAddress
* address
)
106 ENetSocket newSocket
= socket (PF_INET
, type
== ENET_SOCKET_TYPE_DATAGRAM
? SOCK_DGRAM
: SOCK_STREAM
, 0);
107 struct sockaddr_in sin
;
109 if (newSocket
== ENET_SOCKET_NULL
)
110 return ENET_SOCKET_NULL
;
112 memset (& sin
, 0, sizeof (struct sockaddr_in
));
114 sin
.sin_family
= AF_INET
;
118 sin
.sin_port
= ENET_HOST_TO_NET_16 (address
-> port
);
119 sin
.sin_addr
.s_addr
= address
-> host
;
124 sin
.sin_addr
.s_addr
= INADDR_ANY
;
128 (struct sockaddr
*) & sin
,
129 sizeof (struct sockaddr_in
)) == SOCKET_ERROR
||
130 (type
== ENET_SOCKET_TYPE_STREAM
&&
132 address
-> port
!= ENET_PORT_ANY
&&
133 listen (newSocket
, SOMAXCONN
) == SOCKET_ERROR
))
135 closesocket (newSocket
);
137 return ENET_SOCKET_NULL
;
144 enet_socket_set_option (ENetSocket socket
, ENetSocketOption option
, int value
)
146 int result
= SOCKET_ERROR
;
149 case ENET_SOCKOPT_NONBLOCK
:
151 u_long nonBlocking
= (u_long
) value
;
152 result
= ioctlsocket (socket
, FIONBIO
, & nonBlocking
);
156 case ENET_SOCKOPT_BROADCAST
:
157 result
= setsockopt (socket
, SOL_SOCKET
, SO_BROADCAST
, (char *) & value
, sizeof (int));
160 case ENET_SOCKOPT_RCVBUF
:
161 result
= setsockopt (socket
, SOL_SOCKET
, SO_RCVBUF
, (char *) & value
, sizeof (int));
164 case ENET_SOCKOPT_SNDBUF
:
165 result
= setsockopt (socket
, SOL_SOCKET
, SO_SNDBUF
, (char *) & value
, sizeof (int));
171 return result
== SOCKET_ERROR
? -1 : 0;
175 enet_socket_connect (ENetSocket socket
, const ENetAddress
* address
)
177 struct sockaddr_in sin
;
179 memset (& sin
, 0, sizeof (struct sockaddr_in
));
181 sin
.sin_family
= AF_INET
;
182 sin
.sin_port
= ENET_HOST_TO_NET_16 (address
-> port
);
183 sin
.sin_addr
.s_addr
= address
-> host
;
185 return connect (socket
, (struct sockaddr
*) & sin
, sizeof (struct sockaddr_in
));
189 enet_socket_accept (ENetSocket socket
, ENetAddress
* address
)
192 struct sockaddr_in sin
;
193 int sinLength
= sizeof (struct sockaddr_in
);
195 result
= accept (socket
,
196 address
!= NULL
? (struct sockaddr
*) & sin
: NULL
,
197 address
!= NULL
? & sinLength
: NULL
);
199 if (result
== INVALID_SOCKET
)
200 return ENET_SOCKET_NULL
;
204 address
-> host
= (enet_uint32
) sin
.sin_addr
.s_addr
;
205 address
-> port
= ENET_NET_TO_HOST_16 (sin
.sin_port
);
212 enet_socket_destroy (ENetSocket socket
)
214 closesocket (socket
);
218 enet_socket_send (ENetSocket socket
,
219 const ENetAddress
* address
,
220 const ENetBuffer
* buffers
,
223 struct sockaddr_in sin
;
228 memset (& sin
, 0, sizeof (struct sockaddr_in
));
230 sin
.sin_family
= AF_INET
;
231 sin
.sin_port
= ENET_HOST_TO_NET_16 (address
-> port
);
232 sin
.sin_addr
.s_addr
= address
-> host
;
235 if (WSASendTo (socket
,
240 address
!= NULL
? (struct sockaddr
*) & sin
: 0,
241 address
!= NULL
? sizeof (struct sockaddr_in
) : 0,
243 NULL
) == SOCKET_ERROR
)
245 if (WSAGetLastError () == WSAEWOULDBLOCK
)
251 return (int) sentLength
;
255 enet_socket_receive (ENetSocket socket
,
256 ENetAddress
* address
,
257 ENetBuffer
* buffers
,
260 INT sinLength
= sizeof (struct sockaddr_in
);
263 struct sockaddr_in sin
;
265 if (WSARecvFrom (socket
,
270 address
!= NULL
? (struct sockaddr
*) & sin
: NULL
,
271 address
!= NULL
? & sinLength
: NULL
,
273 NULL
) == SOCKET_ERROR
)
275 switch (WSAGetLastError ())
285 if (flags
& MSG_PARTIAL
)
290 address
-> host
= (enet_uint32
) sin
.sin_addr
.s_addr
;
291 address
-> port
= ENET_NET_TO_HOST_16 (sin
.sin_port
);
294 return (int) recvLength
;
298 enet_socket_wait (ENetSocket socket
, enet_uint32
* condition
, enet_uint32 timeout
)
300 fd_set readSet
, writeSet
;
301 struct timeval timeVal
;
304 timeVal
.tv_sec
= timeout
/ 1000;
305 timeVal
.tv_usec
= (timeout
% 1000) * 1000;
308 FD_ZERO (& writeSet
);
310 if (* condition
& ENET_SOCKET_WAIT_SEND
)
311 FD_SET (socket
, & writeSet
);
313 if (* condition
& ENET_SOCKET_WAIT_RECEIVE
)
314 FD_SET (socket
, & readSet
);
316 selectCount
= select (socket
+ 1, & readSet
, & writeSet
, NULL
, & timeVal
);
321 * condition
= ENET_SOCKET_WAIT_NONE
;
323 if (selectCount
== 0)
326 if (FD_ISSET (socket
, & writeSet
))
327 * condition
|= ENET_SOCKET_WAIT_SEND
;
329 if (FD_ISSET (socket
, & readSet
))
330 * condition
|= ENET_SOCKET_WAIT_RECEIVE
;