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

Migrate websocket #115

Merged
merged 100 commits into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
100 commits
Select commit Hold shift + click to select a range
b70c651
Update Makefile
maddsua Jan 30, 2024
996d408
Update server.cpp
maddsua Feb 2, 2024
74097d6
Merge branch 'next' into migrate-websocket
maddsua Feb 2, 2024
ea65d8a
Create websocket.hpp
maddsua Feb 2, 2024
a45a6b1
Create proto.hpp
maddsua Feb 2, 2024
927389f
Create proto.cpp
maddsua Feb 2, 2024
1ce4ad8
Update websocket.hpp
maddsua Feb 2, 2024
d77f3a7
Update connection.cpp
maddsua Feb 2, 2024
509de9a
Update server.hpp
maddsua Feb 2, 2024
a8a4682
.
maddsua Feb 2, 2024
23be5fc
Update pipeline.cpp
maddsua Feb 2, 2024
f3bdc3b
Update websocket.hpp
maddsua Feb 2, 2024
ff32f53
Create websocket.hpp
maddsua Feb 2, 2024
6f9bfd3
Update websocket.hpp
maddsua Feb 2, 2024
ad2f8d8
Create transport.cpp
maddsua Feb 2, 2024
4b9c3ac
Update server.hpp
maddsua Feb 2, 2024
f88e9e5
Update websocket.hpp
maddsua Feb 2, 2024
2518569
Create context.cpp
maddsua Feb 2, 2024
1925a03
Create queue.cpp
maddsua Feb 2, 2024
d6d0922
moved network internals out of index header
maddsua Feb 2, 2024
9341270
.
maddsua Feb 2, 2024
2327d2b
.
maddsua Feb 2, 2024
725cee4
.
maddsua Feb 2, 2024
22b3e49
.
maddsua Feb 2, 2024
ecbce7c
.
maddsua Feb 2, 2024
8aa0699
.
maddsua Feb 2, 2024
0b46a7a
.
maddsua Feb 2, 2024
5673f16
Update transport.cpp
maddsua Feb 2, 2024
fde1166
Update transport.hpp
maddsua Feb 2, 2024
1ed8685
.
maddsua Feb 2, 2024
fc46d27
Update server.hpp
maddsua Feb 2, 2024
969941a
References vs pointers
maddsua Feb 2, 2024
6fc5510
Revert "References vs pointers"
maddsua Feb 2, 2024
4078a8d
Update Makefile.core.mk
maddsua Feb 2, 2024
71d4800
Update network_polyfill.hpp
maddsua Feb 2, 2024
52e488f
Update http.hpp
maddsua Feb 2, 2024
3da44cc
Update server.hpp
maddsua Feb 2, 2024
89dc8c7
hope it helps
maddsua Feb 2, 2024
67dbf66
Update http.hpp
maddsua Feb 2, 2024
c8ae187
Update serverless.cpp
maddsua Feb 2, 2024
e36d3d7
Update connection.cpp
maddsua Feb 2, 2024
13be9e6
Update message.cpp
maddsua Feb 2, 2024
e35cad2
Update websocket.hpp
maddsua Feb 2, 2024
17140f3
Update message.cpp
maddsua Feb 2, 2024
bd692a0
Update server.hpp
maddsua Feb 2, 2024
b81f1c9
a bunch of bs
maddsua Feb 2, 2024
14c0bcd
Update context.cpp
maddsua Feb 2, 2024
731bff5
Update Makefile.core.mk
maddsua Feb 2, 2024
be95893
Delete queue.cpp
maddsua Feb 2, 2024
914d4fb
Update context.cpp
maddsua Feb 2, 2024
6a62e9d
Update websocket.hpp
maddsua Feb 2, 2024
5a92dac
more header rearrangements
maddsua Feb 2, 2024
c0cec09
update crypto module
maddsua Feb 2, 2024
f069107
.
maddsua Feb 2, 2024
37c47a2
removed network polyfill
maddsua Feb 2, 2024
fe2c299
Update internal.hpp
maddsua Feb 2, 2024
57eb7f4
Update context.cpp
maddsua Feb 2, 2024
3c86f55
done timeouts (probably done lol)
maddsua Feb 2, 2024
6d4516c
Update transport.cpp
maddsua Feb 2, 2024
e5c50ae
Update context.cpp
maddsua Feb 2, 2024
ee5a179
Update internal.hpp
maddsua Feb 2, 2024
5b69ba2
Update context.cpp
maddsua Feb 2, 2024
a80c05b
Update transport.cpp
maddsua Feb 2, 2024
2a18f28
Update internal.hpp
maddsua Feb 2, 2024
bcc020f
Update context.cpp
maddsua Feb 2, 2024
5de78a0
.
maddsua Feb 2, 2024
ee044de
Update websocket.hpp
maddsua Feb 2, 2024
40ff843
Update message.cpp
maddsua Feb 2, 2024
532e9cb
Update context.cpp
maddsua Feb 2, 2024
9b5649b
Update context.cpp
maddsua Feb 2, 2024
b0669a8
Update context.cpp
maddsua Feb 2, 2024
30ac794
Create websocket.cpp
maddsua Feb 2, 2024
9a0479a
Update Makefile.examples.mk
maddsua Feb 2, 2024
f2e1577
Update transport.cpp
maddsua Feb 2, 2024
714be18
Update server.hpp
maddsua Feb 2, 2024
8d39ec0
Update websocket.cpp
maddsua Feb 2, 2024
e1437e8
Create websocket.deno.ts
maddsua Feb 2, 2024
33f58ff
Update websocket.deno.ts
maddsua Feb 2, 2024
d0b5536
Update transport.cpp
maddsua Feb 2, 2024
631fabd
Update websocket.deno.ts
maddsua Feb 2, 2024
04421e9
Update websocket.deno.ts
maddsua Feb 2, 2024
80b8e39
Update transport.cpp
maddsua Feb 2, 2024
91f1d21
Update transport.cpp
maddsua Feb 2, 2024
4bfc6f4
Update transport.cpp
maddsua Feb 2, 2024
0c4a8e6
Update server.hpp
maddsua Feb 2, 2024
8ad23f7
Update upgrade.cpp
maddsua Feb 2, 2024
2efa0f1
Update context.cpp
maddsua Feb 2, 2024
34e054f
Update context.cpp
maddsua Feb 2, 2024
fb57643
Update context.cpp
maddsua Feb 2, 2024
4695ebc
Update context.cpp
maddsua Feb 2, 2024
7d96a94
Update upgrade.cpp
maddsua Feb 3, 2024
86f6eca
updated socket handle types
maddsua Feb 3, 2024
57cb321
Delete websocket.deno.ts
maddsua Feb 3, 2024
30b5b16
Create websocket.deno.ts
maddsua Feb 3, 2024
d6ef7ac
Update websocket.cpp
maddsua Feb 3, 2024
7d597f8
Update context.cpp
maddsua Feb 3, 2024
6f558aa
Update context.cpp
maddsua Feb 3, 2024
9cc1ec7
Update message.cpp
maddsua Feb 3, 2024
5102025
Update websocket.hpp
maddsua Feb 3, 2024
cc78395
Update context.cpp
maddsua Feb 3, 2024
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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ include Makefile.examples.mk
libshared: $(LAMBDA_LIBSHARED)

