From 0ac8b4f6e8cc58f90e097cf94d01aa82a231e1e8 Mon Sep 17 00:00:00 2001 From: xX-TheDoctor-Xx Date: Mon, 1 Aug 2016 00:28:22 +0100 Subject: [PATCH] Added Handshake class Fixed NetworkBuffer include Fixed some data types TcpClient: Added SendBytes, SetOnDisconnectCallback, SetOnConnectCallback, SetOnMessageCallback methods and made callback std::function's private and fixed some method implementations Added AcceptConnection on TcpServer Fixed TcpServer AddToClientsList --- .../Release/VoidNetVS.tlog/CL.command.1.tlog | Bin 4722 -> 5310 bytes .../Release/VoidNetVS.tlog/CL.read.1.tlog | Bin 193978 -> 217944 bytes .../Release/VoidNetVS.tlog/CL.write.1.tlog | Bin 3720 -> 5504 bytes .../VoidNetVS.tlog/Lib-link.write.1.tlog | Bin 950 -> 1056 bytes .../Release/VoidNetVS.tlog/Lib.read.1.tlog | Bin 2220 -> 2434 bytes .../Release/VoidNetVS.tlog/lib.command.1.tlog | Bin 1338 -> 1486 bytes VoidNetVS/VoidNetVS/VoidNetVS.vcxproj | 2 + VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters | 6 +++ include/Handshake.hpp | 25 ++++++++++ include/NetworkBuffer.hpp | 1 + include/NetworkMessage.hpp | 4 +- include/TcpClient.hpp | 11 +++-- include/TcpServer.hpp | 3 +- src/Handshake.cpp | 40 ++++++++++++++++ src/NetworkBuffer.cpp | 1 - src/NetworkMessage.cpp | 4 +- src/TcpClientWindows.cpp | 45 +++++++++++++++--- src/TcpServerWindows.cpp | 26 ++++++---- 18 files changed, 143 insertions(+), 25 deletions(-) create mode 100644 include/Handshake.hpp create mode 100644 src/Handshake.cpp diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog index 8e4beea912705a060d18f60c4932599971f27303..334d751a5acd3e81e826902512a27beae8a9731a 100644 GIT binary patch delta 79 zcmeyQvQKlvE4Iyk0xC?C^#l|qajI<=U^ipp_h4{j@MCac2xb7Wyf?>l{$~_K6J&6m UtSF#6iIZ<~4!6|iJkA4b05|s&I{*Lx delta 30 mcmdm|`AKENE4IygoH0z3-|=f~{>OWOZ89IH!sb_ODog;%0t>YO diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog index 99ef74b7ccec5a7990beeae67ba56c79c122d802..03be1db5ea777ea77b8d2c4495910bd4f8f9b9cf 100644 GIT binary patch delta 286 zcmdn>n)}8w-VHB~Y|rXq3do)=rpKr^{aqp>-}HSwM1 delta 365 zcmcbyjCa>-?hP-FY>zNtY%rc2V^KH#oeOi{^oD9izU>C*7{6#tR%2A#t}u-$j(Pir zBF1fun-h|bv21s6VqU;FSuI^*I#46u^abA;1*Vr6FxqVgvPz7nTTNpk$)L3KoaqWa z%pwyj6sFHhWD?q5lE~C%g4LMmALN;vHv9ZO#DwIY=|K0?ZLe`ZP4DZk83gyhuGu|yf-G#<8qpu$3AcJ1D>YI0(^0kb(r`z8*nl)0U2_W#ds9> zJs2Dr{1{vqf*C+8?}-n!C-5dZ+!ZY;_G delta 68 zcmZqB?vUNEk8AP-VZO;1`1mGAh(t_|VNsi0$EE-jE1T@YrvYYrFiK616A7C9fL9L8 Re<#v3*??1G^E@s-MgT0X7P0^U diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib-link.write.1.tlog index 844b0b5daa2f4602b0709dc1ffcfb7cfd0ad3569..e3f2fd64adc36bf244ffed77c00c04cf68411529 100644 GIT binary patch delta 29 icmdnSzJO!GHhvEVM+QFz7lvR45X*b=LdNKY+n51%0te>+ delta 10 RcmZ3$v5kGgw#5pJ%m5Zr19JcX diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/Lib.read.1.tlog index ed0af5f9ec315ada75c9affdb01fc09c500754de..29bfe937699cf3fd6fa572795acc5a876c4373a1 100644 GIT binary patch delta 70 zcmZ1@*d#n*8@~sGBZD7<3qvpih~+(bA!GF9AFKkCEg0h_XRwKEwqQKKB8aAl!FBR~ M*67VYShp|%0FM_E`2YX_ delta 20 ccmZn?UL!bR+hzquC)UXdi~^hQu`OW&07>fxwEzGB diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/lib.command.1.tlog index 5b549a66256f813de3352a7a7a0730817bd2d001..1087b6d7b36170fcacd66f094fa72e46da3aadf1 100644 GIT binary patch delta 65 zcmdnRb&h+&HhvEVM+QFz7lvR45X*b=LdNKg+hUk8#A{ikCvFp%+`$6GjC_+FSbhNj DG9D3( delta 27 icmX@dy^CwYw#^ERG0c+{7zHN3V3C{L$0!1%p8)`iWC{}i diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj index d95fb5e..cba33dd 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj @@ -21,6 +21,7 @@ + @@ -33,6 +34,7 @@ + true diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters index 2f6aa11..712a5e1 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj.filters @@ -42,6 +42,9 @@ include + + include + @@ -77,6 +80,9 @@ src + + src + diff --git a/include/Handshake.hpp b/include/Handshake.hpp new file mode 100644 index 0000000..c5ce380 --- /dev/null +++ b/include/Handshake.hpp @@ -0,0 +1,25 @@ +#ifndef HANDSHAKE_HPP +#define HANDSHAKE_HPP + +#ifdef _MSC_VER +#pragma once +#endif + +#include "Defs.hpp" + +#include + +struct Handshake +{ + Handshake(); + Handshake(uint16 id, byte con_code); + ~Handshake(); + + static const std::vector &EncodeHandshake(const Handshake &handshake); + static Handshake &DecodeHandshake(const std::vector &bytes); + + uint16 id; + byte con_code; +}; + +#endif diff --git a/include/NetworkBuffer.hpp b/include/NetworkBuffer.hpp index 672a5e2..30ccaa6 100644 --- a/include/NetworkBuffer.hpp +++ b/include/NetworkBuffer.hpp @@ -6,6 +6,7 @@ #endif #include "Defs.hpp" +#include "Utility.hpp" #include diff --git a/include/NetworkMessage.hpp b/include/NetworkMessage.hpp index bbc4b0f..175ca85 100644 --- a/include/NetworkMessage.hpp +++ b/include/NetworkMessage.hpp @@ -12,7 +12,7 @@ struct NetworkMessage { NetworkMessage(); NetworkMessage(const NetworkBuffer &buffer); - NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer); + NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, byte subject, NetworkBuffer buffer); ~NetworkMessage(); static const NetworkBuffer &EncodeMessage(const NetworkMessage &message); @@ -23,7 +23,7 @@ struct NetworkMessage byte distribution_mode; uint16 destination_id; byte tag; - uint16 subject; + byte subject; void *data; bool valid = false; diff --git a/include/TcpClient.hpp b/include/TcpClient.hpp index be881d2..b6d1945 100644 --- a/include/TcpClient.hpp +++ b/include/TcpClient.hpp @@ -42,10 +42,11 @@ public: //this is a more manual method with no callbacks const NetworkMessage &ReceiveMessage(); void SendMessage(const NetworkMessage &message); + void SendBytes(const std::vector &bytes); - std::function OnDisconnect; - std::function OnConnect; - std::function OnMessage; + void SetOnDisconnectCallback(void (*func)(uint16)); + void SetOnConnectCallback(void (*func)(uint16)); + void SetOnMessageCallback(void (*func)(uint16, byte, byte, void*)); private: const NetworkBuffer &receive_data_array(); @@ -59,6 +60,10 @@ private: bool initialized = false; bool receive = false; + std::function OnDisconnect; + std::function OnConnect; + std::function OnMessage; + #ifdef _MSC_VER SOCKET tcp_socket = INVALID_SOCKET; struct addrinfo *result = nullptr; diff --git a/include/TcpServer.hpp b/include/TcpServer.hpp index 1a63565..eb5b95c 100644 --- a/include/TcpServer.hpp +++ b/include/TcpServer.hpp @@ -24,12 +24,13 @@ public: void Shutdown(); uint16 AllocateID(); - void AddToClientsList(const TcpClient &client); + void AddToClientsList(TcpClient &client); bool StartServer(bool accept_connections); void AcceptConnections(); void SendMessage(const NetworkMessage &message); void RejectConnection(TcpClient &client); + void AcceptConnection(TcpClient &client); std::function OnMessage; diff --git a/src/Handshake.cpp b/src/Handshake.cpp new file mode 100644 index 0000000..b579bfc --- /dev/null +++ b/src/Handshake.cpp @@ -0,0 +1,40 @@ +#include "Handshake.hpp" +#include "Utility.hpp" + +Handshake::Handshake() +{ + id = -2; +} + +Handshake::Handshake(uint16 id, byte con_code) +{ + this->id = id; + this->con_code = con_code; +} + +Handshake::~Handshake() +{ +} + +const std::vector& Handshake::EncodeHandshake(const Handshake & handshake) +{ + std::vector handshake_bytes; + + std::vector id = Utility::BitConverter::FromUint16(handshake.id); + std::vector con_mode = Utility::BitConverter::FromUint8(handshake.con_code); + + handshake_bytes.insert(handshake_bytes.end(), id.begin(), id.end()); + handshake_bytes.insert(handshake_bytes.end(), con_mode.begin(), con_mode.end()); + + return handshake_bytes; +} + +Handshake & Handshake::DecodeHandshake(const std::vector& bytes) +{ + Handshake handshake; + + handshake.id = Utility::BitConverter::ToUint16(bytes); + handshake.con_code = Utility::BitConverter::ToUint8(bytes, 2); + + return handshake; +} diff --git a/src/NetworkBuffer.cpp b/src/NetworkBuffer.cpp index 66049c0..ecf53a3 100644 --- a/src/NetworkBuffer.cpp +++ b/src/NetworkBuffer.cpp @@ -1,5 +1,4 @@ #include "NetworkBuffer.hpp" -#include "Utility.hpp" NetworkBuffer::NetworkBuffer() { diff --git a/src/NetworkMessage.cpp b/src/NetworkMessage.cpp index f7c7d5a..bdab4b7 100644 --- a/src/NetworkMessage.cpp +++ b/src/NetworkMessage.cpp @@ -9,7 +9,7 @@ NetworkMessage::NetworkMessage() { } -NetworkMessage::NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, uint16 subject, NetworkBuffer buffer) : +NetworkMessage::NetworkMessage(uint16 sender, byte distribution_mode, uint16 destination_id, byte tag, byte subject, NetworkBuffer buffer) : sender(sender), distribution_mode(distribution_mode), destination_id(destination_id), tag(tag), subject(subject), buffer(buffer) { } @@ -52,7 +52,7 @@ const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer) message.distribution_mode = buffer.body[3]; message.destination_id = Utility::BitConverter::ToUint16(buffer.body, 4); message.tag = buffer.body[6]; - message.subject = Utility::BitConverter::ToUint16(buffer.body, 7); + message.subject = Utility::BitConverter::ToUint8(buffer.body, 7); message.buffer = buffer; message.valid = message.sender != -2 && message.tag != CONNECT && message.tag != DISCONNECT; diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp index b9ea324..f0d4f9a 100644 --- a/src/TcpClientWindows.cpp +++ b/src/TcpClientWindows.cpp @@ -3,6 +3,7 @@ #include "Config.hpp" #include "NetworkBuffer.hpp" #include "Tags.hpp" +#include "Handshake.hpp" #include #include @@ -124,8 +125,15 @@ bool TcpClient::Connect() uint16 connect_code = ::connect(tcp_socket, result->ai_addr, result->ai_addrlen); if (connect_code == SOCKET_ERROR) return false; - receive = true; - return true; + + NetworkBuffer message(receive_data_array()); + Handshake handshake = Handshake::DecodeHandshake(message.body); + if (handshake.con_code == ConnectionCode::Accept) + { + receive = true; + return true; + } + return false; } bool TcpClient::DataAvailable(uint16 &size) @@ -137,8 +145,8 @@ const NetworkBuffer &TcpClient::receive_data_array() { NetworkBuffer buffer; - uint16 body_size; - if (DataAvailable(body_size)) + uint16 temp; + if (DataAvailable(temp) && temp > 0) { if (!recv(tcp_socket, reinterpret_cast(&buffer.header[0]), 4, 0)) //invalid header @@ -148,10 +156,11 @@ const NetworkBuffer &TcpClient::receive_data_array() return NetworkBuffer(); uint32 body_size = Utility::BitConverter::ToUint32(buffer.header); - int32 body_received = recv(tcp_socket, reinterpret_cast(&buffer.body[0]), body_size, 0); + int16 body_received = recv(tcp_socket, reinterpret_cast(&buffer.body[0]), body_size, 0); if (body_received == SOCKET_ERROR || body_received != body_size || WSAGetLastError() != 0) { //there was a problem receiving the body of the message or theres no body to receive + return NetworkBuffer(); } return buffer; @@ -188,7 +197,7 @@ void TcpClient::send_network_message(const NetworkMessage &message, TcpClient *c { NetworkBuffer buffer = NetworkMessage::EncodeMessage(message); int32 bytes_sent = send(client->tcp_socket, reinterpret_cast(&buffer.body[0]), Utility::BitConverter::ToUint32(buffer.header), 0); - if (bytes_sent == SOCKET_ERROR || bytes_sent != Utility::BitConverter::ToUint32(buffer.header) || WSAGetLastError() != 0) + if (bytes_sent == SOCKET_ERROR || bytes_sent != Utility::BitConverter::ToInt32(buffer.header) || WSAGetLastError() != 0) { //something went wrong couldnt send anything/some data } @@ -198,3 +207,27 @@ void TcpClient::SendMessage(const NetworkMessage &message) { std::async(std::launch::async, &send_network_message, message, this); } + +void TcpClient::SendBytes(const std::vector& bytes) +{ + int32 bytes_sent = send(tcp_socket, reinterpret_cast(&bytes[0]), bytes.size(), 0); + if (bytes_sent == SOCKET_ERROR || bytes_sent != bytes.size() || WSAGetLastError() != 0) + { + //something went wrong couldnt send anything/some data + } +} + +void TcpClient::SetOnDisconnectCallback(void(*func)(uint16)) +{ + OnDisconnect = func; +} + +void TcpClient::SetOnConnectCallback(void(*func)(uint16)) +{ + OnConnect = func; +} + +void TcpClient::SetOnMessageCallback(void(*func)(uint16, byte, byte, void*)) +{ + OnMessage = func; +} diff --git a/src/TcpServerWindows.cpp b/src/TcpServerWindows.cpp index 6fcaee6..c057923 100644 --- a/src/TcpServerWindows.cpp +++ b/src/TcpServerWindows.cpp @@ -1,6 +1,7 @@ #include "TcpServer.hpp" #include "Config.hpp" #include "Utility.hpp" +#include "Handshake.hpp" #include #include @@ -211,27 +212,32 @@ uint16 TcpServer::AllocateID() // this function is only used in the AddToClients return 0; } -void TcpServer::AddToClientsList(const TcpClient & client_socket) +void TcpServer::AddToClientsList(TcpClient & client_socket) { - TcpClient client(client_socket); uint16 id = AllocateID(); if (id > 0) { - client.SetID(id); - clients.emplace_back(client); + client_socket.SetID(id); + clients.emplace_back(client_socket); + AcceptConnection(client_socket); } else { if (Config::GetUsingConsole()) std::cout << "No available ID's" << std::endl; - RejectConnection(client); + RejectConnection(client_socket); } } void TcpServer::RejectConnection(TcpClient &client) { - NetworkMessage message; - message.sender = -1; - message.tag = Reject; // 0 for rejected connection - client.SendMessage(message); -} \ No newline at end of file + Handshake handshake(client.GetID(), ConnectionCode::Reject); + client.SendBytes(Handshake::EncodeHandshake(handshake)); + client.Shutdown(); +} + +void TcpServer::AcceptConnection(TcpClient & client) +{ + Handshake handshake(client.GetID(), ConnectionCode::Accept); + client.SendBytes(Handshake::EncodeHandshake(handshake)); +}