diff --git a/include/Engine/Network/Client.h b/include/Engine/Network/Client.h index 2e03f981..0ddc03dc 100644 --- a/include/Engine/Network/Client.h +++ b/include/Engine/Network/Client.h @@ -23,7 +23,6 @@ public: ~Client(); void Start(World* world, EventBroker* eventBroker); void Close(); - private: // Threaded void ReadFromServer(); @@ -50,12 +49,14 @@ private: World* m_World; int m_PlayerID = -1; glm::vec2 m_PlayerPositions[MAXCONNECTIONS]; - //std::string m_PlayerNames[MAXCONNECTIONS]; PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS]; std::clock_t m_StartPingTime; double m_DurationOfPingTime; std::string m_PlayerName; bool m_ThreadIsRunning = true; + // Use to check if we should send disconnect message + // if game is turned of by closing window. + bool m_WasStarted = false; // Events EventBroker* m_EventBroker; diff --git a/src/Engine/Network/Client.cpp b/src/Engine/Network/Client.cpp index 5efa796e..17bcde7e 100644 --- a/src/Engine/Network/Client.cpp +++ b/src/Engine/Network/Client.cpp @@ -18,10 +18,11 @@ Client::~Client() void Client::Start(World* world, EventBroker* eventBroker) { // Subscribe to events - m_EventBroker = eventBroker; + m_WasStarted = true; + m_EventBroker = eventBroker; m_World = world; m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Client::OnKeyDown, this, std::placeholders::_1)); - m_EventBroker->Subscribe(m_EKeyDown); + m_EventBroker->Subscribe(m_EKeyDown); std::cout << "Please enter you name: "; std::cin >> m_PlayerName; while (m_PlayerName.size() > 7) { @@ -30,15 +31,16 @@ void Client::Start(World* world, EventBroker* eventBroker) } m_Socket.connect(m_ReceiverEndpoint); std::cout << "I am client. BIP BOP\n"; - ReadFromServer(); } void Client::Close() { - Disconnect(); - m_ThreadIsRunning = false; - m_Socket.close(); + if (m_WasStarted) { + Disconnect(); + m_ThreadIsRunning = false; + m_EventBroker->Unsubscribe(m_EKeyDown); + } } void Client::ReadFromServer() diff --git a/src/Engine/Network/Server.cpp b/src/Engine/Network/Server.cpp index 2e95c0f6..3dece632 100644 --- a/src/Engine/Network/Server.cpp +++ b/src/Engine/Network/Server.cpp @@ -28,36 +28,11 @@ void Server::Start(World* world) void Server::DisplayLoop() { - int lengthOfMessage = -1; - std::clock_t previousePingMessage = std::clock(); - std::clock_t previousSnapshotMessage = std::clock(); - std::clock_t timOutTimer = std::clock(); - int intervallMs = 1000; - int snapshotInterval = 50; - int timeToCheckTimeOutTime = 100; - char* data; + for (;;) { - std::clock_t currentTime = std::clock(); - // int tempTestRemovePlz = (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC); - // Send snapshot - if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) { - SendSnapshot(); - previousSnapshotMessage = currentTime; - } - - // Send pings each - if (intervallMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) { - SendPing(); - previousePingMessage = currentTime; - } - - // Time out logic - if (timeToCheckTimeOutTime < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) { - CheckForTimeOuts(); - timOutTimer = currentTime; - } + } } @@ -65,6 +40,14 @@ void Server::ReadFromClients() { char readBuf[1024] = { 0 }; int bytesRead = 0; + // time for previouse message + std::clock_t previousePingMessage = std::clock(); + std::clock_t previousSnapshotMessage = std::clock(); + std::clock_t timOutTimer = std::clock(); + // How offen we send messages (milliseconds) + int intervallMs = 1000; + int snapshotInterval = 50; + int timeToCheckTimeOutTime = 100; for (;;) { if (m_Socket.available()) { @@ -77,6 +60,26 @@ void Server::ReadFromClients() std::cout << "Read from client crashed: " << err.what(); //} } + + std::clock_t currentTime = std::clock(); + // int tempTestRemovePlz = (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC); + // Send snapshot + if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) { + SendSnapshot(); + previousSnapshotMessage = currentTime; + } + + // Send pings each + if (intervallMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) { + SendPing(); + previousePingMessage = currentTime; + } + + // Time out logic + if (timeToCheckTimeOutTime < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) { + CheckForTimeOuts(); + timOutTimer = currentTime; + } } } } @@ -299,24 +302,24 @@ void Server::ParseEvent(char * data, size_t length) unsigned int entityId = m_PlayerDefinitions[i].EntityID; if ("+Forward" == std::string(data)) { glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"]; - temp.z -= 0.5f; + temp.z -= 0.1f; m_World->GetComponent(entityId, "Transform")["Position"] = temp; } if ("-Forward" == std::string(data)) { glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"]; - temp.z += 0.5f; + temp.z += 0.1f; m_World->GetComponent(entityId, "Transform")["Position"] = temp; } if ("+Right" == std::string(data)) { glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"]; - temp.x += 0.5f; + temp.x += 0.1f; m_World->GetComponent(entityId, "Transform")["Position"] = temp; } if ("-Right" == std::string(data)) { glm::vec3 temp = m_World->GetComponent(entityId, "Transform")["Position"]; - temp.x -= 0.5f; + temp.x -= 0.1f; m_World->GetComponent(entityId, "Transform")["Position"] = temp; } } @@ -340,6 +343,7 @@ void Server::ParseConnect(char * data, size_t length) transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f); ComponentWrapper model = m_World->AttachComponent(m_PlayerDefinitions[i].EntityID, "Model"); model["Resource"] = "Models/Core/UnitSphere.obj"; + model["Color"] = glm::vec4(rand()%255 / 255.f, rand()%255 / 255.f, rand() %255 / 255.f, 1.f); m_PlayerDefinitions[i].Endpoint = m_ReceiverEndpoint; m_PlayerDefinitions[i].Name = std::string(data); diff --git a/src/Game/Game.cpp b/src/Game/Game.cpp index d21e64ea..4b928b15 100644 --- a/src/Game/Game.cpp +++ b/src/Game/Game.cpp @@ -41,15 +41,16 @@ Game::Game(int argc, char* argv[]) // TEMP: Invoke network boost::thread workerThread(&Game::NetworkFunction, this); - m_LastTime = glfwGetTime(); } Game::~Game() { + // Call before to ensure that thread closes correctly. + m_Client.Close(); + delete m_FrameStack; delete m_EventBroker; - m_Client.Close(); } void Game::Tick()