$(LAMBDA_LIBSHARED): $(LIB_DEPS) $(WINDOWS_DLL_DEPS)
g++ $(CFLAGS) $(LIB_DEPS) $(EXTERNAL_LIBS) $(LINK_SYSTEM_LIBS) $(WINDOWS_DLL_DEPS) -shared -o $(LAMBDA_LIBSHARED) $(DLL_LDFLAGS)
g++ $(CFLAGS) $(DLL_LDFLAGS) $(LIB_DEPS) $(EXTERNAL_LIBS) $(LINK_SYSTEM_LIBS) $(WINDOWS_DLL_DEPS) -shared -o $(LAMBDA_LIBSHARED)

dllinfo.res: dllinfo.rc
windres -i dllinfo.rc --input-format=rc -o dllinfo.res -O coff
Expand Down
31 changes: 27 additions & 4 deletions Makefile.core.mk
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@

LIB_CORE = core/core.a
LIB_CORE_DEPS = $(LIB_CORE_POLYFILL_DEPS) $(LIB_CORE_HTTP_DEPS) $(LIB_CORE_ENCODING_DEPS) $(LIB_CORE_NETWORK_DEPS) $(LIB_CORE_COMPRESS_DEPS) $(LIB_CORE_SERVER_DEPS) $(LIB_CORE_CRYPTO_DEPS) $(LIB_CORE_HTML_DEPS) $(LIB_CORE_JSON_DEPS) $(LIB_CORE_ERROR_DEPS)
LIB_CORE_DEPS = $(LIB_CORE_POLYFILL_DEPS) $(LIB_CORE_HTTP_DEPS) $(LIB_CORE_ENCODING_DEPS) $(LIB_CORE_NETWORK_DEPS) $(LIB_CORE_COMPRESS_DEPS) $(LIB_CORE_SERVER_DEPS) $(LIB_CORE_CRYPTO_DEPS) $(LIB_CORE_HTML_DEPS) $(LIB_CORE_JSON_DEPS) $(LIB_CORE_ERROR_DEPS) $(LIB_CORE_WEBSOCKET_DEPS)

