From c50d40341fd0b6485ab602b35105d77d0fc0fc67 Mon Sep 17 00:00:00 2001 From: TheDoctor Date: Tue, 15 Oct 2019 16:27:16 +0100 Subject: [PATCH] Update Removed Message Queue Sending and receiving is now in one thread (gotta make them separate) Created a very crude Plugin System Renamed AllAndMe to AllAndServer --- HLAPI/HLAPI/HLAPI.vcxproj | 10 +- HLAPI/HLAPI/HLAPI.vcxproj.filters | 21 ++- VoidNetVS/VoidNetVS/VoidNetVS.vcxproj | 4 +- include/HLAPI/DataReceivedEvent.hpp | 2 +- include/HLAPI/MessageQueue.hpp | 43 ----- include/HLAPI/NetworkMessage.hpp | 13 +- include/HLAPI/Plugin/Plugin.hpp | 16 ++ include/HLAPI/Plugin/PluginManager.hpp | 29 ++++ include/HLAPI/Server.hpp | 7 +- include/HLAPI/TcpConnection.hpp | 6 +- include/HLAPI/TcpConnectionHandler.hpp | 12 +- include/HLAPI/TcpServer.hpp | 2 +- include/VoidNet/Http.hpp | 4 +- include/VoidNet/IPAddress.hpp | 2 +- include/VoidNet/ISocket.hpp | 6 +- include/VoidNet/Net.hpp | 2 + include/VoidNet/Request.hpp | 6 +- include/VoidNet/Response.hpp | 6 +- include/VoidNet/SecureSocket.hpp | 2 +- include/VoidNet/Socket.hpp | 6 +- include/VoidNet/TcpClient.hpp | 2 +- include/VoidNet/TcpListener.hpp | 2 +- include/VoidNet/TcpSocketBuilder.hpp | 4 +- include/VoidNet/UdpSocket.hpp | 2 +- include/VoidNet/UdpSocketBuilder.hpp | 4 +- include/VoidNet/Util.hpp | 2 +- src/HLAPI/MessageQueue.cpp | 52 ------ src/HLAPI/NetworkMessage.cpp | 30 ++-- src/HLAPI/Plugin/PluginManager.cpp | 11 ++ src/HLAPI/Server.cpp | 13 +- src/HLAPI/TcpConnection.cpp | 6 +- src/HLAPI/TcpConnectionHandler.cpp | 210 +++++++++++-------------- src/HLAPI/TcpServer.cpp | 10 +- src/HLAPI/main.cpp | 54 ++++--- src/VoidNet/Cookies.cpp | 4 +- src/VoidNet/Headers.cpp | 2 +- src/VoidNet/Http.cpp | 6 +- src/VoidNet/IPAddress.cpp | 2 +- src/VoidNet/Request.cpp | 2 +- src/VoidNet/Response.cpp | 4 +- src/VoidNet/SecureSocket.cpp | 2 +- src/VoidNet/Socket.cpp | 4 +- src/VoidNet/TcpClient.cpp | 4 +- src/VoidNet/TcpListener.cpp | 8 +- src/VoidNet/TcpSocketBuilder.cpp | 8 +- src/VoidNet/UdpSocket.cpp | 2 +- src/VoidNet/Uri.cpp | 4 +- src/VoidNet/Util.cpp | 2 +- 48 files changed, 321 insertions(+), 334 deletions(-) delete mode 100644 include/HLAPI/MessageQueue.hpp create mode 100644 include/HLAPI/Plugin/Plugin.hpp create mode 100644 include/HLAPI/Plugin/PluginManager.hpp delete mode 100644 src/HLAPI/MessageQueue.cpp create mode 100644 src/HLAPI/Plugin/PluginManager.cpp diff --git a/HLAPI/HLAPI/HLAPI.vcxproj b/HLAPI/HLAPI/HLAPI.vcxproj index 73d79f2..adb13fe 100644 --- a/HLAPI/HLAPI/HLAPI.vcxproj +++ b/HLAPI/HLAPI/HLAPI.vcxproj @@ -23,10 +23,11 @@ - + + @@ -34,8 +35,8 @@ - + @@ -66,6 +67,7 @@ true v141 MultiByte + Static Application @@ -99,8 +101,10 @@ Disabled true true - ../../include/HLAPI;../../include/VoidNet + ../../include stdcpplatest + MultiThreadedDebugDLL + %(PreprocessorDefinitions) Console diff --git a/HLAPI/HLAPI/HLAPI.vcxproj.filters b/HLAPI/HLAPI/HLAPI.vcxproj.filters index 037db0a..b7d3a00 100644 --- a/HLAPI/HLAPI/HLAPI.vcxproj.filters +++ b/HLAPI/HLAPI/HLAPI.vcxproj.filters @@ -7,6 +7,12 @@ {1c087ed8-3cca-4254-836f-10fb70fda154} + + {e4fcabf7-6c96-45cb-bb30-917081477267} + + + {503a204a-1cb8-46cc-91f5-9f5f583d543d} + @@ -18,9 +24,6 @@ include - - include - include @@ -45,14 +48,17 @@ include + + include\Plugin + + + include\Plugin + src - - src - src @@ -68,5 +74,8 @@ src + + src\Plugin + \ No newline at end of file diff --git a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj index 6fc6191..ba0f60b 100644 --- a/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj +++ b/VoidNetVS/VoidNetVS/VoidNetVS.vcxproj @@ -84,6 +84,7 @@ true v141 MultiByte + Static Application @@ -124,9 +125,10 @@ Level3 Disabled true - ../../include/VoidNet + ../../include stdcpplatest _WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions) + MultiThreadedDebugDLL diff --git a/include/HLAPI/DataReceivedEvent.hpp b/include/HLAPI/DataReceivedEvent.hpp index b758432..b8e695f 100644 --- a/include/HLAPI/DataReceivedEvent.hpp +++ b/include/HLAPI/DataReceivedEvent.hpp @@ -1,6 +1,6 @@ #pragma once -#include "NetworkMessage.hpp" +#include "HLAPI/NetworkMessage.hpp" namespace std::net { diff --git a/include/HLAPI/MessageQueue.hpp b/include/HLAPI/MessageQueue.hpp deleted file mode 100644 index decfe76..0000000 --- a/include/HLAPI/MessageQueue.hpp +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#include -#include - -#include "NetworkMessage.hpp" - -#include "NewConnectionEvent.hpp" -#include "DisconnectedEvent.hpp" -#include "DataReceivedEvent.hpp" - -namespace std::net -{ - class MessageQueue - { - public: - MessageQueue() - { - } - - void EnqueueMessageToSend(const NetworkMessage &msg); - - void EnqueueMessageReceived(const NetworkMessage &msg); - void EnqueueDisconnection(const NetworkMessage &msg); - void EnqueueConnection(const NetworkMessage &msg); - - NetworkMessage DequeueMessageToSend(); - - uint32_t SendSize(); - - private: - std::deque m_messagesToSend; - - std::deque m_connectionEvents; - std::deque m_disconnectedEvents; - std::deque m_dataReceivedEvents; - - std::mutex m_sendMutex; - std::mutex m_receivedMutex; - std::mutex m_disconnectMutex; - std::mutex m_connectionMutex; - }; -} \ No newline at end of file diff --git a/include/HLAPI/NetworkMessage.hpp b/include/HLAPI/NetworkMessage.hpp index 7b91a6b..1a2bd63 100644 --- a/include/HLAPI/NetworkMessage.hpp +++ b/include/HLAPI/NetworkMessage.hpp @@ -1,7 +1,7 @@ #pragma once -#include "NetworkHeader.hpp" -#include "ByteConverter.hpp" +#include "HLAPI/NetworkHeader.hpp" +#include "HLAPI/ByteConverter.hpp" #include #include @@ -14,7 +14,7 @@ namespace std::net Others, OthersAndServer, All, - AllAndMe, + AllAndServer, Server }; @@ -58,14 +58,14 @@ namespace std::net } template - uint8_t * SerializeData(uint32_t &size) + uint8_t * SerializeData(uint32_t &size) const { int32_t sizeOfNetHeader = sizeof(NetworkHeader); NetworkHeader header; header.Size = 13 + sizeOfNetHeader + sizeof(T); - uint8_t *bytes = new uint8_t[header.Size]; + uint8_t *bytes = new uint8_t[header.Size](); memcpy(bytes, &header, sizeOfNetHeader); uint8_t *sender = ByteConverter::ToBytes(m_senderID); // 4 @@ -83,8 +83,9 @@ namespace std::net return bytes; } - uint8_t *SerializeData(uint32_t &size); + uint8_t *SerializeData(uint32_t &size) const; void Deserialize(uint8_t *data, uint32_t size); + void DeserializeWithoutHeader(uint8_t* data, uint32_t size); template T *GetData() const diff --git a/include/HLAPI/Plugin/Plugin.hpp b/include/HLAPI/Plugin/Plugin.hpp new file mode 100644 index 0000000..b82a54a --- /dev/null +++ b/include/HLAPI/Plugin/Plugin.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace std::net +{ + class Plugin + { + public: + Plugin() + { + } + + virtual void HandleMessage(const NetworkMessage& msg) = 0; + }; +} \ No newline at end of file diff --git a/include/HLAPI/Plugin/PluginManager.hpp b/include/HLAPI/Plugin/PluginManager.hpp new file mode 100644 index 0000000..add5254 --- /dev/null +++ b/include/HLAPI/Plugin/PluginManager.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include "HLAPI/Plugin/Plugin.hpp" + +namespace std::net +{ + class PluginManager + { + public: + PluginManager(); + ~PluginManager(); + + void HandleMessage(const NetworkMessage& msg) + { + for (size_t i = 0; i < m_plugins.size(); i++) + m_plugins.at(i)->HandleMessage(msg); + } + + void AddPlugin(Plugin* p) + { + m_plugins.emplace_back(p); + } + + private: + std::vector m_plugins = std::vector(); + }; +} \ No newline at end of file diff --git a/include/HLAPI/Server.hpp b/include/HLAPI/Server.hpp index 9735cbc..c64ec6a 100644 --- a/include/HLAPI/Server.hpp +++ b/include/HLAPI/Server.hpp @@ -1,14 +1,13 @@ #pragma once -#include +#include #include namespace std::net { class TcpServer; - - class MessageQueue; + class Plugin; class Server { @@ -18,6 +17,8 @@ namespace std::net void Start(); void Stop(); + void AddPlugin(Plugin* plugin); + private: std::shared_ptr m_tcpServer; }; diff --git a/include/HLAPI/TcpConnection.hpp b/include/HLAPI/TcpConnection.hpp index 0225a25..abfdc8f 100644 --- a/include/HLAPI/TcpConnection.hpp +++ b/include/HLAPI/TcpConnection.hpp @@ -2,9 +2,9 @@ #include -#include "TcpClient.hpp" +#include "VoidNet/TcpClient.hpp" -#include "NetworkMessage.hpp" +#include "HLAPI/NetworkMessage.hpp" namespace std::net { @@ -32,7 +32,7 @@ namespace std::net std::function NewConnectionEvent; private: - bool sendMessage(NetworkMessage &msg); + bool sendMessage(const NetworkMessage &msg); std::shared_ptr m_client; uint32_t m_id; diff --git a/include/HLAPI/TcpConnectionHandler.hpp b/include/HLAPI/TcpConnectionHandler.hpp index 624179d..ade9438 100644 --- a/include/HLAPI/TcpConnectionHandler.hpp +++ b/include/HLAPI/TcpConnectionHandler.hpp @@ -4,8 +4,9 @@ #include #include -#include -#include +#include +#include +#include namespace std::net { @@ -30,14 +31,15 @@ namespace std::net void AddClient(std::shared_ptr &c); void SetMaxConnections(uint32_t max_connections); + void HandlePluginMessage(const NetworkMessage& message); + uint32_t GetAvailableID(); private: void HandleReceiveMsgAndConns(); - void HandleSend(); + void HandleMessage(const NetworkMessage &msg); void HandleReceiveMsgAndConnsThreaded(); - void HandleSendThreaded(); private: std::vector> m_list; @@ -54,6 +56,8 @@ namespace std::net std::shared_ptr m_listenerPtr; + std::shared_ptr m_pluginManager; + std::vector poll_fds; }; } \ No newline at end of file diff --git a/include/HLAPI/TcpServer.hpp b/include/HLAPI/TcpServer.hpp index 8128000..798400a 100644 --- a/include/HLAPI/TcpServer.hpp +++ b/include/HLAPI/TcpServer.hpp @@ -2,7 +2,7 @@ #include -#include "TcpListener.hpp" +#include "VoidNet/TcpListener.hpp" namespace std::net { diff --git a/include/VoidNet/Http.hpp b/include/VoidNet/Http.hpp index e983549..89f6b78 100644 --- a/include/VoidNet/Http.hpp +++ b/include/VoidNet/Http.hpp @@ -2,8 +2,8 @@ #pragma once -#include "Response.hpp" -#include "Request.hpp" +#include "VoidNet/Response.hpp" +#include "VoidNet/Request.hpp" namespace std::net { diff --git a/include/VoidNet/IPAddress.hpp b/include/VoidNet/IPAddress.hpp index dcb1a03..b9798cd 100644 --- a/include/VoidNet/IPAddress.hpp +++ b/include/VoidNet/IPAddress.hpp @@ -5,7 +5,7 @@ #include #include -#include "Net.hpp" +#include "VoidNet/Net.hpp" namespace std::net { diff --git a/include/VoidNet/ISocket.hpp b/include/VoidNet/ISocket.hpp index 5dc44fc..08a2188 100644 --- a/include/VoidNet/ISocket.hpp +++ b/include/VoidNet/ISocket.hpp @@ -4,9 +4,9 @@ #include #include -#include "Net.hpp" -#include "IPAddress.hpp" -#include "Enums.hpp" +#include "VoidNet/Net.hpp" +#include "VoidNet/IPAddress.hpp" +#include "VoidNet/Enums.hpp" namespace std::net { diff --git a/include/VoidNet/Net.hpp b/include/VoidNet/Net.hpp index 8bc8e1c..674e651 100644 --- a/include/VoidNet/Net.hpp +++ b/include/VoidNet/Net.hpp @@ -7,6 +7,8 @@ #include #undef SendMessage #undef SetPort + #undef max + #undef min #define poll WSAPoll #define ioctl ioctlsocket diff --git a/include/VoidNet/Request.hpp b/include/VoidNet/Request.hpp index 842df07..f31fae9 100644 --- a/include/VoidNet/Request.hpp +++ b/include/VoidNet/Request.hpp @@ -2,9 +2,9 @@ #pragma once -#include "Uri.hpp" -#include "Headers.hpp" -#include "Enums.hpp" +#include "VoidNet/Uri.hpp" +#include "VoidNet/Headers.hpp" +#include "VoidNet/Enums.hpp" namespace std::net { diff --git a/include/VoidNet/Response.hpp b/include/VoidNet/Response.hpp index a7c6a20..e757ed3 100644 --- a/include/VoidNet/Response.hpp +++ b/include/VoidNet/Response.hpp @@ -2,9 +2,9 @@ #pragma once -#include "Headers.hpp" -#include "Cookies.hpp" -#include "Enums.hpp" +#include "VoidNet/Headers.hpp" +#include "VoidNet/Cookies.hpp" +#include "VoidNet/Enums.hpp" #include diff --git a/include/VoidNet/SecureSocket.hpp b/include/VoidNet/SecureSocket.hpp index 58cfd52..fdf95b2 100644 --- a/include/VoidNet/SecureSocket.hpp +++ b/include/VoidNet/SecureSocket.hpp @@ -1,6 +1,6 @@ #pragma once -#include "Socket.hpp" +#include "VoidNet/Socket.hpp" //#include //#include diff --git a/include/VoidNet/Socket.hpp b/include/VoidNet/Socket.hpp index 34c98b4..39871d2 100644 --- a/include/VoidNet/Socket.hpp +++ b/include/VoidNet/Socket.hpp @@ -1,7 +1,7 @@ #pragma once -#include "ISocket.hpp" -#include "Enums.hpp" +#include "VoidNet/ISocket.hpp" +#include "VoidNet/Enums.hpp" namespace std::net { @@ -14,7 +14,7 @@ namespace std::net init(); } - inline Socket(SOCKET newSocket, SocketType socketType, SocketProtocol protocol = SocketProtocol::IPv4) + inline Socket(SOCKET newSocket, SocketType socketType = SocketType::Streaming, SocketProtocol protocol = SocketProtocol::IPv4) : ISocket(socketType, protocol) , m_socket(newSocket) { diff --git a/include/VoidNet/TcpClient.hpp b/include/VoidNet/TcpClient.hpp index 873888c..7f297c1 100644 --- a/include/VoidNet/TcpClient.hpp +++ b/include/VoidNet/TcpClient.hpp @@ -1,6 +1,6 @@ #pragma once -#include "Socket.hpp" +#include "VoidNet/Socket.hpp" namespace std::net { diff --git a/include/VoidNet/TcpListener.hpp b/include/VoidNet/TcpListener.hpp index f586974..dd32e9b 100644 --- a/include/VoidNet/TcpListener.hpp +++ b/include/VoidNet/TcpListener.hpp @@ -3,7 +3,7 @@ #include #include -#include "Socket.hpp" +#include "VoidNet/Socket.hpp" namespace std::net { diff --git a/include/VoidNet/TcpSocketBuilder.hpp b/include/VoidNet/TcpSocketBuilder.hpp index 52aae6a..cbffd95 100644 --- a/include/VoidNet/TcpSocketBuilder.hpp +++ b/include/VoidNet/TcpSocketBuilder.hpp @@ -1,7 +1,7 @@ #pragma once -#include "Enums.hpp" -#include "IPAddress.hpp" +#include "VoidNet/Enums.hpp" +#include "VoidNet/IPAddress.hpp" namespace std::net { diff --git a/include/VoidNet/UdpSocket.hpp b/include/VoidNet/UdpSocket.hpp index 58a6a76..c40418e 100644 --- a/include/VoidNet/UdpSocket.hpp +++ b/include/VoidNet/UdpSocket.hpp @@ -1,6 +1,6 @@ #pragma once -#include "Socket.hpp" +#include "VoidNet/Socket.hpp" #include diff --git a/include/VoidNet/UdpSocketBuilder.hpp b/include/VoidNet/UdpSocketBuilder.hpp index 513daed..156ceb1 100644 --- a/include/VoidNet/UdpSocketBuilder.hpp +++ b/include/VoidNet/UdpSocketBuilder.hpp @@ -1,7 +1,7 @@ #pragma once -#include "UdpSocket.hpp" -#include "IPAddress.hpp" +#include "VoidNet/UdpSocket.hpp" +#include "VoidNet/IPAddress.hpp" #include diff --git a/include/VoidNet/Util.hpp b/include/VoidNet/Util.hpp index cf710b2..e1fccb4 100644 --- a/include/VoidNet/Util.hpp +++ b/include/VoidNet/Util.hpp @@ -3,7 +3,7 @@ #include #include -#include "Net.hpp" +#include "VoidNet/Net.hpp" namespace std::net { diff --git a/src/HLAPI/MessageQueue.cpp b/src/HLAPI/MessageQueue.cpp deleted file mode 100644 index 6b18e5f..0000000 --- a/src/HLAPI/MessageQueue.cpp +++ /dev/null @@ -1,52 +0,0 @@ -#include "MessageQueue.hpp" - -namespace std::net -{ - void MessageQueue::EnqueueMessageToSend(const NetworkMessage & msg) - { - m_sendMutex.lock(); - m_messagesToSend.emplace_back(msg); - m_sendMutex.unlock(); - } - - void MessageQueue::EnqueueMessageReceived(const NetworkMessage & msg) - { - m_receivedMutex.lock(); - DataReceivedEvent ev(msg); - m_dataReceivedEvents.push_back(ev); - m_receivedMutex.unlock(); - } - - void MessageQueue::EnqueueDisconnection(const NetworkMessage & msg) - { - m_disconnectMutex.lock(); - //std::unique_ptr ev(((NetworkMessage)msg).GetData()); - //m_disconnectedEvents.push_back(*(ev.get())); - m_disconnectMutex.unlock(); - } - - void MessageQueue::EnqueueConnection(const NetworkMessage & msg) - { - m_connectionMutex.lock(); - NewConnectionEvent ev(msg.GetSenderID(), msg.GetData()); - m_connectionEvents.push_back(ev); - m_connectionMutex.unlock(); - } - - NetworkMessage MessageQueue::DequeueMessageToSend() - { - m_sendMutex.lock(); - NetworkMessage msg = m_messagesToSend.front(); - m_messagesToSend.erase(m_messagesToSend.begin() + 1); - m_sendMutex.unlock(); - return msg; - } - - uint32_t MessageQueue::SendSize() - { - m_sendMutex.lock(); - uint32_t size = m_messagesToSend.size(); - m_sendMutex.unlock(); - return size; - } -} \ No newline at end of file diff --git a/src/HLAPI/NetworkMessage.cpp b/src/HLAPI/NetworkMessage.cpp index 2132b3a..24ff5b5 100644 --- a/src/HLAPI/NetworkMessage.cpp +++ b/src/HLAPI/NetworkMessage.cpp @@ -1,4 +1,5 @@ -#include "NetworkMessage.hpp" +#include "HLAPI/NetworkMessage.hpp" +#include namespace std::net { @@ -22,7 +23,7 @@ namespace std::net return m_tag; } - uint8_t *NetworkMessage::SerializeData(uint32_t &size) + uint8_t *NetworkMessage::SerializeData(uint32_t &size) const { int32_t sizeOfNetHeader = sizeof(NetworkHeader); @@ -49,15 +50,24 @@ namespace std::net void NetworkMessage::Deserialize(uint8_t *data, uint32_t size) { - NetworkHeader buffer; - uint32_t sizeOfNetHeader = sizeof(NetworkHeader); - memcpy(&(buffer), data, sizeOfNetHeader); + NetworkHeader header; + memcpy(&(header), data, sizeof(NetworkHeader)); - memcpy(&(m_senderID), data + 4 + sizeOfNetHeader, 4); - m_distributionMode = (DistributionMode)data[8 + sizeOfNetHeader]; - memcpy(&(m_destinationID), data + 5 + sizeOfNetHeader, 4); - memcpy(&(m_tag), data + 9 + sizeOfNetHeader, 4); + data += sizeof(NetworkHeader); - m_data = data + 13 + sizeOfNetHeader; + DeserializeWithoutHeader(data, header.Size); + } + void NetworkMessage::DeserializeWithoutHeader(uint8_t* data, uint32_t size) + { + memcpy(&(m_senderID), data, 4); + m_distributionMode = (DistributionMode)data[4]; + memcpy(&(m_destinationID), data + 5, 4); + memcpy(&(m_tag), data + 9, 4); + + if (size - 4 > 13) + { + m_data = data + 13; + m_dataSize = size - 13 - 4; + } } } \ No newline at end of file diff --git a/src/HLAPI/Plugin/PluginManager.cpp b/src/HLAPI/Plugin/PluginManager.cpp new file mode 100644 index 0000000..e310f25 --- /dev/null +++ b/src/HLAPI/Plugin/PluginManager.cpp @@ -0,0 +1,11 @@ +#include "HLAPI/Plugin/PluginManager.hpp" + +#include "HLAPI/Plugin/Plugin.hpp" + +std::net::PluginManager::PluginManager() +{ +} + +std::net::PluginManager::~PluginManager() +{ +} diff --git a/src/HLAPI/Server.cpp b/src/HLAPI/Server.cpp index 99c901a..8cccc61 100644 --- a/src/HLAPI/Server.cpp +++ b/src/HLAPI/Server.cpp @@ -1,7 +1,9 @@ -#include "Server.hpp" +#include "HLAPI/Server.hpp" -#include "MessageQueue.hpp" -#include "TcpServer.hpp" +#include + +#include "HLAPI/TcpServer.hpp" +#include "HLAPI/TcpConnectionHandler.hpp" //#include "UdpServer.hpp namespace std::net @@ -20,4 +22,9 @@ namespace std::net { m_tcpServer->Stop(); } + + void Server::AddPlugin(Plugin* plugin) + { + m_tcpServer->m_connectionHandler->m_pluginManager->AddPlugin(plugin); + } } \ No newline at end of file diff --git a/src/HLAPI/TcpConnection.cpp b/src/HLAPI/TcpConnection.cpp index 490f1c1..5441e6d 100644 --- a/src/HLAPI/TcpConnection.cpp +++ b/src/HLAPI/TcpConnection.cpp @@ -1,6 +1,6 @@ -#include "TcpConnection.hpp" +#include "HLAPI/TcpConnection.hpp" -#include "InternalTags.hpp" +#include "HLAPI/InternalTags.hpp" namespace std::net { @@ -24,7 +24,7 @@ namespace std::net m_id = id; } - bool TcpConnection::sendMessage(NetworkMessage & msg) + bool TcpConnection::sendMessage(const NetworkMessage & msg) { uint32_t size; uint8_t *data = msg.SerializeData(size); diff --git a/src/HLAPI/TcpConnectionHandler.cpp b/src/HLAPI/TcpConnectionHandler.cpp index 7d965b8..57227f5 100644 --- a/src/HLAPI/TcpConnectionHandler.cpp +++ b/src/HLAPI/TcpConnectionHandler.cpp @@ -1,13 +1,12 @@ -#include "TcpConnectionHandler.hpp" +#include "HLAPI/TcpConnectionHandler.hpp" -#include "DisconnectedEvent.hpp" -#include "NewConnectionEvent.hpp" -#include "InternalTags.hpp" +#include "HLAPI/DisconnectedEvent.hpp" +#include "HLAPI/NewConnectionEvent.hpp" +#include "HLAPI/InternalTags.hpp" -#include "NetworkMessage.hpp" -#include "MessageQueue.hpp" -#include "TcpConnection.hpp" -#include "TcpListener.hpp" +#include "HLAPI/NetworkMessage.hpp" +#include "HLAPI/TcpConnection.hpp" +#include "VoidNet/TcpListener.hpp" #include @@ -16,7 +15,7 @@ namespace std::net TcpConnectionHandler::TcpConnectionHandler(std::shared_ptr listener_ptr) : m_run(false) , m_listenerPtr(listener_ptr) - , m_queue(new MessageQueue()) + , m_pluginManager(new PluginManager()) { } @@ -36,9 +35,6 @@ namespace std::net std::thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this); m_receiveThread.swap(receive_thread); - - std::thread send_thread(&TcpConnectionHandler::HandleSendThreaded, this); - m_sendThread.swap(send_thread); } void TcpConnectionHandler::Stop() @@ -82,8 +78,6 @@ namespace std::net client_fd.fd = c->m_client->m_socket->GetNativeSocket(); client_fd.events = POLLRDNORM; poll_fds.emplace_back(client_fd); - - m_queue->EnqueueConnection(msg); } uint32_t TcpConnectionHandler::GetAvailableID() @@ -112,6 +106,11 @@ namespace std::net m_maxConnections = max_connections; } + void TcpConnectionHandler::HandlePluginMessage(const NetworkMessage& message) + { + m_pluginManager->HandleMessage(message); + } + void TcpConnectionHandler::HandleReceiveMsgAndConns() { int res = poll(poll_fds.data(), poll_fds.size(), -1); @@ -145,129 +144,116 @@ namespace std::net else // not the listening socket { SOCKET c = poll_fds.at(i).fd; - std::unique_ptr header(new uint8_t[sizeof(NetworkHeader*)]()); + + uint8_t* header = new uint8_t[sizeof(NetworkHeader)](); int32_t read; - if ((read = recv(c, (char*)header.get(), sizeof(NetworkHeader*), 0)) != sizeof(NetworkHeader*)) + if ((read = recv(c, (char*)header, sizeof(NetworkHeader), 0)) != sizeof(NetworkHeader)) continue; - std::unique_ptr net_header((NetworkHeader*)header.get()); - std::unique_ptr buffer(new uint8_t[net_header->Size]()); + NetworkHeader net_header(*(NetworkHeader*)(header)); + uint8_t *buffer = new uint8_t[net_header.Size](); - if ((read = recv(c, (char*)buffer.get(), net_header->Size, 0)) == net_header->Size) + read = recv(c, (char*)buffer, net_header.Size - 4, 0); + if ((read) == net_header.Size - 4) { NetworkMessage msg; - msg.Deserialize(buffer.get(), net_header->Size); + msg.DeserializeWithoutHeader(buffer, net_header.Size); if (msg.GetTag() == (uint32_t)InternalTags::Disconnect) - { // i? or i+1 poll_fds.erase(poll_fds.begin() + i); - m_queue->EnqueueDisconnection(msg); - } - else if (msg.GetTag() == (uint32_t)InternalTags::Connect) - m_queue->EnqueueConnection(msg); - else - m_queue->EnqueueMessageReceived(msg); + HandleMessage(msg); } - else - continue; } } } - void TcpConnectionHandler::HandleSend() + void TcpConnectionHandler::HandleMessage(const NetworkMessage &msg) { - if (m_queue->SendSize() > 0) + if (msg.GetDistributionMode() == DistributionMode::Others) { - NetworkMessage msg = m_queue->DequeueMessageToSend(); - - if (msg.GetDistributionMode() == DistributionMode::Others) + m_listMutex.lock(); + for (int i = 0; i < m_list.size(); i++) { - m_listMutex.lock(); - for (int i = 0; i < m_list.size(); i++) + std::shared_ptr c = m_list.at(i); + if (c->GetID() != msg.GetSenderID()) { - std::shared_ptr c = m_list.at(i); - if (c->GetID() != msg.GetSenderID()) - { - if (!c->sendMessage(msg)) - { - // it failed - retry? or just disconnect right in the first try - } - } - } - m_listMutex.unlock(); - } - else if (msg.GetDistributionMode() == DistributionMode::OthersAndServer) - { - m_listMutex.lock(); - for (int i = 0; i < m_list.size(); i++) - { - std::shared_ptr c = m_list.at(i); - if (c->GetID() != msg.GetSenderID()) - { - if (!c->sendMessage(msg)) - { - // it failed - retry? or just disconnect right in the first try - } - } - } - m_listMutex.unlock(); - - //handle to plugins too - } - else if (msg.GetDistributionMode() == DistributionMode::ID) - { - m_listMutex.lock(); - for (int i = 0; i < m_list.size(); i++) - { - std::shared_ptr c = m_list.at(i); - if (c->GetID() == msg.GetSenderID()) - { - if (!c->sendMessage(msg)) - { - // it failed - retry? or just disconnect right in the first try - } - } - } - m_listMutex.unlock(); - } - else if (msg.GetDistributionMode() == DistributionMode::All) - { - m_listMutex.lock(); - for (int i = 0; i < m_list.size(); i++) - { - std::shared_ptr c = m_list.at(i); - if (!c->sendMessage(msg)) { // it failed - retry? or just disconnect right in the first try } } - m_listMutex.unlock(); - } - else if (msg.GetDistributionMode() == DistributionMode::AllAndMe) - { - m_listMutex.lock(); - for (int i = 0; i < m_list.size(); i++) - { - std::shared_ptr c = m_list.at(i); - - if (!c->sendMessage(msg)) - { - // it failed - retry? or just disconnect right in the first try - } - } - m_listMutex.unlock(); - - //handle to plugins too - } - else if (msg.GetDistributionMode() == DistributionMode::Server) - { - //handle just in plugins } + m_listMutex.unlock(); } + else if (msg.GetDistributionMode() == DistributionMode::OthersAndServer) + { + HandlePluginMessage(msg); + + m_listMutex.lock(); + for (int i = 0; i < m_list.size(); i++) + { + std::shared_ptr c = m_list.at(i); + if (c->GetID() != msg.GetSenderID()) + { + if (!c->sendMessage(msg)) + { + // it failed - retry? or just disconnect right in the first try + } + } + } + m_listMutex.unlock(); + } + else if (msg.GetDistributionMode() == DistributionMode::ID) + { + m_listMutex.lock(); + for (int i = 0; i < m_list.size(); i++) + { + std::shared_ptr c = m_list.at(i); + if (c->GetID() == msg.GetSenderID()) + { + if (!c->sendMessage(msg)) + { + // it failed - retry? or just disconnect right in the first try + } + } + } + m_listMutex.unlock(); + } + else if (msg.GetDistributionMode() == DistributionMode::All) + { + m_listMutex.lock(); + for (int i = 0; i < m_list.size(); i++) + { + std::shared_ptr c = m_list.at(i); + + if (!c->sendMessage(msg)) + { + // it failed - retry? or just disconnect right in the first try + } + } + m_listMutex.unlock(); + } + else if (msg.GetDistributionMode() == DistributionMode::AllAndServer) + { + HandlePluginMessage(msg); + + m_listMutex.lock(); + for (int i = 0; i < m_list.size(); i++) + { + std::shared_ptr c = m_list.at(i); + + if (!c->sendMessage(msg)) + { + // it failed - retry? or just disconnect right in the first try + } + } + m_listMutex.unlock(); + } + else if (msg.GetDistributionMode() == DistributionMode::Server) + HandlePluginMessage(msg); } void TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded() @@ -275,10 +261,4 @@ namespace std::net while (m_run.load()) HandleReceiveMsgAndConns(); } - - void TcpConnectionHandler::HandleSendThreaded() - { - while (m_run.load()) - HandleSend(); - } } \ No newline at end of file diff --git a/src/HLAPI/TcpServer.cpp b/src/HLAPI/TcpServer.cpp index 778f451..ce30ae8 100644 --- a/src/HLAPI/TcpServer.cpp +++ b/src/HLAPI/TcpServer.cpp @@ -1,9 +1,9 @@ -#include "TcpServer.hpp" +#include "HLAPI/TcpServer.hpp" -#include "TcpConnection.hpp" -#include "TcpSocketBuilder.hpp" -#include "TcpClient.hpp" -#include "TcpConnectionHandler.hpp" +#include "HLAPI/TcpConnection.hpp" +#include "VoidNet/TcpSocketBuilder.hpp" +#include "VoidNet/TcpClient.hpp" +#include "HLAPI/TcpConnectionHandler.hpp" namespace std::net { diff --git a/src/HLAPI/main.cpp b/src/HLAPI/main.cpp index 192f298..fda62ef 100644 --- a/src/HLAPI/main.cpp +++ b/src/HLAPI/main.cpp @@ -1,25 +1,32 @@ -#include "Init.hpp" -#include "Server.hpp" -#include "ByteConverter.hpp" -#include "TcpClient.hpp" -#include "InternalTags.hpp" -#include "NetworkMessage.hpp" +#include "VoidNet/Init.hpp" +#include "HLAPI/Server.hpp" +#include "HLAPI/ByteConverter.hpp" +#include "VoidNet/TcpClient.hpp" +#include "HLAPI/InternalTags.hpp" +#include "HLAPI/NetworkMessage.hpp" +#include "HLAPI/Plugin/Plugin.hpp" #include +class Plugin : public std::net::Plugin +{ + virtual void HandleMessage(const std::net::NetworkMessage& message) override + { + + } +}; + int main() { std::net::Initialize(); std::net::Server server(100); + server.AddPlugin(new Plugin()); server.Start(); std::net::TcpClient client; std::net::IPAddress ip("127.0.0.1"); client.Connect(ip); - std::net::TcpClient client2; - client2.Connect(ip); - while (true) { uint32_t data_size; @@ -36,23 +43,22 @@ int main() uint32_t id = std::ByteConverter::FromBytes((uint8_t*)(message.GetData())); if (message.GetTag() == (uint32_t)InternalTags::AssignID) - std::cout << id << std::endl; - } + { + std::cout << id << std::endl; - while (client2.HasPendingData(data_size)) - { - std::net::NetworkMessage message2; + std::string str = std::string("asd"); + std::net::NetworkMessage msg(100, std::net::DistributionMode::AllAndServer, 150, 1, &str, sizeof(str)); - uint8_t* bytes2 = new uint8_t[data_size](); - - int32_t read2; - client2.Recv(bytes2, data_size, read2); - - message2.Deserialize(bytes2, data_size); - - uint32_t id2 = std::ByteConverter::FromBytes((uint8_t*)(message2.GetData())); - if (message2.GetTag() == (uint32_t)InternalTags::AssignID) - std::cout << id2 << std::endl; + uint32_t dataa_size; + uint8_t* dataaaa = msg.SerializeData(dataa_size); + int32_t sent; + client.Send(dataaaa, dataa_size, sent); + } + else + { + std::string* txt = message.GetData(); + std::cout << txt->c_str() << std::endl; + } } } } \ No newline at end of file diff --git a/src/VoidNet/Cookies.cpp b/src/VoidNet/Cookies.cpp index fdf9241..e88f101 100644 --- a/src/VoidNet/Cookies.cpp +++ b/src/VoidNet/Cookies.cpp @@ -1,7 +1,7 @@ // https://github.com/mfichman/http -#include "Cookies.hpp" -#include "Parse.hpp" +#include "VoidNet/Cookies.hpp" +#include "VoidNet/Parse.hpp" #include diff --git a/src/VoidNet/Headers.cpp b/src/VoidNet/Headers.cpp index 01ba18f..2873b69 100644 --- a/src/VoidNet/Headers.cpp +++ b/src/VoidNet/Headers.cpp @@ -1,6 +1,6 @@ // https://github.com/mfichman/http -#include "Headers.hpp" +#include "VoidNet/Headers.hpp" namespace std::net { diff --git a/src/VoidNet/Http.cpp b/src/VoidNet/Http.cpp index 47c0f38..390332b 100644 --- a/src/VoidNet/Http.cpp +++ b/src/VoidNet/Http.cpp @@ -1,13 +1,13 @@ // https://github.com/mfichman/http -#include "Http.hpp" +#include "VoidNet/Http.hpp" #include #include #include -#include "Socket.hpp" -#include "SecureSocket.hpp" +#include "VoidNet/Socket.hpp" +#include "VoidNet/SecureSocket.hpp" #undef DELETE diff --git a/src/VoidNet/IPAddress.cpp b/src/VoidNet/IPAddress.cpp index 4273ad7..5e5dd0c 100644 --- a/src/VoidNet/IPAddress.cpp +++ b/src/VoidNet/IPAddress.cpp @@ -1,4 +1,4 @@ -#include "IPAddress.hpp" +#include "VoidNet/IPAddress.hpp" namespace std::net { diff --git a/src/VoidNet/Request.cpp b/src/VoidNet/Request.cpp index 88e7f91..b3c97cc 100644 --- a/src/VoidNet/Request.cpp +++ b/src/VoidNet/Request.cpp @@ -1,6 +1,6 @@ // https://github.com/mfichman/http -#include "Request.hpp" +#include "VoidNet/Request.hpp" namespace std::net { diff --git a/src/VoidNet/Response.cpp b/src/VoidNet/Response.cpp index d274012..7bd9603 100644 --- a/src/VoidNet/Response.cpp +++ b/src/VoidNet/Response.cpp @@ -1,7 +1,7 @@ // https://github.com/mfichman/http -#include "Response.hpp" -#include "Parse.hpp" +#include "VoidNet/Response.hpp" +#include "VoidNet/Parse.hpp" namespace std::net { diff --git a/src/VoidNet/SecureSocket.cpp b/src/VoidNet/SecureSocket.cpp index 39a2e6a..7a7e7a1 100644 --- a/src/VoidNet/SecureSocket.cpp +++ b/src/VoidNet/SecureSocket.cpp @@ -1,4 +1,4 @@ -#include "SecureSocket.hpp" +#include "VoidNet/SecureSocket.hpp" #include diff --git a/src/VoidNet/Socket.cpp b/src/VoidNet/Socket.cpp index 2d6e1d8..eb3e02c 100644 --- a/src/VoidNet/Socket.cpp +++ b/src/VoidNet/Socket.cpp @@ -1,5 +1,5 @@ -#include "Socket.hpp" -#include "IPAddress.hpp" +#include "VoidNet/Socket.hpp" +#include "VoidNet/IPAddress.hpp" namespace std::net { diff --git a/src/VoidNet/TcpClient.cpp b/src/VoidNet/TcpClient.cpp index 1b39843..7b438ff 100644 --- a/src/VoidNet/TcpClient.cpp +++ b/src/VoidNet/TcpClient.cpp @@ -1,5 +1,5 @@ -#include "TcpClient.hpp" -#include "TcpSocketBuilder.hpp" +#include "VoidNet/TcpClient.hpp" +#include "VoidNet/TcpSocketBuilder.hpp" namespace std::net { diff --git a/src/VoidNet/TcpListener.cpp b/src/VoidNet/TcpListener.cpp index f49c9af..54c4aba 100644 --- a/src/VoidNet/TcpListener.cpp +++ b/src/VoidNet/TcpListener.cpp @@ -1,7 +1,7 @@ -#include "TcpListener.hpp" -#include "TcpSocketBuilder.hpp" -#include "Socket.hpp" -#include "TcpClient.hpp" +#include "VoidNet/TcpListener.hpp" +#include "VoidNet/TcpSocketBuilder.hpp" +#include "VoidNet/Socket.hpp" +#include "VoidNet/TcpClient.hpp" namespace std::net { diff --git a/src/VoidNet/TcpSocketBuilder.cpp b/src/VoidNet/TcpSocketBuilder.cpp index f1d9718..dfa417b 100644 --- a/src/VoidNet/TcpSocketBuilder.cpp +++ b/src/VoidNet/TcpSocketBuilder.cpp @@ -1,8 +1,8 @@ -#include "TcpSocketBuilder.hpp" +#include "VoidNet/TcpSocketBuilder.hpp" -#include "Socket.hpp" -#include "TcpClient.hpp" -#include "TcpListener.hpp" +#include "VoidNet/Socket.hpp" +#include "VoidNet/TcpClient.hpp" +#include "VoidNet/TcpListener.hpp" namespace std::net { diff --git a/src/VoidNet/UdpSocket.cpp b/src/VoidNet/UdpSocket.cpp index 811d67a..468c266 100644 --- a/src/VoidNet/UdpSocket.cpp +++ b/src/VoidNet/UdpSocket.cpp @@ -1,4 +1,4 @@ -#include "UdpSocket.hpp" +#include "VoidNet/UdpSocket.hpp" namespace std::net { diff --git a/src/VoidNet/Uri.cpp b/src/VoidNet/Uri.cpp index 6d8e88d..743798e 100644 --- a/src/VoidNet/Uri.cpp +++ b/src/VoidNet/Uri.cpp @@ -1,7 +1,7 @@ // https://github.com/mfichman/http -#include "Uri.hpp" -#include "Parse.hpp" +#include "VoidNet/Uri.hpp" +#include "VoidNet/Parse.hpp" namespace std::net { diff --git a/src/VoidNet/Util.cpp b/src/VoidNet/Util.cpp index d0a8c6f..516ae95 100644 --- a/src/VoidNet/Util.cpp +++ b/src/VoidNet/Util.cpp @@ -1,4 +1,4 @@ -#include "Util.hpp" +#include "VoidNet/Util.hpp" std::vector std::net::Split(const std::string& str, const std::string& delimiter) {