Fixed bug where closing glwindow as client crashed.

Server bug is still present.
This commit is contained in:
Jocke
2015-12-09 13:37:02 +01:00
parent f83e217638
commit 0e6353dd79
4 changed files with 49 additions and 41 deletions
+3 -2
View File
@@ -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;
+4 -2
View File
@@ -18,6 +18,7 @@ Client::~Client()
void Client::Start(World* world, EventBroker* eventBroker)
{
// Subscribe to events
m_WasStarted = true;
m_EventBroker = eventBroker;
m_World = world;
m_EKeyDown = decltype(m_EKeyDown)(std::bind(&Client::OnKeyDown, this, std::placeholders::_1));
@@ -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()
{
if (m_WasStarted) {
Disconnect();
m_ThreadIsRunning = false;
m_Socket.close();
m_EventBroker->Unsubscribe(m_EKeyDown);
}
}
void Client::ReadFromServer()
+29 -25
View File
@@ -28,16 +28,38 @@ void Server::Start(World* world)
void Server::DisplayLoop()
{
int lengthOfMessage = -1;
for (;;) {
}
}
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;
char* data;
for (;;) {
if (m_Socket.available()) {
try {
bytesRead = Receive(readBuf, INPUTSIZE);
ParseMessageType(readBuf, bytesRead);
} catch (const std::exception& err) {
// To not spam "socket closed messages"
//if (std::string(err.what()).find("forcefully closed") != std::string::npos) {
std::cout << "Read from client crashed: " << err.what();
//}
}
std::clock_t currentTime = std::clock();
// int tempTestRemovePlz = (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC);
@@ -60,25 +82,6 @@ void Server::DisplayLoop()
}
}
}
void Server::ReadFromClients()
{
char readBuf[1024] = { 0 };
int bytesRead = 0;
for (;;) {
if (m_Socket.available()) {
try {
bytesRead = Receive(readBuf, INPUTSIZE);
ParseMessageType(readBuf, bytesRead);
} catch (const std::exception& err) {
// To not spam "socket closed messages"
//if (std::string(err.what()).find("forcefully closed") != std::string::npos) {
std::cout << "Read from client crashed: " << err.what();
//}
}
}
}
}
void Server::InputLoop()
@@ -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);
+3 -2
View File
@@ -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()