initial commit

This commit is contained in:
Jorijn van der Graaf 2025-11-02 15:00:53 +01:00
commit 0fbc5bad52
18 changed files with 1289 additions and 0 deletions

View file

@ -0,0 +1,36 @@
/*
Crafter®.Network
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Network:ClientHTTP;
import std;
import :ClientTCP;
import :HTTP;
namespace Crafter {
export class ClientHTTP {
public:
ClientHTTP(const char* host, std::uint16_t port);
ClientHTTP(std::string host, std::uint16_t port);
HTTPResponse Send(const char* request, std::uint32_t length) const;
HTTPResponse Send(std::string request) const;
private:
ClientTCP client;
};
}

View file

@ -0,0 +1,45 @@
/*
Crafter®.Network
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Network:ClientTCP;
import std;
namespace Crafter {
export class ClientTCP {
public:
ClientTCP(int socket);
ClientTCP(const char* host, std::uint16_t port);
ClientTCP(std::string host, std::uint16_t port);
~ClientTCP();
void Send(const void* buffer, std::uint32_t size) const;
std::vector<char> RecieveSync() const;
std::vector<char> RecieveUntilCloseSync() const;
std::vector<char> RecieveUntilFullSync(std::uint32_t bufferSize) const;
std::vector<char> RecieveSync(std::uint32_t bufferSize) const;
int RecieveSync(std::uint32_t bufferSize, void* buffer) const;
void RecieveAsync(std::function<void(std::vector<char>)> recieveCallback) const;
void RecieveUntilCloseAsync(std::function<void(std::vector<char>)> recieveCallback) const;
void RecieveAsync(std::uint32_t bufferSize, std::function<void(std::vector<char>)> recieveCallback) const;
void RecieveUntilFullAsync(std::uint32_t bufferSize, std::function<void(std::vector<char>)> recieveCallback) const;
void RecieveAsync(std::uint32_t bufferSize, std::function<void(int)> recieveCallback, char* buffer) const;
private:
int socketid;
};
}

View file

@ -0,0 +1,84 @@
/*
Crafter®.Network
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Network:HTTP;
import std;
namespace Crafter {
export struct HTTPRequest {
std::string method;
std::unordered_map<std::string, std::string> headers;
std::string body;
};
export struct HTTPResponse {
std::string status;
std::unordered_map<std::string, std::string> headers;
std::string body;
};
export constexpr std::string CreateResponseHTTP(std::string status) {
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\n\r\n", status);
}
export constexpr std::string CreateResponseHTTP(std::string status, std::unordered_map<std::string, std::string> headers) {
std::string headersString;
for (auto const& [key, val] : headers) {
headersString+=std::format("{}: {}\r\n", key, val);
}
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\n{}\r\n", status, headersString);
}
export constexpr std::string CreateResponseHTTP(std::string status, std::string body) {
return std::format("HTTP/1.1 {}\r\nContent-Length: {}\r\nConnection: keep-alive\r\n\r\n{}", status, body.size(), body);
}
export constexpr std::string CreateResponseHTTP(std::string status, std::unordered_map<std::string, std::string> headers, std::string body) {
std::string headersString;
for (auto const& [key, val] : headers) {
headersString+=std::format("{}: {}\r\n", key, val);
}
return std::format("HTTP/1.1 {}\r\nConnection: keep-alive\r\nContent-Length: {}\r\n{}\r\n{}", status, body.size(), headersString, body);
}
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host) {
return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nAccept-Encoding: identity\r\nContent-Length: 0\r\nHost: {}\r\n\r\n", method, route, host);
}
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::unordered_map<std::string, std::string> headers, std::string host) {
std::string headersString;
for (auto const& [key, val] : headers) {
headersString+=std::format("{}: {}\r\n", key, val);
}
return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: 0\r\nAccept-Encoding: identity\r\nHost: {}\r\n{}\r\n", method, route, host, headersString);
}
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host, std::string body) {
return std::format("{} {} HTTP/1.1\r\nContent-Length: {}\r\nConnection: keep-alive\r\nAccept-Encoding: identity\r\nHost: {}\r\n\r\n{}", method, route, body.size(), host, body);
}
export constexpr std::string CreateRequestHTTP(std::string method, std::string route, std::string host, std::unordered_map<std::string, std::string> headers, std::string body) {
std::string headersString;
for (auto const& [key, val] : headers) {
headersString+=std::format("{}: {}\r\n", key, val);
}
return std::format("{} {} HTTP/1.1\r\nConnection: keep-alive\r\nContent-Length: {}\r\nHost: {}\r\nAccept-Encoding: identity\r\n{}\r\n{}", method, route, body.size(), host, headersString, body);
}
}

View file

@ -0,0 +1,41 @@
/*
Crafter®.Network
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Network:ListenerHTTP;
import std;
import :HTTP;
namespace Crafter {
export class ListenerHTTP {
public:
bool running = true;
std::unordered_map<std::string, std::function<std::string(const HTTPRequest&)>> routes;
ListenerHTTP(std::uint16_t port, std::unordered_map<std::string, std::function<std::string(const HTTPRequest&)>> routes);
~ListenerHTTP();
void Stop();
void ListenSyncSync() const;
void ListenSyncAsync() const;
void ListenAsyncSync() const;
void ListenAsyncAsync() const;
private:
void CallRoute(int client) const;
int s;
};
}

View file

@ -0,0 +1,43 @@
/*
Crafter®.Network
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Network:ListenerTCP;
import std;
import :ClientTCP;
namespace Crafter {
export class ListenerTCP {
public:
bool running = true;
std::function<void(ClientTCP*)> connectCallback;
std::uint32_t concurrentClientLimit;
std::uint32_t totalClientLimit;
ListenerTCP(std::uint16_t port, std::function<void(ClientTCP*)> connectCallback, std::uint32_t concurrentClientLimit = std::numeric_limits<std::uint32_t>::max(), std::uint32_t totalClientLimit = std::numeric_limits<std::uint32_t>::max());
~ListenerTCP();
void Stop();
void ListenSyncSync();
void ListenSyncAsync();
void ListenAsyncSync();
void ListenAsyncAsync();
private:
std::uint32_t totalClientCounter = 0;
int s;
};
}

27
interfaces/Crafter.Network.cppm Executable file
View file

@ -0,0 +1,27 @@
/*
Crafter®.Thread
Copyright (C) 2025 Catcrafts®
Catcrafts.net
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
export module Crafter.Network;
export import :ClientTCP;
export import :ListenerTCP;
export import :ClientHTTP;
export import :ListenerHTTP;
export import :HTTP;