Compare commits
No commits in common. "main" and "multi_plaform" have entirely different histories.
main
...
multi_plaf
33 changed files with 357 additions and 311 deletions
@ -0,0 +1,6 @@ |
|||||||
|
[submodule "vendor/spdlog"] |
||||||
|
path = vendor/spdlog |
||||||
|
url = https://github.com/gabime/spdlog |
||||||
|
[submodule "vendor/bakatools"] |
||||||
|
path = vendor/bakatools |
||||||
|
url = https://github.com/anulax1225/bakatools |
@ -0,0 +1,82 @@ |
|||||||
|
project "bakanet" |
||||||
|
kind "StaticLib" |
||||||
|
language "C++" |
||||||
|
cppdialect "C++20" |
||||||
|
systemversion "latest" |
||||||
|
staticruntime "on" |
||||||
|
|
||||||
|
targetdir("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}") |
||||||
|
objdir("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}") |
||||||
|
|
||||||
|
defines |
||||||
|
{ |
||||||
|
"BKMOD_ALL" |
||||||
|
} |
||||||
|
|
||||||
|
includedirs |
||||||
|
{ |
||||||
|
"%{IncludeDirs.spdlog}", |
||||||
|
"%{IncludeDirs.bakanet}", |
||||||
|
"%{IncludeDirs.bakatools}" |
||||||
|
} |
||||||
|
|
||||||
|
files |
||||||
|
{ |
||||||
|
"%{prj.location}/src/bakanet/**.h", |
||||||
|
"%{prj.location}/src/bakanet/**.cpp", |
||||||
|
"%{prj.location}/src/baknetpch.h", |
||||||
|
} |
||||||
|
|
||||||
|
links |
||||||
|
{ |
||||||
|
"bakatools" |
||||||
|
} |
||||||
|
|
||||||
|
filter "system:windows" |
||||||
|
buildoptions "/MDd" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_PLATFORM_WINDOWS" |
||||||
|
} |
||||||
|
|
||||||
|
files |
||||||
|
{ |
||||||
|
"%{prj.location}/src/platform/windows/**.h", |
||||||
|
"%{prj.location}/src/platform/windows/**.cpp", |
||||||
|
} |
||||||
|
|
||||||
|
links |
||||||
|
{ |
||||||
|
"WS2_32.lib" |
||||||
|
} |
||||||
|
|
||||||
|
filter "system:linux" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_PLATFORM_LINUX" |
||||||
|
} |
||||||
|
|
||||||
|
files |
||||||
|
{ |
||||||
|
"%{prj.location}/src/platform/linux/**.h", |
||||||
|
"%{prj.location}/src/platform/linux/**.cpp", |
||||||
|
} |
||||||
|
|
||||||
|
filter "configurations:Debug" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_DEBUG", |
||||||
|
"DEBUG" |
||||||
|
} |
||||||
|
runtime "Debug" |
||||||
|
symbols "on" |
||||||
|
|
||||||
|
|
||||||
|
filter "configurations:Release" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_RELEASE", |
||||||
|
"NDEBUG" |
||||||
|
} |
||||||
|
runtime "Release" |
||||||
|
optimize "on" |
@ -0,0 +1,95 @@ |
|||||||
|
#include "linux_socket.h" |
||||||
|
|
||||||
|
namespace Bk::Net { |
||||||
|
LinuxSocket::LinuxSocket(int id, IpVersion ver, IpProtocol proto) |
||||||
|
: id(id), ip_proto(proto) |
||||||
|
{ |
||||||
|
char myIP[16] = " "; |
||||||
|
socklen_t len = sizeof(addr); |
||||||
|
getsockname(id, (struct sockaddr*)&addr, &len); |
||||||
|
inet_ntop((int)ver, &addr, myIP, sizeof(myIP)); |
||||||
|
ip_addr = IpAddress(std::string(myIP, 16), ver); |
||||||
|
} |
||||||
|
|
||||||
|
LinuxSocket::LinuxSocket(IpAddress ip, int port, IpProtocol proto) |
||||||
|
: ip_addr(ip), ip_proto(proto) |
||||||
|
{ |
||||||
|
//LinuxSocket creation step
|
||||||
|
if ((id = socket((int)ip_addr.version, (int)ip_proto, 0)) < 0)
|
||||||
|
{ |
||||||
|
BK_CORE_ERROR("Socket failed"); |
||||||
|
exit(EXIT_FAILURE); |
||||||
|
} |
||||||
|
addr.sin_addr = ip_addr.get_data(); |
||||||
|
addr.sin_family = (int)ip_addr.version; |
||||||
|
addr.sin_port = htons(port); |
||||||
|
} |
||||||
|
|
||||||
|
LinuxSocket::~LinuxSocket() |
||||||
|
{ |
||||||
|
close(id); |
||||||
|
} |
||||||
|
|
||||||
|
bool LinuxSocket::init() |
||||||
|
{ |
||||||
|
//Binding step
|
||||||
|
int status; |
||||||
|
if ((status = bind(id, (struct sockaddr*)&addr, sizeof(addr)) < 0))
|
||||||
|
{ |
||||||
|
BK_CORE_ERROR("Binding failed"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
bool LinuxSocket::start(int cpt_conn) |
||||||
|
{ |
||||||
|
//Listening step
|
||||||
|
if (listen(id, cpt_conn) < 0)
|
||||||
|
{ |
||||||
|
BK_CORE_ERROR("Listening failed"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<Socket> LinuxSocket::ack() |
||||||
|
{ |
||||||
|
socklen_t addrlen = sizeof(addr); |
||||||
|
return std::unique_ptr<Socket>(Socket::create(accept(id, (struct sockaddr*)&addr, &addrlen), ip_addr.version, ip_proto)); |
||||||
|
} |
||||||
|
|
||||||
|
bool LinuxSocket::conn() |
||||||
|
{ |
||||||
|
if (connect(id, (struct sockaddr*)&addr, sizeof(addr)) < 0)
|
||||||
|
{ |
||||||
|
BK_CORE_ERROR("Connection failed"); |
||||||
|
return false; |
||||||
|
} |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
void LinuxSocket::emit(std::vector<char> packet) |
||||||
|
{ |
||||||
|
write(id, packet.data(), packet.size()); |
||||||
|
} |
||||||
|
|
||||||
|
std::vector<char> LinuxSocket::obtain(int size) |
||||||
|
{ |
||||||
|
std::vector<char> buffer; |
||||||
|
buffer.resize(size); |
||||||
|
int read_size = read(id, buffer.data(), buffer.size() - 1); |
||||||
|
buffer.resize(read_size); |
||||||
|
return buffer; |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<Socket> Socket::create(IpAddress ip, int port, IpProtocol proto) |
||||||
|
{ |
||||||
|
return std::unique_ptr<Socket>(new LinuxSocket(ip, port, proto)); |
||||||
|
} |
||||||
|
|
||||||
|
std::unique_ptr<Socket> Socket::create(int id, IpVersion ver, IpProtocol proto) |
||||||
|
{ |
||||||
|
return std::unique_ptr<Socket>(new LinuxSocket(id, ver, proto)); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
|
||||||
|
|
||||||
|
handle_error() { |
||||||
|
echo "An error occurred on line $1" |
||||||
|
exit 1 |
||||||
|
} |
||||||
|
trap 'handle_error $LINENO' ERR |
||||||
|
|
||||||
|
if [ "-clear" == "$1" ]; then |
||||||
|
rm -rf bin bin-int |
||||||
|
fi |
||||||
|
|
||||||
|
if [ "-clear" == "$2" ]; then |
||||||
|
rm -rf bin bin-int |
||||||
|
fi |
||||||
|
|
||||||
|
clear |
||||||
|
premake5 gmake2 |
||||||
|
make |
||||||
|
|
||||||
|
if [ "-exec" == "$1" ]; then |
||||||
|
./bin/linux-x86_64-Debug/server/server |
||||||
|
fi |
@ -1,5 +0,0 @@ |
|||||||
IncludeDirs["bakanet"] = "%{wks.location}/vendor/bakanet/src/" |
|
||||||
|
|
||||||
group "Bakanet" |
|
||||||
include "vendor/bakanet" |
|
||||||
group "" |
|
@ -1,20 +0,0 @@ |
|||||||
{ |
|
||||||
"name": "Bakanet", |
|
||||||
"author": "Anulax", |
|
||||||
"git": "https://github.com/anulax1225/bakanet", |
|
||||||
"links": |
|
||||||
[ |
|
||||||
"bakanet" |
|
||||||
], |
|
||||||
"includes": |
|
||||||
[ |
|
||||||
"bakanet" |
|
||||||
], |
|
||||||
"packages": |
|
||||||
[ |
|
||||||
{ |
|
||||||
"author": "anulax", |
|
||||||
"name": "bakatools" |
|
||||||
} |
|
||||||
] |
|
||||||
} |
|
@ -1,77 +1,27 @@ |
|||||||
project "bakanet" |
workspace "BakaraNet" |
||||||
kind "StaticLib" |
architecture "x64" |
||||||
language "C++" |
configurations { "Debug", "Release" } |
||||||
cppdialect "C++20" |
startproject "server" |
||||||
systemversion "latest" |
flags |
||||||
staticruntime "on" |
{ |
||||||
|
"MultiProcessorCompile" |
||||||
targetdir("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}") |
} |
||||||
objdir("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}") |
|
||||||
|
outputdir = "%{cfg.system}-%{cfg.architecture}-%{cfg.buildcfg}" |
||||||
includedirs |
|
||||||
{ |
IncludeDirs = {} |
||||||
"%{IncludeDirs.spdlog}", |
IncludeDirs["bakanet"] = "%{wks.location}/bakanet/src/" |
||||||
"%{IncludeDirs.bakanet}", |
IncludeDirs["bakatools"] = "%{wks.location}/vendor/bakatools/src/" |
||||||
"%{IncludeDirs.bakatools}" |
IncludeDirs["spdlog"] = "%{wks.location}/vendor/spdlog/include" |
||||||
} |
|
||||||
|
group "BakaModules" |
||||||
files |
include "vendor/bakatools" |
||||||
{ |
group "" |
||||||
"%{prj.location}/src/bakanet/**.h", |
|
||||||
"%{prj.location}/src/bakanet/**.cpp", |
group "NetCore" |
||||||
"%{prj.location}/src/baknetpch.h", |
include "bakanet" |
||||||
} |
group "" |
||||||
|
|
||||||
links |
group "Sandbox" |
||||||
{ |
include "sandbox" |
||||||
"bakatools" |
group "" |
||||||
} |
|
||||||
|
|
||||||
filter "system:windows" |
|
||||||
buildoptions { "/MT", "/utf-8" } |
|
||||||
defines |
|
||||||
{ |
|
||||||
"BK_PLATFORM_WINDOWS" |
|
||||||
} |
|
||||||
|
|
||||||
files |
|
||||||
{ |
|
||||||
"%{prj.location}/src/platform/windows/**.h", |
|
||||||
"%{prj.location}/src/platform/windows/**.cpp", |
|
||||||
} |
|
||||||
|
|
||||||
links |
|
||||||
{ |
|
||||||
"WS2_32.lib" |
|
||||||
} |
|
||||||
|
|
||||||
filter "system:linux" |
|
||||||
defines |
|
||||||
{ |
|
||||||
"BK_PLATFORM_LINUX" |
|
||||||
} |
|
||||||
|
|
||||||
files |
|
||||||
{ |
|
||||||
"%{prj.location}/src/platform/linux/**.h", |
|
||||||
"%{prj.location}/src/platform/linux/**.cpp", |
|
||||||
} |
|
||||||
|
|
||||||
filter "configurations:Debug" |
|
||||||
defines |
|
||||||
{ |
|
||||||
"BK_DEBUG", |
|
||||||
"DEBUG" |
|
||||||
} |
|
||||||
runtime "Debug" |
|
||||||
symbols "on" |
|
||||||
|
|
||||||
|
|
||||||
filter "configurations:Release" |
|
||||||
defines |
|
||||||
{ |
|
||||||
"BK_RELEASE", |
|
||||||
"NDEBUG" |
|
||||||
} |
|
||||||
runtime "Release" |
|
||||||
optimize "on" |
|
@ -0,0 +1,66 @@ |
|||||||
|
project "server" |
||||||
|
location "./server" |
||||||
|
kind "ConsoleApp" |
||||||
|
language "C++" |
||||||
|
cppdialect "C++20" |
||||||
|
systemversion "latest" |
||||||
|
|
||||||
|
targetdir("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}") |
||||||
|
objdir("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}") |
||||||
|
|
||||||
|
defines |
||||||
|
{ |
||||||
|
"BKMOD_ALL" |
||||||
|
} |
||||||
|
|
||||||
|
includedirs |
||||||
|
{ |
||||||
|
"%{IncludeDirs.spdlog}", |
||||||
|
"%{IncludeDirs.bakanet}", |
||||||
|
"%{IncludeDirs.bakatools}" |
||||||
|
} |
||||||
|
|
||||||
|
files |
||||||
|
{ |
||||||
|
"%{prj.location}/**.h", |
||||||
|
"%{prj.location}/**.cpp", |
||||||
|
} |
||||||
|
|
||||||
|
links |
||||||
|
{ |
||||||
|
"bakanet", |
||||||
|
"bakatools" |
||||||
|
} |
||||||
|
|
||||||
|
filter "system:linux" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_PLATFORM_LINUX" |
||||||
|
} |
||||||
|
|
||||||
|
filter "system:windows" |
||||||
|
buildoptions "/MDd" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_PLATFORM_WINDOWS" |
||||||
|
} |
||||||
|
|
||||||
|
filter "configurations:Debug" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_DEBUG", |
||||||
|
"DEBUG" |
||||||
|
} |
||||||
|
runtime "Debug" |
||||||
|
symbols "on" |
||||||
|
|
||||||
|
filter "configurations:Release" |
||||||
|
defines |
||||||
|
{ |
||||||
|
"BK_RELEASE", |
||||||
|
"NDEBUG" |
||||||
|
} |
||||||
|
runtime "Release" |
||||||
|
optimize "on" |
||||||
|
|
||||||
|
|
@ -1,104 +0,0 @@ |
|||||||
#include "linux_socket.h" |
|
||||||
#include <cstddef> |
|
||||||
#include <unistd.h> |
|
||||||
#include <netinet/in.h> |
|
||||||
#include <sys/socket.h> |
|
||||||
|
|
||||||
namespace Bk::Net { |
|
||||||
LinuxSocket::LinuxSocket(int id, struct sockaddr_in client_addr, IpVersion ver, IpProtocol proto) |
|
||||||
: id(id), ip_proto(proto), addr(client_addr) |
|
||||||
{ |
|
||||||
char* myIP = inet_ntoa(addr.sin_addr); |
|
||||||
ip_addr = IpAddress(std::string(myIP), ver); |
|
||||||
} |
|
||||||
|
|
||||||
LinuxSocket::LinuxSocket(IpAddress ip, int port, IpProtocol proto) |
|
||||||
: ip_addr(ip), ip_proto(proto) |
|
||||||
{ |
|
||||||
//LinuxSocket creation step
|
|
||||||
if ((id = socket((int)ip_addr.version, (int)ip_proto, 0)) < 0)
|
|
||||||
{ |
|
||||||
BK_CORE_ERROR("Socket failed"); |
|
||||||
exit(EXIT_FAILURE); |
|
||||||
} |
|
||||||
addr.sin_addr = ip_addr.get_data(); |
|
||||||
addr.sin_family = (int)ip_addr.version; |
|
||||||
addr.sin_port = htons(port); |
|
||||||
} |
|
||||||
|
|
||||||
LinuxSocket::~LinuxSocket() |
|
||||||
{ |
|
||||||
if(id > 0) close(id); |
|
||||||
} |
|
||||||
|
|
||||||
bool LinuxSocket::init() |
|
||||||
{ |
|
||||||
//Binding step
|
|
||||||
int status; |
|
||||||
if ((status = bind(id, (struct sockaddr*)&addr, sizeof(addr)) < 0))
|
|
||||||
{ |
|
||||||
BK_CORE_ERROR("Binding failed"); |
|
||||||
return false; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
bool LinuxSocket::start(int cpt_conn) |
|
||||||
{ |
|
||||||
//Listening step
|
|
||||||
if (listen(id, cpt_conn) < 0)
|
|
||||||
{ |
|
||||||
BK_CORE_ERROR("Listening failed"); |
|
||||||
return false; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
Socket* LinuxSocket::ack() |
|
||||||
{ |
|
||||||
struct sockaddr_in client_addr; |
|
||||||
socklen_t addrlen = sizeof(client_addr); |
|
||||||
return new LinuxSocket(accept(id, (struct sockaddr*)&client_addr, &addrlen), client_addr, ip_addr.version, ip_proto); |
|
||||||
} |
|
||||||
|
|
||||||
bool LinuxSocket::conn() |
|
||||||
{ |
|
||||||
if (connect(id, (struct sockaddr*)&addr, sizeof(addr)) != 0)
|
|
||||||
{ |
|
||||||
BK_CORE_ERROR("Connection failed"); |
|
||||||
return false; |
|
||||||
} |
|
||||||
return true; |
|
||||||
} |
|
||||||
|
|
||||||
bool LinuxSocket::isConnected() |
|
||||||
{ |
|
||||||
char data = 0; |
|
||||||
int read_size = recv(id, &data, 1, MSG_PEEK); |
|
||||||
return (bool)read_size; |
|
||||||
} |
|
||||||
|
|
||||||
void LinuxSocket::emit(std::vector<char> packet) |
|
||||||
{ |
|
||||||
size_t size = send(id, packet.data(), packet.size(), 0); |
|
||||||
} |
|
||||||
|
|
||||||
std::vector<char> LinuxSocket::obtain(int size) |
|
||||||
{ |
|
||||||
if(size > this->size) |
|
||||||
{ |
|
||||||
delete [] buffer; |
|
||||||
buffer = new char[size]; |
|
||||||
this->size = size; |
|
||||||
} |
|
||||||
int read_size = recv(id, buffer, this->size - 1, 0); |
|
||||||
if(read_size == -1) return std::vector<char>(0); |
|
||||||
std::vector<char> packet(buffer, buffer + read_size); |
|
||||||
return packet; |
|
||||||
} |
|
||||||
|
|
||||||
Socket* Socket::create(IpAddress ip, int port, IpProtocol proto) |
|
||||||
{ |
|
||||||
return new LinuxSocket(ip, port, proto); |
|
||||||
} |
|
||||||
} |
|
@ -1,40 +0,0 @@ |
|||||||
#include <bakanet/core/dns_lookup.h> |
|
||||||
|
|
||||||
namespace Bk::Net { |
|
||||||
std::vector<std::string> dns_lookup(const std::string& host_name, IpVersion ipv) |
|
||||||
{ |
|
||||||
std::vector<std::string> output; |
|
||||||
struct addrinfo hints, *res, *p; |
|
||||||
char ip_address[INET6_ADDRSTRLEN]; |
|
||||||
memset(&hints, 0, sizeof hints); |
|
||||||
hints.ai_family = (int)ipv; |
|
||||||
hints.ai_socktype = SOCK_STREAM; |
|
||||||
|
|
||||||
if (getaddrinfo(host_name.c_str(), NULL, &hints, &res) != 0) |
|
||||||
{ |
|
||||||
output.push_back(""); |
|
||||||
return output; |
|
||||||
} |
|
||||||
|
|
||||||
for (p = res;p != NULL; p = p->ai_next) |
|
||||||
{ |
|
||||||
void* addr; |
|
||||||
if (p->ai_family == AF_INET) |
|
||||||
{ |
|
||||||
struct sockaddr_in* ipv4 = (struct sockaddr_in*)p->ai_addr; |
|
||||||
addr = &(ipv4->sin_addr); |
|
||||||
} |
|
||||||
else |
|
||||||
{ |
|
||||||
struct sockaddr_in6* ipv6 = (struct sockaddr_in6*)p->ai_addr; |
|
||||||
addr = &(ipv6->sin6_addr); |
|
||||||
} |
|
||||||
inet_ntop(p->ai_family, addr, ip_address, sizeof ip_address); |
|
||||||
output.push_back(ip_address); |
|
||||||
} |
|
||||||
|
|
||||||
freeaddrinfo(res); // free the linked list
|
|
||||||
|
|
||||||
return output; |
|
||||||
} |
|
||||||
} |
|
@ -0,0 +1 @@ |
|||||||
|
Subproject commit 40d24e39a7a7b39a056418d78162103036e6534a |
@ -0,0 +1 @@ |
|||||||
|
Subproject commit 23587b0d9aebad231d7ba4f16873d70edd2b9dee |
Loading…
Reference in New Issue