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 (const ENetAddress
* address
, char * name
, size_t nameLength
)
80 struct hostent
* hostEntry
;
82 in
.s_addr
= address
-> host
;
84 hostEntry
= gethostbyaddr ((char *) & in
, sizeof (struct in_addr
), AF_INET
);
85 if (hostEntry
== NULL
)
87 char * addr
= inet_ntoa (* (struct in_addr
*) & address
-> host
);
90 strncpy (name
, addr
, nameLength
);
94 strncpy (name
, hostEntry
-> h_name
, nameLength
);
100 enet_socket_create (ENetSocketType type
, const ENetAddress
* address
)
102 ENetSocket newSocket
= socket (PF_INET
, type
== ENET_SOCKET_TYPE_DATAGRAM
? SOCK_DGRAM
: SOCK_STREAM
, 0);
103 u_long nonBlocking
= 1;
104 int receiveBufferSize
= ENET_HOST_RECEIVE_BUFFER_SIZE
,
105 allowBroadcasting
= 1;
106 struct sockaddr_in sin
;
108 if (newSocket
== ENET_SOCKET_NULL
)
109 return ENET_SOCKET_NULL
;
111 if (type
== ENET_SOCKET_TYPE_DATAGRAM
)
113 ioctlsocket (newSocket
, FIONBIO
, & nonBlocking
);
115 setsockopt (newSocket
, SOL_SOCKET
, SO_RCVBUF
, (char *) & receiveBufferSize
, sizeof (int));
116 setsockopt (newSocket
, SOL_SOCKET
, SO_BROADCAST
, (char *) & allowBroadcasting
, sizeof (int));
119 memset (& sin
, 0, sizeof (struct sockaddr_in
));
121 sin
.sin_family
= AF_INET
;
125 sin
.sin_port
= ENET_HOST_TO_NET_16 (address
-> port
);
126 sin
.sin_addr
.s_addr
= address
-> host
;
131 sin
.sin_addr
.s_addr
= INADDR_ANY
;
135 (struct sockaddr
*) & sin
,
136 sizeof (struct sockaddr_in
)) == SOCKET_ERROR
||
137 (type
== ENET_SOCKET_TYPE_STREAM
&&
139 listen (newSocket
, SOMAXCONN
) == SOCKET_ERROR
))
141 closesocket (newSocket
);
143 return ENET_SOCKET_NULL
;
150 enet_socket_connect (ENetSocket socket
, const ENetAddress
* address
)
152 struct sockaddr_in sin
;
154 memset (& sin
, 0, sizeof (struct sockaddr_in
));
156 sin
.sin_family
= AF_INET
;
157 sin
.sin_port
= ENET_HOST_TO_NET_16 (address
-> port
);
158 sin
.sin_addr
.s_addr
= address
-> host
;
160 return connect (socket
, (struct sockaddr
*) & sin
, sizeof (struct sockaddr_in
));
164 enet_socket_accept (ENetSocket socket
, ENetAddress
* address
)
167 struct sockaddr_in sin
;
168 int sinLength
= sizeof (struct sockaddr_in
);
170 result
= accept (socket
,
171 address
!= NULL
? (struct sockaddr
*) & sin
: NULL
,
172 address
!= NULL
? & sinLength
: NULL
);
174 if (result
== INVALID_SOCKET
)
175 return ENET_SOCKET_NULL
;
179 address
-> host
= (enet_uint32
) sin
.sin_addr
.s_addr
;
180 address
-> port
= ENET_NET_TO_HOST_16 (sin
.sin_port
);
187 enet_socket_destroy (ENetSocket socket
)
189 closesocket (socket
);
193 enet_socket_send (ENetSocket socket
,
194 const ENetAddress
* address
,
195 const ENetBuffer
* buffers
,
198 struct sockaddr_in sin
;
203 sin
.sin_family
= AF_INET
;
204 sin
.sin_port
= ENET_HOST_TO_NET_16 (address
-> port
);
205 sin
.sin_addr
.s_addr
= address
-> host
;
208 if (WSASendTo (socket
,
213 address
!= NULL
? (struct sockaddr
*) & sin
: 0,
214 address
!= NULL
? sizeof (struct sockaddr_in
) : 0,
216 NULL
) == SOCKET_ERROR
)
218 if (WSAGetLastError () == WSAEWOULDBLOCK
)
224 return (int) sentLength
;
228 enet_socket_receive (ENetSocket socket
,
229 ENetAddress
* address
,
230 ENetBuffer
* buffers
,
233 INT sinLength
= sizeof (struct sockaddr_in
);
236 struct sockaddr_in sin
;
238 if (WSARecvFrom (socket
,
243 address
!= NULL
? (struct sockaddr
*) & sin
: NULL
,
244 address
!= NULL
? & sinLength
: NULL
,
246 NULL
) == SOCKET_ERROR
)
248 switch (WSAGetLastError ())
258 if (flags
& MSG_PARTIAL
)
263 address
-> host
= (enet_uint32
) sin
.sin_addr
.s_addr
;
264 address
-> port
= ENET_NET_TO_HOST_16 (sin
.sin_port
);
267 return (int) recvLength
;
271 enet_socket_wait (ENetSocket socket
, enet_uint32
* condition
, enet_uint32 timeout
)
273 fd_set readSet
, writeSet
;
274 struct timeval timeVal
;
277 timeVal
.tv_sec
= timeout
/ 1000;
278 timeVal
.tv_usec
= (timeout
% 1000) * 1000;
281 FD_ZERO (& writeSet
);
283 if (* condition
& ENET_SOCKET_WAIT_SEND
)
284 FD_SET (socket
, & writeSet
);
286 if (* condition
& ENET_SOCKET_WAIT_RECEIVE
)
287 FD_SET (socket
, & readSet
);
289 selectCount
= select (socket
+ 1, & readSet
, & writeSet
, NULL
, & timeVal
);
294 * condition
= ENET_SOCKET_WAIT_NONE
;
296 if (selectCount
== 0)
299 if (FD_ISSET (socket
, & writeSet
))
300 * condition
|= ENET_SOCKET_WAIT_SEND
;
302 if (FD_ISSET (socket
, & readSet
))
303 * condition
|= ENET_SOCKET_WAIT_RECEIVE
;