diff --git a/core/network/compat.hpp b/core/network/compat.hpp deleted file mode 100644 index 9e07eb401..000000000 --- a/core/network/compat.hpp +++ /dev/null @@ -1,21 +0,0 @@ -#ifndef __LIB_MADDSUA_LAMBDA_NETWORK_COMPAT__ -#define __LIB_MADDSUA_LAMBDA_NETWORK_COMPAT__ - -#include - -namespace Lambda::Network { - - #ifdef _WIN32 - - typedef uint64_t SockHandle; - static const SockHandle invalid_socket = ~0; - - #else - - typedef int32_t SockHandle; - static const SockHandle invalid_socket = -1; - - #endif -}; - -#endif diff --git a/core/network/network.hpp b/core/network/network.hpp index 2f0c66d9b..2453a9369 100644 --- a/core/network/network.hpp +++ b/core/network/network.hpp @@ -35,6 +35,12 @@ namespace Lambda::Network { enum struct SetTimeoutsDirection { Both, Send, Receive }; + + #ifdef _WIN32 + typedef uint64_t SockHandle; + #else + typedef int32_t SockHandle; + #endif }; #endif diff --git a/core/network/sysnetw.hpp b/core/network/sysnetw.hpp index de0b8a4c8..18471bc0a 100644 --- a/core/network/sysnetw.hpp +++ b/core/network/sysnetw.hpp @@ -40,11 +40,12 @@ #include #include + #define LNE_TIMEDOUT ETIMEDOUT + #define INVALID_SOCKET (-1) + #define closesocket(socketHandle) (close(socketHandle)) #define SD_BOTH (SHUT_RDWR) - #define LNE_TIMEDOUT ETIMEDOUT - #endif #endif diff --git a/core/network/tcp/connection.cpp b/core/network/tcp/connection.cpp index 091c0e7ee..e07b3249b 100644 --- a/core/network/tcp/connection.cpp +++ b/core/network/tcp/connection.cpp @@ -4,38 +4,38 @@ using namespace Lambda::Network; using namespace Lambda::Network::TCP; -Connection::Connection(const ConnInit& init) { - this->m_info = init.info; - this->hSocket = init.hSocket; -} +Connection::Connection( + SockHandle handleInit, + const ConnectionInfo& infoInit +) : hSocket(handleInit), m_info(infoInit) {} Connection& Connection::operator= (Connection&& other) noexcept { this->hSocket = other.hSocket; this->m_info = other.m_info; - other.hSocket = Network::invalid_socket; + other.hSocket = INVALID_SOCKET; return *this; } Connection::Connection(Connection&& other) noexcept { this->hSocket = other.hSocket; this->m_info = other.m_info; - other.hSocket = Network::invalid_socket; + other.hSocket = INVALID_SOCKET; } Connection::~Connection() { - if (this->hSocket == Network::invalid_socket) return; + if (this->hSocket == INVALID_SOCKET) return; shutdown(this->hSocket, SD_BOTH); closesocket(this->hSocket); } void Connection::end() noexcept { - if (this->hSocket == Network::invalid_socket) return; + if (this->hSocket == INVALID_SOCKET) return; // swapping handle to a temp variable so that // no race condition can occur further down the chain auto tempHandle = this->hSocket; - this->hSocket = Network::invalid_socket; + this->hSocket = INVALID_SOCKET; shutdown(tempHandle, SD_BOTH); closesocket(tempHandle); @@ -46,12 +46,12 @@ const ConnectionInfo& Connection::info() const noexcept { } bool Connection::active() const noexcept { - return this->hSocket != Network::invalid_socket; + return this->hSocket != INVALID_SOCKET; } void Connection::write(const std::vector& data) { - if (this->hSocket == Network::invalid_socket) + if (this->hSocket == INVALID_SOCKET) throw std::runtime_error("cann't write to a closed connection"); std::lock_guard lock(this->m_writeMutex); @@ -68,7 +68,7 @@ std::vector Connection::read() { std::vector Connection::read(size_t expectedSize) { - if (this->hSocket == Network::invalid_socket) + if (this->hSocket == INVALID_SOCKET) throw std::runtime_error("can't read from a closed connection"); std::lock_guard lock(this->m_readMutex); diff --git a/core/network/tcp/connection.hpp b/core/network/tcp/connection.hpp index 8c31a94ad..318c9afea 100644 --- a/core/network/tcp/connection.hpp +++ b/core/network/tcp/connection.hpp @@ -2,7 +2,6 @@ #define __LIB_MADDSUA_LAMBDA_NETWORK_TCP_CONNECTION__ #include "../network.hpp" -#include "../compat.hpp" #include #include @@ -12,19 +11,14 @@ namespace Lambda::Network::TCP { class Connection { protected: - SockHandle hSocket = -1; + SockHandle hSocket; ConnectionInfo m_info; std::mutex m_readMutex; std::mutex m_writeMutex; public: - struct ConnInit { - SockHandle hSocket; - ConnectionInfo info; - }; - - Connection(const ConnInit& init); + Connection(SockHandle handleInit, const ConnectionInfo& infoInit); Connection(Connection&& other) noexcept; Connection(const Connection& other) = delete; ~Connection(); diff --git a/core/network/tcp/listener.cpp b/core/network/tcp/listener.cpp index 0678a0c80..2a2e1715d 100644 --- a/core/network/tcp/listener.cpp +++ b/core/network/tcp/listener.cpp @@ -5,18 +5,15 @@ using namespace Lambda::Network; using namespace Lambda::Network::TCP; -ListenSocket::ListenSocket(const ListenConfig& init) { +ListenSocket::ListenSocket(const ListenConfig& init) : config(init) { // special threatment for windows and it's fucking WSA #ifdef _WIN32 wsaWakeUp(); #endif - // just save config for later - this->config = init; - this->hSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (this->hSocket == Network::invalid_socket) { + if (this->hSocket == INVALID_SOCKET) { throw Lambda::APIError("failed to create listen socket"); } @@ -52,7 +49,7 @@ ListenSocket::ListenSocket(const ListenConfig& init) { } ListenSocket::~ListenSocket() { - if (this->hSocket == Network::invalid_socket) return; + if (this->hSocket == INVALID_SOCKET) return; shutdown(this->hSocket, SD_BOTH); closesocket(this->hSocket); } @@ -60,20 +57,20 @@ ListenSocket::~ListenSocket() { ListenSocket::ListenSocket(ListenSocket&& other) noexcept { this->hSocket = other.hSocket; this->config = other.config; - other.hSocket = Network::invalid_socket; + other.hSocket = INVALID_SOCKET; } ListenSocket& ListenSocket::operator= (ListenSocket&& other) noexcept { this->hSocket = other.hSocket; this->config = other.config; - other.hSocket = Network::invalid_socket; + other.hSocket = INVALID_SOCKET; return *this; } std::optional ListenSocket::acceptConnection() { // check that we have a valid socket - if (this->hSocket == Network::invalid_socket) { + if (this->hSocket == INVALID_SOCKET) { throw std::runtime_error("cannot accept anything from a closed socket"); } @@ -83,8 +80,8 @@ std::optional ListenSocket::acceptConnection() { auto nextSocket = accept(this->hSocket, (sockaddr*)&peerAddr, &clientAddrLen); // verify that we have a valid socket - if (nextSocket == Network::invalid_socket) { - if (this->hSocket == Network::invalid_socket) return std::nullopt; + if (nextSocket == INVALID_SOCKET) { + if (this->hSocket == INVALID_SOCKET) return std::nullopt; throw Lambda::APIError("socket accept failed"); } @@ -116,7 +113,7 @@ std::optional ListenSocket::acceptConnection() { } bool ListenSocket::active() const noexcept { - return this->hSocket != Network::invalid_socket; + return this->hSocket != INVALID_SOCKET; } const ListenConfig& ListenSocket::getConfig() const noexcept { @@ -125,10 +122,10 @@ const ListenConfig& ListenSocket::getConfig() const noexcept { void ListenSocket::stop() noexcept { - if (this->hSocket == Network::invalid_socket) return; + if (this->hSocket == INVALID_SOCKET) return; auto tempHandle = this->hSocket; - this->hSocket = Network::invalid_socket; + this->hSocket = INVALID_SOCKET; shutdown(tempHandle, SD_BOTH); closesocket(tempHandle); diff --git a/core/network/tcp/listener.hpp b/core/network/tcp/listener.hpp index 7d2c41329..361881adf 100644 --- a/core/network/tcp/listener.hpp +++ b/core/network/tcp/listener.hpp @@ -2,7 +2,6 @@ #define __LIB_MADDSUA_LAMBDA_NETWORK_TCP_LISTENER__ #include "../network.hpp" -#include "../compat.hpp" #include "./connection.hpp" #include @@ -17,7 +16,7 @@ namespace Lambda::Network::TCP { class ListenSocket { protected: - SockHandle hSocket = -1; + SockHandle hSocket; ListenConfig config; public: