diff --git a/DEV_INFO b/DEV_INFO
index eb5d275..cc71118 100644
--- a/DEV_INFO
+++ b/DEV_INFO
@@ -1,2 +1,4 @@
id -1 is server / valid message if other parameters are valid as well
-id -2 is invalid network message
\ No newline at end of file
+id -2 is invalid network message
+
+NetworkMessage: subject 1 is reserved for handshake validation
\ No newline at end of file
diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog
index 8b65bd7..5e21b61 100644
Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.command.1.tlog differ
diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog
index 56ae1e2..0111a24 100644
Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.read.1.tlog differ
diff --git a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog
index 813be0b..bf30e6d 100644
Binary files a/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog and b/VoidNetVS/VoidNetVS/Release/VoidNetVS.tlog/CL.write.1.tlog differ
diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj
index a7e809d..3e9b936 100644
--- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj
+++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj
@@ -134,7 +134,7 @@
AnySuitable
Speed
MultiThreaded
- F:\VoidNet\include;F:\VoidNet\src
+ ..\..\include;..\..\src
true
diff --git a/include/Defs.hpp b/include/Defs.hpp
index 75d8065..8cdec9c 100644
--- a/include/Defs.hpp
+++ b/include/Defs.hpp
@@ -233,7 +233,8 @@ enum DistributionType
enum ConnectionCode
{
Accept,
- Reject
+ Reject,
+ Close
};
enum InternalTags
diff --git a/include/Handshake.hpp b/include/Handshake.hpp
index 3ebe63b..14125e6 100644
--- a/include/Handshake.hpp
+++ b/include/Handshake.hpp
@@ -6,6 +6,7 @@
#endif
#include "Defs.hpp"
+#include "NetworkMessage.hpp"
#include
@@ -17,6 +18,8 @@ struct Handshake
static const std::vector &EncodeHandshake(const Handshake &handshake);
static Handshake &DecodeHandshake(const std::vector &bytes);
+ static const NetworkMessage &HandshakeToNetworkMessage(const Handshake &handshake);
+ static const Handshake &NetworkMessageToHandshake(const NetworkMessage &message);
uint16 id;
byte con_code;
diff --git a/include/TcpServer.hpp b/include/TcpServer.hpp
index 4ac6ddf..f4bd958 100644
--- a/include/TcpServer.hpp
+++ b/include/TcpServer.hpp
@@ -19,13 +19,10 @@ struct TcpServer
void Shutdown();
- uint16 AllocateID();
- void AddToClientsList(TcpClient &client);
bool StartServer(bool accept_connections); // if accept_connections is false the user must call the funcion AcceptConnections()
void AcceptConnections();
void SendMessage(const NetworkMessage &message);
- void SendHandshake(const Handshake &handshake);
void RejectConnection(TcpClient &client);
void AcceptConnection(uint16 client);
@@ -44,6 +41,10 @@ private:
static void process_client_messages(TcpServer *server, TcpClient &client);
static void accept_connections(TcpServer *server);
+ void add_to_clients_list(TcpClient &client);
+
+ uint16 allocate_id();
+
void shutdown_internal();
bool initialize(uint16 port = default_server_port);
diff --git a/include/Utility.hpp b/include/Utility.hpp
index a257d00..76730f9 100644
--- a/include/Utility.hpp
+++ b/include/Utility.hpp
@@ -56,6 +56,8 @@ struct Utility
static const std::string &ToString(int64 value);
static const std::string &ToString(const std::vector &bytes);
+ static const std::vector &FromString(const std::string &str);
+
static const std::string &ToString(const std::vector &bytes, uint16 start_index = 0, uint16 lenght = 0);
static const std::string &Trim(std::string &str, char ch);
diff --git a/include/VoidNetServer.hpp b/include/VoidNetServer.hpp
index a2dff8f..8bd5b68 100644
--- a/include/VoidNetServer.hpp
+++ b/include/VoidNetServer.hpp
@@ -9,6 +9,8 @@
struct VoidNetServer
{
+ void StartServer(uint16 port);
+ void StopServer();
private:
TcpServer server;
};
diff --git a/src/Handshake.cpp b/src/Handshake.cpp
index c28ddff..1453c4c 100644
--- a/src/Handshake.cpp
+++ b/src/Handshake.cpp
@@ -24,8 +24,9 @@ const std::vector& Handshake::EncodeHandshake(const Handshake & handshake)
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());
+ handshake_bytes.emplace_back(Utility::BitConverter::FromUint8(1));
+ handshake_bytes.emplace_back(id.begin(), id.end());
+ handshake_bytes.emplace_back(con_mode.begin(), con_mode.end());
return handshake_bytes;
}
@@ -34,8 +35,25 @@ Handshake & Handshake::DecodeHandshake(const std::vector& bytes)
{
Handshake handshake;
- handshake.id = Utility::BitConverter::ToUint16(bytes);
- handshake.con_code = Utility::BitConverter::ToUint8(bytes, 2);
+ handshake.id = Utility::BitConverter::ToUint16(bytes, 1);
+ handshake.con_code = Utility::BitConverter::ToUint8(bytes, 3);
return handshake;
}
+
+const NetworkMessage & Handshake::HandshakeToNetworkMessage(const Handshake & handshake)
+{
+ NetworkMessage message;
+ message.sender = handshake.id;
+ message.tag = handshake.con_code;
+ message.subject = 1;
+ return message;
+}
+
+const Handshake & Handshake::NetworkMessageToHandshake(const NetworkMessage & message)
+{
+ Handshake handshake;
+ handshake.id = message.sender;
+ handshake.con_code = message.tag;
+ return handshake;
+}
diff --git a/src/NetworkMessage.cpp b/src/NetworkMessage.cpp
index f152de4..82dde05 100644
--- a/src/NetworkMessage.cpp
+++ b/src/NetworkMessage.cpp
@@ -1,6 +1,7 @@
#include "NetworkMessage.hpp"
#include "Utility.hpp"
#include "Serializer.hpp"
+#include "Handshake.hpp"
#include
@@ -28,25 +29,36 @@ const NetworkBuffer &NetworkMessage::EncodeMessage(const NetworkMessage &message
{
NetworkBuffer net_buffer;
- std::vector sender = Utility::BitConverter::FromUint16(message.sender);
- std::vector distribution_mode = Utility::BitConverter::FromUint8(message.distribution_mode);
- std::vector destination_id = Utility::BitConverter::FromUint16(message.destination_id);
- std::vector tag = Utility::BitConverter::FromUint8(message.tag);
- std::vector subject = Utility::BitConverter::FromUint16(message.subject);
- std::vector data;
- if (message.data != nullptr)
- data = Serializer::to_bytes(message.data);
+ if (message.subject != 1)
+ {
+ std::vector sender = Utility::BitConverter::FromUint16(message.sender);
+ std::vector distribution_mode = Utility::BitConverter::FromUint8(message.distribution_mode);
+ std::vector destination_id = Utility::BitConverter::FromUint16(message.destination_id);
+ std::vector tag = Utility::BitConverter::FromUint8(message.tag);
+ std::vector subject = Utility::BitConverter::FromUint16(message.subject);
+ std::vector data;
+ if (message.data != nullptr)
+ data = Serializer::to_bytes(message.data);
- net_buffer.body.insert(net_buffer.body.end(), sender.begin(), sender.end());
- net_buffer.body.insert(net_buffer.body.end(), distribution_mode.begin(), distribution_mode.end());
- net_buffer.body.insert(net_buffer.body.end(), destination_id.begin(), destination_id.end());
- net_buffer.body.insert(net_buffer.body.end(), tag.begin(), tag.end());
- net_buffer.body.insert(net_buffer.body.end(), subject.begin(), subject.end());
- if (message.data != nullptr && data.size() > 0)
- net_buffer.body.insert(net_buffer.body.end(), data.begin(), data.end());
- net_buffer.header = Utility::BitConverter::FromInt32(sender.size() + distribution_mode.size() + destination_id.size() +
- tag.size() + subject.size() + data.size());
- net_buffer.valid = true;
+ net_buffer.body.emplace_back(Utility::BitConverter::FromInt8(0));
+ net_buffer.body.emplace_back(sender.begin(), sender.end());
+ net_buffer.body.emplace_back(distribution_mode.begin(), distribution_mode.end());
+ net_buffer.body.emplace_back(destination_id.begin(), destination_id.end());
+ net_buffer.body.emplace_back(tag.begin(), tag.end());
+ net_buffer.body.emplace_back(subject.begin(), subject.end());
+ if (message.data != nullptr && data.size() > 0)
+ net_buffer.body.emplace_back(data.begin(), data.end());
+ net_buffer.header = Utility::BitConverter::FromInt32(sender.size() + distribution_mode.size() + destination_id.size() +
+ tag.size() + subject.size() + data.size());
+ net_buffer.valid = true;
+ }
+ else
+ {
+ std::vector handshake_bytes = Handshake::EncodeHandshake(Handshake::NetworkMessageToHandshake(message));
+ net_buffer.header = Utility::BitConverter::FromInt32(handshake_bytes.size());
+ net_buffer.body = handshake_bytes;
+ }
+ return net_buffer;
}
return NetworkBuffer();
}
@@ -55,32 +67,42 @@ const NetworkMessage &NetworkMessage::DecodeMessage(const NetworkBuffer &buffer)
{
if (buffer.valid)
{
- NetworkMessage message;
- message.sender = Utility::BitConverter::ToUint16(buffer.body, 1);
- message.distribution_mode = buffer.body[3];
- message.destination_id = Utility::BitConverter::ToUint16(buffer.body, 4);
- message.tag = buffer.body[6];
- message.subject = Utility::BitConverter::ToUint8(buffer.body, 7);
- message.valid = message.sender != -2 && message.tag != CONNECT && message.tag != DISCONNECT;
-
- if (Utility::BitConverter::ToInt32(buffer.header) < 9)
- return message;
- byte version = buffer.body[0];
- switch (version)
+ byte type = buffer.body[0];
+ switch (type)
{
case 0:
{
+ NetworkMessage message;
+ message.sender = Utility::BitConverter::ToUint16(buffer.body, 1);
+ message.distribution_mode = buffer.body[3];
+ message.destination_id = Utility::BitConverter::ToUint16(buffer.body, 4);
+ message.tag = buffer.body[6];
+ message.subject = Utility::BitConverter::ToUint8(buffer.body, 7);
+ message.valid = message.sender != -2 && message.tag != CONNECT && message.tag != DISCONNECT;
+ if (Utility::BitConverter::ToInt32(buffer.header) < 9)
+ return message;
void *object;
object = Serializer::from_bytes(buffer.body, object);
+ return message;
+ }
+ case 1:
+ {
+ NetworkMessage message;
+ message.sender = Utility::BitConverter::ToUint16(buffer.body, 1);
+ message.tag = Utility::BitConverter::ToUint8(buffer.body, 3);
+ message.subject = 1;
+ message.valid = message.sender != -2 && (message.tag == DISCONNECT || message.tag == CONNECT || message.tag == ConnectionCode::Accept ||
+ message.tag == ConnectionCode::Close || message.tag == ConnectionCode::Reject);
+ return message;
+ break;
}
default:
{
- //version not supported
+ //type not supported
throw std::runtime_error("NetworkMessage - Decoding version not supported");
}
}
- return message;
}
return NetworkMessage();
}
\ No newline at end of file
diff --git a/src/TcpClient.cpp b/src/TcpClient.cpp
index bd664b1..c4aa5bc 100644
--- a/src/TcpClient.cpp
+++ b/src/TcpClient.cpp
@@ -56,10 +56,13 @@ void TcpClient::receive_data(TcpClient *client)
NetworkMessage message(client->ReceiveMessage());
if (message.valid)
{
- if (message.tag == CONNECT) // some user has connected
- std::async(std::launch::async, client->OnConnect, message.sender);
- else if (message.tag == DISCONNECT) // some user has disconnected
- std::async(std::launch::async, client->OnDisconnect, message.sender);
+ if (message.subject == 1) // its a handshake
+ {
+ if (message.tag == CONNECT) // some user has connected - not us, never
+ std::async(std::launch::async, client->OnConnect, message.sender);
+ else if (message.tag == DISCONNECT || message.tag == ConnectionCode::Close) // some user has disconnected
+ std::async(std::launch::async, client->OnDisconnect, message.sender);
+ }
else
std::async(std::launch::async, client->OnMessage, message.sender, message.tag, message.subject, message.data); // we received data
}
diff --git a/src/TcpClientWindows.cpp b/src/TcpClientWindows.cpp
index 2e2c4d9..57e35e5 100644
--- a/src/TcpClientWindows.cpp
+++ b/src/TcpClientWindows.cpp
@@ -53,11 +53,8 @@ TcpClient::~TcpClient()
void TcpClient::Shutdown()
{
- NetworkMessage message;
- message.sender = id;
- message.distribution_mode = Server;
- message.tag = DISCONNECT;
- SendMessage(message);
+ Handshake handshake(id, Close, Server);
+ SendMessage(Handshake::HandshakeToNetworkMessage(handshake));
uint16 code = closesocket(tcp_socket);
if (code == SOCKET_ERROR)
{
@@ -82,14 +79,13 @@ bool TcpClient::Connect()
if (connect_code == SOCKET_ERROR)
return false;
- NetworkBuffer buffer(receive_data_array());
- if (buffer.valid)
+ NetworkMessage message(receive_data_array());
+ if (message.valid && message.subject == 1)
{
- Handshake handshake = Handshake::DecodeHandshake(buffer.body);
- if (handshake.con_code == ConnectionCode::Accept)
+ if (message.tag == ConnectionCode::Accept)
{
receive = true;
- OnConnect(handshake.id);
+ OnConnect(message.sender);
return true;
}
}
diff --git a/src/TcpServer.cpp b/src/TcpServer.cpp
index d153329..5963aa3 100644
--- a/src/TcpServer.cpp
+++ b/src/TcpServer.cpp
@@ -34,8 +34,11 @@ void TcpServer::Shutdown()
void TcpServer::AcceptConnections()
{
- running = true;
- std::async(std::launch::async, &accept_connections, this);
+ if (!running)
+ {
+ running = true;
+ std::async(std::launch::async, &accept_connections, this);
+ }
}
void TcpServer::process_client_messages(TcpServer *server, TcpClient & client)
@@ -109,36 +112,7 @@ void TcpServer::SendMessage(const NetworkMessage & message)
}
}
-void TcpServer::SendHandshake(const Handshake & handshake)
-{
- switch (handshake.distribution_mode)
- {
- case AllAndMe: // this will send the message to EVERYONE including the user that sent it
- {
- for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it)
- {
- TcpClient client = *it;
- client.SendBytes(Handshake::EncodeHandshake(handshake));
- }
- break;
- }
- case ID: // this will send the message to a specific id
- {
- for (std::vector::iterator it = clients.begin(); it != clients.end(); ++it)
- {
- TcpClient client = *it;
- if (handshake.id == client.GetID())
- {
- client.SendBytes(Handshake::EncodeHandshake(handshake));
- break;
- }
- }
- break;
- }
- }
-}
-
-uint16 TcpServer::AllocateID() // this function is only used in the AddToClientsList function
+uint16 TcpServer::allocate_id() // this function is only used in the AddToClientsList function
{
for (uint16 i = 1; i < max_connections; ++i)
{
@@ -159,9 +133,9 @@ uint16 TcpServer::AllocateID() // this function is only used in the AddToClients
return 0;
}
-void TcpServer::AddToClientsList(TcpClient & client_socket)
+void TcpServer::add_to_clients_list(TcpClient & client_socket)
{
- uint16 id = AllocateID();
+ uint16 id = allocate_id();
if (id > 0)
{
client_socket.SetID(id);
@@ -179,14 +153,13 @@ void TcpServer::AddToClientsList(TcpClient & client_socket)
void TcpServer::RejectConnection(TcpClient &client)
{
Handshake handshake(client.GetID(), ConnectionCode::Reject, ID);
- client.SendBytes(Handshake::EncodeHandshake(handshake));
- CloseSocket(client);
+ SendMessage(Handshake::HandshakeToNetworkMessage(handshake));
}
void TcpServer::AcceptConnection(uint16 id)
{
Handshake handshake(id, ConnectionCode::Accept, AllAndMe);
- SendHandshake(handshake);
+ SendMessage(Handshake::HandshakeToNetworkMessage(handshake));
}
void TcpServer::CloseSocket(TcpClient & client)
diff --git a/src/Utility.cpp b/src/Utility.cpp
index 7285157..dad36a4 100644
--- a/src/Utility.cpp
+++ b/src/Utility.cpp
@@ -185,10 +185,10 @@ void Utility::ConfigReader::ReadConfig(const std::string & file_name)
return;
std::fstream file;
file.open(file_name);
- if (file.is_open)
+ if (file.is_open())
{
longlong file_lenght = file.gcount();
- char *content = new char[file_lenght]();
+ char *content = new char[static_cast(file_lenght)]();
file.read(content, file_lenght);
file_content = std::string(content);
}