Merge remote-tracking branch 'origin/Networking' into Networking

This commit is contained in:
stiffly
2016-01-18 11:06:51 +01:00
5 changed files with 58 additions and 17 deletions
+1 -1
Submodule assets updated: 6cbf2365d4...a3c92ac876
+2
View File
@@ -57,6 +57,7 @@ private:
SnapshotDefinitions m_NextSnapshot;
double m_DurationOfPingTime;
std::clock_t m_StartPingTime;
std::vector<Events::InputCommand> 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);
+4
View File
@@ -73,6 +73,10 @@ private:
void parseServerPing();
void identifyPacketLoss();
EntityID createPlayer();
// Debug event
EventRelay<Server, Events::InputCommand> m_EInputCommand;
bool OnInputCommand(const Events::InputCommand& e);
};
#endif
+19 -8
View File
@@ -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));
+32 -8
View File
@@ -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<Server>();
}
@@ -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<int>();
e.Value = packet.ReadPrimitive<float>();
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<float>();
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<EntityID>();
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;
}