Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unix networking #148

Merged
merged 10 commits into from
Feb 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ lambda_sfi.hpp
*.tar
*.gz
*.tgz
*.so
3 changes: 2 additions & 1 deletion core/compression/compression.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#ifndef __LIB_MADDSUA_LAMBDA_EXTRA_COMPRESSION__
#define __LIB_MADDSUA_LAMBDA_EXTRA_COMPRESSION__

#include <stdint.h>
#include <cstdint>
#include <cstddef>
#include <vector>

namespace Lambda::Compress {
Expand Down
3 changes: 0 additions & 3 deletions core/network/sysnetw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include <ws2tcpip.h>
#include <stdexcept>

#define LNE_TIMEDOUT WSAETIMEDOUT

inline bool wsaWakeUp() {

static bool wsaInitCalled = false;
Expand All @@ -40,7 +38,6 @@
#include <arpa/inet.h>
#include <cerrno>

#define LNE_TIMEDOUT ETIMEDOUT
#define INVALID_SOCKET (-1)

#define closesocket(socketHandle) (close(socketHandle))
Expand Down
57 changes: 38 additions & 19 deletions core/network/tcp/connection.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
#include "./connection.hpp"
#include "../sysnetw.hpp"

#include <algorithm>

using namespace Lambda::Network;
using namespace Lambda::Network::TCP;

static const std::initializer_list<int> blockingEndedCodes = {
#ifdef _WIN32
WSAETIMEDOUT
#else
EAGAIN, ETIMEDOUT, EWOULDBLOCK
#endif
};

Connection::Connection(
SockHandle handleInit,
const ConnectionInfo& infoInit
Expand Down Expand Up @@ -73,10 +83,9 @@ std::vector<uint8_t> Connection::read(size_t expectedSize) {

std::lock_guard<std::mutex> lock(this->m_readMutex);

std::vector<uint8_t> chunk;
chunk.resize(expectedSize);
auto chunk = std::vector<uint8_t>(expectedSize);

auto bytesReceived = recv(this->hSocket, (char*)chunk.data(), chunk.size(), 0);
auto bytesReceived = recv(this->hSocket, reinterpret_cast<char*>(chunk.data()), chunk.size(), 0);

if (bytesReceived == 0) {

Expand All @@ -87,20 +96,20 @@ std::vector<uint8_t> Connection::read(size_t expectedSize) {

auto apiError = Errors::getApiError();

switch (apiError) {

case LNE_TIMEDOUT: {

if (this->flags.closeOnTimeout) {
this->end();
}
// I could use std::any_of here,
// but that syntax sugar seems out of place here
for (const auto code : blockingEndedCodes) {

if (apiError != code) continue;

return {};
if (this->flags.closeOnTimeout) {
this->end();
}

default:
throw Lambda::APIError(apiError, "network error while receiving data");
}
return {};
}

throw Lambda::APIError(apiError, "network error while receiving data");
}

chunk.resize(bytesReceived);
Expand All @@ -109,18 +118,28 @@ std::vector<uint8_t> Connection::read(size_t expectedSize) {
return chunk;
}

void Connection::setTimeouts(uint32_t value, SetTimeoutsDirection direction) {
void Connection::setTimeouts(uint32_t valueMs, SetTimeoutsDirection direction) {

#ifdef _WIN32
const auto timeoutValue = valueMs;
#else
timeval timeoutValue;
timeoutValue.tv_sec = valueMs / 1000;
timeoutValue.tv_usec = 0;
#endif

const auto timeouValuePtr = reinterpret_cast<const char*>(&timeoutValue);

if (direction != SetTimeoutsDirection::Receive) {
if (setsockopt(hSocket, SOL_SOCKET, SO_SNDTIMEO, (const char*)&value, sizeof(value)))
if (setsockopt(hSocket, SOL_SOCKET, SO_SNDTIMEO, timeouValuePtr, sizeof(timeoutValue)))
throw Lambda::APIError("failed to set socket TX timeout");
this->m_info.timeouts.send = value;
this->m_info.timeouts.send = valueMs;
}

if (direction != SetTimeoutsDirection::Send) {
if (setsockopt(hSocket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&value, sizeof(value)))
if (setsockopt(hSocket, SOL_SOCKET, SO_RCVTIMEO, timeouValuePtr, sizeof(timeoutValue)))
throw Lambda::APIError("failed to set socket RX timeout");
this->m_info.timeouts.receive = value;
this->m_info.timeouts.receive = valueMs;
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/network/tcp/connection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace Lambda::Network::TCP {
void end() noexcept;
bool active() const noexcept;

void setTimeouts(uint32_t value, SetTimeoutsDirection direction);
void setTimeouts(uint32_t value);
void setTimeouts(uint32_t valueMs, SetTimeoutsDirection direction);
void setTimeouts(uint32_t valueMs);

static const uint32_t TimeoutMs = 15000;
static const uint32_t ReadChunkSize = 2048;
Expand Down
1 change: 1 addition & 0 deletions core/polyfill/polyfill.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <vector>
#include <string>
#include <cstring>
#include <ctime>

namespace Lambda {

Expand Down
4 changes: 3 additions & 1 deletion core/server/instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ void LambdaInstance::start() {
}
});

syncout.log("[Service] Started server at http://localhost:" + std::to_string(this->config.service.port) + '/');
if (config.loglevel.startMessage) {
syncout.log("[Service] Started server at http://localhost:" + std::to_string(this->config.service.port) + '/');
}
}

void LambdaInstance::shutdownn() {
Expand Down
1 change: 1 addition & 0 deletions core/server/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace Lambda {
bool connections = true;
bool requests = true;
bool timestamps = false;
bool startMessage = true;
};

enum struct ErrorResponseType {
Expand Down