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
This commit is contained in:
TheDoctor
2019-10-15 16:27:16 +01:00
parent 32473472e4
commit c50d40341f
48 changed files with 321 additions and 334 deletions

View File

@ -23,10 +23,11 @@
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp" /> <ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp" />
<ClInclude Include="..\..\include\HLAPI\DisconnectedEvent.hpp" /> <ClInclude Include="..\..\include\HLAPI\DisconnectedEvent.hpp" />
<ClInclude Include="..\..\include\HLAPI\InternalTags.hpp" /> <ClInclude Include="..\..\include\HLAPI\InternalTags.hpp" />
<ClInclude Include="..\..\include\HLAPI\MessageQueue.hpp" />
<ClInclude Include="..\..\include\HLAPI\NetworkHeader.hpp" /> <ClInclude Include="..\..\include\HLAPI\NetworkHeader.hpp" />
<ClInclude Include="..\..\include\HLAPI\NetworkMessage.hpp" /> <ClInclude Include="..\..\include\HLAPI\NetworkMessage.hpp" />
<ClInclude Include="..\..\include\HLAPI\NewConnectionEvent.hpp" /> <ClInclude Include="..\..\include\HLAPI\NewConnectionEvent.hpp" />
<ClInclude Include="..\..\include\HLAPI\Plugin\Plugin.hpp" />
<ClInclude Include="..\..\include\HLAPI\Plugin\PluginManager.hpp" />
<ClInclude Include="..\..\include\HLAPI\Server.hpp" /> <ClInclude Include="..\..\include\HLAPI\Server.hpp" />
<ClInclude Include="..\..\include\HLAPI\TcpConnection.hpp" /> <ClInclude Include="..\..\include\HLAPI\TcpConnection.hpp" />
<ClInclude Include="..\..\include\HLAPI\TcpConnectionHandler.hpp" /> <ClInclude Include="..\..\include\HLAPI\TcpConnectionHandler.hpp" />
@ -34,8 +35,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\HLAPI\main.cpp" /> <ClCompile Include="..\..\src\HLAPI\main.cpp" />
<ClCompile Include="..\..\src\HLAPI\MessageQueue.cpp" />
<ClCompile Include="..\..\src\HLAPI\NetworkMessage.cpp" /> <ClCompile Include="..\..\src\HLAPI\NetworkMessage.cpp" />
<ClCompile Include="..\..\src\HLAPI\Plugin\PluginManager.cpp" />
<ClCompile Include="..\..\src\HLAPI\Server.cpp" /> <ClCompile Include="..\..\src\HLAPI\Server.cpp" />
<ClCompile Include="..\..\src\HLAPI\TcpConnection.cpp" /> <ClCompile Include="..\..\src\HLAPI\TcpConnection.cpp" />
<ClCompile Include="..\..\src\HLAPI\TcpConnectionHandler.cpp" /> <ClCompile Include="..\..\src\HLAPI\TcpConnectionHandler.cpp" />
@ -66,6 +67,7 @@
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset> <PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet> <CharacterSet>MultiByte</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
@ -99,8 +101,10 @@
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<AdditionalIncludeDirectories>../../include/HLAPI;../../include/VoidNet</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../../include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard> <LanguageStandard>stdcpplatest</LanguageStandard>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>

View File

