From ef1389d5d1de2d874f3939ab7790400c813ac4af Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Wed, 13 Mar 2024 14:50:05 +0100 Subject: [PATCH] Building on linux --- .gitignore | 10 +++ LICENSE | 2 +- bakanet/premake5.lua | 1 - bakanet/src/bakanet.h | 0 bakanet/src/bakanet/http/http_parser.cpp | 42 ------------ bakanet/src/bakanet/http/http_parser.h | 26 -------- bakanet/src/bakanet/http/http_server.cpp | 48 -------------- bakanet/src/bakanet/http/http_server.h | 7 -- bakanet/src/bakanet/http/http_tools.cpp | 34 ---------- bakanet/src/bakanet/http/http_tools.h | 7 -- bakanet/src/bakanet/sock_layer/dns_lookup.h | 0 bakanet/src/bakanet/sock_layer/ip_address.h | 6 +- bakanet/src/bakanet/sock_layer/ip_protocol.h | 0 bakanet/src/bakanet/sock_layer/ip_version.h | 0 bakanet/src/bakanet/sock_layer/packet.h | 1 + bakanet/src/bakanet/sock_layer/socket.h | 0 bakanet/src/bakanetpch.h | 0 .../src/plaform/linux/linux_dns_lookup.cpp | 2 +- .../src/plaform/linux/linux_ip_address.cpp | 7 +- bakanet/src/plaform/linux/linux_socket.cpp | 6 +- bakanet/src/plaform/linux/linux_socket.h | 2 +- .../plaform/windows/windows_dns_lookup.cpp | 0 .../src/plaform/windows/windows_ip_adress.cpp | 0 .../src/plaform/windows/windows_socket.cpp | 0 bakanet/src/plaform/windows/windows_socket.h | 0 build | 0 premake5.lua | 64 ++----------------- 27 files changed, 28 insertions(+), 237 deletions(-) create mode 100755 .gitignore mode change 100644 => 100755 LICENSE mode change 100644 => 100755 bakanet/premake5.lua mode change 100644 => 100755 bakanet/src/bakanet.h delete mode 100644 bakanet/src/bakanet/http/http_parser.cpp delete mode 100644 bakanet/src/bakanet/http/http_parser.h delete mode 100644 bakanet/src/bakanet/http/http_server.cpp delete mode 100644 bakanet/src/bakanet/http/http_server.h delete mode 100644 bakanet/src/bakanet/http/http_tools.cpp delete mode 100644 bakanet/src/bakanet/http/http_tools.h mode change 100644 => 100755 bakanet/src/bakanet/sock_layer/dns_lookup.h mode change 100644 => 100755 bakanet/src/bakanet/sock_layer/ip_address.h mode change 100644 => 100755 bakanet/src/bakanet/sock_layer/ip_protocol.h mode change 100644 => 100755 bakanet/src/bakanet/sock_layer/ip_version.h mode change 100644 => 100755 bakanet/src/bakanet/sock_layer/packet.h mode change 100644 => 100755 bakanet/src/bakanet/sock_layer/socket.h mode change 100644 => 100755 bakanet/src/bakanetpch.h mode change 100644 => 100755 bakanet/src/plaform/linux/linux_dns_lookup.cpp mode change 100644 => 100755 bakanet/src/plaform/linux/linux_ip_address.cpp mode change 100644 => 100755 bakanet/src/plaform/linux/linux_socket.cpp mode change 100644 => 100755 bakanet/src/plaform/linux/linux_socket.h mode change 100644 => 100755 bakanet/src/plaform/windows/windows_dns_lookup.cpp mode change 100644 => 100755 bakanet/src/plaform/windows/windows_ip_adress.cpp mode change 100644 => 100755 bakanet/src/plaform/windows/windows_socket.cpp mode change 100644 => 100755 bakanet/src/plaform/windows/windows_socket.h mode change 100644 => 100755 build mode change 100644 => 100755 premake5.lua diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..9c60ff8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,10 @@ +docs/ +**.log +bin/ +bin-int/ +.vscode/ +.vs/ +**.sln +**Makefile +**.make +**.vcxproj* \ No newline at end of file diff --git a/LICENSE b/LICENSE old mode 100644 new mode 100755 index 261eeb9..b2d46df --- a/LICENSE +++ b/LICENSE @@ -186,7 +186,7 @@ same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright [yyyy] [name of copyright owner] + Copyright [2024] [anulax1225] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/bakanet/premake5.lua b/bakanet/premake5.lua old mode 100644 new mode 100755 index fd74a2d..41c363b --- a/bakanet/premake5.lua +++ b/bakanet/premake5.lua @@ -1,5 +1,4 @@ project "bakanet" - location "./bakanet" kind "StaticLib" language "C++" cppdialect "C++17" diff --git a/bakanet/src/bakanet.h b/bakanet/src/bakanet.h old mode 100644 new mode 100755 diff --git a/bakanet/src/bakanet/http/http_parser.cpp b/bakanet/src/bakanet/http/http_parser.cpp deleted file mode 100644 index 8554440..0000000 --- a/bakanet/src/bakanet/http/http_parser.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "http_parser.h" - -HttpRequest http_parser(std::string req) -{ - std::string url = "", method = "", body = ""; - std::unordered_map params; - - auto lines = string_split(req, "\n"); - - auto first_line = string_split(lines->at(0), " "); - method = string_to_upper(first_line->at(0)); - url = first_line->at(1); - body = lines->at(lines->size() - 1); - - lines->erase(lines->begin()); - lines->pop_back(); - - for (auto line : *lines) - { - auto param = string_split(line, ":", 1); - if (param->size() == 2) - { - params.insert({ param->at(0), string_trim(param->at(1))}); - } - } - return HttpRequest { - http_resolve_methode(method), - url, - params, - body - }; -} - - -HttpMethod http_resolve_methode(std::string method) -{ - if (method.compare("GET")) return HttpMethod::GET; - else if (method.compare("POST")) return HttpMethod::POST; - else if (method.compare("PUT")) return HttpMethod::PUT; - else if (method.compare("DELETE")) return HttpMethod::DELETE; - else return HttpMethod::NONE; -} \ No newline at end of file diff --git a/bakanet/src/bakanet/http/http_parser.h b/bakanet/src/bakanet/http/http_parser.h deleted file mode 100644 index d725ebd..0000000 --- a/bakanet/src/bakanet/http/http_parser.h +++ /dev/null @@ -1,26 +0,0 @@ -#include - -#include - -#include "http_tools.h" - -enum class HttpMethod -{ - NONE = 0, - GET = 1, - POST = 2, - PUT = 3, - DELETE = 4, -}; - -class HttpRequest -{ - public: - HttpMethod method; - std::string url; - std::unordered_map params; - std::string body; -}; - -HttpRequest http_parser(std::string req); -HttpMethod http_resolve_methode(std::string method); \ No newline at end of file diff --git a/bakanet/src/bakanet/http/http_server.cpp b/bakanet/src/bakanet/http/http_server.cpp deleted file mode 100644 index 49c1d0e..0000000 --- a/bakanet/src/bakanet/http/http_server.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "http_server.h" - -void http_server() -{ - IpAddress ip("127.0.0.1"); - Socket sock(ip, PORT, IpProtocol::TCP); - bool running = sock.init() && sock.start(5); - char input = 'N'; - - do - { - Connection conn = sock.ack(); - if (conn >= 0) - { - std::string http_request(http_handler(sock, conn)); - - if (http_request == "") continue; - HttpRequest req = http_parser(http_request); - log("Http request"); - log("Method " << (int)req.method) - log("URL " << req.url) - log("Body " << req.body) - } - log("Close?") - input(input); - } while (input != 'y'); - -} - -std::string http_handler(Socket& sock, Connection conn) -{ - Packet req; - bool reading = true; - while(reading) - { - std::vector raw_data; - raw_data = sock.recv(conn, 4); - reading = req.append_data(raw_data); - } - - close(conn); - - int req_size = req.size(); - std::unique_ptr req_test = req.pull(req_size); - - if (req_size) return std::string(req_test.release(), req_size); - return ""; -} \ No newline at end of file diff --git a/bakanet/src/bakanet/http/http_server.h b/bakanet/src/bakanet/http/http_server.h deleted file mode 100644 index f8c6e69..0000000 --- a/bakanet/src/bakanet/http/http_server.h +++ /dev/null @@ -1,7 +0,0 @@ -#include - -#include "http_parser.h" - -using namespace Bk::Net; -void http_server(); -std::string http_handler(Socket& sock, Connection conn); \ No newline at end of file diff --git a/bakanet/src/bakanet/http/http_tools.cpp b/bakanet/src/bakanet/http/http_tools.cpp deleted file mode 100644 index d192bf4..0000000 --- a/bakanet/src/bakanet/http/http_tools.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include "http_tools.h" - -std::string string_to_upper(std::string& str) -{ - for (int i = 0; i < str.length(); i++) - { - str[i] = std::toupper(str[i]); - } - return str; -} - -std::unique_ptr> string_split(std::string s, std::string delimiter, int cpt) -{ - std::unique_ptr> splits(new std::vector(0)); - size_t pos = 0; - while (((pos = s.find(delimiter)) != std::string::npos) && cpt-- != 0) { - splits->push_back(s.substr(0, pos)); - s.erase(0, pos + delimiter.length()); - } - splits->push_back(s); - return splits; -} - -std::string string_trim(const std::string& str, const std::string& whitespace) -{ - const auto strBegin = str.find_first_not_of(whitespace); - if (strBegin == std::string::npos) - return ""; // no content - - const auto strEnd = str.find_last_not_of(whitespace); - const auto strRange = strEnd - strBegin + 1; - - return str.substr(strBegin, strRange); -} \ No newline at end of file diff --git a/bakanet/src/bakanet/http/http_tools.h b/bakanet/src/bakanet/http/http_tools.h deleted file mode 100644 index c555b7d..0000000 --- a/bakanet/src/bakanet/http/http_tools.h +++ /dev/null @@ -1,7 +0,0 @@ -#include - -#include - -std::string string_to_upper(std::string& str); -std::unique_ptr> string_split(std::string s, std::string delimiter, int cpt = -1); -std::string string_trim(const std::string& str, const std::string& whitespace = " "); \ No newline at end of file diff --git a/bakanet/src/bakanet/sock_layer/dns_lookup.h b/bakanet/src/bakanet/sock_layer/dns_lookup.h old mode 100644 new mode 100755 diff --git a/bakanet/src/bakanet/sock_layer/ip_address.h b/bakanet/src/bakanet/sock_layer/ip_address.h old mode 100644 new mode 100755 index 42e5ce4..4ddf6ba --- a/bakanet/src/bakanet/sock_layer/ip_address.h +++ b/bakanet/src/bakanet/sock_layer/ip_address.h @@ -7,8 +7,10 @@ namespace Bk::Net { class IpAddress { public: - IpAddress(std::string ip, IpVersion ipv = IpVersion::IPv4); - void* get_data(); + IpAddress(std::string ip, IpVersion ipv = IpVersion::IPv4) + : str(ip), version(ipv) { } + + std::unique_ptr get_data(); std::string str; IpVersion version; }; diff --git a/bakanet/src/bakanet/sock_layer/ip_protocol.h b/bakanet/src/bakanet/sock_layer/ip_protocol.h old mode 100644 new mode 100755 diff --git a/bakanet/src/bakanet/sock_layer/ip_version.h b/bakanet/src/bakanet/sock_layer/ip_version.h old mode 100644 new mode 100755 diff --git a/bakanet/src/bakanet/sock_layer/packet.h b/bakanet/src/bakanet/sock_layer/packet.h old mode 100644 new mode 100755 index 1f66f0c..9e65d58 --- a/bakanet/src/bakanet/sock_layer/packet.h +++ b/bakanet/src/bakanet/sock_layer/packet.h @@ -1,3 +1,4 @@ +#pragma once namespace Bk::Net { class Packet diff --git a/bakanet/src/bakanet/sock_layer/socket.h b/bakanet/src/bakanet/sock_layer/socket.h old mode 100644 new mode 100755 diff --git a/bakanet/src/bakanetpch.h b/bakanet/src/bakanetpch.h old mode 100644 new mode 100755 diff --git a/bakanet/src/plaform/linux/linux_dns_lookup.cpp b/bakanet/src/plaform/linux/linux_dns_lookup.cpp old mode 100644 new mode 100755 index 70a1303..e2a9d34 --- a/bakanet/src/plaform/linux/linux_dns_lookup.cpp +++ b/bakanet/src/plaform/linux/linux_dns_lookup.cpp @@ -1,4 +1,4 @@ -#include "dns_lookup.h" +#include namespace Bk::Net { std::vector dns_lookup(const std::string &host_name, IpVersion ipv) diff --git a/bakanet/src/plaform/linux/linux_ip_address.cpp b/bakanet/src/plaform/linux/linux_ip_address.cpp old mode 100644 new mode 100755 index ba70aa3..43292e7 --- a/bakanet/src/plaform/linux/linux_ip_address.cpp +++ b/bakanet/src/plaform/linux/linux_ip_address.cpp @@ -2,13 +2,10 @@ #include namespace Bk::Net { - IpAddress::IpAddress(std::string ip, IpVersion ipv) - : str(ip), version(ipv) { } - - void* IpAddress::get_data() + std::unique_ptr IpAddress::get_data() { struct in_addr addr; if (inet_pton(AF_INET, str.c_str(), &addr) <= 0) perror("Bad IP"); - return (void*)addr; + return std::make_unique(&addr); } } \ No newline at end of file diff --git a/bakanet/src/plaform/linux/linux_socket.cpp b/bakanet/src/plaform/linux/linux_socket.cpp old mode 100644 new mode 100755 index 7e460b2..2dd46e5 --- a/bakanet/src/plaform/linux/linux_socket.cpp +++ b/bakanet/src/plaform/linux/linux_socket.cpp @@ -1,4 +1,4 @@ -#include "socket.h" +#include "linux_socket.h" #include namespace Bk::Net { @@ -11,7 +11,7 @@ namespace Bk::Net { perror("socket failed"); exit(EXIT_FAILURE); } - addr.sin_addr = (struct in_addr)ip_addr.get_data(); + addr.sin_addr = *(struct in_addr*)*ip_addr.get_data(); addr.sin_family = (int)ip_addr.version; addr.sin_port = htons(port); } @@ -88,6 +88,6 @@ namespace Bk::Net { std::unique_ptr Socket::Create(IpAddress ip, int port, IpProtocol proto) { - return std::unique_ptr(new LinuxSocket(ip, port, proto)); + return std::unique_ptr(new LinuxSocket(ip, port, proto)); } } \ No newline at end of file diff --git a/bakanet/src/plaform/linux/linux_socket.h b/bakanet/src/plaform/linux/linux_socket.h old mode 100644 new mode 100755 index 2602360..5314b55 --- a/bakanet/src/plaform/linux/linux_socket.h +++ b/bakanet/src/plaform/linux/linux_socket.h @@ -1,6 +1,6 @@ #pragma once -#include +#include namespace Bk::Net { class LinuxSocket : public Socket diff --git a/bakanet/src/plaform/windows/windows_dns_lookup.cpp b/bakanet/src/plaform/windows/windows_dns_lookup.cpp old mode 100644 new mode 100755 diff --git a/bakanet/src/plaform/windows/windows_ip_adress.cpp b/bakanet/src/plaform/windows/windows_ip_adress.cpp old mode 100644 new mode 100755 diff --git a/bakanet/src/plaform/windows/windows_socket.cpp b/bakanet/src/plaform/windows/windows_socket.cpp old mode 100644 new mode 100755 diff --git a/bakanet/src/plaform/windows/windows_socket.h b/bakanet/src/plaform/windows/windows_socket.h old mode 100644 new mode 100755 diff --git a/build b/build old mode 100644 new mode 100755 diff --git a/premake5.lua b/premake5.lua old mode 100644 new mode 100755 index a57640d..cefb04e --- a/premake5.lua +++ b/premake5.lua @@ -1,7 +1,7 @@ -workspace "socket_unix" +workspace "BakaraNet" architecture "x64" configurations { "Debug", "Release" } - startproject "server" + startproject "bakanet" flags { "MultiProcessorCompile" @@ -10,62 +10,8 @@ workspace "socket_unix" outputdir = "%{cfg.system}-%{cfg.architecture}-%{cfg.buildcfg}" IncludeDirs = {} - IncludeDirs["bakanet"] = "%{wks.location}/bakanet/src/" -project "server" - location "./sandbox/server" - kind "ConsoleApp" - language "C++" - cppdialect "C++17" - systemversion "latest" - - targetdir("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}") - objdir("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}") - - includedirs - { - "%{IncludeDirs.bakanet}", - "./sandbox/" - } - - files - { - "%{prj.location}/**.h", - "%{prj.location}/**.cpp", - "./sandbox/commun.h" - } - - links - { - "bakanet" - } - - - -project "client" - location "./sandbox/client" - kind "ConsoleApp" - language "C++" - cppdialect "C++17" - systemversion "latest" - - targetdir("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}") - objdir("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}") - - includedirs - { - "%{IncludeDirs.bakanet}" - } - - files - { - "%{prj.location}/**.h", - "%{prj.location}/**.cpp", - "./sandbox/commun.h" - } - - links - { - "bakanet" - } \ No newline at end of file +group "NetCore" + include "bakanet" +group "" \ No newline at end of file