2019-01-05 20:38:43 -08:00
|
|
|
/*
|
|
|
|
* IXNetSystem.h
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2019 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-02-26 00:50:35 +03:00
|
|
|
#include <cstdint>
|
|
|
|
|
2023-03-13 09:36:25 -07:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#endif
|
|
|
|
|
2019-01-05 20:38:43 -08:00
|
|
|
#ifdef _WIN32
|
2021-03-15 19:58:18 -07:00
|
|
|
|
|
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
|
2021-12-23 09:48:20 +03:00
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <winsock2.h>
|
2019-05-30 08:46:50 -07:00
|
|
|
#include <basetsd.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <ws2def.h>
|
2022-01-04 23:13:19 +03:00
|
|
|
#include <cerrno>
|
2019-08-19 22:26:25 -07:00
|
|
|
|
2021-05-17 19:04:02 -07:00
|
|
|
#undef EWOULDBLOCK
|
|
|
|
#undef EAGAIN
|
|
|
|
#undef EINPROGRESS
|
|
|
|
#undef EBADF
|
|
|
|
#undef EINVAL
|
|
|
|
|
|
|
|
// map to WSA error codes
|
|
|
|
#define EWOULDBLOCK WSAEWOULDBLOCK
|
|
|
|
#define EAGAIN WSATRY_AGAIN
|
|
|
|
#define EINPROGRESS WSAEINPROGRESS
|
|
|
|
#define EBADF WSAEBADF
|
|
|
|
#define EINVAL WSAEINVAL
|
|
|
|
|
2019-09-08 11:14:49 -07:00
|
|
|
// Define our own poll on Windows, as a wrapper on top of select
|
2019-08-22 10:34:17 -07:00
|
|
|
typedef unsigned long int nfds_t;
|
|
|
|
|
2021-11-16 23:22:51 +00:00
|
|
|
// pollfd is not defined by some versions of mingw64 since _WIN32_WINNT is too low
|
|
|
|
#if _WIN32_WINNT < 0x0600
|
2021-03-16 09:56:08 -07:00
|
|
|
struct pollfd
|
|
|
|
{
|
|
|
|
int fd; /* file descriptor */
|
|
|
|
short events; /* requested events */
|
|
|
|
short revents; /* returned events */
|
2021-03-13 18:49:29 -08:00
|
|
|
};
|
|
|
|
|
2021-03-16 09:56:08 -07:00
|
|
|
#define POLLIN 0x001 /* There is data to read. */
|
|
|
|
#define POLLOUT 0x004 /* Writing now will not block. */
|
|
|
|
#define POLLERR 0x008 /* Error condition. */
|
|
|
|
#define POLLHUP 0x010 /* Hung up. */
|
|
|
|
#define POLLNVAL 0x020 /* Invalid polling request. */
|
2021-03-13 18:49:29 -08:00
|
|
|
#endif
|
|
|
|
|
2019-01-05 20:38:43 -08:00
|
|
|
#else
|
2019-05-30 08:46:50 -07:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <errno.h>
|
2020-05-06 16:26:30 -07:00
|
|
|
#include <fcntl.h>
|
2019-05-30 08:46:50 -07:00
|
|
|
#include <netdb.h>
|
2019-10-09 15:38:40 -07:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
2019-10-13 13:37:34 -07:00
|
|
|
#include <netinet/tcp.h>
|
2019-08-22 10:34:17 -07:00
|
|
|
#include <poll.h>
|
2019-05-30 08:46:50 -07:00
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <unistd.h>
|
2019-01-05 20:38:43 -08:00
|
|
|
#endif
|
2019-04-26 02:26:53 +03:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
2020-12-12 11:00:55 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
typedef SOCKET socket_t;
|
|
|
|
#else
|
|
|
|
typedef int socket_t;
|
|
|
|
#endif
|
|
|
|
|
2019-04-26 02:26:53 +03:00
|
|
|
bool initNetSystem();
|
|
|
|
bool uninitNetSystem();
|
2019-09-08 11:14:49 -07:00
|
|
|
|
2022-01-05 19:21:33 +01:00
|
|
|
int poll(struct pollfd* fds, nfds_t nfds, int timeout, void** event);
|
2021-03-23 20:53:19 -07:00
|
|
|
|
|
|
|
const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
|
|
|
|
int inet_pton(int af, const char* src, void* dst);
|
2021-10-22 19:23:43 +02:00
|
|
|
|
|
|
|
unsigned short network_to_host_short(unsigned short value);
|
2019-05-30 08:46:50 -07:00
|
|
|
} // namespace ix
|