@ -7,6 +7,12 @@
<Filter Include="src"> <Filter Include="src">
<UniqueIdentifier>{1c087ed8-3cca-4254-836f-10fb70fda154}</UniqueIdentifier> <UniqueIdentifier>{1c087ed8-3cca-4254-836f-10fb70fda154}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="include\Plugin">
<UniqueIdentifier>{e4fcabf7-6c96-45cb-bb30-917081477267}</UniqueIdentifier>
</Filter>
<Filter Include="src\Plugin">
<UniqueIdentifier>{503a204a-1cb8-46cc-91f5-9f5f583d543d}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp"> <ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp">
@ -18,9 +24,6 @@
<ClInclude Include="..\..\include\HLAPI\InternalTags.hpp"> <ClInclude Include="..\..\include\HLAPI\InternalTags.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\HLAPI\MessageQueue.hpp">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="..\..\include\HLAPI\NetworkHeader.hpp"> <ClInclude Include="..\..\include\HLAPI\NetworkHeader.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
@ -45,14 +48,17 @@
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp"> <ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp">
<Filter>include</Filter> <Filter>include</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\..\include\HLAPI\Plugin\Plugin.hpp">
<Filter>include\Plugin</Filter>
</ClInclude>
<ClInclude Include="..\..\include\HLAPI\Plugin\PluginManager.hpp">
<Filter>include\Plugin</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="..\..\src\HLAPI\main.cpp"> <ClCompile Include="..\..\src\HLAPI\main.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\HLAPI\MessageQueue.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="..\..\src\HLAPI\NetworkMessage.cpp"> <ClCompile Include="..\..\src\HLAPI\NetworkMessage.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
@ -68,5 +74,8 @@
<ClCompile Include="..\..\src\HLAPI\TcpServer.cpp"> <ClCompile Include="..\..\src\HLAPI\TcpServer.cpp">
<Filter>src</Filter> <Filter>src</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\src\HLAPI\Plugin\PluginManager.cpp">
<Filter>src\Plugin</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -84,6 +84,7 @@
<UseDebugLibraries>true</UseDebugLibraries> <UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v141</PlatformToolset> <PlatformToolset>v141</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet> <CharacterSet>MultiByte</CharacterSet>
<UseOfMfc>Static</UseOfMfc>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType> <ConfigurationType>Application</ConfigurationType>
@ -124,9 +125,10 @@
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization> <Optimization>Disabled</Optimization>
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>../../include/VoidNet</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../../include</AdditionalIncludeDirectories>
<LanguageStandard>stdcpplatest</LanguageStandard> <LanguageStandard>stdcpplatest</LanguageStandard>
<PreprocessorDefinitions>_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> <PreprocessorDefinitions>_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
</ClCompile> </ClCompile>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "NetworkMessage.hpp" #include "HLAPI/NetworkMessage.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,43 +0,0 @@
#pragma once
#include <queue>
#include <mutex>
#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<NetworkMessage> m_messagesToSend;
std::deque<NewConnectionEvent> m_connectionEvents;
std::deque<DisconnectedEvent> m_disconnectedEvents;
std::deque<DataReceivedEvent> m_dataReceivedEvents;
std::mutex m_sendMutex;
std::mutex m_receivedMutex;
std::mutex m_disconnectMutex;
std::mutex m_connectionMutex;
};
}

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "NetworkHeader.hpp" #include "HLAPI/NetworkHeader.hpp"
#include "ByteConverter.hpp" #include "HLAPI/ByteConverter.hpp"
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
@ -14,7 +14,7 @@ namespace std::net
Others, Others,
OthersAndServer, OthersAndServer,
All, All,
AllAndMe, AllAndServer,
Server Server
}; };
@ -58,14 +58,14 @@ namespace std::net
} }
template<typename T> template<typename T>
uint8_t * SerializeData(uint32_t &size) uint8_t * SerializeData(uint32_t &size) const
{ {
int32_t sizeOfNetHeader = sizeof(NetworkHeader); int32_t sizeOfNetHeader = sizeof(NetworkHeader);
NetworkHeader header; NetworkHeader header;
header.Size = 13 + sizeOfNetHeader + sizeof(T); 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); memcpy(bytes, &header, sizeOfNetHeader);
uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4 uint8_t *sender = ByteConverter::ToBytes<uint32_t>(m_senderID); // 4
@ -83,8 +83,9 @@ namespace std::net
return bytes; return bytes;
} }
uint8_t *SerializeData(uint32_t &size); uint8_t *SerializeData(uint32_t &size) const;
void Deserialize(uint8_t *data, uint32_t size); void Deserialize(uint8_t *data, uint32_t size);
void DeserializeWithoutHeader(uint8_t* data, uint32_t size);
template<typename T> template<typename T>
T *GetData() const T *GetData() const

View File

@ -0,0 +1,16 @@
#pragma once
#include <HLAPI/NetworkMessage.hpp>
namespace std::net
{
class Plugin
{
public:
Plugin()
{
}
virtual void HandleMessage(const NetworkMessage& msg) = 0;
};
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <vector>
#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<Plugin*> m_plugins = std::vector<Plugin*>();
};
}

View File

