2004-10-20 10:17:51 -07:00
|
|
|
/**
|
|
|
|
@file win32.c
|
|
|
|
@brief ENet Win32 system specific functions
|
|
|
|
*/
|
2013-04-06 21:11:12 -07:00
|
|
|
#ifdef _WIN32
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
#define ENET_BUILDING_LIB 1
|
|
|
|
#include "enet/enet.h"
|
2014-05-28 11:16:14 -07:00
|
|
|
#include <windows.h>
|
|
|
|
#include <mmsystem.h>
|
2024-04-14 20:06:53 -07:00
|
|
|
#include <ws2tcpip.h>
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
static enet_uint32 timeBase = 0;
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_initialize (void)
|
|
|
|
{
|
|
|
|
WORD versionRequested = MAKEWORD (1, 1);
|
|
|
|
WSADATA wsaData;
|
|
|
|
|
|
|
|
if (WSAStartup (versionRequested, & wsaData))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (LOBYTE (wsaData.wVersion) != 1||
|
|
|
|
HIBYTE (wsaData.wVersion) != 1)
|
|
|
|
{
|
|
|
|
WSACleanup ();
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeBeginPeriod (1);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
enet_deinitialize (void)
|
|
|
|
{
|
|
|
|
timeEndPeriod (1);
|
|
|
|
|
|
|
|
WSACleanup ();
|
|
|
|
}
|
|
|
|
|
2013-08-08 23:41:57 -07:00
|
|
|
enet_uint32
|
|
|
|
enet_host_random_seed (void)
|
|
|
|
{
|
|
|
|
return (enet_uint32) timeGetTime ();
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
enet_uint32
|
|
|
|
enet_time_get (void)
|
|
|
|
{
|
|
|
|
return (enet_uint32) timeGetTime () - timeBase;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
enet_time_set (enet_uint32 newTimeBase)
|
|
|
|
{
|
|
|
|
timeBase = (enet_uint32) timeGetTime () - newTimeBase;
|
|
|
|
}
|
|
|
|
|
2017-05-06 11:55:03 -07:00
|
|
|
int
|
|
|
|
enet_address_set_host_ip (ENetAddress * address, const char * name)
|
|
|
|
{
|
|
|
|
enet_uint8 vals [4] = { 0, 0, 0, 0 };
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 4; ++ i)
|
|
|
|
{
|
|
|
|
const char * next = name + 1;
|
|
|
|
if (* name != '0')
|
|
|
|
{
|
|
|
|
long val = strtol (name, (char **) & next, 10);
|
|
|
|
if (val < 0 || val > 255 || next == name || next - name > 3)
|
|
|
|
return -1;
|
|
|
|
vals [i] = (enet_uint8) val;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (* next != (i < 3 ? '.' : '\0'))
|
|
|
|
return -1;
|
|
|
|
name = next + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (& address -> host, vals, sizeof (enet_uint32));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
int
|
|
|
|
enet_address_set_host (ENetAddress * address, const char * name)
|
|
|
|
{
|
|
|
|
struct hostent * hostEntry;
|
|
|
|
|
|
|
|
hostEntry = gethostbyname (name);
|
|
|
|
if (hostEntry == NULL ||
|
|
|
|
hostEntry -> h_addrtype != AF_INET)
|
2017-05-06 11:55:03 -07:00
|
|
|
return enet_address_set_host_ip (address, name);
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
address -> host = * (enet_uint32 *) hostEntry -> h_addr_list [0];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-12 21:36:07 -07:00
|
|
|
int
|
|
|
|
enet_address_get_host_ip (const ENetAddress * address, char * name, size_t nameLength)
|
|
|
|
{
|
|
|
|
char * addr = inet_ntoa (* (struct in_addr *) & address -> host);
|
|
|
|
if (addr == NULL)
|
|
|
|
return -1;
|
2014-03-08 09:46:39 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t addrLen = strlen(addr);
|
|
|
|
if (addrLen >= nameLength)
|
|
|
|
return -1;
|
|
|
|
memcpy (name, addr, addrLen + 1);
|
|
|
|
}
|
2006-12-12 21:36:07 -07:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
int
|
|
|
|
enet_address_get_host (const ENetAddress * address, char * name, size_t nameLength)
|
|
|
|
{
|
|
|
|
struct in_addr in;
|
|
|
|
struct hostent * hostEntry;
|
2014-03-08 09:46:39 -07:00
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
in.s_addr = address -> host;
|
|
|
|
|
|
|
|
hostEntry = gethostbyaddr ((char *) & in, sizeof (struct in_addr), AF_INET);
|
|
|
|
if (hostEntry == NULL)
|
2006-12-12 21:36:07 -07:00
|
|
|
return enet_address_get_host_ip (address, name, nameLength);
|
2014-03-08 09:46:39 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t hostLen = strlen (hostEntry -> h_name);
|
|
|
|
if (hostLen >= nameLength)
|
|
|
|
return -1;
|
|
|
|
memcpy (name, hostEntry -> h_name, hostLen + 1);
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-15 16:11:22 -07:00
|
|
|
int
|
|
|
|
enet_socket_bind (ENetSocket socket, const ENetAddress * address)
|
2004-10-20 10:17:51 -07:00
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
memset (& sin, 0, sizeof (struct sockaddr_in));
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
2008-09-15 16:11:22 -07:00
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
|
|
|
sin.sin_addr.s_addr = address -> host;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sin.sin_port = 0;
|
|
|
|
sin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
}
|
|
|
|
|
2008-09-15 16:11:22 -07:00
|
|
|
return bind (socket,
|
|
|
|
(struct sockaddr *) & sin,
|
|
|
|
sizeof (struct sockaddr_in)) == SOCKET_ERROR ? -1 : 0;
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2013-03-19 15:43:34 -07:00
|
|
|
int
|
|
|
|
enet_socket_get_address (ENetSocket socket, ENetAddress * address)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int sinLength = sizeof (struct sockaddr_in);
|
|
|
|
|
|
|
|
if (getsockname (socket, (struct sockaddr *) & sin, & sinLength) == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
address -> host = (enet_uint32) sin.sin_addr.s_addr;
|
|
|
|
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-15 16:11:22 -07:00
|
|
|
int
|
|
|
|
enet_socket_listen (ENetSocket socket, int backlog)
|
|
|
|
{
|
|
|
|
return listen (socket, backlog < 0 ? SOMAXCONN : backlog) == SOCKET_ERROR ? -1 : 0;
|
|
|
|
}
|
2004-10-20 10:17:51 -07:00
|
|
|
|
2008-09-15 16:11:22 -07:00
|
|
|
ENetSocket
|
|
|
|
enet_socket_create (ENetSocketType type)
|
|
|
|
{
|
|
|
|
return socket (PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
2007-10-02 21:10:09 -07:00
|
|
|
int
|
|
|
|
enet_socket_set_option (ENetSocket socket, ENetSocketOption option, int value)
|
|
|
|
{
|
|
|
|
int result = SOCKET_ERROR;
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case ENET_SOCKOPT_NONBLOCK:
|
|
|
|
{
|
|
|
|
u_long nonBlocking = (u_long) value;
|
|
|
|
result = ioctlsocket (socket, FIONBIO, & nonBlocking);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_BROADCAST:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_BROADCAST, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
2008-09-15 16:11:22 -07:00
|
|
|
case ENET_SOCKOPT_REUSEADDR:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_REUSEADDR, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
2007-10-02 21:10:09 -07:00
|
|
|
case ENET_SOCKOPT_RCVBUF:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_RCVBUF, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_SNDBUF:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_SNDBUF, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
2012-02-18 03:31:56 -07:00
|
|
|
case ENET_SOCKOPT_RCVTIMEO:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_RCVTIMEO, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_SNDTIMEO:
|
|
|
|
result = setsockopt (socket, SOL_SOCKET, SO_SNDTIMEO, (char *) & value, sizeof (int));
|
2013-09-26 23:42:37 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case ENET_SOCKOPT_NODELAY:
|
|
|
|
result = setsockopt (socket, IPPROTO_TCP, TCP_NODELAY, (char *) & value, sizeof (int));
|
2012-02-18 03:31:56 -07:00
|
|
|
break;
|
|
|
|
|
2023-01-08 07:58:31 -07:00
|
|
|
case ENET_SOCKOPT_TTL:
|
|
|
|
result = setsockopt (socket, IPPROTO_IP, IP_TTL, (char *) & value, sizeof (int));
|
|
|
|
break;
|
|
|
|
|
2007-10-02 21:10:09 -07:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result == SOCKET_ERROR ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:14:02 -07:00
|
|
|
int
|
|
|
|
enet_socket_get_option (ENetSocket socket, ENetSocketOption option, int * value)
|
|
|
|
{
|
2013-08-15 15:18:21 -07:00
|
|
|
int result = SOCKET_ERROR, len;
|
2013-08-15 14:14:02 -07:00
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case ENET_SOCKOPT_ERROR:
|
2013-08-15 15:18:21 -07:00
|
|
|
len = sizeof(int);
|
|
|
|
result = getsockopt (socket, SOL_SOCKET, SO_ERROR, (char *) value, & len);
|
2013-08-15 14:35:28 -07:00
|
|
|
break;
|
|
|
|
|
2023-01-08 07:58:31 -07:00
|
|
|
case ENET_SOCKOPT_TTL:
|
|
|
|
len = sizeof(int);
|
|
|
|
result = getsockopt (socket, IPPROTO_IP, IP_TTL, (char *) value, & len);
|
|
|
|
break;
|
|
|
|
|
2013-08-15 14:35:28 -07:00
|
|
|
default:
|
2013-08-15 14:14:02 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result == SOCKET_ERROR ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
int
|
|
|
|
enet_socket_connect (ENetSocket socket, const ENetAddress * address)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
2012-09-18 06:38:10 -07:00
|
|
|
int result;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
memset (& sin, 0, sizeof (struct sockaddr_in));
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
|
|
|
sin.sin_addr.s_addr = address -> host;
|
|
|
|
|
2012-09-18 06:38:10 -07:00
|
|
|
result = connect (socket, (struct sockaddr *) & sin, sizeof (struct sockaddr_in));
|
2012-09-26 04:06:58 -07:00
|
|
|
if (result == SOCKET_ERROR && WSAGetLastError () != WSAEWOULDBLOCK)
|
2012-09-18 06:38:10 -07:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ENetSocket
|
|
|
|
enet_socket_accept (ENetSocket socket, ENetAddress * address)
|
|
|
|
{
|
2006-03-14 16:21:50 -07:00
|
|
|
SOCKET result;
|
2004-10-20 10:17:51 -07:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
int sinLength = sizeof (struct sockaddr_in);
|
|
|
|
|
|
|
|
result = accept (socket,
|
|
|
|
address != NULL ? (struct sockaddr *) & sin : NULL,
|
|
|
|
address != NULL ? & sinLength : NULL);
|
|
|
|
|
2006-03-14 16:21:50 -07:00
|
|
|
if (result == INVALID_SOCKET)
|
2004-10-20 10:17:51 -07:00
|
|
|
return ENET_SOCKET_NULL;
|
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
address -> host = (enet_uint32) sin.sin_addr.s_addr;
|
|
|
|
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-09-18 06:38:10 -07:00
|
|
|
int
|
|
|
|
enet_socket_shutdown (ENetSocket socket, ENetSocketShutdown how)
|
|
|
|
{
|
|
|
|
return shutdown (socket, (int) how) == SOCKET_ERROR ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
void
|
|
|
|
enet_socket_destroy (ENetSocket socket)
|
|
|
|
{
|
2012-09-10 16:28:50 -07:00
|
|
|
if (socket != INVALID_SOCKET)
|
|
|
|
closesocket (socket);
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_socket_send (ENetSocket socket,
|
|
|
|
const ENetAddress * address,
|
|
|
|
const ENetBuffer * buffers,
|
|
|
|
size_t bufferCount)
|
|
|
|
{
|
|
|
|
struct sockaddr_in sin;
|
2020-04-07 04:02:48 -07:00
|
|
|
DWORD sentLength = 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
2008-02-24 07:19:47 -07:00
|
|
|
memset (& sin, 0, sizeof (struct sockaddr_in));
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = ENET_HOST_TO_NET_16 (address -> port);
|
|
|
|
sin.sin_addr.s_addr = address -> host;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (WSASendTo (socket,
|
|
|
|
(LPWSABUF) buffers,
|
|
|
|
(DWORD) bufferCount,
|
|
|
|
& sentLength,
|
|
|
|
0,
|
2011-12-31 05:37:26 -07:00
|
|
|
address != NULL ? (struct sockaddr *) & sin : NULL,
|
2004-10-20 10:17:51 -07:00
|
|
|
address != NULL ? sizeof (struct sockaddr_in) : 0,
|
|
|
|
NULL,
|
|
|
|
NULL) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
if (WSAGetLastError () == WSAEWOULDBLOCK)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) sentLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
enet_socket_receive (ENetSocket socket,
|
|
|
|
ENetAddress * address,
|
|
|
|
ENetBuffer * buffers,
|
|
|
|
size_t bufferCount)
|
|
|
|
{
|
2004-11-23 09:23:36 -07:00
|
|
|
INT sinLength = sizeof (struct sockaddr_in);
|
|
|
|
DWORD flags = 0,
|
2020-04-07 03:33:26 -07:00
|
|
|
recvLength = 0;
|
2004-10-20 10:17:51 -07:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
if (WSARecvFrom (socket,
|
|
|
|
(LPWSABUF) buffers,
|
|
|
|
(DWORD) bufferCount,
|
|
|
|
& recvLength,
|
|
|
|
& flags,
|
|
|
|
address != NULL ? (struct sockaddr *) & sin : NULL,
|
|
|
|
address != NULL ? & sinLength : NULL,
|
|
|
|
NULL,
|
|
|
|
NULL) == SOCKET_ERROR)
|
|
|
|
{
|
|
|
|
switch (WSAGetLastError ())
|
|
|
|
{
|
|
|
|
case WSAEWOULDBLOCK:
|
|
|
|
case WSAECONNRESET:
|
|
|
|
return 0;
|
2024-05-20 11:19:30 -07:00
|
|
|
case WSAEINTR:
|
2023-07-23 18:05:47 -07:00
|
|
|
case WSAEMSGSIZE:
|
|
|
|
return -2;
|
2024-05-20 11:19:30 -07:00
|
|
|
default:
|
|
|
|
return -1;
|
2004-10-20 10:17:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & MSG_PARTIAL)
|
2023-07-23 18:05:47 -07:00
|
|
|
return -2;
|
2004-10-20 10:17:51 -07:00
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
address -> host = (enet_uint32) sin.sin_addr.s_addr;
|
|
|
|
address -> port = ENET_NET_TO_HOST_16 (sin.sin_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (int) recvLength;
|
|
|
|
}
|
|
|
|
|
2008-09-15 16:11:22 -07:00
|
|
|
int
|
|
|
|
enet_socketset_select (ENetSocket maxSocket, ENetSocketSet * readSet, ENetSocketSet * writeSet, enet_uint32 timeout)
|
|
|
|
{
|
|
|
|
struct timeval timeVal;
|
|
|
|
|
|
|
|
timeVal.tv_sec = timeout / 1000;
|
|
|
|
timeVal.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
|
|
|
|
return select (maxSocket + 1, readSet, writeSet, NULL, & timeVal);
|
|
|
|
}
|
|
|
|
|
2004-10-20 10:17:51 -07:00
|
|
|
int
|
|
|
|
enet_socket_wait (ENetSocket socket, enet_uint32 * condition, enet_uint32 timeout)
|
|
|
|
{
|
|
|
|
fd_set readSet, writeSet;
|
|
|
|
struct timeval timeVal;
|
|
|
|
int selectCount;
|
|
|
|
|
|
|
|
timeVal.tv_sec = timeout / 1000;
|
|
|
|
timeVal.tv_usec = (timeout % 1000) * 1000;
|
|
|
|
|
|
|
|
FD_ZERO (& readSet);
|
|
|
|
FD_ZERO (& writeSet);
|
|
|
|
|
|
|
|
if (* condition & ENET_SOCKET_WAIT_SEND)
|
|
|
|
FD_SET (socket, & writeSet);
|
|
|
|
|
|
|
|
if (* condition & ENET_SOCKET_WAIT_RECEIVE)
|
|
|
|
FD_SET (socket, & readSet);
|
|
|
|
|
|
|
|
selectCount = select (socket + 1, & readSet, & writeSet, NULL, & timeVal);
|
|
|
|
|
|
|
|
if (selectCount < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
* condition = ENET_SOCKET_WAIT_NONE;
|
|
|
|
|
|
|
|
if (selectCount == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (FD_ISSET (socket, & writeSet))
|
|
|
|
* condition |= ENET_SOCKET_WAIT_SEND;
|
|
|
|
|
|
|
|
if (FD_ISSET (socket, & readSet))
|
|
|
|
* condition |= ENET_SOCKET_WAIT_RECEIVE;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|