LIB_CORE_POLYFILL = core/polyfill.a
LIB_CORE_POLYFILL_DEPS = core/polyfill/strings.o core/polyfill/date.o core/polyfill/mimetype.o

LIB_CORE_HTTP = core/http.a
LIB_CORE_HTTP_DEPS = core/http/request.o core/http/response.o core/http/cookies.o core/http/kvcontainer.o core/http/url.o core/http/urlsearchparams.o core/http/method.o core/http/status.o core/http/body.o

LIB_CORE_WEBSOCKET = core/websocket.a
LIB_CORE_WEBSOCKET_DEPS = core/websocket/message.o

LIB_CORE_ENCODING = core/encoding.a
LIB_CORE_ENCODING_DEPS = core/encoding/base64.o core/encoding/hex.o core/encoding/url.o

Expand All @@ -18,7 +21,7 @@ LIB_CORE_COMPRESS = core/compression.a
LIB_CORE_COMPRESS_DEPS = core/compression/streams.o core/compression/brotli.o core/compression/zlib.o

LIB_CORE_SERVER = core/server.a
LIB_CORE_SERVER_DEPS = core/server/server.o core/server/http/transport.o core/server/console.o core/server/handlers/serverless.o core/server/handlers/connection.o
LIB_CORE_SERVER_DEPS = core/server/instance.o core/server/http/connection.o core/server/http/transport.o core/server/http/upgrade.o core/server/console.o core/server/handlers/serverless.o core/server/handlers/connection.o core/server/websocket/context.o core/server/websocket/transport.o

LIB_CORE_HTML = core/html.a
LIB_CORE_HTML_TEMPLATES = core/html/resources/servicepage.res
Expand Down Expand Up @@ -130,12 +133,18 @@ core/compression/zlib.o: core/compression/zlib.cpp
$(LIB_CORE_SERVER): $(LIB_CORE_SERVER_DEPS)
ar rvs $(LIB_CORE_SERVER) $(LIB_CORE_SERVER_DEPS)

core/server/server.o: core/server/server.cpp
g++ -c $(CFLAGS) core/server/server.cpp -o core/server/server.o
core/server/instance.o: core/server/instance.cpp
g++ -c $(CFLAGS) core/server/instance.cpp -o core/server/instance.o

core/server/http/connection.o: core/server/http/connection.cpp
g++ -c $(CFLAGS) core/server/http/connection.cpp -o core/server/http/connection.o

core/server/http/transport.o: core/server/http/transport.cpp
g++ -c $(CFLAGS) core/server/http/transport.cpp -o core/server/http/transport.o

core/server/http/upgrade.o: core/server/http/upgrade.cpp
g++ -c $(CFLAGS) core/server/http/upgrade.cpp -o core/server/http/upgrade.o

core/server/handlers/serverless.o: core/server/handlers/serverless.cpp
g++ -c $(CFLAGS) core/server/handlers/serverless.cpp -o core/server/handlers/serverless.o

Expand All @@ -145,6 +154,12 @@ core/server/handlers/connection.o: core/server/handlers/connection.cpp
core/server/console.o: core/server/console.cpp
g++ -c $(CFLAGS) core/server/console.cpp -o core/server/console.o

core/server/websocket/context.o: core/server/websocket/context.cpp
g++ -c $(CFLAGS) core/server/websocket/context.cpp -o core/server/websocket/context.o

