Building on linux

multi_plaform
anulax1225 ago%!(EXTRA string=1 year)
parent d9659374c7
commit ef1389d5d1
  1. 10
      .gitignore
  2. 2
      LICENSE
  3. 1
      bakanet/premake5.lua
  4. 0
      bakanet/src/bakanet.h
  5. 42
      bakanet/src/bakanet/http/http_parser.cpp
  6. 26
      bakanet/src/bakanet/http/http_parser.h
  7. 48
      bakanet/src/bakanet/http/http_server.cpp
  8. 7
      bakanet/src/bakanet/http/http_server.h
  9. 34
      bakanet/src/bakanet/http/http_tools.cpp
  10. 7
      bakanet/src/bakanet/http/http_tools.h
  11. 0
      bakanet/src/bakanet/sock_layer/dns_lookup.h
  12. 6
      bakanet/src/bakanet/sock_layer/ip_address.h
  13. 0
      bakanet/src/bakanet/sock_layer/ip_protocol.h
  14. 0
      bakanet/src/bakanet/sock_layer/ip_version.h
  15. 1
      bakanet/src/bakanet/sock_layer/packet.h
  16. 0
      bakanet/src/bakanet/sock_layer/socket.h
  17. 0
      bakanet/src/bakanetpch.h
  18. 2
      bakanet/src/plaform/linux/linux_dns_lookup.cpp
  19. 7
      bakanet/src/plaform/linux/linux_ip_address.cpp
  20. 6
      bakanet/src/plaform/linux/linux_socket.cpp
  21. 2
      bakanet/src/plaform/linux/linux_socket.h
  22. 0
      bakanet/src/plaform/windows/windows_dns_lookup.cpp
  23. 0
      bakanet/src/plaform/windows/windows_ip_adress.cpp
  24. 0
      bakanet/src/plaform/windows/windows_socket.cpp
  25. 0
      bakanet/src/plaform/windows/windows_socket.h
  26. 0
      build
  27. 64
      premake5.lua

10
.gitignore vendored

@ -0,0 +1,10 @@
docs/
**.log
bin/
bin-int/
.vscode/
.vs/
**.sln
**Makefile
**.make
**.vcxproj*

@ -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.

@ -1,5 +1,4 @@
project "bakanet"
location "./bakanet"
kind "StaticLib"
language "C++"
cppdialect "C++17"

@ -1,42 +0,0 @@
#include "http_parser.h"
HttpRequest http_parser(std::string req)
{
std::string url = "", method = "", body = "";
std::unordered_map<std::string, std::string> 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;
}

@ -1,26 +0,0 @@
#include <unordered_map>
#include <commun.h>
#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<std::string, std::string> params;
std::string body;
};
HttpRequest http_parser(std::string req);
HttpMethod http_resolve_methode(std::string method);

@ -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<char> raw_data;
raw_data = sock.recv(conn, 4);
reading = req.append_data(raw_data);
}
close(conn);
int req_size = req.size();
std::unique_ptr<char[]> req_test = req.pull<char>(req_size);
if (req_size) return std::string(req_test.release(), req_size);
return "";
}

@ -1,7 +0,0 @@
#include <commun.h>
#include "http_parser.h"
using namespace Bk::Net;
void http_server();
std::string http_handler(Socket& sock, Connection conn);

@ -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<std::vector<std::string>> string_split(std::string s, std::string delimiter, int cpt)
{
std::unique_ptr<std::vector<std::string>> splits(new std::vector<std::string>(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);
}

@ -1,7 +0,0 @@
#include <cstring>
#include <commun.h>
std::string string_to_upper(std::string& str);
std::unique_ptr<std::vector<std::string>> string_split(std::string s, std::string delimiter, int cpt = -1);
std::string string_trim(const std::string& str, const std::string& whitespace = " ");

@ -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<void*> get_data();
std::string str;
IpVersion version;
};

@ -1,3 +1,4 @@
#pragma once
namespace Bk::Net {
class Packet

@ -1,4 +1,4 @@
#include "dns_lookup.h"
#include <bakanet/sock_layer/dns_lookup.h>
namespace Bk::Net {
std::vector<std::string> dns_lookup(const std::string &host_name, IpVersion ipv)

@ -2,13 +2,10 @@
#include <string.h>
namespace Bk::Net {
IpAddress::IpAddress(std::string ip, IpVersion ipv)
: str(ip), version(ipv) { }
void* IpAddress::get_data()
std::unique_ptr<void*> 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<void*>(&addr);
}
}

@ -1,4 +1,4 @@
#include "socket.h"
#include "linux_socket.h"
#include <iostream>
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> Socket::Create(IpAddress ip, int port, IpProtocol proto)
{
return std::unique_ptr(new LinuxSocket(ip, port, proto));
return std::unique_ptr<Socket>(new LinuxSocket(ip, port, proto));
}
}

@ -1,6 +1,6 @@
#pragma once
#include <bakanet/tcp_ip/socket.h>
#include <bakanet/sock_layer/socket.h>
namespace Bk::Net {
class LinuxSocket : public Socket

@ -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"
}
group "NetCore"
include "bakanet"
group ""
Loading…
Cancel
Save