Redid timers.

This commit is contained in:
Jocke
2016-03-12 21:38:46 +01:00
parent f8f536802f
commit b7d88808e2
4 changed files with 32 additions and 27 deletions
+6 -6
View File
@@ -77,10 +77,10 @@ private:
// Network logic // Network logic
PlayerDefinition m_PlayerDefinitions[8]; PlayerDefinition m_PlayerDefinitions[8];
SnapshotDefinitions m_NextSnapshot; SnapshotDefinitions m_NextSnapshot;
double m_DurationOfPingTime; double m_DurationOfPingTime = 0;
std::clock_t m_StartPingTime; double m_StartPingTime = 0;
std::clock_t m_TimeSinceSentInputs; double m_TimeSinceSentInputs = 0;
unsigned int m_SendInputIntervalMs; double m_SendInputInterval = 0.033;
std::vector<Events::InputCommand> m_InputCommandBuffer; std::vector<Events::InputCommand> m_InputCommandBuffer;
// Private member functions // Private member functions
@@ -141,8 +141,8 @@ private:
UDPClient m_ServerlistRequest; UDPClient m_ServerlistRequest;
std::vector<ServerInfo> m_Serverlist; std::vector<ServerInfo> m_Serverlist;
bool m_SearchingForServers = false; bool m_SearchingForServers = false;
std::clock_t m_StartSearchTime; double m_TimeSearched = 0;
double m_SearchingTime = 200; // Config I guess double m_SearchingTime = 0.2; // Config I guess
}; };
#endif #endif
+7 -7
View File
@@ -52,14 +52,14 @@ private:
char readBuffer[BUFFERSIZE] = { 0 }; char readBuffer[BUFFERSIZE] = { 0 };
size_t bytesRead = 0; size_t bytesRead = 0;
// time for previouse message // time for previouse message
std::clock_t previousePingMessage = std::clock(); double previousePingMessage = 0;
std::clock_t previousSnapshotMessage = std::clock(); double previousSnapshotMessage = 0;
std::clock_t timOutTimer = std::clock(); double timOutTimer = 0;
// How often we send messages (milliseconds) // How often we send messages (seconds)
float pingIntervalMs; double pingInterval = 1;
float snapshotInterval; double snapshotInterval = 0.05;
int checkTimeOutInterval = 100; double checkTimeOutInterval = 0.1;
int m_NextPlayerID = 0; int m_NextPlayerID = 0;
std::vector<Events::InputCommand> m_InputCommandsToBroadcast; std::vector<Events::InputCommand> m_InputCommandsToBroadcast;
//Timers //Timers
+8 -5
View File
@@ -12,7 +12,7 @@ Client::Client(World* world, EventBroker* eventBroker)
auto config = ResourceManager::Load<ConfigFile>("Config.ini"); auto config = ResourceManager::Load<ConfigFile>("Config.ini");
m_PlayerName = config->Get<std::string>("Networking.Name", "Raptorcopter"); m_PlayerName = config->Get<std::string>("Networking.Name", "Raptorcopter");
m_SendInputIntervalMs = config->Get<int>("Networking.SendInputIntervalMs", 33); m_SendInputInterval = config->Get<int>("Networking.SendInputIntervalMs", 33) / 1000.0;
LOG_INFO("Client initialized"); LOG_INFO("Client initialized");
m_ServerlistRequest.Connect(m_PlayerName, "192.168.1.255", 32554); m_ServerlistRequest.Connect(m_PlayerName, "192.168.1.255", 32554);
@@ -85,7 +85,9 @@ void Client::Update(double dt)
} }
if (m_SearchingForServers) { if (m_SearchingForServers) {
if (m_SearchingTime < (1000* (std::clock() - m_StartSearchTime) / (double)CLOCKS_PER_SEC)) { m_TimeSearched += dt;
if (m_SearchingTime < m_TimeSearched) {
m_TimeSearched = 0;
m_SearchingForServers = false; m_SearchingForServers = false;
//displayServerlist(); //displayServerlist();
Events::DisplayServerlist e; Events::DisplayServerlist e;
@@ -96,9 +98,10 @@ void Client::Update(double dt)
if (m_IsConnected) { if (m_IsConnected) {
// Don't send 1 input in 1 packet, bunch em up. // Don't send 1 input in 1 packet, bunch em up.
if (m_SendInputIntervalMs < (1000 * (std::clock() - m_TimeSinceSentInputs) / (double)CLOCKS_PER_SEC)) { m_TimeSinceSentInputs += dt;
if (m_SendInputInterval < m_TimeSinceSentInputs) {
sendInputCommands(); sendInputCommands();
m_TimeSinceSentInputs = std::clock(); m_TimeSinceSentInputs = 0;
} }
// HACK: Send absolute player positions for now to avoid desync until we have reliable messages // HACK: Send absolute player positions for now to avoid desync until we have reliable messages
sendLocalPlayerTransform(); sendLocalPlayerTransform();
@@ -593,7 +596,7 @@ bool Client::OnConnectRequest(const Events::ConnectRequest& e)
bool Client::OnSearchForServers(const Events::SearchForServers& e) bool Client::OnSearchForServers(const Events::SearchForServers& e)
{ {
m_SearchingForServers = true; m_SearchingForServers = true;
m_StartSearchTime = std::clock(); m_TimeSearched = 0;
m_Serverlist.clear(); m_Serverlist.clear();
Packet packet(MessageType::ServerlistRequest); Packet packet(MessageType::ServerlistRequest);
m_ServerlistRequest.Broadcast(packet, 13); // TODO: Config m_ServerlistRequest.Broadcast(packet, 13); // TODO: Config
+11 -9
View File
@@ -5,8 +5,8 @@ Server::Server(World* world, EventBroker* eventBroker, int port)
, m_ServerlistRequest(13) , m_ServerlistRequest(13)
{ {
ConfigFile* config = ResourceManager::Load<ConfigFile>("Config.ini"); ConfigFile* config = ResourceManager::Load<ConfigFile>("Config.ini");
snapshotInterval = 1000 * config->Get<float>("Networking.SnapshotInterval", 0.05f); snapshotInterval = config->Get<float>("Networking.SnapshotInterval", 0.05);
pingIntervalMs = config->Get<float>("Networking.PingIntervalMs", 1000); pingInterval = config->Get<float>("Networking.PingIntervalMs", 1000) / 1000.0;
m_ServerName = config->Get<std::string>("Networking.Name", "Unnamed"); m_ServerName = config->Get<std::string>("Networking.Name", "Unnamed");
// Subscribe to events // Subscribe to events
@@ -87,22 +87,24 @@ void Server::Update(double dt)
} }
m_PlayersToDisconnect.clear(); m_PlayersToDisconnect.clear();
std::clock_t currentTime = std::clock();
// Send snapshot // Send snapshot
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) { previousSnapshotMessage += dt;
if (snapshotInterval < previousSnapshotMessage) {
sendSnapshot(); sendSnapshot();
previousSnapshotMessage = currentTime; previousSnapshotMessage = 0;
} }
// Send pings each // Send pings each
if (pingIntervalMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) { previousePingMessage += dt;
if (pingInterval < previousePingMessage) {
sendPing(); sendPing();
previousePingMessage = currentTime; previousePingMessage = 0;
} }
// Time out logic // Time out logic
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) { timOutTimer += dt;
if (checkTimeOutInterval < timOutTimer) {
checkForTimeOuts(); checkForTimeOuts();
timOutTimer = currentTime; timOutTimer = 0;
} }
m_EventBroker->Process<Server>(); m_EventBroker->Process<Server>();
if (isReadingData) { if (isReadingData) {