core/server/websocket/transport.o: core/server/websocket/transport.cpp
g++ -c $(CFLAGS) core/server/websocket/transport.cpp -o core/server/websocket/transport.o


# html templates and stuff
$(LIB_CORE_HTML): $(LIB_CORE_HTML_DEPS)
Expand Down Expand Up @@ -191,3 +206,11 @@ $(LIB_CORE_ERROR): $(LIB_CORE_ERROR_DEPS)

core/error/apierror.o: core/error/apierror.cpp
g++ -c $(CFLAGS) core/error/apierror.cpp -o core/error/apierror.o


# websocket protocol
$(LIB_CORE_WEBSOCKET): $(LIB_CORE_WEBSOCKET_DEPS)
ar rvs $(LIB_CORE_WEBSOCKET) $(LIB_CORE_WEBSOCKET_DEPS)

core/websocket/message.o: core/websocket/message.cpp
g++ -c $(CFLAGS) core/websocket/message.cpp -o core/websocket/message.o
10 changes: 10 additions & 0 deletions Makefile.examples.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ conn_handler.example$(EXEEXT): examples/conn_handler.o $(LAMBDA_LIBSHARED)

examples/conn_handler.o: examples/conn_handler.cpp
g++ -c $(CFLAGS) examples/conn_handler.cpp -o examples/conn_handler.o


# websocket server example
example.websocket: websocket.example$(EXEEXT)

websocket.example$(EXEEXT): examples/websocket.o $(LAMBDA_LIBSHARED)
g++ $(CFLAGS) examples/websocket.o $(LAMBDA_LIBSHARED) $(EXTERNAL_LIBS) $(LINK_SYSTEM_LIBS) -o websocket.example$(EXEEXT)

examples/websocket.o: examples/websocket.cpp
g++ -c $(CFLAGS) examples/websocket.cpp -o examples/websocket.o
9 changes: 5 additions & 4 deletions core/crypto/crypto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,24 @@

#include <string>
#include <vector>
#include <array>

