Update
This commit is contained in:
@ -20,15 +20,13 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp" />
|
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp" />
|
||||||
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp" />
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\DisconnectedEvent.hpp" />
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\InternalTags.hpp" />
|
<ClInclude Include="..\..\include\HLAPI\InternalTags.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\Plugin\Plugin.hpp" />
|
<ClInclude Include="..\..\include\HLAPI\Plugin\Plugin.hpp" />
|
||||||
<ClInclude Include="..\..\include\HLAPI\Plugin\PluginManager.hpp" />
|
<ClInclude Include="..\..\include\HLAPI\Plugin\PluginManager.hpp" />
|
||||||
<ClInclude Include="..\..\include\HLAPI\Server.hpp" />
|
<ClInclude Include="..\..\include\HLAPI\Server.hpp" />
|
||||||
|
<ClInclude Include="..\..\include\HLAPI\ServerConfig.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" />
|
||||||
<ClInclude Include="..\..\include\HLAPI\TcpServer.hpp" />
|
<ClInclude Include="..\..\include\HLAPI\TcpServer.hpp" />
|
||||||
|
|||||||
@ -13,26 +13,23 @@
|
|||||||
<Filter Include="src\Plugin">
|
<Filter Include="src\Plugin">
|
||||||
<UniqueIdentifier>{503a204a-1cb8-46cc-91f5-9f5f583d543d}</UniqueIdentifier>
|
<UniqueIdentifier>{503a204a-1cb8-46cc-91f5-9f5f583d543d}</UniqueIdentifier>
|
||||||
</Filter>
|
</Filter>
|
||||||
|
<Filter Include="include\Messages">
|
||||||
|
<UniqueIdentifier>{b431ca1a-333d-4242-93a0-c09caa38ff83}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="include\Events">
|
||||||
|
<UniqueIdentifier>{b03232e9-9ada-4c4a-91a2-8363c1a40714}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="src\Messages">
|
||||||
|
<UniqueIdentifier>{a0afa482-e925-4c73-8ea7-476fc9f9ec01}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="include\Config">
|
||||||
|
<UniqueIdentifier>{d97e32c8-5abd-4c76-81b6-7e71a96728ce}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
|
<Filter Include="src\Config">
|
||||||
|
<UniqueIdentifier>{26f7a155-a0ad-4bb4-9cee-da6c56fd73c5}</UniqueIdentifier>
|
||||||
|
</Filter>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="..\..\include\HLAPI\DataReceivedEvent.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\DisconnectedEvent.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\InternalTags.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\NetworkHeader.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\NetworkMessage.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\NewConnectionEvent.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\Server.hpp">
|
<ClInclude Include="..\..\include\HLAPI\Server.hpp">
|
||||||
<Filter>include</Filter>
|
<Filter>include</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
@ -45,23 +42,32 @@
|
|||||||
<ClInclude Include="..\..\include\HLAPI\TcpServer.hpp">
|
<ClInclude Include="..\..\include\HLAPI\TcpServer.hpp">
|
||||||
<Filter>include</Filter>
|
<Filter>include</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp">
|
|
||||||
<Filter>include</Filter>
|
|
||||||
</ClInclude>
|
|
||||||
<ClInclude Include="..\..\include\HLAPI\Plugin\Plugin.hpp">
|
<ClInclude Include="..\..\include\HLAPI\Plugin\Plugin.hpp">
|
||||||
<Filter>include\Plugin</Filter>
|
<Filter>include\Plugin</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
<ClInclude Include="..\..\include\HLAPI\Plugin\PluginManager.hpp">
|
<ClInclude Include="..\..\include\HLAPI\Plugin\PluginManager.hpp">
|
||||||
<Filter>include\Plugin</Filter>
|
<Filter>include\Plugin</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\include\HLAPI\NetworkHeader.hpp">
|
||||||
|
<Filter>include\Messages</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\include\HLAPI\NetworkMessage.hpp">
|
||||||
|
<Filter>include\Messages</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\include\HLAPI\ByteConverter.hpp">
|
||||||
|
<Filter>include\Messages</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\include\HLAPI\InternalTags.hpp">
|
||||||
|
<Filter>include\Messages</Filter>
|
||||||
|
</ClInclude>
|
||||||
|
<ClInclude Include="..\..\include\HLAPI\ServerConfig.hpp">
|
||||||
|
<Filter>include\Config</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\NetworkMessage.cpp">
|
|
||||||
<Filter>src</Filter>
|
|
||||||
</ClCompile>
|
|
||||||
<ClCompile Include="..\..\src\HLAPI\Server.cpp">
|
<ClCompile Include="..\..\src\HLAPI\Server.cpp">
|
||||||
<Filter>src</Filter>
|
<Filter>src</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
@ -77,5 +83,8 @@
|
|||||||
<ClCompile Include="..\..\src\HLAPI\Plugin\PluginManager.cpp">
|
<ClCompile Include="..\..\src\HLAPI\Plugin\PluginManager.cpp">
|
||||||
<Filter>src\Plugin</Filter>
|
<Filter>src\Plugin</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="..\..\src\HLAPI\NetworkMessage.cpp">
|
||||||
|
<Filter>src\Messages</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
@ -1,27 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include "HLAPI/NetworkMessage.hpp"
|
|
||||||
|
|
||||||
namespace std::net
|
|
||||||
{
|
|
||||||
class DataReceivedEvent
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
inline DataReceivedEvent(const NetworkMessage &msg)
|
|
||||||
{
|
|
||||||
SenderID = msg.GetSenderID();
|
|
||||||
DistributionMode = msg.GetDistributionMode();
|
|
||||||
DestinationID = msg.GetDestinationID();
|
|
||||||
Tag = msg.GetTag();
|
|
||||||
Data = msg.GetData<void>();
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
uint32_t SenderID;
|
|
||||||
DistributionMode DistributionMode;
|
|
||||||
uint32_t DestinationID;
|
|
||||||
uint32_t Tag;
|
|
||||||
|
|
||||||
void *Data;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,36 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace std::net
|
|
||||||
{
|
|
||||||
class DisconnectedEvent // can be processed by client and server
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
DisconnectedEvent();
|
|
||||||
|
|
||||||
public:
|
|
||||||
inline DisconnectedEvent(uint32_t id, char *reason, int32_t reason_id)
|
|
||||||
: m_reason(reason)
|
|
||||||
, m_clientID(id)
|
|
||||||
, m_reasonID(reason_id)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
inline DisconnectedEvent(uint32_t id, const std::string &reason, int32_t reason_id)
|
|
||||||
: m_reason(reason)
|
|
||||||
, m_clientID(id)
|
|
||||||
, m_reasonID(reason_id)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
inline const std::string &GetReason() const { return m_reason; }
|
|
||||||
inline int32_t GetReasonID() const { return m_reasonID; }
|
|
||||||
inline uint32_t GetID() const { return m_clientID; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
std::string m_reason;
|
|
||||||
int32_t m_reasonID;
|
|
||||||
uint32_t m_clientID;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
namespace std::net
|
|
||||||
{
|
|
||||||
class NewConnectionEvent
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
inline NewConnectionEvent(uint32_t id, void *data)
|
|
||||||
: m_id(id)
|
|
||||||
, m_data(data)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
uint32_t m_id;
|
|
||||||
void *m_data;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <VoidNet/Net.hpp>
|
#include <VoidNet/Net.hpp>
|
||||||
|
#include "HLAPI/ServerConfig.hpp"
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
@ -19,6 +20,9 @@ namespace std::net
|
|||||||
|
|
||||||
void AddPlugin(Plugin* plugin);
|
void AddPlugin(Plugin* plugin);
|
||||||
|
|
||||||
|
public:
|
||||||
|
ServerConfig Config;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<std::net::TcpServer> m_tcpServer;
|
std::shared_ptr<std::net::TcpServer> m_tcpServer;
|
||||||
};
|
};
|
||||||
|
|||||||
10
include/HLAPI/ServerConfig.hpp
Normal file
10
include/HLAPI/ServerConfig.hpp
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace std::net
|
||||||
|
{
|
||||||
|
class ServerConfig
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
bool BroadcastConnectionEvents;
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -13,23 +13,27 @@ namespace std::net
|
|||||||
public:
|
public:
|
||||||
friend class TcpConnectionHandler;
|
friend class TcpConnectionHandler;
|
||||||
|
|
||||||
|
TcpConnection();
|
||||||
TcpConnection(TcpClient *client);
|
TcpConnection(TcpClient *client);
|
||||||
|
|
||||||
std::shared_ptr<TcpClient> GetClient();
|
std::shared_ptr<TcpClient> GetClient();
|
||||||
uint32_t GetID();
|
uint32_t GetID();
|
||||||
void SetID(uint32_t id);
|
|
||||||
|
bool Connect(IPAddress addr);
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void SendMessage(DistributionMode mode, uint32_t destinationId, uint32_t tag, void *data)
|
void SendMessage(DistributionMode mode, uint32_t destinationId, uint32_t tag, void *data)
|
||||||
{
|
{
|
||||||
NetworkMessage msg(m_id, mode, destinationId, tag, data, sizeof(T));
|
NetworkMessage msg(m_id, mode, destinationId, tag, data, sizeof(T));
|
||||||
|
sendMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ReceiveData();
|
void ReceiveData();
|
||||||
|
|
||||||
std::function<void(uint32_t, DistributionMode, uint32_t, uint32_t, void*)> DataReceivedEvent;
|
std::function<void(uint32_t, DistributionMode, uint32_t, uint32_t, void*)> DataReceivedEvent;
|
||||||
std::function<void(uint32_t, std::string, int32_t)> DisconnectedEvent;
|
std::function<void(std::string)> DisconnectedEvent;
|
||||||
std::function<void(uint32_t, void*)> NewConnectionEvent;
|
std::function<void(uint32_t, void*)> NewConnectionEvent;
|
||||||
|
std::function<void()> OnConnectionEvent;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool sendMessage(const NetworkMessage &msg);
|
bool sendMessage(const NetworkMessage &msg);
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include <VoidNet/TcpListener.hpp>
|
#include <VoidNet/TcpListener.hpp>
|
||||||
#include <HLAPI/TcpConnection.hpp>
|
#include <HLAPI/TcpConnection.hpp>
|
||||||
#include <HLAPI/Plugin/PluginManager.hpp>
|
#include <HLAPI/Plugin/PluginManager.hpp>
|
||||||
|
#include <HLAPI/ServerConfig.hpp>
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
@ -58,6 +59,8 @@ namespace std::net
|
|||||||
|
|
||||||
std::shared_ptr<PluginManager> m_pluginManager;
|
std::shared_ptr<PluginManager> m_pluginManager;
|
||||||
|
|
||||||
std::vector<pollfd> poll_fds;
|
std::vector<pollfd> m_pollFds;
|
||||||
|
|
||||||
|
ServerConfig m_config;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -6,7 +6,9 @@
|
|||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
#include <ws2tcpip.h>
|
#include <ws2tcpip.h>
|
||||||
#undef SendMessage
|
#undef SendMessage
|
||||||
|
#undef GetMessage
|
||||||
#undef SetPort
|
#undef SetPort
|
||||||
|
#undef GetPort
|
||||||
#undef max
|
#undef max
|
||||||
#undef min
|
#undef min
|
||||||
|
|
||||||
|
|||||||
@ -8,9 +8,11 @@
|
|||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
Server::Server(uint32_t max_connections, uint16_t port)
|
Server::Server(uint32_t max_connections, uint16_t port) :
|
||||||
|
Config(ServerConfig())
|
||||||
{
|
{
|
||||||
m_tcpServer = std::make_shared<std::net::TcpServer>(max_connections, port);
|
m_tcpServer = std::make_shared<std::net::TcpServer>(max_connections, port);
|
||||||
|
m_tcpServer->m_connectionHandler->m_config = Config;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::Start()
|
void Server::Start()
|
||||||
|
|||||||
@ -2,8 +2,39 @@
|
|||||||
|
|
||||||
#include "HLAPI/InternalTags.hpp"
|
#include "HLAPI/InternalTags.hpp"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
namespace std::net
|
namespace std::net
|
||||||
{
|
{
|
||||||
|
void received(uint32_t, DistributionMode, uint32_t, uint32_t, void*)
|
||||||
|
{
|
||||||
|
std::cout << "received" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void disconnected(std::string s)
|
||||||
|
{
|
||||||
|
std::cout << s << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void new_connection(uint32_t, void*)
|
||||||
|
{
|
||||||
|
std::cout << "new client connection" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void on_connect()
|
||||||
|
{
|
||||||
|
std::cout << "i connected" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
TcpConnection::TcpConnection() :
|
||||||
|
m_client(new TcpClient())
|
||||||
|
{
|
||||||
|
DataReceivedEvent = received;
|
||||||
|
DisconnectedEvent = disconnected;
|
||||||
|
NewConnectionEvent = new_connection;
|
||||||
|
OnConnectionEvent = on_connect;
|
||||||
|
}
|
||||||
|
|
||||||
TcpConnection::TcpConnection(TcpClient * client)
|
TcpConnection::TcpConnection(TcpClient * client)
|
||||||
: m_client(client)
|
: m_client(client)
|
||||||
{
|
{
|
||||||
@ -19,9 +50,9 @@ namespace std::net
|
|||||||
return m_id;
|
return m_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TcpConnection::SetID(uint32_t id)
|
bool TcpConnection::Connect(IPAddress addr)
|
||||||
{
|
{
|
||||||
m_id = id;
|
return m_client->Connect(addr);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool TcpConnection::sendMessage(const NetworkMessage & msg)
|
bool TcpConnection::sendMessage(const NetworkMessage & msg)
|
||||||
@ -34,39 +65,40 @@ namespace std::net
|
|||||||
|
|
||||||
void TcpConnection::ReceiveData()
|
void TcpConnection::ReceiveData()
|
||||||
{
|
{
|
||||||
std::unique_ptr<uint8_t> header(new uint8_t[sizeof(NetworkHeader*)]());
|
uint32_t data_size;
|
||||||
|
while (m_client->HasPendingData(data_size))
|
||||||
int32_t read;
|
|
||||||
if (!m_client->Recv(header.get(), sizeof(NetworkHeader*), read))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (read == sizeof(NetworkHeader*))
|
|
||||||
{
|
{
|
||||||
std::unique_ptr<NetworkHeader> net_header((NetworkHeader*)header.get());
|
std::net::NetworkMessage message;
|
||||||
|
|
||||||
|
uint8_t* bytes = new uint8_t[data_size]();
|
||||||
|
|
||||||
std::unique_ptr<uint8_t> buffer(new uint8_t[net_header->Size]());
|
|
||||||
int32_t read;
|
int32_t read;
|
||||||
if (!m_client->Recv(buffer.get(), net_header->Size, read))
|
m_client->Recv(bytes, data_size, read);
|
||||||
|
|
||||||
|
message.Deserialize(bytes, data_size);
|
||||||
|
|
||||||
|
if (message.GetTag() == (uint32_t)InternalTags::Disconnect)
|
||||||
{
|
{
|
||||||
if (read != net_header->Size)
|
if (DisconnectedEvent)
|
||||||
return; // wrong message?
|
DisconnectedEvent(*(message.GetData<std::string>()));
|
||||||
|
}
|
||||||
NetworkMessage msg;
|
else if (message.GetTag() == (uint32_t)InternalTags::Connect)
|
||||||
msg.Deserialize(buffer.get(), net_header->Size);
|
{
|
||||||
|
if (NewConnectionEvent)
|
||||||
if (msg.GetTag() == (uint32_t)InternalTags::Disconnect)
|
NewConnectionEvent(message.GetSenderID(), message.GetData<void>());
|
||||||
{
|
}
|
||||||
//DisconnectedEvent(msg.m_senderID, );
|
else if (message.GetTag() == (uint32_t)InternalTags::AssignID)
|
||||||
}
|
{
|
||||||
else if (msg.GetTag() == (uint32_t)InternalTags::Connect)
|
m_id = *(message.GetData<uint32_t>());
|
||||||
NewConnectionEvent(msg.GetSenderID(), msg.GetData<void>());
|
|
||||||
else
|
if (OnConnectionEvent)
|
||||||
DataReceivedEvent(msg.GetSenderID(), msg.GetDistributionMode(), msg.GetDestinationID(), msg.GetTag(), msg.GetData<void>());
|
OnConnectionEvent();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (DataReceivedEvent)
|
||||||
|
DataReceivedEvent(message.GetSenderID(), message.GetDistributionMode(), message.GetDestinationID(), message.GetTag(), message.GetData<void>());
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else // wrong message
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,7 +1,5 @@
|
|||||||
#include "HLAPI/TcpConnectionHandler.hpp"
|
#include "HLAPI/TcpConnectionHandler.hpp"
|
||||||
|
|
||||||
#include "HLAPI/DisconnectedEvent.hpp"
|
|
||||||
#include "HLAPI/NewConnectionEvent.hpp"
|
|
||||||
#include "HLAPI/InternalTags.hpp"
|
#include "HLAPI/InternalTags.hpp"
|
||||||
|
|
||||||
#include "HLAPI/NetworkMessage.hpp"
|
#include "HLAPI/NetworkMessage.hpp"
|
||||||
@ -31,7 +29,7 @@ namespace std::net
|
|||||||
pollfd master_fd;
|
pollfd master_fd;
|
||||||
master_fd.fd = m_listenerPtr->m_socket->GetNativeSocket();
|
master_fd.fd = m_listenerPtr->m_socket->GetNativeSocket();
|
||||||
master_fd.events = POLLRDNORM;
|
master_fd.events = POLLRDNORM;
|
||||||
poll_fds.emplace_back(master_fd);
|
m_pollFds.emplace_back(master_fd);
|
||||||
|
|
||||||
std::thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this);
|
std::thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this);
|
||||||
m_receiveThread.swap(receive_thread);
|
m_receiveThread.swap(receive_thread);
|
||||||
@ -47,22 +45,20 @@ namespace std::net
|
|||||||
uint32_t id = GetAvailableID();
|
uint32_t id = GetAvailableID();
|
||||||
if (id == -1)
|
if (id == -1)
|
||||||
{
|
{
|
||||||
// this can be handled just by the server
|
|
||||||
// what if the server owner wants to know if a user wanted to join but couldnt
|
|
||||||
DisconnectedEvent disconnected_event(id, "Server Full", -1);
|
|
||||||
std::shared_ptr<TcpClient> client = c->GetClient();
|
std::shared_ptr<TcpClient> client = c->GetClient();
|
||||||
/*int32_t size = 0;
|
std::string reason("Server is full");
|
||||||
uint8_t *buffer = disconnected_event.Serialize(size);
|
NetworkMessage msg(0, DistributionMode::ID, 0, (uint32_t)InternalTags::Disconnect, &reason, sizeof(reason));
|
||||||
int32_t sent = 0;
|
uint32_t size;
|
||||||
client->Send(buffer, size, sent);*/
|
uint8_t* msgArr = msg.SerializeData(size);
|
||||||
|
int32_t sent;
|
||||||
|
client->Send(msgArr, size, sent);
|
||||||
client->Close();
|
client->Close();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
c->SetID(id);
|
c->m_id = id;
|
||||||
|
|
||||||
uint32_t *id_ptr = &id;
|
NetworkMessage msg(0, DistributionMode::ID, id, (uint32_t)InternalTags::AssignID, &id, sizeof(uint32_t));
|
||||||
|
|
||||||
NetworkMessage msg(-1, DistributionMode::ID, id, (uint32_t)InternalTags::AssignID, id_ptr, sizeof(id_ptr));
|
|
||||||
|
|
||||||
if (!c->sendMessage(msg))
|
if (!c->sendMessage(msg))
|
||||||
{
|
{
|
||||||
@ -77,7 +73,7 @@ namespace std::net
|
|||||||
pollfd client_fd;
|
pollfd client_fd;
|
||||||
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);
|
m_pollFds.emplace_back(client_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t TcpConnectionHandler::GetAvailableID()
|
uint32_t TcpConnectionHandler::GetAvailableID()
|
||||||
@ -113,7 +109,7 @@ namespace std::net
|
|||||||
|
|
||||||
void TcpConnectionHandler::HandleReceiveMsgAndConns()
|
void TcpConnectionHandler::HandleReceiveMsgAndConns()
|
||||||
{
|
{
|
||||||
int res = poll(poll_fds.data(), poll_fds.size(), -1);
|
int res = poll(m_pollFds.data(), m_pollFds.size(), -1);
|
||||||
|
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
{
|
{
|
||||||
@ -126,12 +122,12 @@ namespace std::net
|
|||||||
//timeout
|
//timeout
|
||||||
//}
|
//}
|
||||||
|
|
||||||
for (int i = 0; i < poll_fds.size(); i++)
|
for (int i = 0; i < m_pollFds.size(); i++)
|
||||||
{
|
{
|
||||||
if (poll_fds.at(i).revents == 0 || poll_fds[i].revents != POLLRDNORM)
|
if (m_pollFds.at(i).revents == 0 || m_pollFds[i].revents != POLLRDNORM)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (poll_fds.at(i).fd == m_listenerPtr->m_socket->GetNativeSocket())
|
if (m_pollFds.at(i).fd == m_listenerPtr->m_socket->GetNativeSocket())
|
||||||
{
|
{
|
||||||
TcpClient *c = m_listenerPtr->AcceptClient();
|
TcpClient *c = m_listenerPtr->AcceptClient();
|
||||||
if (c)
|
if (c)
|
||||||
@ -143,7 +139,7 @@ namespace std::net
|
|||||||
}
|
}
|
||||||
else // not the listening socket
|
else // not the listening socket
|
||||||
{
|
{
|
||||||
SOCKET c = poll_fds.at(i).fd;
|
SOCKET c = m_pollFds.at(i).fd;
|
||||||
|
|
||||||
uint8_t* header = new uint8_t[sizeof(NetworkHeader)]();
|
uint8_t* header = new uint8_t[sizeof(NetworkHeader)]();
|
||||||
|
|
||||||
@ -162,8 +158,9 @@ namespace std::net
|
|||||||
|
|
||||||
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);
|
m_pollFds.erase(m_pollFds.begin() + i);
|
||||||
|
|
||||||
|
// put this in a separate thread
|
||||||
HandleMessage(msg);
|
HandleMessage(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -172,6 +169,12 @@ namespace std::net
|
|||||||
|
|
||||||
void TcpConnectionHandler::HandleMessage(const NetworkMessage &msg)
|
void TcpConnectionHandler::HandleMessage(const NetworkMessage &msg)
|
||||||
{
|
{
|
||||||
|
if (msg.GetTag() == (uint32_t)InternalTags::Connect)
|
||||||
|
{
|
||||||
|
if (m_config.BroadcastConnectionEvents)
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (msg.GetDistributionMode() == DistributionMode::Others)
|
if (msg.GetDistributionMode() == DistributionMode::Others)
|
||||||
{
|
{
|
||||||
m_listMutex.lock();
|
m_listMutex.lock();
|
||||||
|
|||||||
@ -12,7 +12,7 @@ namespace std::net
|
|||||||
, m_port(port)
|
, m_port(port)
|
||||||
, m_run(false)
|
, m_run(false)
|
||||||
{
|
{
|
||||||
if (max_connections == 0 || port == 0)
|
if (port == 0)
|
||||||
throw std::invalid_argument("TcpServer::TcpServer()");
|
throw std::invalid_argument("TcpServer::TcpServer()");
|
||||||
|
|
||||||
listener = std::shared_ptr<TcpListener>(TcpSocketBuilder().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().BuildListener().release());
|
listener = std::shared_ptr<TcpListener>(TcpSocketBuilder().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().BuildListener().release());
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include "HLAPI/Plugin/Plugin.hpp"
|
#include "HLAPI/Plugin/Plugin.hpp"
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <HLAPI\TcpConnection.hpp>
|
||||||
|
|
||||||
class Plugin : public std::net::Plugin
|
class Plugin : public std::net::Plugin
|
||||||
{
|
{
|
||||||
@ -19,46 +20,15 @@ class Plugin : public std::net::Plugin
|
|||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::net::Initialize();
|
std::net::Initialize();
|
||||||
std::net::Server server(100);
|
std::net::Server server(0);
|
||||||
server.AddPlugin(new Plugin());
|
server.AddPlugin(new Plugin());
|
||||||
server.Start();
|
server.Start();
|
||||||
|
|
||||||
std::net::TcpClient client;
|
std::net::TcpConnection con;
|
||||||
std::net::IPAddress ip("127.0.0.1");
|
con.Connect(std::net::IPAddress("127.0.0.1"));
|
||||||
client.Connect(ip);
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
uint32_t data_size;
|
con.ReceiveData();
|
||||||
while (client.HasPendingData(data_size))
|
|
||||||
{
|
|
||||||
std::net::NetworkMessage message;
|
|
||||||
|
|
||||||
uint8_t *bytes = new uint8_t[data_size]();
|
|
||||||
|
|
||||||
int32_t read;
|
|
||||||
client.Recv(bytes, data_size, read);
|
|
||||||
|
|
||||||
message.Deserialize(bytes, data_size);
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
std::string str = std::string("asd");
|
|
||||||
std::net::NetworkMessage msg(100, std::net::DistributionMode::AllAndServer, 150, 1, &str, sizeof(str));
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user