From 0481a7d642b45492c0393c2eb1048727af704cf1 Mon Sep 17 00:00:00 2001 From: TheDoctor Date: Tue, 15 Oct 2019 19:03:35 +0100 Subject: [PATCH] Update --- HLAPI/HLAPI/HLAPI.vcxproj | 4 +- HLAPI/HLAPI/HLAPI.vcxproj.filters | 57 +++++++++------- include/HLAPI/DataReceivedEvent.hpp | 27 -------- include/HLAPI/DisconnectedEvent.hpp | 36 ---------- include/HLAPI/NewConnectionEvent.hpp | 18 ----- include/HLAPI/Server.hpp | 4 ++ include/HLAPI/ServerConfig.hpp | 10 +++ include/HLAPI/TcpConnection.hpp | 8 ++- include/HLAPI/TcpConnectionHandler.hpp | 5 +- include/VoidNet/Net.hpp | 2 + src/HLAPI/Server.cpp | 4 +- src/HLAPI/TcpConnection.cpp | 94 +++++++++++++++++--------- src/HLAPI/TcpConnectionHandler.cpp | 45 ++++++------ src/HLAPI/TcpServer.cpp | 2 +- src/HLAPI/main.cpp | 42 ++---------- 15 files changed, 157 insertions(+), 201 deletions(-) delete mode 100644 include/HLAPI/DataReceivedEvent.hpp delete mode 100644 include/HLAPI/DisconnectedEvent.hpp delete mode 100644 include/HLAPI/NewConnectionEvent.hpp create mode 100644 include/HLAPI/ServerConfig.hpp diff --git a/HLAPI/HLAPI/HLAPI.vcxproj b/HLAPI/HLAPI/HLAPI.vcxproj index adb13fe..0814672 100644 --- a/HLAPI/HLAPI/HLAPI.vcxproj +++ b/HLAPI/HLAPI/HLAPI.vcxproj @@ -20,15 +20,13 @@ - - - + diff --git a/HLAPI/HLAPI/HLAPI.vcxproj.filters b/HLAPI/HLAPI/HLAPI.vcxproj.filters index b7d3a00..fad8bcc 100644 --- a/HLAPI/HLAPI/HLAPI.vcxproj.filters +++ b/HLAPI/HLAPI/HLAPI.vcxproj.filters @@ -13,26 +13,23 @@ {503a204a-1cb8-46cc-91f5-9f5f583d543d} + + {b431ca1a-333d-4242-93a0-c09caa38ff83} + + + {b03232e9-9ada-4c4a-91a2-8363c1a40714} + + + {a0afa482-e925-4c73-8ea7-476fc9f9ec01} + + + {d97e32c8-5abd-4c76-81b6-7e71a96728ce} + + + {26f7a155-a0ad-4bb4-9cee-da6c56fd73c5} + - - include - - - include - - - include - - - include - - - include - - - include - include @@ -45,23 +42,32 @@ include - - include - include\Plugin include\Plugin + + include\Messages + + + include\Messages + + + include\Messages + + + include\Messages + + + include\Config + src - - src - src @@ -77,5 +83,8 @@ src\Plugin + + src\Messages + \ No newline at end of file diff --git a/include/HLAPI/DataReceivedEvent.hpp b/include/HLAPI/DataReceivedEvent.hpp deleted file mode 100644 index b8e695f..0000000 --- a/include/HLAPI/DataReceivedEvent.hpp +++ /dev/null @@ -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(); - } - - public: - uint32_t SenderID; - DistributionMode DistributionMode; - uint32_t DestinationID; - uint32_t Tag; - - void *Data; - }; -} \ No newline at end of file diff --git a/include/HLAPI/DisconnectedEvent.hpp b/include/HLAPI/DisconnectedEvent.hpp deleted file mode 100644 index b8943db..0000000 --- a/include/HLAPI/DisconnectedEvent.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#pragma once - -#include - -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; - }; -} \ No newline at end of file diff --git a/include/HLAPI/NewConnectionEvent.hpp b/include/HLAPI/NewConnectionEvent.hpp deleted file mode 100644 index 3a3ea37..0000000 --- a/include/HLAPI/NewConnectionEvent.hpp +++ /dev/null @@ -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; - }; -} \ No newline at end of file diff --git a/include/HLAPI/Server.hpp b/include/HLAPI/Server.hpp index c64ec6a..4a137fd 100644 --- a/include/HLAPI/Server.hpp +++ b/include/HLAPI/Server.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include "HLAPI/ServerConfig.hpp" #include @@ -19,6 +20,9 @@ namespace std::net void AddPlugin(Plugin* plugin); + public: + ServerConfig Config; + private: std::shared_ptr m_tcpServer; }; diff --git a/include/HLAPI/ServerConfig.hpp b/include/HLAPI/ServerConfig.hpp new file mode 100644 index 0000000..d6db342 --- /dev/null +++ b/include/HLAPI/ServerConfig.hpp @@ -0,0 +1,10 @@ +#pragma once + +namespace std::net +{ + class ServerConfig + { + public: + bool BroadcastConnectionEvents; + }; +} \ No newline at end of file diff --git a/include/HLAPI/TcpConnection.hpp b/include/HLAPI/TcpConnection.hpp index abfdc8f..5569a26 100644 --- a/include/HLAPI/TcpConnection.hpp +++ b/include/HLAPI/TcpConnection.hpp @@ -13,23 +13,27 @@ namespace std::net public: friend class TcpConnectionHandler; + TcpConnection(); TcpConnection(TcpClient *client); std::shared_ptr GetClient(); uint32_t GetID(); - void SetID(uint32_t id); + + bool Connect(IPAddress addr); template void SendMessage(DistributionMode mode, uint32_t destinationId, uint32_t tag, void *data) { NetworkMessage msg(m_id, mode, destinationId, tag, data, sizeof(T)); + sendMessage(msg); } void ReceiveData(); std::function DataReceivedEvent; - std::function DisconnectedEvent; + std::function DisconnectedEvent; std::function NewConnectionEvent; + std::function OnConnectionEvent; private: bool sendMessage(const NetworkMessage &msg); diff --git a/include/HLAPI/TcpConnectionHandler.hpp b/include/HLAPI/TcpConnectionHandler.hpp index ade9438..8a3ac8b 100644 --- a/include/HLAPI/TcpConnectionHandler.hpp +++ b/include/HLAPI/TcpConnectionHandler.hpp @@ -7,6 +7,7 @@ #include #include #include +#include namespace std::net { @@ -58,6 +59,8 @@ namespace std::net std::shared_ptr m_pluginManager; - std::vector poll_fds; + std::vector m_pollFds; + + ServerConfig m_config; }; } \ No newline at end of file diff --git a/include/VoidNet/Net.hpp b/include/VoidNet/Net.hpp index 674e651..e759cd2 100644 --- a/include/VoidNet/Net.hpp +++ b/include/VoidNet/Net.hpp @@ -6,7 +6,9 @@ #include #include #undef SendMessage + #undef GetMessage #undef SetPort + #undef GetPort #undef max #undef min diff --git a/src/HLAPI/Server.cpp b/src/HLAPI/Server.cpp index 8cccc61..e510ae6 100644 --- a/src/HLAPI/Server.cpp +++ b/src/HLAPI/Server.cpp @@ -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(max_connections, port); + m_tcpServer->m_connectionHandler->m_config = Config; } void Server::Start() diff --git a/src/HLAPI/TcpConnection.cpp b/src/HLAPI/TcpConnection.cpp index 5441e6d..8a30a40 100644 --- a/src/HLAPI/TcpConnection.cpp +++ b/src/HLAPI/TcpConnection.cpp @@ -2,9 +2,40 @@ #include "HLAPI/InternalTags.hpp" +#include + 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 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 net_header((NetworkHeader*)header.get()); + std::net::NetworkMessage message; + + uint8_t* bytes = new uint8_t[data_size](); - std::unique_ptr 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()); - else - DataReceivedEvent(msg.GetSenderID(), msg.GetDistributionMode(), msg.GetDestinationID(), msg.GetTag(), msg.GetData()); + if (DisconnectedEvent) + DisconnectedEvent(*(message.GetData())); + } + else if (message.GetTag() == (uint32_t)InternalTags::Connect) + { + if (NewConnectionEvent) + NewConnectionEvent(message.GetSenderID(), message.GetData()); + } + else if (message.GetTag() == (uint32_t)InternalTags::AssignID) + { + m_id = *(message.GetData()); + + if (OnConnectionEvent) + OnConnectionEvent(); + } + else + { + if (DataReceivedEvent) + DataReceivedEvent(message.GetSenderID(), message.GetDistributionMode(), message.GetDestinationID(), message.GetTag(), message.GetData()); } - } - else // wrong message - { - return; } } } \ No newline at end of file diff --git a/src/HLAPI/TcpConnectionHandler.cpp b/src/HLAPI/TcpConnectionHandler.cpp index 57227f5..8bc5e79 100644 --- a/src/HLAPI/TcpConnectionHandler.cpp +++ b/src/HLAPI/TcpConnectionHandler.cpp @@ -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 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(); diff --git a/src/HLAPI/TcpServer.cpp b/src/HLAPI/TcpServer.cpp index ce30ae8..e4507f9 100644 --- a/src/HLAPI/TcpServer.cpp +++ b/src/HLAPI/TcpServer.cpp @@ -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(TcpSocketBuilder().AsReusable().Bind(IPAddress(0, 0, 0, 0, port)).Listening().BuildListener().release()); diff --git a/src/HLAPI/main.cpp b/src/HLAPI/main.cpp index fda62ef..57c39bf 100644 --- a/src/HLAPI/main.cpp +++ b/src/HLAPI/main.cpp @@ -7,6 +7,7 @@ #include "HLAPI/Plugin/Plugin.hpp" #include +#include 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((uint8_t*)(message.GetData())); - 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::cout << txt->c_str() << std::endl; - } - } + con.ReceiveData(); } } \ No newline at end of file