namespace Lambda::Crypto {

class SHA1 {
private:
void* hashctx = nullptr;
void sha1_transform();
void update(const uint8_t* data, size_t dataSize);

public:
static const size_t BlockSize = 20;

SHA1();
~SHA1();
void reset();
void update(const std::vector<uint8_t>& data);
std::array <uint8_t, SHA1::BlockSize> digest();
SHA1& reset();
SHA1& update(const std::vector<uint8_t>& buffer);
SHA1& update(const std::string& text);
std::vector<uint8_t> digest();
};

class ShortID {
Expand Down
25 changes: 20 additions & 5 deletions core/crypto/sha1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ SHA1::~SHA1() {
delete (SHA1_CTX*)this->hashctx;
}

void SHA1::reset() {
SHA1& SHA1::reset() {

auto ctx = (SHA1_CTX*)this->hashctx;
memset(this->hashctx, 0, sizeof(SHA1_CTX));
Expand All @@ -91,11 +91,13 @@ void SHA1::reset() {
ctx->k[1] = 0x6ed9eba1;
ctx->k[2] = 0x8f1bbcdc;
ctx->k[3] = 0xca62c1d6;

return *this;
}

void SHA1::update(const std::vector<uint8_t>& data) {
void SHA1::update(const uint8_t* data, size_t dataSize) {
auto ctx = (SHA1_CTX*)this->hashctx;
for (size_t i = 0; i < data.size(); ++i) {
for (size_t i = 0; i < dataSize; ++i) {
ctx->data[ctx->datalen] = data[i];
ctx->datalen++;
if (ctx->datalen == 64) {
Expand All @@ -106,10 +108,23 @@ void SHA1::update(const std::vector<uint8_t>& data) {
}
}

std::array <uint8_t, SHA1::BlockSize> SHA1::digest() {
SHA1& SHA1::update(const std::vector<uint8_t>& buffer) {
this->update(buffer.data(), buffer.size());
return *this;
}

SHA1& SHA1::update(const std::string& text) {
this->update((const uint8_t*)text.data(), text.size());
return *this;
}

std::vector<uint8_t> SHA1::digest() {

auto ctx = (SHA1_CTX*)this->hashctx;
std::array <uint8_t, SHA1::BlockSize> hash;

std::vector<uint8_t> hash;
hash.resize(SHA1::BlockSize);

uint32_t i = ctx->datalen;

// Pad whatever data is left in the buffer.
Expand Down
21 changes: 21 additions & 0 deletions core/network/compat.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef __LIB_MADDSUA_LAMBDA_NETWORK_COMPAT__
#define __LIB_MADDSUA_LAMBDA_NETWORK_COMPAT__

#include <cstdint>

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
14 changes: 11 additions & 3 deletions core/network/network.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#ifndef __LIB_MADDSUA_LAMBDA_NETWORK__
#define __LIB_MADDSUA_LAMBDA_NETWORK__

#include <stdint.h>
#include <cstdint>
#include <string>

namespace Lambda::Network {

enum struct ConnectionTransport : int16_t {
enum struct ConnectionTransport {
TCP, UDP
};

Expand All @@ -16,9 +16,14 @@ namespace Lambda::Network {
ConnectionTransport transport;
};

struct ConnectionTimeouts {
uint32_t receive = 15000;
uint32_t send = 15000;
};

struct ConnectionInfo {
Address remoteAddr;
uint32_t timeout;
ConnectionTimeouts timeouts;
uint16_t hostPort;
};

Expand All @@ -27,6 +32,9 @@ namespace Lambda::Network {
class ListenSocket;
};

enum struct SetTimeoutsDirection {
Both, Send, Receive
};
};

#endif
16 changes: 2 additions & 14 deletions core/network/sysnetw.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
Should work in theory. If it does not, well, something is wrong 🗿🚬
*/

#ifndef __LIB_MADDSUA_LAMBDA_INTERNAL_NETWORK_TCPIP__
#define __LIB_MADDSUA_LAMBDA_INTERNAL_NETWORK_TCPIP__
#ifndef __LIB_MADDSUA_LAMBDA_NETWORK_INTERNAL__
#define __LIB_MADDSUA_LAMBDA_NETWORK_INTERNAL__

#include <stdint.h>
#include "../error/error.hpp"
Expand Down Expand Up @@ -40,18 +40,6 @@
#include <arpa/inet.h>
#include <cerrno>

#ifndef SOCKET
typedef int SOCKET;
#endif

#ifndef INVALID_SOCKET
#define INVALID_SOCKET (-1)
#endif

#ifndef SOCKET_ERROR
#define SOCKET_ERROR (-1)
#endif

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

Expand Down
43 changes: 33 additions & 10 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(ConnCreateInit init) {
Connection::Connection(const ConnInit& init) {
this->m_info = init.info;
this->hSocket = init.hSocket;
}

Connection& Connection::operator= (Connection&& other) noexcept {
this->hSocket = other.hSocket;
this->m_info = other.m_info;
other.hSocket = INVALID_SOCKET;
other.hSocket = Network::invalid_socket;
return *this;
}

Connection::Connection(Connection&& other) noexcept {
this->hSocket = other.hSocket;
this->m_info = other.m_info;
other.hSocket = INVALID_SOCKET;
other.hSocket = Network::invalid_socket;
}

Connection::~Connection() {
if (this->hSocket == INVALID_SOCKET) return;
if (this->hSocket == Network::invalid_socket) return;
shutdown(this->hSocket, SD_BOTH);
closesocket(this->hSocket);
}

void Connection::end() noexcept {

if (this->hSocket == INVALID_SOCKET) return;
if (this->hSocket == Network::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 = INVALID_SOCKET;
this->hSocket = Network::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 != INVALID_SOCKET;
return this->hSocket != Network::invalid_socket;
}

void Connection::write(const std::vector<uint8_t>& data) {

if (this->hSocket == INVALID_SOCKET)
if (this->hSocket == Network::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 == INVALID_SOCKET)
if (this->hSocket == Network::invalid_socket)
throw std::runtime_error("can't read from a closed connection");

std::lock_guard<std::mutex> lock(this->m_readMutex);
Expand All @@ -90,7 +90,11 @@ std::vector<uint8_t> Connection::read(size_t expectedSize) {
switch (apiError) {

case LNE_TIMEDOUT: {
this->end();

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

return {};
}

Expand All @@ -104,3 +108,22 @@ std::vector<uint8_t> Connection::read(size_t expectedSize) {

return chunk;
}

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

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

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

void Connection::setTimeouts(uint32_t value) {
this->setTimeouts(value, SetTimeoutsDirection::Both);
}
Loading