2021-04-29 14:30:11 +02:00
|
|
|
#include <utils/networking.h>
|
2021-07-16 01:49:33 +02:00
|
|
|
#include <assert.h>
|
2023-02-25 18:12:34 +01:00
|
|
|
#include <utils/flog.h>
|
2023-03-10 17:59:13 +01:00
|
|
|
#include <stdexcept>
|
2021-04-29 14:30:11 +02:00
|
|
|
|
2021-07-16 01:49:33 +02:00
|
|
|
namespace net {
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
extern bool winsock_init = false;
|
|
|
|
#endif
|
|
|
|
|
2021-07-30 21:56:34 +02:00
|
|
|
ConnClass::ConnClass(Socket sock, struct sockaddr_in raddr, bool udp) {
|
2021-07-16 01:49:33 +02:00
|
|
|
_sock = sock;
|
2021-07-30 21:56:34 +02:00
|
|
|
_udp = udp;
|
|
|
|
remoteAddr = raddr;
|
2021-07-16 01:49:33 +02:00
|
|
|
connectionOpen = true;
|
|
|
|
readWorkerThread = std::thread(&ConnClass::readWorker, this);
|
|
|
|
writeWorkerThread = std::thread(&ConnClass::writeWorker, this);
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-07-16 01:49:33 +02:00
|
|
|
ConnClass::~ConnClass() {
|
|
|
|
ConnClass::close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnClass::close() {
|
|
|
|
std::lock_guard lck(closeMtx);
|
|
|
|
// Set stopWorkers to true
|
|
|
|
{
|
|
|
|
std::lock_guard lck1(readQueueMtx);
|
|
|
|
std::lock_guard lck2(writeQueueMtx);
|
|
|
|
stopWorkers = true;
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-07-16 01:49:33 +02:00
|
|
|
// Notify the workers of the change
|
|
|
|
readQueueCnd.notify_all();
|
|
|
|
writeQueueCnd.notify_all();
|
|
|
|
|
|
|
|
if (connectionOpen) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket(_sock);
|
|
|
|
#else
|
2021-07-19 15:57:37 +02:00
|
|
|
::shutdown(_sock, SHUT_RDWR);
|
2021-07-16 01:49:33 +02:00
|
|
|
::close(_sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the theads to terminate
|
|
|
|
if (readWorkerThread.joinable()) { readWorkerThread.join(); }
|
|
|
|
if (writeWorkerThread.joinable()) { writeWorkerThread.join(); }
|
|
|
|
|
|
|
|
{
|
|
|
|
std::lock_guard lck(connectionOpenMtx);
|
|
|
|
connectionOpen = false;
|
|
|
|
}
|
|
|
|
connectionOpenCnd.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ConnClass::isOpen() {
|
|
|
|
return connectionOpen;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnClass::waitForEnd() {
|
|
|
|
std::unique_lock lck(readQueueMtx);
|
2021-12-19 22:11:44 +01:00
|
|
|
connectionOpenCnd.wait(lck, [this]() { return !connectionOpen; });
|
2021-07-16 01:49:33 +02:00
|
|
|
}
|
|
|
|
|
2022-01-22 16:36:48 +01:00
|
|
|
int ConnClass::read(int count, uint8_t* buf, bool enforceSize) {
|
2021-07-16 18:13:18 +02:00
|
|
|
if (!connectionOpen) { return -1; }
|
2021-07-16 01:49:33 +02:00
|
|
|
std::lock_guard lck(readMtx);
|
2021-07-30 21:56:34 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (_udp) {
|
2021-07-30 22:06:06 +02:00
|
|
|
socklen_t fromLen = sizeof(remoteAddr);
|
2021-07-30 21:56:34 +02:00
|
|
|
ret = recvfrom(_sock, (char*)buf, count, 0, (struct sockaddr*)&remoteAddr, &fromLen);
|
2022-01-21 20:22:13 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
{
|
|
|
|
std::lock_guard lck(connectionOpenMtx);
|
|
|
|
connectionOpen = false;
|
|
|
|
}
|
|
|
|
connectionOpenCnd.notify_all();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return count;
|
2021-07-30 21:56:34 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 20:22:13 +01:00
|
|
|
int beenRead = 0;
|
|
|
|
while (beenRead < count) {
|
|
|
|
ret = recv(_sock, (char*)&buf[beenRead], count - beenRead, 0);
|
|
|
|
|
|
|
|
if (ret <= 0) {
|
|
|
|
{
|
|
|
|
std::lock_guard lck(connectionOpenMtx);
|
|
|
|
connectionOpen = false;
|
|
|
|
}
|
|
|
|
connectionOpenCnd.notify_all();
|
|
|
|
return -1;
|
2021-07-16 01:49:33 +02:00
|
|
|
}
|
2022-01-21 20:22:13 +01:00
|
|
|
|
2022-01-22 16:36:48 +01:00
|
|
|
if (!enforceSize) { return ret; }
|
|
|
|
|
2022-01-21 20:22:13 +01:00
|
|
|
beenRead += ret;
|
2021-07-16 01:49:33 +02:00
|
|
|
}
|
2022-01-21 20:22:13 +01:00
|
|
|
|
|
|
|
return beenRead;
|
2021-07-16 01:49:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ConnClass::write(int count, uint8_t* buf) {
|
2021-07-16 18:13:18 +02:00
|
|
|
if (!connectionOpen) { return false; }
|
2021-07-16 01:49:33 +02:00
|
|
|
std::lock_guard lck(writeMtx);
|
2021-07-30 21:56:34 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (_udp) {
|
|
|
|
ret = sendto(_sock, (char*)buf, count, 0, (struct sockaddr*)&remoteAddr, sizeof(remoteAddr));
|
2022-01-21 20:22:13 +01:00
|
|
|
if (ret <= 0) {
|
|
|
|
{
|
|
|
|
std::lock_guard lck(connectionOpenMtx);
|
|
|
|
connectionOpen = false;
|
|
|
|
}
|
|
|
|
connectionOpenCnd.notify_all();
|
|
|
|
}
|
|
|
|
return (ret > 0);
|
2021-07-30 21:56:34 +02:00
|
|
|
}
|
|
|
|
|
2022-01-21 20:22:13 +01:00
|
|
|
int beenWritten = 0;
|
|
|
|
while (beenWritten < count) {
|
|
|
|
ret = send(_sock, (char*)buf, count, 0);
|
|
|
|
if (ret <= 0) {
|
|
|
|
{
|
|
|
|
std::lock_guard lck(connectionOpenMtx);
|
|
|
|
connectionOpen = false;
|
|
|
|
}
|
|
|
|
connectionOpenCnd.notify_all();
|
|
|
|
return false;
|
2021-07-16 01:49:33 +02:00
|
|
|
}
|
2022-01-21 20:22:13 +01:00
|
|
|
beenWritten += ret;
|
2021-07-16 01:49:33 +02:00
|
|
|
}
|
2022-01-21 20:22:13 +01:00
|
|
|
|
|
|
|
return true;
|
2021-07-16 01:49:33 +02:00
|
|
|
}
|
|
|
|
|
2022-01-22 16:36:48 +01:00
|
|
|
void ConnClass::readAsync(int count, uint8_t* buf, void (*handler)(int count, uint8_t* buf, void* ctx), void* ctx, bool enforceSize) {
|
2021-07-16 18:13:18 +02:00
|
|
|
if (!connectionOpen) { return; }
|
2021-07-16 01:49:33 +02:00
|
|
|
// Create entry
|
|
|
|
ConnReadEntry entry;
|
|
|
|
entry.count = count;
|
|
|
|
entry.buf = buf;
|
|
|
|
entry.handler = handler;
|
|
|
|
entry.ctx = ctx;
|
2022-01-22 16:36:48 +01:00
|
|
|
entry.enforceSize = enforceSize;
|
2021-07-16 01:49:33 +02:00
|
|
|
|
|
|
|
// Add entry to queue
|
|
|
|
{
|
|
|
|
std::lock_guard lck(readQueueMtx);
|
|
|
|
readQueue.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify read worker
|
|
|
|
readQueueCnd.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnClass::writeAsync(int count, uint8_t* buf) {
|
2021-07-16 18:13:18 +02:00
|
|
|
if (!connectionOpen) { return; }
|
2021-07-16 01:49:33 +02:00
|
|
|
// Create entry
|
|
|
|
ConnWriteEntry entry;
|
|
|
|
entry.count = count;
|
|
|
|
entry.buf = buf;
|
|
|
|
|
|
|
|
// Add entry to queue
|
|
|
|
{
|
|
|
|
std::lock_guard lck(writeQueueMtx);
|
|
|
|
writeQueue.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify write worker
|
|
|
|
writeQueueCnd.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnClass::readWorker() {
|
|
|
|
while (true) {
|
|
|
|
// Wait for wakeup and exit if it's for terminating the thread
|
|
|
|
std::unique_lock lck(readQueueMtx);
|
2021-12-19 22:11:44 +01:00
|
|
|
readQueueCnd.wait(lck, [this]() { return (readQueue.size() > 0 || stopWorkers); });
|
2021-07-16 01:49:33 +02:00
|
|
|
if (stopWorkers || !connectionOpen) { return; }
|
|
|
|
|
|
|
|
// Pop first element off the list
|
|
|
|
ConnReadEntry entry = readQueue[0];
|
|
|
|
readQueue.erase(readQueue.begin());
|
|
|
|
lck.unlock();
|
|
|
|
|
|
|
|
// Read from socket and send data to the handler
|
2022-01-22 16:36:48 +01:00
|
|
|
int ret = read(entry.count, entry.buf, entry.enforceSize);
|
2021-07-16 01:49:33 +02:00
|
|
|
if (ret <= 0) {
|
|
|
|
{
|
|
|
|
std::lock_guard lck(connectionOpenMtx);
|
|
|
|
connectionOpen = false;
|
|
|
|
}
|
|
|
|
connectionOpenCnd.notify_all();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
entry.handler(ret, entry.buf, entry.ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConnClass::writeWorker() {
|
|
|
|
while (true) {
|
|
|
|
// Wait for wakeup and exit if it's for terminating the thread
|
|
|
|
std::unique_lock lck(writeQueueMtx);
|
2021-12-19 22:11:44 +01:00
|
|
|
writeQueueCnd.wait(lck, [this]() { return (writeQueue.size() > 0 || stopWorkers); });
|
2021-07-16 01:49:33 +02:00
|
|
|
if (stopWorkers || !connectionOpen) { return; }
|
|
|
|
|
|
|
|
// Pop first element off the list
|
|
|
|
ConnWriteEntry entry = writeQueue[0];
|
|
|
|
writeQueue.erase(writeQueue.begin());
|
|
|
|
lck.unlock();
|
|
|
|
|
|
|
|
// Write to socket
|
|
|
|
if (!write(entry.count, entry.buf)) {
|
|
|
|
{
|
|
|
|
std::lock_guard lck(connectionOpenMtx);
|
|
|
|
connectionOpen = false;
|
|
|
|
}
|
|
|
|
connectionOpenCnd.notify_all();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ListenerClass::ListenerClass(Socket listenSock) {
|
|
|
|
sock = listenSock;
|
|
|
|
listening = true;
|
|
|
|
acceptWorkerThread = std::thread(&ListenerClass::worker, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
ListenerClass::~ListenerClass() {
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
Conn ListenerClass::accept() {
|
2021-07-16 18:13:18 +02:00
|
|
|
if (!listening) { return NULL; }
|
2021-07-16 01:49:33 +02:00
|
|
|
std::lock_guard lck(acceptMtx);
|
|
|
|
Socket _sock;
|
|
|
|
|
|
|
|
// Accept socket
|
|
|
|
_sock = ::accept(sock, NULL, NULL);
|
2021-07-30 22:06:06 +02:00
|
|
|
#ifdef _WIN32
|
2021-07-30 21:56:34 +02:00
|
|
|
if (_sock < 0 || _sock == SOCKET_ERROR) {
|
2021-07-30 22:06:06 +02:00
|
|
|
#else
|
|
|
|
if (_sock < 0) {
|
|
|
|
#endif
|
2021-07-16 01:49:33 +02:00
|
|
|
listening = false;
|
|
|
|
throw std::runtime_error("Could not bind socket");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Conn(new ConnClass(_sock));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListenerClass::acceptAsync(void (*handler)(Conn conn, void* ctx), void* ctx) {
|
2021-07-16 18:13:18 +02:00
|
|
|
if (!listening) { return; }
|
2021-07-16 01:49:33 +02:00
|
|
|
// Create entry
|
|
|
|
ListenerAcceptEntry entry;
|
|
|
|
entry.handler = handler;
|
|
|
|
entry.ctx = ctx;
|
|
|
|
|
|
|
|
// Add entry to queue
|
|
|
|
{
|
|
|
|
std::lock_guard lck(acceptQueueMtx);
|
|
|
|
acceptQueue.push_back(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify write worker
|
|
|
|
acceptQueueCnd.notify_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListenerClass::close() {
|
|
|
|
{
|
|
|
|
std::lock_guard lck(acceptQueueMtx);
|
|
|
|
stopWorker = true;
|
|
|
|
}
|
2021-07-16 18:13:18 +02:00
|
|
|
acceptQueueCnd.notify_all();
|
2021-07-16 01:49:33 +02:00
|
|
|
|
|
|
|
if (listening) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
closesocket(sock);
|
|
|
|
#else
|
2021-07-16 18:13:18 +02:00
|
|
|
::shutdown(sock, SHUT_RDWR);
|
2021-07-16 01:49:33 +02:00
|
|
|
::close(sock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acceptWorkerThread.joinable()) { acceptWorkerThread.join(); }
|
|
|
|
|
|
|
|
|
|
|
|
listening = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ListenerClass::isListening() {
|
|
|
|
return listening;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ListenerClass::worker() {
|
|
|
|
while (true) {
|
|
|
|
// Wait for wakeup and exit if it's for terminating the thread
|
|
|
|
std::unique_lock lck(acceptQueueMtx);
|
2021-12-19 22:11:44 +01:00
|
|
|
acceptQueueCnd.wait(lck, [this]() { return (acceptQueue.size() > 0 || stopWorker); });
|
2021-07-16 01:49:33 +02:00
|
|
|
if (stopWorker || !listening) { return; }
|
|
|
|
|
|
|
|
// Pop first element off the list
|
|
|
|
ListenerAcceptEntry entry = acceptQueue[0];
|
|
|
|
acceptQueue.erase(acceptQueue.begin());
|
|
|
|
lck.unlock();
|
|
|
|
|
|
|
|
// Read from socket and send data to the handler
|
|
|
|
try {
|
|
|
|
Conn client = accept();
|
|
|
|
if (!client) {
|
|
|
|
listening = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
entry.handler(std::move(client), entry.ctx);
|
|
|
|
}
|
2024-01-22 19:46:01 +01:00
|
|
|
catch (const std::exception& e) {
|
2021-07-16 01:49:33 +02:00
|
|
|
listening = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-07-30 21:56:34 +02:00
|
|
|
Conn connect(std::string host, uint16_t port) {
|
2021-12-19 22:11:44 +01:00
|
|
|
Socket sock;
|
2021-07-16 01:49:33 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-09-20 19:59:35 +02:00
|
|
|
// Initialize WinSock2
|
2021-07-16 01:49:33 +02:00
|
|
|
if (!winsock_init) {
|
|
|
|
WSADATA wsa;
|
2021-12-19 22:11:44 +01:00
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsa)) {
|
2021-07-16 01:49:33 +02:00
|
|
|
throw std::runtime_error("Could not initialize WinSock2");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
winsock_init = true;
|
|
|
|
}
|
|
|
|
assert(winsock_init);
|
2021-07-30 23:39:28 +02:00
|
|
|
#else
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2021-07-16 01:49:33 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Create a socket
|
2021-07-30 21:56:34 +02:00
|
|
|
sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2021-07-16 01:49:33 +02:00
|
|
|
if (sock < 0) {
|
|
|
|
throw std::runtime_error("Could not create socket");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get address from hostname/ip
|
|
|
|
hostent* remoteHost = gethostbyname(host.c_str());
|
|
|
|
if (remoteHost == NULL || remoteHost->h_addr_list[0] == NULL) {
|
|
|
|
throw std::runtime_error("Could get address from host");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uint32_t* naddr = (uint32_t*)remoteHost->h_addr_list[0];
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-07-16 01:49:33 +02:00
|
|
|
// Create host address
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
addr.sin_addr.s_addr = *naddr;
|
|
|
|
addr.sin_family = AF_INET;
|
2021-12-19 22:11:44 +01:00
|
|
|
addr.sin_port = htons(port);
|
2021-07-16 01:49:33 +02:00
|
|
|
|
|
|
|
// Connect to host
|
|
|
|
if (::connect(sock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
|
|
|
throw std::runtime_error("Could not connect to host");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Conn(new ConnClass(sock));
|
|
|
|
}
|
|
|
|
|
2021-07-30 21:56:34 +02:00
|
|
|
Listener listen(std::string host, uint16_t port) {
|
2021-07-16 01:49:33 +02:00
|
|
|
Socket listenSock;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-09-20 19:59:35 +02:00
|
|
|
// Initialize WinSock2
|
2021-07-16 01:49:33 +02:00
|
|
|
if (!winsock_init) {
|
|
|
|
WSADATA wsa;
|
2021-12-19 22:11:44 +01:00
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsa)) {
|
2021-07-16 01:49:33 +02:00
|
|
|
throw std::runtime_error("Could not initialize WinSock2");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
winsock_init = true;
|
|
|
|
}
|
|
|
|
assert(winsock_init);
|
2021-07-30 23:39:28 +02:00
|
|
|
#else
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2021-07-16 01:49:33 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Create a socket
|
2021-07-30 21:56:34 +02:00
|
|
|
listenSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
2021-07-16 01:49:33 +02:00
|
|
|
if (listenSock < 0) {
|
|
|
|
throw std::runtime_error("Could not create socket");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-08-21 09:41:40 +03:00
|
|
|
#ifndef _WIN32
|
2021-12-19 22:11:44 +01:00
|
|
|
// Allow port reusing if the app was killed or crashed
|
2021-08-21 09:41:40 +03:00
|
|
|
// and the socket is stuck in TIME_WAIT state.
|
2021-12-19 22:11:44 +01:00
|
|
|
// This option has a different meaning on Windows,
|
2021-08-21 09:41:40 +03:00
|
|
|
// so we use it only for non-Windows systems
|
|
|
|
int enable = 1;
|
2021-12-19 22:11:44 +01:00
|
|
|
if (setsockopt(listenSock, SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int)) < 0) {
|
2021-08-21 09:41:40 +03:00
|
|
|
throw std::runtime_error("Could not configure socket");
|
2021-12-19 22:11:44 +01:00
|
|
|
return NULL;
|
2021-08-21 09:41:40 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-07-16 01:49:33 +02:00
|
|
|
// Get address from hostname/ip
|
|
|
|
hostent* remoteHost = gethostbyname(host.c_str());
|
|
|
|
if (remoteHost == NULL || remoteHost->h_addr_list[0] == NULL) {
|
|
|
|
throw std::runtime_error("Could get address from host");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uint32_t* naddr = (uint32_t*)remoteHost->h_addr_list[0];
|
|
|
|
|
|
|
|
// Create host address
|
|
|
|
struct sockaddr_in addr;
|
|
|
|
addr.sin_addr.s_addr = *naddr;
|
|
|
|
addr.sin_family = AF_INET;
|
2021-12-19 22:11:44 +01:00
|
|
|
addr.sin_port = htons(port);
|
2021-07-16 01:49:33 +02:00
|
|
|
|
|
|
|
// Bind socket
|
|
|
|
if (bind(listenSock, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
|
|
|
|
throw std::runtime_error("Could not bind socket");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Listen
|
|
|
|
if (::listen(listenSock, SOMAXCONN) != 0) {
|
|
|
|
throw std::runtime_error("Could not listen");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return Listener(new ListenerClass(listenSock));
|
|
|
|
}
|
2021-07-30 21:56:34 +02:00
|
|
|
|
|
|
|
Conn openUDP(std::string host, uint16_t port, std::string remoteHost, uint16_t remotePort, bool bindSocket) {
|
|
|
|
Socket sock;
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2021-09-20 19:59:35 +02:00
|
|
|
// Initialize WinSock2
|
2021-07-30 21:56:34 +02:00
|
|
|
if (!winsock_init) {
|
|
|
|
WSADATA wsa;
|
2021-12-19 22:11:44 +01:00
|
|
|
if (WSAStartup(MAKEWORD(2, 2), &wsa)) {
|
2021-07-30 21:56:34 +02:00
|
|
|
throw std::runtime_error("Could not initialize WinSock2");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
winsock_init = true;
|
|
|
|
}
|
|
|
|
assert(winsock_init);
|
2021-07-30 23:39:28 +02:00
|
|
|
#else
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2021-07-30 21:56:34 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Create a socket
|
2021-11-09 00:00:13 +01:00
|
|
|
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
2021-07-30 21:56:34 +02:00
|
|
|
if (sock < 0) {
|
|
|
|
throw std::runtime_error("Could not create socket");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get address from local hostname/ip
|
|
|
|
hostent* _host = gethostbyname(host.c_str());
|
|
|
|
if (_host == NULL || _host->h_addr_list[0] == NULL) {
|
|
|
|
throw std::runtime_error("Could get address from host");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get address from remote hostname/ip
|
|
|
|
hostent* _remoteHost = gethostbyname(remoteHost.c_str());
|
|
|
|
if (_remoteHost == NULL || _remoteHost->h_addr_list[0] == NULL) {
|
|
|
|
throw std::runtime_error("Could get address from host");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
uint32_t* rnaddr = (uint32_t*)_remoteHost->h_addr_list[0];
|
|
|
|
|
|
|
|
// Create host address
|
|
|
|
struct sockaddr_in addr;
|
2021-12-19 22:11:44 +01:00
|
|
|
addr.sin_addr.s_addr = INADDR_ANY; //*naddr;
|
2021-07-30 21:56:34 +02:00
|
|
|
addr.sin_family = AF_INET;
|
2021-12-19 22:11:44 +01:00
|
|
|
addr.sin_port = htons(port);
|
2021-07-30 21:56:34 +02:00
|
|
|
|
|
|
|
// Create remote host address
|
|
|
|
struct sockaddr_in raddr;
|
|
|
|
raddr.sin_addr.s_addr = *rnaddr;
|
|
|
|
raddr.sin_family = AF_INET;
|
2021-12-19 22:11:44 +01:00
|
|
|
raddr.sin_port = htons(remotePort);
|
2021-07-30 21:56:34 +02:00
|
|
|
|
|
|
|
// Bind socket
|
|
|
|
if (bindSocket) {
|
2021-11-09 00:00:13 +01:00
|
|
|
int err = bind(sock, (struct sockaddr*)&addr, sizeof(addr));
|
|
|
|
if (err < 0) {
|
2021-07-30 21:56:34 +02:00
|
|
|
throw std::runtime_error("Could not bind socket");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2021-12-19 22:11:44 +01:00
|
|
|
|
2021-07-30 21:56:34 +02:00
|
|
|
return Conn(new ConnClass(sock, raddr, true));
|
|
|
|
}
|
2021-07-16 01:49:33 +02:00
|
|
|
}
|