2020-09-13 21:32:55 -04:00
|
|
|
#pragma once
|
|
|
|
|
2020-11-21 12:52:38 -05:00
|
|
|
/*!
|
2022-05-06 18:19:37 -04:00
|
|
|
* @file XSocket.h
|
2020-11-21 12:52:38 -05:00
|
|
|
* Cross platform socket library used for the listener.
|
|
|
|
*/
|
|
|
|
|
2022-06-22 23:37:46 -04:00
|
|
|
// clang-format off
|
2022-12-22 17:12:05 -05:00
|
|
|
#include "common/common_types.h"
|
|
|
|
#ifdef OS_POSIX
|
2022-06-22 23:37:46 -04:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netinet/in.h>
|
2020-09-07 19:58:54 -04:00
|
|
|
#include <netinet/tcp.h>
|
2022-06-22 23:37:46 -04:00
|
|
|
#include <sys/socket.h>
|
2020-09-07 19:58:54 -04:00
|
|
|
#include <unistd.h>
|
|
|
|
#elif _WIN32
|
2022-05-06 18:19:37 -04:00
|
|
|
#define NOMINMAX
|
|
|
|
#define WIN32_LEAN_AND_MEAN
|
|
|
|
#include <Windows.h>
|
2020-09-07 19:58:54 -04:00
|
|
|
#include <WinSock2.h>
|
|
|
|
#endif
|
2022-06-22 23:37:46 -04:00
|
|
|
// clang-format on
|
2020-09-07 19:58:54 -04:00
|
|
|
|
|
|
|
#ifdef __linux
|
|
|
|
const int TCP_SOCKET_LEVEL = SOL_TCP;
|
|
|
|
#elif _WIN32
|
2020-09-08 00:01:46 -04:00
|
|
|
const int TCP_SOCKET_LEVEL = IPPROTO_TCP;
|
2022-12-22 17:12:05 -05:00
|
|
|
#elif __APPLE__
|
|
|
|
const int TCP_SOCKET_LEVEL = IPPROTO_TCP;
|
2020-09-07 19:58:54 -04:00
|
|
|
#endif
|
|
|
|
|
2020-09-08 00:01:46 -04:00
|
|
|
int open_socket(int af, int type, int protocol);
|
2022-06-11 11:32:27 -04:00
|
|
|
int connect_socket(int socket, sockaddr* addr, int nameLen);
|
2022-12-22 17:12:05 -05:00
|
|
|
#ifdef OS_POSIX
|
2022-05-06 18:19:37 -04:00
|
|
|
int accept_socket(int socket, sockaddr* addr, socklen_t* addrLen);
|
2022-05-21 15:49:01 -04:00
|
|
|
int select_and_accept_socket(int socket, sockaddr* addr, socklen_t* addrLen, int microSeconds);
|
2022-05-06 18:19:37 -04:00
|
|
|
#elif _WIN32
|
|
|
|
int accept_socket(int socket, sockaddr* addr, int* addrLen);
|
2022-05-21 15:49:01 -04:00
|
|
|
int select_and_accept_socket(int socket, sockaddr* addr, int* addrLen, int microSeconds);
|
2022-05-06 18:19:37 -04:00
|
|
|
#endif
|
2020-09-07 19:58:54 -04:00
|
|
|
void close_socket(int sock);
|
2020-09-08 19:15:14 -04:00
|
|
|
int set_socket_option(int socket, int level, int optname, const void* optval, int optlen);
|
|
|
|
int set_socket_timeout(int socket, long microSeconds);
|
2020-09-07 19:58:54 -04:00
|
|
|
int write_to_socket(int socket, const char* buf, int len);
|
|
|
|
int read_from_socket(int socket, char* buf, int len);
|
2022-05-06 18:19:37 -04:00
|
|
|
bool socket_timed_out();
|
2023-08-08 20:53:16 -04:00
|
|
|
std::string address_to_string(const sockaddr_in& addr);
|