diff --git a/bakanet/src/bakanet/http/http_packet.cpp b/bakanet/src/bakanet/http/http_packet.cpp index 7171905..f875bcd 100755 --- a/bakanet/src/bakanet/http/http_packet.cpp +++ b/bakanet/src/bakanet/http/http_packet.cpp @@ -62,6 +62,7 @@ namespace Bk::Net { HttpReponse::HttpReponse(std::string data) { + //To do status = ""; version = ""; params = HttpParams(0); diff --git a/bakanet/src/bakanet/http/http_server.cpp b/bakanet/src/bakanet/http/http_server.cpp index d21cfb0..1ae2731 100755 --- a/bakanet/src/bakanet/http/http_server.cpp +++ b/bakanet/src/bakanet/http/http_server.cpp @@ -10,11 +10,9 @@ namespace Bk::Net { bool running = socket->init() && socket->start(5); while (running) { - log("BEFORE") Connection conn = socket->ack(); if(conn >= 0) { - log("AFTER") route_request(conn, recv_request(conn)); closesocket(conn); } diff --git a/bakanet/src/bakanet/http/radix_tree.cpp b/bakanet/src/bakanet/http/radix_tree.cpp new file mode 100644 index 0000000..e69de29 diff --git a/bakanet/src/bakanet/http/radix_tree.h b/bakanet/src/bakanet/http/radix_tree.h new file mode 100644 index 0000000..e69de29 diff --git a/bakanet/src/bakanet/sock_layer/socket.h b/bakanet/src/bakanet/sock_layer/socket.h index 4aa24fc..5eaf870 100755 --- a/bakanet/src/bakanet/sock_layer/socket.h +++ b/bakanet/src/bakanet/sock_layer/socket.h @@ -15,7 +15,7 @@ namespace Bk::Net { virtual bool init() = 0; virtual bool start(int cpt_conn) = 0; - virtual Connection ack() = 0; + virtual std::unique_ptr ack() = 0; virtual bool conn() = 0; virtual int get_raw_socket() = 0; @@ -26,13 +26,10 @@ namespace Bk::Net { virtual std::vector obtain(Connection conn, int size) = 0; template - static bool set_option(Socket& socket, int level, int option_name, const T* option_value) - { - int status = setsockopt(socket.get_raw_socket(), level, option_name, (void*)option_value, sizeof(T)); - log(status) - return status == 0 ? true : false; - } + static bool set_option(Socket& socket, int level, int option_name, const T* option_value) { return setsockopt(socket.get_raw_socket(), level, option_name, (void*)option_value, sizeof(T)) == 0 ? true : false; } static std::unique_ptr create(IpAddress ip, int port, IpProtocol proto); + private: + static std::unique_ptr create(int id, IpVersion ver, IpProtocol proto); }; } \ No newline at end of file diff --git a/bakanet/src/platform/linux/linux_socket.cpp b/bakanet/src/platform/linux/linux_socket.cpp index 491227e..823a0fa 100755 --- a/bakanet/src/platform/linux/linux_socket.cpp +++ b/bakanet/src/platform/linux/linux_socket.cpp @@ -63,11 +63,6 @@ namespace Bk::Net { write(socket_id, packet.data(), packet.size()); } - void LinuxSocket::emit(Connection conn, std::vector packet) - { - write(conn, packet.data(), packet.size()); - } - std::vector LinuxSocket::obtain(int size) { std::vector buffer; @@ -76,15 +71,6 @@ namespace Bk::Net { buffer.resize(read_size); return buffer; } - - std::vector LinuxSocket::obtain(Connection conn, int size) - { - std::vector buffer; - buffer.resize(size); - int read_size = read(conn, buffer.data(), buffer.size() - 1); - buffer.resize(read_size); - return buffer; - } std::unique_ptr Socket::create(IpAddress ip, int port, IpProtocol proto) { diff --git a/bakanet/src/platform/linux/linux_socket.h b/bakanet/src/platform/linux/linux_socket.h index 409a7b0..66c2a46 100755 --- a/bakanet/src/platform/linux/linux_socket.h +++ b/bakanet/src/platform/linux/linux_socket.h @@ -17,9 +17,7 @@ namespace Bk::Net { int get_raw_socket() override { return socket_id; } void emit(std::vector packet) override; - void emit(Connection socket, std::vector packet) override; std::vector obtain(int size) override; - std::vector obtain(Connection conn, int size) override; private: Connection socket_id; diff --git a/bakanet/src/platform/windows/windows_socket.cpp b/bakanet/src/platform/windows/windows_socket.cpp index 6729a75..6f0e9f5 100755 --- a/bakanet/src/platform/windows/windows_socket.cpp +++ b/bakanet/src/platform/windows/windows_socket.cpp @@ -4,8 +4,19 @@ namespace Bk::Net { int WindowsSocket::socket_count = 0; + WindowsSocket::WindowsSocket(int id, IpVersion ver, IpProtocol proto) + : id(id), ip_proto(proto), main(false) + { + char myIP[16]; + struct sockaddr sock_addr; + getsockname(id, (struct sockaddr*)&addr, sizeof(addr)); + inet_ntop((int)ver, &addr.sin_addr, myIP, sizeof(myIP)); + ip_addr = IpAddress(std::string(myIP, 16), ver); + addr = sock_addr; + } + WindowsSocket::WindowsSocket(IpAddress ip, int port, IpProtocol proto) - : ip_addr(ip), ip_proto(proto) + : ip_addr(ip), ip_proto(proto), main(true) { if (socket_count++ < 1) { @@ -22,27 +33,28 @@ namespace Bk::Net { } //WindowsSocket creation step - if ((socket_id = (int)socket((int)ip_addr.version, (int)ip_proto, 0)) < 0) + if ((id = (int)socket((int)ip_addr.version, (int)ip_proto, 0)) < 0) { log("socket failed " << WSAGetLastError()); exit(EXIT_FAILURE); } - addr.sin_addr = ip_addr.get_data(); - addr.sin_family = (int)ip_addr.version; - addr.sin_port = htons(port); + struct sockaddr_in in_addr; + in_addr.sin_addr = ip_addr.get_data(); + in_addr.sin_family = (int)ip_addr.version; + in_addr.sin_port = htons(port); } WindowsSocket::~WindowsSocket() { - WSACleanup(); - closesocket(socket_id); + if (main && socket_count-- < 1) WSACleanup(); + closesocket(id); } bool WindowsSocket::init() { //Binding step int status; - if ((status = bind(socket_id, (struct sockaddr*)&addr, sizeof(addr)) < 0)) + if ((status = bind(id, (struct sockaddr*)&addr, sizeof(addr)) < 0)) { log("bind failed " << WSAGetLastError()); return false; @@ -53,58 +65,43 @@ namespace Bk::Net { bool WindowsSocket::start(int cpt_conn = SOMAXCONN) { //Listening step - if (listen(socket_id, cpt_conn) == SOCKET_ERROR) - { - return false; - } + if (listen(id, cpt_conn) == SOCKET_ERROR) { return false; } return true; } Connection WindowsSocket::ack() { socklen_t addrlen = sizeof(addr); - return accept(socket_id, (struct sockaddr*)&addr, &addrlen); + return accept(id, (struct sockaddr*)&addr, &addrlen); } bool WindowsSocket::conn() { - if (connect(socket_id, (struct sockaddr*)&addr, sizeof(addr)) < 0) - { - return false; - } + if (connect(id, (struct sockaddr*)&addr, sizeof(addr)) < 0) { return false; } return true; } void WindowsSocket::emit(std::vector packet) { - send((SOCKET)socket_id, packet.data(), packet.size(), 0); - } - - void WindowsSocket::emit(Connection conn, std::vector packet) - { - send((SOCKET)conn, packet.data(), packet.size(), 0); + send((SOCKET)id, packet.data(), packet.size(), 0); } std::vector WindowsSocket::obtain(int size) { std::vector buffer; buffer.resize(size); - int read_size = recv((SOCKET)socket_id, buffer.data(), buffer.size() - 1, 0); + int read_size = recv((SOCKET)id, buffer.data(), buffer.size() - 1, 0); buffer.resize(read_size); return buffer; } - std::vector WindowsSocket::obtain(Connection conn, int size) + std::unique_ptr Socket::create(IpAddress ip, int port, IpProtocol proto) { - std::vector buffer; - buffer.resize(size); - int read_size = recv((SOCKET)conn, buffer.data(), buffer.size() - 1, 0); - buffer.resize(read_size); - return buffer; + return std::unique_ptr(new WindowsSocket(ip, port, proto)); } - std::unique_ptr Socket::create(IpAddress ip, int port, IpProtocol proto) + std::unique_ptr Socket::create(int id, IpVersion ver, IpProtocol proto) { - return std::unique_ptr(new WindowsSocket(ip, port, proto)); + return std::unique_ptr(new WindowsSocket(int id, IpVersion ver, IpProtocol proto)); } } \ No newline at end of file diff --git a/bakanet/src/platform/windows/windows_socket.h b/bakanet/src/platform/windows/windows_socket.h index c354b9e..d1fc644 100755 --- a/bakanet/src/platform/windows/windows_socket.h +++ b/bakanet/src/platform/windows/windows_socket.h @@ -9,6 +9,7 @@ namespace Bk::Net { static int socket_count; WindowsSocket(IpAddress ip, int port, IpProtocol proto); + WindowsSocket(int id, IpVersion ver, IpProtocol proto); virtual ~WindowsSocket(); bool init() override; @@ -16,17 +17,16 @@ namespace Bk::Net { Connection ack() override; bool conn() override; - int get_raw_socket() override { return socket_id; } + int get_raw_socket() override { return id; } void emit(std::vector packet) override; - void emit(Connection socket, std::vector packet) override; std::vector obtain(int size) override; - std::vector obtain(Connection conn, int size) override; private: - Connection socket_id; - struct sockaddr_in addr; + Connection id; + struct sockaddr addr; IpAddress ip_addr; IpProtocol ip_proto; + bool main; }; } \ No newline at end of file