Skip to content

Commit

Permalink
Fix socket compat (#130)
Browse files Browse the repository at this point in the history
* Update sysnetw.hpp

* Update network.hpp

* Update connection.cpp

* Update connection.hpp

* Update listener.cpp

* Update listener.hpp

* Delete compat.hpp
  • Loading branch information
maddsua authored Feb 4, 2024
1 parent aeebd69 commit aaee53a
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 59 deletions.
21 changes: 0 additions & 21 deletions core/network/compat.hpp

This file was deleted.

6 changes: 6 additions & 0 deletions core/network/network.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 3 additions & 2 deletions core/network/sysnetw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@
#include <arpa/inet.h>
#include <cerrno>

#define LNE_TIMEDOUT ETIMEDOUT
#define INVALID_SOCKET (-1)

#define closesocket(socketHandle) (close(socketHandle))
#define SD_BOTH (SHUT_RDWR)

#define LNE_TIMEDOUT ETIMEDOUT

#endif

#endif
24 changes: 12 additions & 12 deletions core/network/tcp/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<uint8_t>& 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<std::mutex> lock(this->m_writeMutex);
Expand All @@ -68,7 +68,7 @@ std::vector<uint8_t> Connection::read() {

std::vector<uint8_t> 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<std::mutex> lock(this->m_readMutex);
Expand Down
10 changes: 2 additions & 8 deletions core/network/tcp/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define __LIB_MADDSUA_LAMBDA_NETWORK_TCP_CONNECTION__

#include "../network.hpp"
#include "../compat.hpp"

#include <vector>
#include <string>
Expand All @@ -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();
Expand Down
25 changes: 11 additions & 14 deletions core/network/tcp/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -52,28 +49,28 @@ 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);
}

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<Connection> 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");
}

Expand All @@ -83,8 +80,8 @@ std::optional<Connection> 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");
}

Expand Down Expand Up @@ -116,7 +113,7 @@ std::optional<Connection> ListenSocket::acceptConnection() {
}

bool ListenSocket::active() const noexcept {
return this->hSocket != Network::invalid_socket;
return this->hSocket != INVALID_SOCKET;
}

const ListenConfig& ListenSocket::getConfig() const noexcept {
Expand All @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions core/network/tcp/listener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#define __LIB_MADDSUA_LAMBDA_NETWORK_TCP_LISTENER__

#include "../network.hpp"
#include "../compat.hpp"
#include "./connection.hpp"

#include <optional>
Expand All @@ -17,7 +16,7 @@ namespace Lambda::Network::TCP {

class ListenSocket {
protected:
SockHandle hSocket = -1;
SockHandle hSocket;
ListenConfig config;

public:
Expand Down

0 comments on commit aaee53a

Please sign in to comment.