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