diff --git a/assets b/assets index 6cbf2365..a3c92ac8 160000 --- a/assets +++ b/assets @@ -1 +1 @@ -Subproject commit 6cbf2365d49e6280750ea3bcd0f9c271779e6f15 +Subproject commit a3c92ac876dd061776c36d1594bd82264372f028 diff --git a/include/Engine/Network/Client.h b/include/Engine/Network/Client.h index 81b2b834..7aa20126 100644 --- a/include/Engine/Network/Client.h +++ b/include/Engine/Network/Client.h @@ -57,6 +57,7 @@ private: SnapshotDefinitions m_NextSnapshot; double m_DurationOfPingTime; std::clock_t m_StartPingTime; + std::vector m_InputCommandBuffer; // Private member functions void readFromServer(); @@ -75,6 +76,7 @@ private: void identifyPacketLoss(); bool isConnected(); EntityID createPlayer(); + void sendInputCommands(); // Mapping Logic // Returns if local EntityID exist in map bool clientServerMapsHasEntity(EntityID clientEntityID); diff --git a/include/Engine/Network/Server.h b/include/Engine/Network/Server.h index 4c71be95..9aba921a 100644 --- a/include/Engine/Network/Server.h +++ b/include/Engine/Network/Server.h @@ -73,6 +73,10 @@ private: void parseServerPing(); void identifyPacketLoss(); EntityID createPlayer(); + // Debug event + + EventRelay m_EInputCommand; + bool OnInputCommand(const Events::InputCommand& e); }; #endif diff --git a/src/Engine/Network/Client.cpp b/src/Engine/Network/Client.cpp index 55292234..e07bcf7a 100644 --- a/src/Engine/Network/Client.cpp +++ b/src/Engine/Network/Client.cpp @@ -25,6 +25,7 @@ void Client::Start(World* world, EventBroker* eventBroker) // Subscribe to events EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Client::OnInputCommand); + EVENT_SUBSCRIBE_MEMBER(m_EPlayeDamage, &Client::OnPlayerDamage); m_Socket.connect(m_ReceiverEndpoint); LOG_INFO("I am client. BIP BOP"); @@ -44,6 +45,7 @@ void Client::readFromServer() parseMessageType(packet); } } + sendInputCommands(); } void Client::parseMessageType(Packet& packet) @@ -92,7 +94,7 @@ void Client::parseConnect(Packet& packet) } void Client::parsePlayerConnected(Packet & packet) -{ +{ // Map ServerEntityID and other player's PlayerID LOG_INFO("A Player connected"); } @@ -235,12 +237,8 @@ bool Client::OnInputCommand(const Events::InputCommand & e) LOG_DEBUG("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID); return true; } else { - Packet packet(MessageType::OnInputCommand, m_SendPacketID); - packet.WriteString(e.Command); - packet.WritePrimitive(e.PlayerID); - packet.WritePrimitive(e.Value); - send(packet); - LOG_DEBUG("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID); + m_InputCommandBuffer.push_back(e); + LOG_INFO("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID); return true; } return false; @@ -285,6 +283,19 @@ EntityID Client::createPlayer() return entityID; } +void Client::sendInputCommands() +{ + if (m_InputCommandBuffer.size() > 0) { + Packet packet(MessageType::OnInputCommand, m_SendPacketID); + for (int i = 0; i < m_InputCommandBuffer.size(); i++) { + packet.WriteString(m_InputCommandBuffer[i].Command); + packet.WritePrimitive(m_InputCommandBuffer[i].Value); + } + send(packet); + m_InputCommandBuffer.clear(); + } +} + bool Client::clientServerMapsHasEntity(EntityID clientEntityID) { return m_ClientIDToServerID.find(clientEntityID) != m_ClientIDToServerID.end(); @@ -296,7 +307,7 @@ bool Client::serverClientMapsHasEntity(EntityID serverEntityID) } void Client::insertIntoServerClientMaps(EntityID serverEntityID, EntityID clientEntityID) -{ +{ m_ServerIDToClientID.insert(std::make_pair(serverEntityID, clientEntityID)); m_ClientIDToServerID.insert(std::make_pair(clientEntityID, serverEntityID)); diff --git a/src/Engine/Network/Server.cpp b/src/Engine/Network/Server.cpp index 62934f44..6324cb06 100644 --- a/src/Engine/Network/Server.cpp +++ b/src/Engine/Network/Server.cpp @@ -13,6 +13,8 @@ void Server::Start(World* world, EventBroker* eventBroker) { m_World = world; m_EventBroker = eventBroker; + // Subscribe to events + EVENT_SUBSCRIBE_MEMBER(m_EInputCommand, &Server::OnInputCommand); for (size_t i = 0; i < MAXCONNECTIONS; i++) { m_StopTimes[i] = std::clock(); } @@ -22,6 +24,7 @@ void Server::Start(World* world, EventBroker* eventBroker) void Server::Update() { readFromClients(); + m_EventBroker->Process(); } @@ -207,12 +210,26 @@ void Server::disconnect(int i) void Server::parseOnInputCommand(Packet& packet) { - Events::InputCommand e; - e.Command = packet.ReadString(); - e.PlayerID = packet.ReadPrimitive(); - e.Value = packet.ReadPrimitive(); - m_EventBroker->Publish(e); - LOG_DEBUG("Server::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID); + int playerID = -1; + // Check which player it was who sent the message + for (int i = 0; i < MAXCONNECTIONS; i++) { + // if the player is connected set playerID to the correct PlayerID + if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address() + && m_PlayerDefinitions[i].Endpoint.port() == m_ReceiverEndpoint.port()) { + playerID = i; + break; + } + } + if (playerID != -1) { + while (packet.DataReadSize() < packet.Size()) { + Events::InputCommand e; + e.Command = packet.ReadString(); + e.PlayerID = playerID; // Set correct player id + e.Value = packet.ReadPrimitive(); + m_EventBroker->Publish(e); + LOG_DEBUG("Server::parseOnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID); + } + } } void Server::parseOnPlayerDamage(Packet & packet) @@ -222,7 +239,7 @@ void Server::parseOnPlayerDamage(Packet & packet) e.PlayerDamagedID = packet.ReadPrimitive(); e.TypeOfDamage = packet.ReadString(); m_EventBroker->Publish(e); - LOG_DEBUG("Server::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.DamageAmount, e.PlayerDamagedID, e.TypeOfDamage.c_str()); + LOG_DEBUG("Server::parseOnPlayerDamage: Command is %s. Value is %f. PlayerID is %i.", e.DamageAmount, e.PlayerDamagedID, e.TypeOfDamage.c_str()); } void Server::parseConnect(Packet& packet) @@ -230,7 +247,8 @@ void Server::parseConnect(Packet& packet) LOG_INFO("Parsing connections"); // Check if player is already connected for (int i = 0; i < MAXCONNECTIONS; i++) { - if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address()) { + if (m_PlayerDefinitions[i].Endpoint.address() == m_ReceiverEndpoint.address() && + m_PlayerDefinitions[i].Endpoint.port() == m_ReceiverEndpoint.port()) { return; } } @@ -312,3 +330,9 @@ EntityID Server::createPlayer() ComponentWrapper player = m_World->AttachComponent(entityID, "Player"); return entityID; } + +bool Server::OnInputCommand(const Events::InputCommand & e) +{ + LOG_INFO("Server::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID); + return true; +}