Update
This commit is contained in:
@ -8,9 +8,11 @@
|
||||
|
||||
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->m_connectionHandler->m_config = Config;
|
||||
}
|
||||
|
||||
void Server::Start()
|
||||
|
||||
@ -2,9 +2,40 @@
|
||||
|
||||
#include "HLAPI/InternalTags.hpp"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace std::net
|
||||
{
|
||||
TcpConnection::TcpConnection(TcpClient * client)
|
||||
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)
|
||||
: m_client(client)
|
||||
{
|
||||
}
|
||||
@ -19,9 +50,9 @@ namespace std::net
|
||||
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)
|
||||
@ -34,39 +65,40 @@ namespace std::net
|
||||
|
||||
void TcpConnection::ReceiveData()
|
||||
{
|
||||
std::unique_ptr<uint8_t> header(new uint8_t[sizeof(NetworkHeader*)]());
|
||||
|
||||
int32_t read;
|
||||
if (!m_client->Recv(header.get(), sizeof(NetworkHeader*), read))
|
||||
return;
|
||||
|
||||
if (read == sizeof(NetworkHeader*))
|
||||
uint32_t data_size;
|
||||
while (m_client->HasPendingData(data_size))
|
||||
{
|
||||
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;
|
||||
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)
|
||||
return; // wrong message?
|
||||
|
||||
NetworkMessage msg;
|
||||
msg.Deserialize(buffer.get(), net_header->Size);
|
||||
|
||||
if (msg.GetTag() == (uint32_t)InternalTags::Disconnect)
|
||||
{
|
||||
//DisconnectedEvent(msg.m_senderID, );
|
||||
}
|
||||
else if (msg.GetTag() == (uint32_t)InternalTags::Connect)
|
||||
NewConnectionEvent(msg.GetSenderID(), msg.GetData<void>());
|
||||
else
|
||||
DataReceivedEvent(msg.GetSenderID(), msg.GetDistributionMode(), msg.GetDestinationID(), msg.GetTag(), msg.GetData<void>());
|
||||
if (DisconnectedEvent)
|
||||
DisconnectedEvent(*(message.GetData<std::string>()));
|
||||
}
|
||||
else if (message.GetTag() == (uint32_t)InternalTags::Connect)
|
||||
{
|
||||
if (NewConnectionEvent)
|
||||
NewConnectionEvent(message.GetSenderID(), message.GetData<void>());
|
||||
}
|
||||
else if (message.GetTag() == (uint32_t)InternalTags::AssignID)
|
||||
{
|
||||
m_id = *(message.GetData<uint32_t>());
|
||||
|
||||
if (OnConnectionEvent)
|
||||
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/DisconnectedEvent.hpp"
|
||||
#include "HLAPI/NewConnectionEvent.hpp"
|
||||
#include "HLAPI/InternalTags.hpp"
|
||||
|
||||
#include "HLAPI/NetworkMessage.hpp"
|
||||
@ -31,7 +29,7 @@ namespace std::net
|
||||
pollfd master_fd;
|
||||
master_fd.fd = m_listenerPtr->m_socket->GetNativeSocket();
|
||||
master_fd.events = POLLRDNORM;
|
||||
poll_fds.emplace_back(master_fd);
|
||||
m_pollFds.emplace_back(master_fd);
|
||||
|
||||
std::thread receive_thread(&TcpConnectionHandler::HandleReceiveMsgAndConnsThreaded, this);
|
||||
m_receiveThread.swap(receive_thread);
|
||||
@ -47,22 +45,20 @@ namespace std::net
|
||||
uint32_t id = GetAvailableID();
|
||||
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();
|
||||
/*int32_t size = 0;
|
||||
uint8_t *buffer = disconnected_event.Serialize(size);
|
||||
int32_t sent = 0;
|
||||
client->Send(buffer, size, sent);*/
|
||||
std::string reason("Server is full");
|
||||
NetworkMessage msg(0, DistributionMode::ID, 0, (uint32_t)InternalTags::Disconnect, &reason, sizeof(reason));
|
||||
uint32_t size;
|
||||
uint8_t* msgArr = msg.SerializeData(size);
|
||||
int32_t sent;
|
||||
client->Send(msgArr, size, sent);
|
||||
client->Close();
|
||||
return;
|
||||
}
|
||||
|
||||
c->SetID(id);
|
||||
c->m_id = id;
|
||||
|
||||
uint32_t *id_ptr = &id;
|
||||
|
||||
NetworkMessage msg(-1, DistributionMode::ID, id, (uint32_t)InternalTags::AssignID, id_ptr, sizeof(id_ptr));
|
||||
NetworkMessage msg(0, DistributionMode::ID, id, (uint32_t)InternalTags::AssignID, &id, sizeof(uint32_t));
|
||||
|
||||
if (!c->sendMessage(msg))
|
||||
{
|
||||
@ -77,7 +73,7 @@ namespace std::net
|
||||
pollfd client_fd;
|
||||
client_fd.fd = c->m_client->m_socket->GetNativeSocket();
|
||||
client_fd.events = POLLRDNORM;
|
||||
poll_fds.emplace_back(client_fd);
|
||||
m_pollFds.emplace_back(client_fd);
|
||||
}
|
||||
|
||||
uint32_t TcpConnectionHandler::GetAvailableID()
|
||||
@ -113,7 +109,7 @@ namespace std::net
|
||||
|
||||
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)
|
||||
{
|
||||
@ -126,12 +122,12 @@ namespace std::net
|
||||
//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;
|
||||
|
||||
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();
|
||||
if (c)
|
||||
@ -143,7 +139,7 @@ namespace std::net
|
||||
}
|
||||
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)]();
|
||||
|
||||
@ -162,8 +158,9 @@ namespace std::net
|
||||
|
||||
if (msg.GetTag() == (uint32_t)InternalTags::Disconnect)
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
@ -172,6 +169,12 @@ namespace std::net
|
||||
|
||||
void TcpConnectionHandler::HandleMessage(const NetworkMessage &msg)
|
||||
{
|
||||
if (msg.GetTag() == (uint32_t)InternalTags::Connect)
|
||||
{
|
||||
if (m_config.BroadcastConnectionEvents)
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.GetDistributionMode() == DistributionMode::Others)
|
||||
{
|
||||
m_listMutex.lock();
|
||||
|
||||
@ -12,7 +12,7 @@ namespace std::net
|
||||
, m_port(port)
|
||||
, m_run(false)
|
||||
{
|
||||
if (max_connections == 0 || port == 0)
|
||||
if (port == 0)
|
||||
throw std::invalid_argument("TcpServer::TcpServer()");
|
||||
|
||||
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 <iostream>
|
||||
#include <HLAPI\TcpConnection.hpp>
|
||||
|
||||
class Plugin : public std::net::Plugin
|
||||
{
|
||||
@ -19,46 +20,15 @@ class Plugin : public std::net::Plugin
|
||||
int main()
|
||||
{
|
||||
std::net::Initialize();
|
||||
std::net::Server server(100);
|
||||
std::net::Server server(0);
|
||||
server.AddPlugin(new Plugin());
|
||||
server.Start();
|
||||
|
||||
std::net::TcpClient client;
|
||||
std::net::IPAddress ip("127.0.0.1");
|
||||
client.Connect(ip);
|
||||
|
||||
std::net::TcpConnection con;
|
||||
con.Connect(std::net::IPAddress("127.0.0.1"));
|
||||
|
||||
while (true)
|
||||
{
|
||||
uint32_t data_size;
|
||||
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;
|
||||
}
|
||||
}
|
||||
con.ReceiveData();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user