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
This commit is contained in:
@ -23,10 +23,11 @@
|
||||
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp" />
|
||||
<ClInclude Include="..\..\include\HLAPI\DisconnectedEvent.hpp" />
|
||||
<ClInclude Include="..\..\include\HLAPI\InternalTags.hpp" />
|
||||
<ClInclude Include="..\..\include\HLAPI\MessageQueue.hpp" />
|
||||
<ClInclude Include="..\..\include\HLAPI\NetworkHeader.hpp" />
|
||||
<ClInclude Include="..\..\include\HLAPI\NetworkMessage.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\TcpConnection.hpp" />
|
||||
<ClInclude Include="..\..\include\HLAPI\TcpConnectionHandler.hpp" />
|
||||
@ -34,8 +35,8 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="..\..\src\HLAPI\main.cpp" />
|
||||
<ClCompile Include="..\..\src\HLAPI\MessageQueue.cpp" />
|
||||
<ClCompile Include="..\..\src\HLAPI\NetworkMessage.cpp" />
|
||||
<ClCompile Include="..\..\src\HLAPI\Plugin\PluginManager.cpp" />
|
||||
<ClCompile Include="..\..\src\HLAPI\Server.cpp" />
|
||||
<ClCompile Include="..\..\src\HLAPI\TcpConnection.cpp" />
|
||||
<ClCompile Include="..\..\src\HLAPI\TcpConnectionHandler.cpp" />
|
||||
@ -66,6 +67,7 @@
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
<UseOfMfc>Static</UseOfMfc>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
@ -99,8 +101,10 @@
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
<AdditionalIncludeDirectories>../../include/HLAPI;../../include/VoidNet</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>../../include</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
<PreprocessorDefinitions>%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
|
||||
@ -7,6 +7,12 @@
|
||||
<Filter Include="src">
|
||||
<UniqueIdentifier>{1c087ed8-3cca-4254-836f-10fb70fda154}</UniqueIdentifier>
|
||||
</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>
|
||||
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp">
|
||||
@ -18,9 +24,6 @@
|
||||
<ClInclude Include="..\..\include\HLAPI\InternalTags.hpp">
|
||||
<Filter>include</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\HLAPI\MessageQueue.hpp">
|
||||
<Filter>include</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\include\HLAPI\NetworkHeader.hpp">
|
||||
<Filter>include</Filter>
|
||||
</ClInclude>
|
||||
@ -45,14 +48,17 @@
|
||||
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp">
|
||||
<Filter>include</Filter>
|
||||
</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>
|
||||
<ClCompile Include="..\..\src\HLAPI\main.cpp">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\HLAPI\MessageQueue.cpp">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\HLAPI\NetworkMessage.cpp">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
@ -68,5 +74,8 @@
|
||||
<ClCompile Include="..\..\src\HLAPI\TcpServer.cpp">
|
||||
<Filter>src</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\HLAPI\Plugin\PluginManager.cpp">
|
||||
<Filter>src\Plugin</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@ -84,6 +84,7 @@
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v141</PlatformToolset>
|
||||
<CharacterSet>MultiByte</CharacterSet>
|
||||
<UseOfMfc>Static</UseOfMfc>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
@ -124,9 +125,10 @@
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<Optimization>Disabled</Optimization>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<AdditionalIncludeDirectories>../../include/VoidNet</AdditionalIncludeDirectories>
|
||||
<AdditionalIncludeDirectories>../../include</AdditionalIncludeDirectories>
|
||||
<LanguageStandard>stdcpplatest</LanguageStandard>
|
||||
<PreprocessorDefinitions>_WINSOCK_DEPRECATED_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
|
||||
</ClCompile>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "NetworkMessage.hpp"
|
||||
#include "HLAPI/NetworkMessage.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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;
|
||||
};
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "NetworkHeader.hpp"
|
||||
#include "ByteConverter.hpp"
|
||||
#include "HLAPI/NetworkHeader.hpp"
|
||||
#include "HLAPI/ByteConverter.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
@ -14,7 +14,7 @@ namespace std::net
|
||||
Others,
|
||||
OthersAndServer,
|
||||
All,
|
||||
AllAndMe,
|
||||
AllAndServer,
|
||||
Server
|
||||
};
|
||||
|
||||
@ -58,14 +58,14 @@ namespace std::net
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
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<uint32_t>(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<typename T>
|
||||
T *GetData() const
|
||||
|
||||
16
include/HLAPI/Plugin/Plugin.hpp
Normal file
16
include/HLAPI/Plugin/Plugin.hpp
Normal 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;
|
||||
};
|
||||
}
|
||||
29
include/HLAPI/Plugin/PluginManager.hpp
Normal file
29
include/HLAPI/Plugin/PluginManager.hpp
Normal 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*>();
|
||||
};
|
||||
}
|
||||
@ -1,14 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <Net.hpp>
|
||||
#include <VoidNet/Net.hpp>
|
||||
|
||||
#include <memory>
|
||||
|
||||
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<std::net::TcpServer> m_tcpServer;
|
||||
};
|
||||
|
||||
@ -2,9 +2,9 @@
|
||||
|
||||
#include <functional>
|
||||
|
||||
#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<void(uint32_t, void*)> NewConnectionEvent;
|
||||
|
||||
private:
|
||||
bool sendMessage(NetworkMessage &msg);
|
||||
bool sendMessage(const NetworkMessage &msg);
|
||||
|
||||
std::shared_ptr<TcpClient> m_client;
|
||||
uint32_t m_id;
|
||||
|
||||
@ -4,8 +4,9 @@
|
||||
#include <atomic>
|
||||
#include <queue>
|
||||
|
||||
#include <TcpListener.hpp>
|
||||
#include <TcpConnection.hpp>
|
||||
#include <VoidNet/TcpListener.hpp>
|
||||
#include <HLAPI/TcpConnection.hpp>
|
||||
#include <HLAPI/Plugin/PluginManager.hpp>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
@ -30,14 +31,15 @@ namespace std::net
|
||||
void AddClient(std::shared_ptr<TcpConnection> &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<std::shared_ptr<TcpConnection>> m_list;
|
||||
@ -54,6 +56,8 @@ namespace std::net
|
||||
|
||||
std::shared_ptr<TcpListener> m_listenerPtr;
|
||||
|
||||
std::shared_ptr<PluginManager> m_pluginManager;
|
||||
|
||||
std::vector<pollfd> poll_fds;
|
||||
};
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
#include <atomic>
|
||||
|
||||
#include "TcpListener.hpp"
|
||||
#include "VoidNet/TcpListener.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -2,8 +2,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Response.hpp"
|
||||
#include "Request.hpp"
|
||||
#include "VoidNet/Response.hpp"
|
||||
#include "VoidNet/Request.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <ostream>
|
||||
#include <string>
|
||||
|
||||
#include "Net.hpp"
|
||||
#include "VoidNet/Net.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -4,9 +4,9 @@
|
||||
#include <chrono>
|
||||
#include <memory>
|
||||
|
||||
#include "Net.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
#include "Enums.hpp"
|
||||
#include "VoidNet/Net.hpp"
|
||||
#include "VoidNet/IPAddress.hpp"
|
||||
#include "VoidNet/Enums.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
#include <ws2tcpip.h>
|
||||
#undef SendMessage
|
||||
#undef SetPort
|
||||
#undef max
|
||||
#undef min
|
||||
|
||||
#define poll WSAPoll
|
||||
#define ioctl ioctlsocket
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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 <string>
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
|
||||
//#include <openssl/rand.h>
|
||||
//#include <openssl/ssl.h>
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "Enums.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
#include "VoidNet/Enums.hpp"
|
||||
#include "VoidNet/IPAddress.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "UdpSocket.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
#include "VoidNet/UdpSocket.hpp"
|
||||
#include "VoidNet/IPAddress.hpp"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
#include "Net.hpp"
|
||||
#include "VoidNet/Net.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -1,4 +1,5 @@
|
||||
#include "NetworkMessage.hpp"
|
||||
#include "HLAPI/NetworkMessage.hpp"
|
||||
#include <cassert>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
11
src/HLAPI/Plugin/PluginManager.cpp
Normal file
11
src/HLAPI/Plugin/PluginManager.cpp
Normal file
@ -0,0 +1,11 @@
|
||||
#include "HLAPI/Plugin/PluginManager.hpp"
|
||||
|
||||
#include "HLAPI/Plugin/Plugin.hpp"
|
||||
|
||||
std::net::PluginManager::PluginManager()
|
||||
{
|
||||
}
|
||||
|
||||
std::net::PluginManager::~PluginManager()
|
||||
{
|
||||
}
|
||||
@ -1,7 +1,9 @@
|
||||
#include "Server.hpp"
|
||||
#include "HLAPI/Server.hpp"
|
||||
|
||||
#include "MessageQueue.hpp"
|
||||
#include "TcpServer.hpp"
|
||||
#include <HLAPI/Plugin/Plugin.hpp>
|
||||
|
||||
#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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
|
||||
@ -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 <chrono>
|
||||
|
||||
@ -16,7 +15,7 @@ namespace std::net
|
||||
TcpConnectionHandler::TcpConnectionHandler(std::shared_ptr<TcpListener> 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,44 +144,34 @@ namespace std::net
|
||||
else // not the listening socket
|
||||
{
|
||||
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;
|
||||
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<NetworkHeader> net_header((NetworkHeader*)header.get());
|
||||
std::unique_ptr<uint8_t> 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);
|
||||
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)
|
||||
{
|
||||
NetworkMessage msg = m_queue->DequeueMessageToSend();
|
||||
|
||||
if (msg.GetDistributionMode() == DistributionMode::Others)
|
||||
{
|
||||
m_listMutex.lock();
|
||||
@ -201,6 +190,8 @@ namespace std::net
|
||||
}
|
||||
else if (msg.GetDistributionMode() == DistributionMode::OthersAndServer)
|
||||
{
|
||||
HandlePluginMessage(msg);
|
||||
|
||||
m_listMutex.lock();
|
||||
for (int i = 0; i < m_list.size(); i++)
|
||||
{
|
||||
@ -214,8 +205,6 @@ namespace std::net
|
||||
}
|
||||
}
|
||||
m_listMutex.unlock();
|
||||
|
||||
//handle to plugins too
|
||||
}
|
||||
else if (msg.GetDistributionMode() == DistributionMode::ID)
|
||||
{
|
||||
@ -247,8 +236,10 @@ namespace std::net
|
||||
}
|
||||
m_listMutex.unlock();
|
||||
}
|
||||
else if (msg.GetDistributionMode() == DistributionMode::AllAndMe)
|
||||
else if (msg.GetDistributionMode() == DistributionMode::AllAndServer)
|
||||
{
|
||||
HandlePluginMessage(msg);
|
||||
|
||||
m_listMutex.lock();
|
||||
for (int i = 0; i < m_list.size(); i++)
|
||||
{
|
||||
@ -260,14 +251,9 @@ namespace std::net
|
||||
}
|
||||
}
|
||||
m_listMutex.unlock();
|
||||
|
||||
//handle to plugins too
|
||||
}
|
||||
else if (msg.GetDistributionMode() == DistributionMode::Server)
|
||||
{
|
||||
//handle just in plugins
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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 <iostream>
|
||||
|
||||
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<uint32_t>((uint8_t*)(message.GetData<void>()));
|
||||
if (message.GetTag() == (uint32_t)InternalTags::AssignID)
|
||||
std::cout << id << std::endl;
|
||||
}
|
||||
|
||||
while (client2.HasPendingData(data_size))
|
||||
{
|
||||
std::net::NetworkMessage message2;
|
||||
std::cout << id << std::endl;
|
||||
|
||||
uint8_t* bytes2 = new uint8_t[data_size]();
|
||||
std::string str = std::string("asd");
|
||||
std::net::NetworkMessage msg(100, std::net::DistributionMode::AllAndServer, 150, 1, &str, sizeof(str));
|
||||
|
||||
int32_t read2;
|
||||
client2.Recv(bytes2, data_size, read2);
|
||||
|
||||
message2.Deserialize(bytes2, data_size);
|
||||
|
||||
uint32_t id2 = std::ByteConverter::FromBytes<uint32_t>((uint8_t*)(message2.GetData<void>()));
|
||||
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::string>();
|
||||
std::cout << txt->c_str() << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,7 +1,7 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#include "Cookies.hpp"
|
||||
#include "Parse.hpp"
|
||||
#include "VoidNet/Cookies.hpp"
|
||||
#include "VoidNet/Parse.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#include "Headers.hpp"
|
||||
#include "VoidNet/Headers.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -1,13 +1,13 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#include "Http.hpp"
|
||||
#include "VoidNet/Http.hpp"
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <sstream>
|
||||
|
||||
#include "Socket.hpp"
|
||||
#include "SecureSocket.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
#include "VoidNet/SecureSocket.hpp"
|
||||
|
||||
#undef DELETE
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
#include "IPAddress.hpp"
|
||||
#include "VoidNet/IPAddress.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
// https://github.com/mfichman/http
|
||||
|
||||
#include "Request.hpp"
|
||||
#include "VoidNet/Request.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
#include "SecureSocket.hpp"
|
||||
#include "VoidNet/SecureSocket.hpp"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#include "Socket.hpp"
|
||||
#include "IPAddress.hpp"
|
||||
#include "VoidNet/Socket.hpp"
|
||||
#include "VoidNet/IPAddress.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
#include "TcpClient.hpp"
|
||||
#include "TcpSocketBuilder.hpp"
|
||||
#include "VoidNet/TcpClient.hpp"
|
||||
#include "VoidNet/TcpSocketBuilder.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
#include "UdpSocket.hpp"
|
||||
#include "VoidNet/UdpSocket.hpp"
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
|
||||
@ -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
|
||||
{
|
||||
|
||||
@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user