WIP Timestamps
This commit is contained in:
@@ -16,6 +16,8 @@ struct InputCommand : Event
|
|||||||
std::string Command;
|
std::string Command;
|
||||||
/** The value of the command. */
|
/** The value of the command. */
|
||||||
float Value = 0;
|
float Value = 0;
|
||||||
|
/** Timestamp of the command. */
|
||||||
|
double TimeStamp = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ public:
|
|||||||
~Client();
|
~Client();
|
||||||
|
|
||||||
void Connect(std::string address, int port);
|
void Connect(std::string address, int port);
|
||||||
void Update() override;
|
void Update(double dt) override;
|
||||||
|
private:
|
||||||
std::vector<Events::PlayerSpawned> m_PlayerSpawnEvents;
|
std::vector<Events::PlayerSpawned> m_PlayerSpawnEvents;
|
||||||
void parseSpawnEvents();
|
void parseSpawnEvents();
|
||||||
// Save for children
|
// Save for children
|
||||||
@@ -56,13 +56,13 @@ public:
|
|||||||
bool m_IsConnected = false;
|
bool m_IsConnected = false;
|
||||||
EntityWrapper m_LocalPlayer = EntityWrapper::Invalid;
|
EntityWrapper m_LocalPlayer = EntityWrapper::Invalid;
|
||||||
// Server Client Lookup map
|
// Server Client Lookup map
|
||||||
// Assumes that root node for client and server is EntityID 0.
|
|
||||||
|
|
||||||
// Don't Add items to these two maps with insert, use insertIntoServerClientMaps(EntityID, EntityID)!!!!
|
// Don't Add items to these two maps with insert, use insertIntoServerClientMaps(EntityID, EntityID)!!!!
|
||||||
std::unordered_map<EntityID, EntityID> m_ServerIDToClientID;
|
std::unordered_map<EntityID, EntityID> m_ServerIDToClientID;
|
||||||
std::unordered_map<EntityID, EntityID> m_ClientIDToServerID;
|
std::unordered_map<EntityID, EntityID> m_ClientIDToServerID;
|
||||||
|
|
||||||
// Network logic
|
// Network logic
|
||||||
|
UDPClient m_Unreliable;
|
||||||
|
TCPClient m_Reliable;
|
||||||
PlayerDefinition m_PlayerDefinitions[8];
|
PlayerDefinition m_PlayerDefinitions[8];
|
||||||
SnapshotDefinitions m_NextSnapshot;
|
SnapshotDefinitions m_NextSnapshot;
|
||||||
double m_DurationOfPingTime;
|
double m_DurationOfPingTime;
|
||||||
@@ -70,9 +70,9 @@ public:
|
|||||||
std::clock_t m_TimeSinceSentInputs;
|
std::clock_t m_TimeSinceSentInputs;
|
||||||
unsigned int m_SendInputIntervalMs;
|
unsigned int m_SendInputIntervalMs;
|
||||||
std::vector<Events::InputCommand> m_InputCommandBuffer;
|
std::vector<Events::InputCommand> m_InputCommandBuffer;
|
||||||
|
std::vector<Events::InputCommand> m_ReceivedInputCommands;
|
||||||
|
|
||||||
// Private member functions
|
// Private member functions
|
||||||
size_t receive(char* data);
|
|
||||||
void disconnect();
|
void disconnect();
|
||||||
void parseMessageType(Packet& packet);
|
void parseMessageType(Packet& packet);
|
||||||
void updateFields(Packet& packet, const ComponentInfo& componentInfo, const EntityID& entityID);
|
void updateFields(Packet& packet, const ComponentInfo& componentInfo, const EntityID& entityID);
|
||||||
@@ -88,6 +88,8 @@ public:
|
|||||||
void parseComponentDeletion(Packet& packet);
|
void parseComponentDeletion(Packet& packet);
|
||||||
void InterpolateFields(Packet & packet, const ComponentInfo & componentInfo, const EntityID & entityID, const std::string & componentType);
|
void InterpolateFields(Packet & packet, const ComponentInfo & componentInfo, const EntityID & entityID, const std::string & componentType);
|
||||||
void parseSnapshot(Packet& packet);
|
void parseSnapshot(Packet& packet);
|
||||||
|
void parseOnInputCommand(Packet& packet);
|
||||||
|
void publishInputCommands(double dt);
|
||||||
void identifyPacketLoss();
|
void identifyPacketLoss();
|
||||||
void hasServerTimedOut();
|
void hasServerTimedOut();
|
||||||
EntityID createPlayer();
|
EntityID createPlayer();
|
||||||
@@ -110,9 +112,6 @@ public:
|
|||||||
EventRelay<Client, Events::PlayerSpawned> m_EPlayerSpawned;
|
EventRelay<Client, Events::PlayerSpawned> m_EPlayerSpawned;
|
||||||
bool OnPlayerSpawned(const Events::PlayerSpawned& e);
|
bool OnPlayerSpawned(const Events::PlayerSpawned& e);
|
||||||
void parsePlayerDamage(Packet& packet);
|
void parsePlayerDamage(Packet& packet);
|
||||||
private:
|
|
||||||
UDPClient m_Unreliable;
|
|
||||||
TCPClient m_Reliable;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -22,11 +22,13 @@ public:
|
|||||||
Network(World* world, EventBroker* eventBroker);
|
Network(World* world, EventBroker* eventBroker);
|
||||||
virtual ~Network() { };
|
virtual ~Network() { };
|
||||||
|
|
||||||
virtual void Update() = 0;
|
virtual void Update(double dt) = 0;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
World* m_World;
|
World* m_World;
|
||||||
EventBroker* m_EventBroker;
|
EventBroker* m_EventBroker;
|
||||||
|
// for network
|
||||||
|
double m_TimeStamp = 0;
|
||||||
|
|
||||||
// For Debug
|
// For Debug
|
||||||
bool isReadingData = false;
|
bool isReadingData = false;
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ public:
|
|||||||
Server(World* world, EventBroker* eventBroker, int port);
|
Server(World* world, EventBroker* eventBroker, int port);
|
||||||
~Server();
|
~Server();
|
||||||
|
|
||||||
void Update() override;
|
void Update(double dt) override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// Network channels
|
// Network channels
|
||||||
|
|||||||
@@ -41,9 +41,11 @@ void Client::Connect(std::string address, int port)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::Update()
|
void Client::Update(double dt)
|
||||||
{
|
{
|
||||||
m_EventBroker->Process<Client>();
|
m_EventBroker->Process<Client>();
|
||||||
|
m_TimeStamp += dt;
|
||||||
|
publishInputCommands(dt);
|
||||||
while (m_Unreliable.IsSocketAvailable()) {
|
while (m_Unreliable.IsSocketAvailable()) {
|
||||||
// Packet will get real data in receive
|
// Packet will get real data in receive
|
||||||
Packet packet(MessageType::Invalid);
|
Packet packet(MessageType::Invalid);
|
||||||
@@ -122,6 +124,9 @@ void Client::parseMessageType(Packet& packet)
|
|||||||
case MessageType::OnPlayerDamage:
|
case MessageType::OnPlayerDamage:
|
||||||
parsePlayerDamage(packet);
|
parsePlayerDamage(packet);
|
||||||
break;
|
break;
|
||||||
|
case MessageType::OnInputCommand:
|
||||||
|
//parsePlayerDamage(packet);
|
||||||
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -284,21 +289,23 @@ void Client::ignoreFields(Packet& packet, const ComponentInfo& componentInfo)
|
|||||||
|
|
||||||
void Client::parseSnapshot(Packet& packet)
|
void Client::parseSnapshot(Packet& packet)
|
||||||
{
|
{
|
||||||
// Read input commands
|
//// Read input commands
|
||||||
std::size_t numInputCommands = packet.ReadPrimitive<std::size_t>();
|
//std::size_t numInputCommands = packet.ReadPrimitive<std::size_t>();
|
||||||
for (std::size_t i = 0; i < numInputCommands; ++i) {
|
//for (std::size_t i = 0; i < numInputCommands; ++i) {
|
||||||
Events::InputCommand e;
|
// Events::InputCommand e;
|
||||||
e.PlayerID = packet.ReadPrimitive<EntityID>();
|
// e.PlayerID = packet.ReadPrimitive<EntityID>();
|
||||||
EntityID player = packet.ReadPrimitive<EntityID>();
|
// EntityID player = packet.ReadPrimitive<EntityID>();
|
||||||
std::string command = packet.ReadString();
|
// std::string command = packet.ReadString();
|
||||||
float value = packet.ReadPrimitive<float>();
|
// float value = packet.ReadPrimitive<float>();
|
||||||
if (m_ServerIDToClientID.find(player) != m_ServerIDToClientID.end()) {
|
// double timestamp = packet.ReadPrimitive<double>();
|
||||||
e.Player = EntityWrapper(m_World, m_ServerIDToClientID.at(player));
|
// if (m_ServerIDToClientID.find(player) != m_ServerIDToClientID.end()) {
|
||||||
e.Command = command;
|
// e.Player = EntityWrapper(m_World, m_ServerIDToClientID.at(player));
|
||||||
e.Value = value;
|
// e.Command = command;
|
||||||
m_EventBroker->Publish(e);
|
// e.Value = value;
|
||||||
}
|
// e.TimeStamp = timestamp;
|
||||||
}
|
// m_EventBroker->Publish(e);
|
||||||
|
// }
|
||||||
|
//}
|
||||||
|
|
||||||
// Read world state
|
// Read world state
|
||||||
while (packet.DataReadSize() < packet.Size()) {
|
while (packet.DataReadSize() < packet.Size()) {
|
||||||
@@ -359,6 +366,36 @@ void Client::parseSnapshot(Packet& packet)
|
|||||||
parseSpawnEvents();
|
parseSpawnEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Client::parseOnInputCommand(Packet & packet)
|
||||||
|
{
|
||||||
|
while (packet.DataReadSize() < packet.Size()) {
|
||||||
|
Events::InputCommand e;
|
||||||
|
e.PlayerID = packet.ReadPrimitive<int>();
|
||||||
|
e.Player = EntityWrapper(m_World, packet.ReadPrimitive<int>());
|
||||||
|
e.Command = packet.ReadString();
|
||||||
|
e.Value = packet.ReadPrimitive<float>();
|
||||||
|
e.TimeStamp = packet.ReadPrimitive<double>();
|
||||||
|
m_EventBroker->Publish(e);
|
||||||
|
m_ReceivedInputCommands.push_back(e);
|
||||||
|
//LOG_INFO("Server::parseOnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Client::publishInputCommands(double dt)
|
||||||
|
{
|
||||||
|
std::vector<Events::InputCommand> notPublishedEvents;
|
||||||
|
for (int i = 0; i < m_ReceivedInputCommands.size(); i++) {
|
||||||
|
if (m_ReceivedInputCommands.at(i).TimeStamp < m_TimeStamp) {
|
||||||
|
m_EventBroker->Publish(m_ReceivedInputCommands.at(i).TimeStamp);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
notPublishedEvents.push_back(m_ReceivedInputCommands.at(i));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_ReceivedInputCommands = notPublishedEvents;
|
||||||
|
}
|
||||||
|
|
||||||
void Client::disconnect()
|
void Client::disconnect()
|
||||||
{
|
{
|
||||||
m_IsConnected = false;
|
m_IsConnected = false;
|
||||||
@@ -402,7 +439,9 @@ bool Client::OnInputCommand(const Events::InputCommand & e)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (m_IsConnected) {
|
if (m_IsConnected) {
|
||||||
m_InputCommandBuffer.push_back(e);
|
Events::InputCommand setTimestamp = e;
|
||||||
|
setTimestamp.TimeStamp = m_TimeStamp;
|
||||||
|
m_InputCommandBuffer.push_back(setTimestamp);
|
||||||
}
|
}
|
||||||
//LOG_DEBUG("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID);
|
//LOG_DEBUG("Client::OnInputCommand: Command is %s. Value is %f. PlayerID is %i.", e.Command.c_str(), e.Value, e.PlayerID);
|
||||||
return true;
|
return true;
|
||||||
@@ -516,6 +555,7 @@ void Client::sendInputCommands()
|
|||||||
for (int i = 0; i < m_InputCommandBuffer.size(); i++) {
|
for (int i = 0; i < m_InputCommandBuffer.size(); i++) {
|
||||||
packet.WriteString(m_InputCommandBuffer[i].Command);
|
packet.WriteString(m_InputCommandBuffer[i].Command);
|
||||||
packet.WritePrimitive(m_InputCommandBuffer[i].Value);
|
packet.WritePrimitive(m_InputCommandBuffer[i].Value);
|
||||||
|
packet.WritePrimitive(m_InputCommandBuffer[i].TimeStamp);
|
||||||
}
|
}
|
||||||
m_Reliable.Send(packet);
|
m_Reliable.Send(packet);
|
||||||
m_InputCommandBuffer.clear();
|
m_InputCommandBuffer.clear();
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ Network::Network(World* world, EventBroker* eventBroker)
|
|||||||
m_TimeoutMs = config->Get<int>("Networking.TimeoutMs", 20000);
|
m_TimeoutMs = config->Get<int>("Networking.TimeoutMs", 20000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Network::Update()
|
void Network::Update(double dt)
|
||||||
{
|
{
|
||||||
updateNetworkData();
|
updateNetworkData();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,10 +26,11 @@ Server::~Server()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::Update()
|
void Server::Update(double dt)
|
||||||
{
|
{
|
||||||
|
m_EventBroker->Process<Server>();
|
||||||
|
m_TimeStamp += dt;
|
||||||
PlayerDefinition pd;
|
PlayerDefinition pd;
|
||||||
|
|
||||||
m_Reliable.AcceptNewConnections(m_NextPlayerID, m_ConnectedPlayers);
|
m_Reliable.AcceptNewConnections(m_NextPlayerID, m_ConnectedPlayers);
|
||||||
for (auto& kv : m_ConnectedPlayers) {
|
for (auto& kv : m_ConnectedPlayers) {
|
||||||
while (kv.second.TCPSocket->available()) {
|
while (kv.second.TCPSocket->available()) {
|
||||||
@@ -80,9 +81,8 @@ void Server::Update()
|
|||||||
checkForTimeOuts();
|
checkForTimeOuts();
|
||||||
timOutTimer = currentTime;
|
timOutTimer = currentTime;
|
||||||
}
|
}
|
||||||
m_EventBroker->Process<Server>();
|
|
||||||
if (isReadingData) {
|
if (isReadingData) {
|
||||||
Network::Update();
|
Network::Update(dt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,7 +145,7 @@ void Server::unreliableBroadcast(Packet& packet)
|
|||||||
void Server::sendSnapshot()
|
void Server::sendSnapshot()
|
||||||
{
|
{
|
||||||
Packet packet(MessageType::Snapshot);
|
Packet packet(MessageType::Snapshot);
|
||||||
addInputCommandsToPacket(packet);
|
//addInputCommandsToPacket(packet);
|
||||||
addChildrenToPacket(packet, EntityID_Invalid);
|
addChildrenToPacket(packet, EntityID_Invalid);
|
||||||
unreliableBroadcast(packet);
|
unreliableBroadcast(packet);
|
||||||
}
|
}
|
||||||
@@ -159,6 +159,7 @@ void Server::addInputCommandsToPacket(Packet& packet)
|
|||||||
packet.WritePrimitive(m_ConnectedPlayers.at(command.PlayerID).EntityID);
|
packet.WritePrimitive(m_ConnectedPlayers.at(command.PlayerID).EntityID);
|
||||||
packet.WriteString(command.Command);
|
packet.WriteString(command.Command);
|
||||||
packet.WritePrimitive(command.Value);
|
packet.WritePrimitive(command.Value);
|
||||||
|
packet.WritePrimitive(command.TimeStamp);
|
||||||
}
|
}
|
||||||
m_InputCommandsToBroadcast.clear();
|
m_InputCommandsToBroadcast.clear();
|
||||||
}
|
}
|
||||||
@@ -465,7 +466,9 @@ void Server::parseOnInputCommand(Packet& packet)
|
|||||||
e.PlayerID = player; // Set correct player id
|
e.PlayerID = player; // Set correct player id
|
||||||
e.Player = EntityWrapper(m_World, m_ConnectedPlayers.at(player).EntityID);
|
e.Player = EntityWrapper(m_World, m_ConnectedPlayers.at(player).EntityID);
|
||||||
e.Value = packet.ReadPrimitive<float>();
|
e.Value = packet.ReadPrimitive<float>();
|
||||||
m_EventBroker->Publish(e);
|
e.TimeStamp = packet.ReadPrimitive<double>();
|
||||||
|
/* m_EventBroker->Publish(e);*/
|
||||||
|
|
||||||
if (e.Command == "PrimaryFire" || e.Command == "Reload") {
|
if (e.Command == "PrimaryFire" || e.Command == "Reload") {
|
||||||
m_InputCommandsToBroadcast.push_back(e);
|
m_InputCommandsToBroadcast.push_back(e);
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -199,10 +199,10 @@ void Game::Tick()
|
|||||||
// Update network
|
// Update network
|
||||||
m_EventBroker->Process<MultiplayerSnapshotFilter>();
|
m_EventBroker->Process<MultiplayerSnapshotFilter>();
|
||||||
if (m_NetworkClient != nullptr) {
|
if (m_NetworkClient != nullptr) {
|
||||||
m_NetworkClient->Update();
|
m_NetworkClient->Update(dt);
|
||||||
}
|
}
|
||||||
if (m_NetworkServer != nullptr) {
|
if (m_NetworkServer != nullptr) {
|
||||||
m_NetworkServer->Update();
|
m_NetworkServer->Update(dt);
|
||||||
}
|
}
|
||||||
//m_SoundManager->Update(dt);
|
//m_SoundManager->Update(dt);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user