@ -1,14 +1,13 @@
#pragma once #pragma once
#include <Net.hpp> #include <VoidNet/Net.hpp>
#include <memory> #include <memory>
namespace std::net namespace std::net
{ {
class TcpServer; class TcpServer;
class Plugin;
class MessageQueue;
class Server class Server
{ {
@ -18,6 +17,8 @@ namespace std::net
void Start(); void Start();
void Stop(); void Stop();
void AddPlugin(Plugin* plugin);
private: private:
std::shared_ptr<std::net::TcpServer> m_tcpServer; std::shared_ptr<std::net::TcpServer> m_tcpServer;
}; };

View File

@ -2,9 +2,9 @@
#include <functional> #include <functional>
#include "TcpClient.hpp" #include "VoidNet/TcpClient.hpp"
#include "NetworkMessage.hpp" #include "HLAPI/NetworkMessage.hpp"
namespace std::net namespace std::net
{ {
@ -32,7 +32,7 @@ namespace std::net
std::function<void(uint32_t, void*)> NewConnectionEvent; std::function<void(uint32_t, void*)> NewConnectionEvent;
private: private:
bool sendMessage(NetworkMessage &msg); bool sendMessage(const NetworkMessage &msg);
std::shared_ptr<TcpClient> m_client; std::shared_ptr<TcpClient> m_client;
uint32_t m_id; uint32_t m_id;

View File

@ -4,8 +4,9 @@
#include <atomic> #include <atomic>
#include <queue> #include <queue>
#include <TcpListener.hpp> #include <VoidNet/TcpListener.hpp>
#include <TcpConnection.hpp> #include <HLAPI/TcpConnection.hpp>
#include <HLAPI/Plugin/PluginManager.hpp>
namespace std::net namespace std::net
{ {
@ -30,14 +31,15 @@ namespace std::net
void AddClient(std::shared_ptr<TcpConnection> &c); void AddClient(std::shared_ptr<TcpConnection> &c);
void SetMaxConnections(uint32_t max_connections); void SetMaxConnections(uint32_t max_connections);
void HandlePluginMessage(const NetworkMessage& message);
uint32_t GetAvailableID(); uint32_t GetAvailableID();
private: private:
void HandleReceiveMsgAndConns(); void HandleReceiveMsgAndConns();
void HandleSend(); void HandleMessage(const NetworkMessage &msg);
void HandleReceiveMsgAndConnsThreaded(); void HandleReceiveMsgAndConnsThreaded();
void HandleSendThreaded();
private: private:
std::vector<std::shared_ptr<TcpConnection>> m_list; std::vector<std::shared_ptr<TcpConnection>> m_list;
@ -54,6 +56,8 @@ namespace std::net
std::shared_ptr<TcpListener> m_listenerPtr; std::shared_ptr<TcpListener> m_listenerPtr;
std::shared_ptr<PluginManager> m_pluginManager;
std::vector<pollfd> poll_fds; std::vector<pollfd> poll_fds;
}; };
} }

View File

@ -2,7 +2,7 @@
#include <atomic> #include <atomic>
#include "TcpListener.hpp" #include "VoidNet/TcpListener.hpp"
namespace std::net namespace std::net
{ {

View File

@ -2,8 +2,8 @@
#pragma once #pragma once
#include "Response.hpp" #include "VoidNet/Response.hpp"
#include "Request.hpp" #include "VoidNet/Request.hpp"
namespace std::net namespace std::net
{ {

View File

@ -5,7 +5,7 @@
#include <ostream> #include <ostream>
#include <string> #include <string>
#include "Net.hpp" #include "VoidNet/Net.hpp"
namespace std::net namespace std::net
{ {

View File

@ -4,9 +4,9 @@
#include <chrono> #include <chrono>
#include <memory> #include <memory>
#include "Net.hpp" #include "VoidNet/Net.hpp"
#include "IPAddress.hpp" #include "VoidNet/IPAddress.hpp"
#include "Enums.hpp" #include "VoidNet/Enums.hpp"
namespace std::net namespace std::net
{ {

View File

@ -7,6 +7,8 @@
#include <ws2tcpip.h> #include <ws2tcpip.h>
#undef SendMessage #undef SendMessage
#undef SetPort #undef SetPort
#undef max
#undef min
#define poll WSAPoll #define poll WSAPoll
#define ioctl ioctlsocket #define ioctl ioctlsocket

View File

@ -2,9 +2,9 @@
#pragma once #pragma once
#include "Uri.hpp" #include "VoidNet/Uri.hpp"
#include "Headers.hpp" #include "VoidNet/Headers.hpp"
#include "Enums.hpp" #include "VoidNet/Enums.hpp"
namespace std::net namespace std::net
{ {

View File

@ -2,9 +2,9 @@
#pragma once #pragma once
#include "Headers.hpp" #include "VoidNet/Headers.hpp"
#include "Cookies.hpp" #include "VoidNet/Cookies.hpp"
#include "Enums.hpp" #include "VoidNet/Enums.hpp"
#include <string> #include <string>

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Socket.hpp" #include "VoidNet/Socket.hpp"
//#include <openssl/rand.h> //#include <openssl/rand.h>
//#include <openssl/ssl.h> //#include <openssl/ssl.h>

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "ISocket.hpp" #include "VoidNet/ISocket.hpp"
#include "Enums.hpp" #include "VoidNet/Enums.hpp"
namespace std::net namespace std::net
{ {
@ -14,7 +14,7 @@ namespace std::net
init(); 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) : ISocket(socketType, protocol)
, m_socket(newSocket) , m_socket(newSocket)
{ {

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Socket.hpp" #include "VoidNet/Socket.hpp"
namespace std::net namespace std::net
{ {

View File

@ -3,7 +3,7 @@
#include <thread> #include <thread>
#include <queue> #include <queue>
#include "Socket.hpp" #include "VoidNet/Socket.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "Enums.hpp" #include "VoidNet/Enums.hpp"
#include "IPAddress.hpp" #include "VoidNet/IPAddress.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "Socket.hpp" #include "VoidNet/Socket.hpp"
#include <memory> #include <memory>

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "UdpSocket.hpp" #include "VoidNet/UdpSocket.hpp"
#include "IPAddress.hpp" #include "VoidNet/IPAddress.hpp"
#include <vector> #include <vector>

View File

@ -3,7 +3,7 @@
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include "Net.hpp" #include "VoidNet/Net.hpp"
namespace std::net namespace std::net
{ {

View File

@ -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<DisconnectedEvent> ev(((NetworkMessage)msg).GetData<DisconnectedEvent>());
//m_disconnectedEvents.push_back(*(ev.get()));
m_disconnectMutex.unlock();
}
void MessageQueue::EnqueueConnection(const NetworkMessage & msg)
{
m_connectionMutex.lock();
NewConnectionEvent ev(msg.GetSenderID(), msg.GetData<void>());
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;
}
}

View File

@ -1,4 +1,5 @@
#include "NetworkMessage.hpp" #include "HLAPI/NetworkMessage.hpp"
#include <cassert>
namespace std::net namespace std::net
{ {
@ -22,7 +23,7 @@ namespace std::net
return m_tag; return m_tag;
} }
uint8_t *NetworkMessage::SerializeData(uint32_t &size) uint8_t *NetworkMessage::SerializeData(uint32_t &size) const
{ {
int32_t sizeOfNetHeader = sizeof(NetworkHeader); int32_t sizeOfNetHeader = sizeof(NetworkHeader);
@ -49,15 +50,24 @@ namespace std::net
void NetworkMessage::Deserialize(uint8_t *data, uint32_t size) void NetworkMessage::Deserialize(uint8_t *data, uint32_t size)
{ {
NetworkHeader buffer; NetworkHeader header;
uint32_t sizeOfNetHeader = sizeof(NetworkHeader); memcpy(&(header), data, sizeof(NetworkHeader));
memcpy(&(buffer), data, sizeOfNetHeader);
memcpy(&(m_senderID), data + 4 + sizeOfNetHeader, 4); data += sizeof(NetworkHeader);
m_distributionMode = (DistributionMode)data[8 + sizeOfNetHeader];
memcpy(&(m_destinationID), data + 5 + sizeOfNetHeader, 4);
memcpy(&(m_tag), data + 9 + sizeOfNetHeader, 4);
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;
}
} }
} }

View File

@ -0,0 +1,11 @@
#include "HLAPI/Plugin/PluginManager.hpp"
#include "HLAPI/Plugin/Plugin.hpp"
std::net::PluginManager::PluginManager()
{
}
std::net::PluginManager::~PluginManager()
{
}

View File

@ -1,7 +1,9 @@
#include "Server.hpp" #include "HLAPI/Server.hpp"
#include "MessageQueue.hpp" #include <HLAPI/Plugin/Plugin.hpp>
#include "TcpServer.hpp"
#include "HLAPI/TcpServer.hpp"
#include "HLAPI/TcpConnectionHandler.hpp"
//#include "UdpServer.hpp //#include "UdpServer.hpp
namespace std::net namespace std::net
@ -20,4 +22,9 @@ namespace std::net
{ {
m_tcpServer->Stop(); m_tcpServer->Stop();
} }
void Server::AddPlugin(Plugin* plugin)
{
m_tcpServer->m_connectionHandler->m_pluginManager->AddPlugin(plugin);
}
} }

View File

@ -1,6 +1,6 @@
#include "TcpConnection.hpp" #include "HLAPI/TcpConnection.hpp"
#include "InternalTags.hpp" #include "HLAPI/InternalTags.hpp"
namespace std::net namespace std::net
{ {
@ -24,7 +24,7 @@ namespace std::net
m_id = id; m_id = id;
} }
bool TcpConnection::sendMessage(NetworkMessage & msg) bool TcpConnection::sendMessage(const NetworkMessage & msg)
{ {
uint32_t size; uint32_t size;
uint8_t *data = msg.SerializeData(size); uint8_t *data = msg.SerializeData(size);

View File

@ -1,13 +1,12 @@
#include "TcpConnectionHandler.hpp" #include "HLAPI/TcpConnectionHandler.hpp"
#include "DisconnectedEvent.hpp" #include "HLAPI/DisconnectedEvent.hpp"
#include "NewConnectionEvent.hpp" #include "HLAPI/NewConnectionEvent.hpp"
#include "InternalTags.hpp" #include "HLAPI/InternalTags.hpp"
#include "NetworkMessage.hpp" #include "HLAPI/NetworkMessage.hpp"
#include "MessageQueue.hpp" #include "HLAPI/TcpConnection.hpp"
#include "TcpConnection.hpp" #include "VoidNet/TcpListener.hpp"
#include "TcpListener.hpp"
#include <chrono> #include <chrono>
@ -16,7 +15,7 @@ namespace std::net
TcpConnectionHandler::TcpConnectionHandler(std::shared_ptr<TcpListener> listener_ptr) TcpConnectionHandler::TcpConnectionHandler(std::shared_ptr<TcpListener> listener_ptr)
: m_run(false) : m_run(false)
, m_listenerPtr(listener_ptr) , 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); std::thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this);
m_receiveThread.swap(receive_thread); m_receiveThread.swap(receive_thread);
std::thread send_thread(&TcpConnectionHandler::HandleSendThreaded, this);
m_sendThread.swap(send_thread);
} }
void TcpConnectionHandler::Stop() void TcpConnectionHandler::Stop()
@ -82,8 +78,6 @@ namespace std::net
client_fd.fd = c->m_client->m_socket->GetNativeSocket(); client_fd.fd = c->m_client->m_socket->GetNativeSocket();
client_fd.events = POLLRDNORM; client_fd.events = POLLRDNORM;
poll_fds.emplace_back(client_fd); poll_fds.emplace_back(client_fd);
m_queue->EnqueueConnection(msg);
} }
uint32_t TcpConnectionHandler::GetAvailableID() uint32_t TcpConnectionHandler::GetAvailableID()
@ -112,6 +106,11 @@ namespace std::net
m_maxConnections = max_connections; m_maxConnections = max_connections;
} }
void TcpConnectionHandler::HandlePluginMessage(const NetworkMessage& message)
{
m_pluginManager->HandleMessage(message);
}
void TcpConnectionHandler::HandleReceiveMsgAndConns() void TcpConnectionHandler::HandleReceiveMsgAndConns()
{ {
int res = poll(poll_fds.data(), poll_fds.size(), -1); int res = poll(poll_fds.data(), poll_fds.size(), -1);
@ -145,129 +144,116 @@ namespace std::net
else // not the listening socket else // not the listening socket
{ {
SOCKET c = poll_fds.at(i).fd; SOCKET c = poll_fds.at(i).fd;
std::unique_ptr<uint8_t> header(new uint8_t[sizeof(NetworkHeader*)]());
uint8_t* header = new uint8_t[sizeof(NetworkHeader)]();
int32_t read; 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; continue;
std::unique_ptr<NetworkHeader> net_header((NetworkHeader*)header.get()); NetworkHeader net_header(*(NetworkHeader*)(header));
std::unique_ptr<uint8_t> buffer(new uint8_t[net_header->Size]()); 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; NetworkMessage msg;
msg.Deserialize(buffer.get(), net_header->Size); msg.DeserializeWithoutHeader(buffer, net_header.Size);
if (msg.GetTag() == (uint32_t)InternalTags::Disconnect) if (msg.GetTag() == (uint32_t)InternalTags::Disconnect)
{
// i? or i+1 // i? or i+1
poll_fds.erase(poll_fds.begin() + i); poll_fds.erase(poll_fds.begin() + i);
m_queue->EnqueueDisconnection(msg); HandleMessage(msg);
}
else if (msg.GetTag() == (uint32_t)InternalTags::Connect)
m_queue->EnqueueConnection(msg);
else
m_queue->EnqueueMessageReceived(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(); m_listMutex.lock();
for (int i = 0; i < m_list.size(); i++)
if (msg.GetDistributionMode() == DistributionMode::Others)
{ {
m_listMutex.lock(); std::shared_ptr<TcpConnection> c = m_list.at(i);
for (int i = 0; i < m_list.size(); i++) if (c->GetID() != msg.GetSenderID())
{ {
std::shared_ptr<TcpConnection> 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<TcpConnection> 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<TcpConnection> 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<TcpConnection> c = m_list.at(i);
if (!c->sendMessage(msg)) if (!c->sendMessage(msg))
{ {
// it failed - retry? or just disconnect right in the first try // 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<TcpConnection> 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<TcpConnection> 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<TcpConnection> 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<TcpConnection> 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<TcpConnection> 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() void TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded()
@ -275,10 +261,4 @@ namespace std::net
while (m_run.load()) while (m_run.load())
HandleReceiveMsgAndConns(); HandleReceiveMsgAndConns();
} }
void TcpConnectionHandler::HandleSendThreaded()
{
while (m_run.load())
HandleSend();
}
} }

View File

@ -1,9 +1,9 @@
#include "TcpServer.hpp" #include "HLAPI/TcpServer.hpp"
#include "TcpConnection.hpp" #include "HLAPI/TcpConnection.hpp"
#include "TcpSocketBuilder.hpp" #include "VoidNet/TcpSocketBuilder.hpp"
#include "TcpClient.hpp" #include "VoidNet/TcpClient.hpp"
#include "TcpConnectionHandler.hpp" #include "HLAPI/TcpConnectionHandler.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,25 +1,32 @@
#include "Init.hpp" #include "VoidNet/Init.hpp"
#include "Server.hpp" #include "HLAPI/Server.hpp"
#include "ByteConverter.hpp" #include "HLAPI/ByteConverter.hpp"
#include "TcpClient.hpp" #include "VoidNet/TcpClient.hpp"
#include "InternalTags.hpp" #include "HLAPI/InternalTags.hpp"
#include "NetworkMessage.hpp" #include "HLAPI/NetworkMessage.hpp"
#include "HLAPI/Plugin/Plugin.hpp"
#include <iostream> #include <iostream>
class Plugin : public std::net::Plugin
{
virtual void HandleMessage(const std::net::NetworkMessage& message) override
{
}
};
int main() int main()
{ {
std::net::Initialize(); std::net::Initialize();
std::net::Server server(100); std::net::Server server(100);
server.AddPlugin(new Plugin());
server.Start(); server.Start();
std::net::TcpClient client; std::net::TcpClient client;
std::net::IPAddress ip("127.0.0.1"); std::net::IPAddress ip("127.0.0.1");
client.Connect(ip); client.Connect(ip);
std::net::TcpClient client2;
client2.Connect(ip);
while (true) while (true)
{ {
uint32_t data_size; uint32_t data_size;
@ -36,23 +43,22 @@ int main()
uint32_t id = std::ByteConverter::FromBytes<uint32_t>((uint8_t*)(message.GetData<void>())); uint32_t id = std::ByteConverter::FromBytes<uint32_t>((uint8_t*)(message.GetData<void>()));
if (message.GetTag() == (uint32_t)InternalTags::AssignID) if (message.GetTag() == (uint32_t)InternalTags::AssignID)
std::cout << id << std::endl; {
} std::cout << id << std::endl;
while (client2.HasPendingData(data_size)) std::string str = std::string("asd");
{ std::net::NetworkMessage msg(100, std::net::DistributionMode::AllAndServer, 150, 1, &str, sizeof(str));
std::net::NetworkMessage message2;
uint8_t* bytes2 = new uint8_t[data_size](); uint32_t dataa_size;
uint8_t* dataaaa = msg.SerializeData(dataa_size);
int32_t read2; int32_t sent;
client2.Recv(bytes2, data_size, read2); client.Send(dataaaa, dataa_size, sent);
}
message2.Deserialize(bytes2, data_size); else
{
uint32_t id2 = std::ByteConverter::FromBytes<uint32_t>((uint8_t*)(message2.GetData<void>())); std::string* txt = message.GetData<std::string>();
if (message2.GetTag() == (uint32_t)InternalTags::AssignID) std::cout << txt->c_str() << std::endl;
std::cout << id2 << std::endl; }
} }
} }
} }

View File

@ -1,7 +1,7 @@
// https://github.com/mfichman/http // https://github.com/mfichman/http
#include "Cookies.hpp" #include "VoidNet/Cookies.hpp"
#include "Parse.hpp" #include "VoidNet/Parse.hpp"
#include <cassert> #include <cassert>

View File

@ -1,6 +1,6 @@
// https://github.com/mfichman/http // https://github.com/mfichman/http
#include "Headers.hpp" #include "VoidNet/Headers.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,13 +1,13 @@
// https://github.com/mfichman/http // https://github.com/mfichman/http
#include "Http.hpp" #include "VoidNet/Http.hpp"
#include <cassert> #include <cassert>
#include <vector> #include <vector>
#include <sstream> #include <sstream>
#include "Socket.hpp" #include "VoidNet/Socket.hpp"
#include "SecureSocket.hpp" #include "VoidNet/SecureSocket.hpp"
#undef DELETE #undef DELETE

View File

@ -1,4 +1,4 @@
#include "IPAddress.hpp" #include "VoidNet/IPAddress.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,6 +1,6 @@
// https://github.com/mfichman/http // https://github.com/mfichman/http
#include "Request.hpp" #include "VoidNet/Request.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,7 +1,7 @@
// https://github.com/mfichman/http // https://github.com/mfichman/http
#include "Response.hpp" #include "VoidNet/Response.hpp"
#include "Parse.hpp" #include "VoidNet/Parse.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,4 +1,4 @@
#include "SecureSocket.hpp" #include "VoidNet/SecureSocket.hpp"
#include <cassert> #include <cassert>

View File

@ -1,5 +1,5 @@
#include "Socket.hpp" #include "VoidNet/Socket.hpp"
#include "IPAddress.hpp" #include "VoidNet/IPAddress.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,5 +1,5 @@
#include "TcpClient.hpp" #include "VoidNet/TcpClient.hpp"
#include "TcpSocketBuilder.hpp" #include "VoidNet/TcpSocketBuilder.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,7 +1,7 @@
#include "TcpListener.hpp" #include "VoidNet/TcpListener.hpp"
#include "TcpSocketBuilder.hpp" #include "VoidNet/TcpSocketBuilder.hpp"
#include "Socket.hpp" #include "VoidNet/Socket.hpp"
#include "TcpClient.hpp" #include "VoidNet/TcpClient.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,8 +1,8 @@
#include "TcpSocketBuilder.hpp" #include "VoidNet/TcpSocketBuilder.hpp"
#include "Socket.hpp" #include "VoidNet/Socket.hpp"
#include "TcpClient.hpp" #include "VoidNet/TcpClient.hpp"
#include "TcpListener.hpp" #include "VoidNet/TcpListener.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,4 +1,4 @@
#include "UdpSocket.hpp" #include "VoidNet/UdpSocket.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,7 +1,7 @@
// https://github.com/mfichman/http // https://github.com/mfichman/http
#include "Uri.hpp" #include "VoidNet/Uri.hpp"
#include "Parse.hpp" #include "VoidNet/Parse.hpp"
namespace std::net namespace std::net
{ {

View File

@ -1,4 +1,4 @@
#include "Util.hpp" #include "VoidNet/Util.hpp"
std::vector<std::string> std::net::Split(const std::string& str, const std::string& delimiter) std::vector<std::string> std::net::Split(const std::string& str, const std::string& delimiter)
{ {