2015-05-05 19:51:38 +00:00
|
|
|
/**
|
|
|
|
@file unix.c
|
|
|
|
@brief ENet Unix system specific functions
|
|
|
|
*/
|
|
|
|
#ifndef WIN32
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#define ENET_BUILDING_LIB 1
|
|
|
|
#include "enet/enet.h"
|
|
|
|
|
|
|
|
#ifdef __APPLE__
|
|
|
|
#ifdef HAS_POLL
|
|
|
|
#undef HAS_POLL
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_FCNTL
|
|
|
|
#define HAS_FCNTL 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_INET_PTON
|
|
|
|
#define HAS_INET_PTON 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_INET_NTOP
|
|
|
|
#define HAS_INET_NTOP 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_MSGHDR_FLAGS
|
|
|
|
#define HAS_MSGHDR_FLAGS 1
|
|
|
|
#endif
|
|
|
|
#ifndef HAS_SOCKLEN_T
|
|
|
|
#define HAS_SOCKLEN_T 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_FCNTL
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAS_POLL
|
|
|
|
#include <sys/poll.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAS_SOCKLEN_T
|
|
|
|
typedef int socklen_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MSG_NOSIGNAL
|
|
|
|
#define MSG_NOSIGNAL 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static enet_uint32 timeBase = 0;
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_initialize(void)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void enet_deinitialize(void)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
enet_uint32
|
2018-09-23 21:17:31 +00:00
|
|
|
enet_time_get(void)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct timeval timeVal;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
gettimeofday(&timeVal, NULL);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - timeBase;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void enet_time_set(enet_uint32 newTimeBase)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct timeval timeVal;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
gettimeofday(&timeVal, NULL);
|
|
|
|
|
|
|
|
timeBase = timeVal.tv_sec * 1000 + timeVal.tv_usec / 1000 - newTimeBase;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_address_set_host(ENetAddress *address, const char *name)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct hostent *hostEntry = NULL;
|
2015-05-05 19:51:38 +00:00
|
|
|
#ifdef HAS_GETHOSTBYNAME_R
|
2018-09-23 21:17:31 +00:00
|
|
|
struct hostent hostData;
|
|
|
|
char buffer[2048];
|
|
|
|
int errnum;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
|
|
|
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2018-09-23 21:17:31 +00:00
|
|
|
gethostbyname_r(name, &hostData, buffer, sizeof(buffer), &hostEntry, &errnum);
|
2015-05-05 19:51:38 +00:00
|
|
|
#else
|
2018-09-23 21:17:31 +00:00
|
|
|
hostEntry = gethostbyname_r(name, &hostData, buffer, sizeof(buffer), &errnum);
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2018-09-23 21:17:31 +00:00
|
|
|
hostEntry = gethostbyname(name);
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (hostEntry == NULL ||
|
|
|
|
hostEntry->h_addrtype != AF_INET)
|
|
|
|
{
|
2015-05-05 19:51:38 +00:00
|
|
|
#ifdef HAS_INET_PTON
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!inet_pton(AF_INET, name, &address->host))
|
2015-05-05 19:51:38 +00:00
|
|
|
#else
|
2018-09-23 21:17:31 +00:00
|
|
|
if (!inet_aton(name, (struct in_addr *)&address->host))
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
2018-09-23 21:17:31 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
address->host = *(enet_uint32 *)hostEntry->h_addr_list[0];
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_address_get_host_ip(const ENetAddress *address, char *name, size_t nameLength)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
|
|
|
#ifdef HAS_INET_NTOP
|
2018-09-23 21:17:31 +00:00
|
|
|
if (inet_ntop(AF_INET, &address->host, name, nameLength) == NULL)
|
2015-05-05 19:51:38 +00:00
|
|
|
#else
|
2018-09-23 21:17:31 +00:00
|
|
|
char *addr = inet_ntoa(*(struct in_addr *)&address->host);
|
|
|
|
if (addr != NULL)
|
|
|
|
strncpy(name, addr, nameLength);
|
|
|
|
else
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
2018-09-23 21:17:31 +00:00
|
|
|
return -1;
|
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_address_get_host(const ENetAddress *address, char *name, size_t nameLength)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct in_addr in;
|
|
|
|
struct hostent *hostEntry = NULL;
|
2015-05-05 19:51:38 +00:00
|
|
|
#ifdef HAS_GETHOSTBYADDR_R
|
2018-09-23 21:17:31 +00:00
|
|
|
struct hostent hostData;
|
|
|
|
char buffer[2048];
|
|
|
|
int errnum;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
in.s_addr = address->host;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
|
|
|
#if defined(linux) || defined(__linux) || defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
|
2018-09-23 21:17:31 +00:00
|
|
|
gethostbyaddr_r((char *)&in, sizeof(struct in_addr), AF_INET, &hostData, buffer, sizeof(buffer), &hostEntry, &errnum);
|
2015-05-05 19:51:38 +00:00
|
|
|
#else
|
2018-09-23 21:17:31 +00:00
|
|
|
hostEntry = gethostbyaddr_r((char *)&in, sizeof(struct in_addr), AF_INET, &hostData, buffer, sizeof(buffer), &errnum);
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
|
|
|
#else
|
2018-09-23 21:17:31 +00:00
|
|
|
in.s_addr = address->host;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
hostEntry = gethostbyaddr((char *)&in, sizeof(struct in_addr), AF_INET);
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (hostEntry == NULL)
|
|
|
|
return enet_address_get_host_ip(address, name, nameLength);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
strncpy(name, hostEntry->h_name, nameLength);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_socket_bind(ENetSocket socket, const ENetAddress *address)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
|
|
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bind(socket,
|
|
|
|
(struct sockaddr *)&sin,
|
|
|
|
sizeof(struct sockaddr_in));
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_socket_listen(ENetSocket socket, int backlog)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
return listen(socket, backlog < 0 ? SOMAXCONN : backlog);
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ENetSocket
|
2018-09-23 21:17:31 +00:00
|
|
|
enet_socket_create(ENetSocketType type)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
return socket(PF_INET, type == ENET_SOCKET_TYPE_DATAGRAM ? SOCK_DGRAM : SOCK_STREAM, 0);
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_socket_set_option(ENetSocket socket, ENetSocketOption option, int value)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
int result = -1;
|
|
|
|
switch (option)
|
|
|
|
{
|
|
|
|
case ENET_SOCKOPT_NONBLOCK:
|
2015-05-05 19:51:38 +00:00
|
|
|
#ifdef HAS_FCNTL
|
2018-09-23 21:17:31 +00:00
|
|
|
result = fcntl(socket, F_SETFL, O_NONBLOCK | fcntl(socket, F_GETFL));
|
2015-05-05 19:51:38 +00:00
|
|
|
#else
|
2018-09-23 21:17:31 +00:00
|
|
|
result = ioctl(socket, FIONBIO, &value);
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
2018-09-23 21:17:31 +00:00
|
|
|
break;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
case ENET_SOCKOPT_BROADCAST:
|
|
|
|
result = setsockopt(socket, SOL_SOCKET, SO_BROADCAST, (char *)&value, sizeof(int));
|
|
|
|
break;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
case ENET_SOCKOPT_REUSEADDR:
|
|
|
|
result = setsockopt(socket, SOL_SOCKET, SO_REUSEADDR, (char *)&value, sizeof(int));
|
|
|
|
break;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
case ENET_SOCKOPT_RCVBUF:
|
|
|
|
result = setsockopt(socket, SOL_SOCKET, SO_RCVBUF, (char *)&value, sizeof(int));
|
|
|
|
break;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
case ENET_SOCKOPT_SNDBUF:
|
|
|
|
result = setsockopt(socket, SOL_SOCKET, SO_SNDBUF, (char *)&value, sizeof(int));
|
|
|
|
break;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
case ENET_SOCKOPT_RCVTIMEO:
|
|
|
|
result = setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&value, sizeof(int));
|
|
|
|
break;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
case ENET_SOCKOPT_SNDTIMEO:
|
|
|
|
result = setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&value, sizeof(int));
|
|
|
|
break;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return result == -1 ? -1 : 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_socket_connect(ENetSocket socket, const ENetAddress *address)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct sockaddr_in sin;
|
|
|
|
int result;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
memset(&sin, 0, sizeof(struct sockaddr_in));
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = ENET_HOST_TO_NET_16(address->port);
|
|
|
|
sin.sin_addr.s_addr = address->host;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
result = connect(socket, (struct sockaddr *)&sin, sizeof(struct sockaddr_in));
|
|
|
|
if (result == -1 && errno == EINPROGRESS)
|
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return result;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ENetSocket
|
2018-09-23 21:17:31 +00:00
|
|
|
enet_socket_accept(ENetSocket socket, ENetAddress *address)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
int result;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
socklen_t sinLength = sizeof(struct sockaddr_in);
|
|
|
|
|
|
|
|
result = accept(socket,
|
|
|
|
address != NULL ? (struct sockaddr *)&sin : NULL,
|
|
|
|
address != NULL ? &sinLength : NULL);
|
|
|
|
|
|
|
|
if (result == -1)
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
int enet_socket_shutdown(ENetSocket socket, ENetSocketShutdown how)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
return shutdown(socket, (int)how);
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
void enet_socket_destroy(ENetSocket socket)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
if (socket != -1)
|
|
|
|
close(socket);
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_socket_send(ENetSocket socket,
|
|
|
|
const ENetAddress *address,
|
|
|
|
const ENetBuffer *buffers,
|
|
|
|
size_t bufferCount)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct msghdr msgHdr;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int sentLength;
|
|
|
|
|
|
|
|
memset(&msgHdr, 0, sizeof(struct msghdr));
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
memset(&sin, 0, sizeof(struct sockaddr_in));
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = ENET_HOST_TO_NET_16(address->port);
|
|
|
|
sin.sin_addr.s_addr = address->host;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
msgHdr.msg_name = &sin;
|
|
|
|
msgHdr.msg_namelen = sizeof(struct sockaddr_in);
|
|
|
|
}
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
msgHdr.msg_iov = (struct iovec *)buffers;
|
|
|
|
msgHdr.msg_iovlen = bufferCount;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
sentLength = sendmsg(socket, &msgHdr, MSG_NOSIGNAL);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (sentLength == -1)
|
|
|
|
{
|
|
|
|
if (errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return sentLength;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_socket_receive(ENetSocket socket,
|
|
|
|
ENetAddress *address,
|
|
|
|
ENetBuffer *buffers,
|
|
|
|
size_t bufferCount)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct msghdr msgHdr;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
int recvLength;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
memset(&msgHdr, 0, sizeof(struct msghdr));
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
msgHdr.msg_name = &sin;
|
|
|
|
msgHdr.msg_namelen = sizeof(struct sockaddr_in);
|
|
|
|
}
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
msgHdr.msg_iov = (struct iovec *)buffers;
|
|
|
|
msgHdr.msg_iovlen = bufferCount;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
recvLength = recvmsg(socket, &msgHdr, MSG_NOSIGNAL);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (recvLength == -1)
|
|
|
|
{
|
|
|
|
if (errno == EWOULDBLOCK)
|
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2015-05-05 19:51:38 +00:00
|
|
|
|
|
|
|
#ifdef HAS_MSGHDR_FLAGS
|
2018-09-23 21:17:31 +00:00
|
|
|
if (msgHdr.msg_flags & MSG_TRUNC)
|
|
|
|
return -1;
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (address != NULL)
|
|
|
|
{
|
|
|
|
address->host = (enet_uint32)sin.sin_addr.s_addr;
|
|
|
|
address->port = ENET_NET_TO_HOST_16(sin.sin_port);
|
|
|
|
}
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return recvLength;
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_socketset_select(ENetSocket maxSocket, ENetSocketSet *readSet, ENetSocketSet *writeSet, enet_uint32 timeout)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
2018-09-23 21:17:31 +00:00
|
|
|
struct timeval timeVal;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
timeVal.tv_sec = timeout / 1000;
|
|
|
|
timeVal.tv_usec = (timeout % 1000) * 1000;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return select(maxSocket + 1, readSet, writeSet, NULL, &timeVal);
|
2015-05-05 19:51:38 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
int enet_socket_wait(ENetSocket socket, enet_uint32 *condition, enet_uint32 timeout)
|
2015-05-05 19:51:38 +00:00
|
|
|
{
|
|
|
|
#ifdef HAS_POLL
|
2018-09-23 21:17:31 +00:00
|
|
|
struct pollfd pollSocket;
|
|
|
|
int pollCount;
|
|
|
|
|
|
|
|
pollSocket.fd = socket;
|
|
|
|
pollSocket.events = 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (*condition & ENET_SOCKET_WAIT_SEND)
|
|
|
|
pollSocket.events |= POLLOUT;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (*condition & ENET_SOCKET_WAIT_RECEIVE)
|
|
|
|
pollSocket.events |= POLLIN;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
pollCount = poll(&pollSocket, 1, timeout);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (pollCount < 0)
|
|
|
|
return -1;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
*condition = ENET_SOCKET_WAIT_NONE;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (pollCount == 0)
|
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (pollSocket.revents & POLLOUT)
|
|
|
|
*condition |= ENET_SOCKET_WAIT_SEND;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (pollSocket.revents & POLLIN)
|
|
|
|
*condition |= ENET_SOCKET_WAIT_RECEIVE;
|
|
|
|
|
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
#else
|
2018-09-23 21:17:31 +00:00
|
|
|
fd_set readSet, writeSet;
|
|
|
|
struct timeval timeVal;
|
|
|
|
int selectCount;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
timeVal.tv_sec = timeout / 1000;
|
|
|
|
timeVal.tv_usec = (timeout % 1000) * 1000;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
FD_ZERO(&readSet);
|
|
|
|
FD_ZERO(&writeSet);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (*condition & ENET_SOCKET_WAIT_SEND)
|
|
|
|
FD_SET(socket, &writeSet);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (*condition & ENET_SOCKET_WAIT_RECEIVE)
|
|
|
|
FD_SET(socket, &readSet);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
selectCount = select(socket + 1, &readSet, &writeSet, NULL, &timeVal);
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (selectCount < 0)
|
|
|
|
return -1;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
*condition = ENET_SOCKET_WAIT_NONE;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (selectCount == 0)
|
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (FD_ISSET(socket, &writeSet))
|
|
|
|
*condition |= ENET_SOCKET_WAIT_SEND;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
if (FD_ISSET(socket, &readSet))
|
|
|
|
*condition |= ENET_SOCKET_WAIT_RECEIVE;
|
2015-05-05 19:51:38 +00:00
|
|
|
|
2018-09-23 21:17:31 +00:00
|
|
|
return 0;
|
2015-05-05 19:51:38 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|