Network not longer threaded
This commit is contained in:
@@ -30,6 +30,12 @@ private:
|
|||||||
boost::asio::io_service m_IOService;
|
boost::asio::io_service m_IOService;
|
||||||
boost::asio::ip::udp::socket m_Socket;
|
boost::asio::ip::udp::socket m_Socket;
|
||||||
|
|
||||||
|
// Sending message to server logic
|
||||||
|
int bytesRead = -1;
|
||||||
|
char readBuf[1024] = { 0 };
|
||||||
|
int snapshotInterval = 33;
|
||||||
|
std::clock_t previousSnapshotMessage = std::clock();
|
||||||
|
|
||||||
// Packet loss logic
|
// Packet loss logic
|
||||||
unsigned int m_PacketID = 0;
|
unsigned int m_PacketID = 0;
|
||||||
unsigned int m_PreviousPacketID = 0;
|
unsigned int m_PreviousPacketID = 0;
|
||||||
@@ -37,8 +43,6 @@ private:
|
|||||||
|
|
||||||
// Game logic
|
// Game logic
|
||||||
World* m_World;
|
World* m_World;
|
||||||
std::vector<unsigned int> m_PlayersToCreate;
|
|
||||||
glm::vec2 m_PlayerPositions[MAXCONNECTIONS];
|
|
||||||
std::string m_PlayerName;
|
std::string m_PlayerName;
|
||||||
int m_PlayerID = -1;
|
int m_PlayerID = -1;
|
||||||
|
|
||||||
@@ -67,9 +71,9 @@ private:
|
|||||||
void parsePing();
|
void parsePing();
|
||||||
void parseServerPing();
|
void parseServerPing();
|
||||||
void parseSnapshot(Packet& packet);
|
void parseSnapshot(Packet& packet);
|
||||||
void createNewPlayer(int i);
|
|
||||||
void identifyPacketLoss();
|
void identifyPacketLoss();
|
||||||
bool isConnected();
|
bool isConnected();
|
||||||
|
EntityID createPlayer();
|
||||||
|
|
||||||
// Events
|
// Events
|
||||||
EventBroker* m_EventBroker;
|
EventBroker* m_EventBroker;
|
||||||
|
|||||||
@@ -29,6 +29,18 @@ private:
|
|||||||
boost::asio::ip::udp::socket m_Socket;
|
boost::asio::ip::udp::socket m_Socket;
|
||||||
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
PlayerDefinition m_PlayerDefinitions[MAXCONNECTIONS];
|
||||||
|
|
||||||
|
// Sending messages to client logic
|
||||||
|
char readBuffer[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 often we send messages (milliseconds)
|
||||||
|
int intervalMs = 1000;
|
||||||
|
int snapshotInterval = 50;
|
||||||
|
int checkTimeOutInterval = 100;
|
||||||
|
|
||||||
//Timers
|
//Timers
|
||||||
std::clock_t m_StartPingTime;
|
std::clock_t m_StartPingTime;
|
||||||
std::clock_t m_StopTimes[8];
|
std::clock_t m_StopTimes[8];
|
||||||
@@ -67,6 +79,7 @@ private:
|
|||||||
void parseServerPing();
|
void parseServerPing();
|
||||||
void parseSnapshot(Packet& packet);
|
void parseSnapshot(Packet& packet);
|
||||||
void identifyPacketLoss();
|
void identifyPacketLoss();
|
||||||
|
EntityID createPlayer();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ void InputProxy::Process()
|
|||||||
e.Command = command;
|
e.Command = command;
|
||||||
e.Value = currentValue;
|
e.Value = currentValue;
|
||||||
m_EventBroker->Publish(e);
|
m_EventBroker->Publish(e);
|
||||||
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
//LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||||
m_LastCommandValues[command] = currentValue;
|
m_LastCommandValues[command] = currentValue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,7 +78,7 @@ void InputProxy::Process()
|
|||||||
}
|
}
|
||||||
//e.Value = std::max(-1.f, std::min(e.Value, 1.f));
|
//e.Value = std::max(-1.f, std::min(e.Value, 1.f));
|
||||||
m_EventBroker->Publish(e);
|
m_EventBroker->Publish(e);
|
||||||
LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
//LOG_DEBUG("Input: Published command %s=%f for player %i", e.Command.c_str(), e.Value, e.PlayerID);
|
||||||
}
|
}
|
||||||
m_CommandQueue.clear();
|
m_CommandQueue.clear();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,21 +37,11 @@ void Client::Start(World* world, EventBroker* eventBroker)
|
|||||||
}
|
}
|
||||||
m_Socket.connect(m_ReceiverEndpoint);
|
m_Socket.connect(m_ReceiverEndpoint);
|
||||||
LOG_INFO("I am client. BIP BOP");
|
LOG_INFO("I am client. BIP BOP");
|
||||||
readFromServer();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::Update()
|
void Client::Update()
|
||||||
{
|
{
|
||||||
while (m_PlayersToCreate.size() > 0) {
|
readFromServer();
|
||||||
unsigned int i = m_PlayersToCreate.size() - 1;
|
|
||||||
unsigned int tempID = m_World->CreateEntity();
|
|
||||||
ComponentWrapper transform = m_World->AttachComponent(tempID, "Transform");
|
|
||||||
ComponentWrapper model = m_World->AttachComponent(tempID, "Model");
|
|
||||||
model["Resource"] = "Models/Core/UnitSphere.obj";
|
|
||||||
ComponentWrapper player = m_World->AttachComponent(tempID, "Player");
|
|
||||||
m_PlayerDefinitions[m_PlayersToCreate[i]].EntityID = tempID;
|
|
||||||
m_PlayersToCreate.pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::Close()
|
void Client::Close()
|
||||||
@@ -65,27 +55,19 @@ void Client::Close()
|
|||||||
|
|
||||||
void Client::readFromServer()
|
void Client::readFromServer()
|
||||||
{
|
{
|
||||||
int bytesRead = -1;
|
if (m_Socket.available()) {
|
||||||
char readBuf[1024] = { 0 };
|
bytesRead = receive(readBuf, INPUTSIZE);
|
||||||
|
if (bytesRead > 0) {
|
||||||
int snapshotInterval = 33;
|
Packet packet(readBuf, bytesRead);
|
||||||
std::clock_t previousSnapshotMessage = std::clock();
|
parseMessageType(packet);
|
||||||
|
|
||||||
while (m_ThreadIsRunning) {
|
|
||||||
if (m_Socket.available()) {
|
|
||||||
bytesRead = receive(readBuf, INPUTSIZE);
|
|
||||||
if (bytesRead > 0) {
|
|
||||||
Packet packet(readBuf, bytesRead);
|
|
||||||
parseMessageType(packet);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
std::clock_t currentTime = std::clock();
|
}
|
||||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
std::clock_t currentTime = std::clock();
|
||||||
if (isConnected()) {
|
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||||
sendSnapshotToServer();
|
if (isConnected()) {
|
||||||
}
|
sendSnapshotToServer();
|
||||||
previousSnapshotMessage = currentTime;
|
|
||||||
}
|
}
|
||||||
|
previousSnapshotMessage = currentTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -215,7 +197,7 @@ void Client::parseSnapshot(Packet& packet)
|
|||||||
// New player connected on the server side
|
// New player connected on the server side
|
||||||
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
if (m_PlayerDefinitions[i].Name == "" && tempName != "") {
|
||||||
m_PlayerDefinitions[i].Name = tempName;
|
m_PlayerDefinitions[i].Name = tempName;
|
||||||
m_PlayersToCreate.push_back(i);
|
m_PlayerDefinitions[i].EntityID = createPlayer();
|
||||||
} else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
|
} else if (m_PlayerDefinitions[i].Name != "" && tempName == "") {
|
||||||
// Someone disconnected
|
// Someone disconnected
|
||||||
// TODO: Insert code here
|
// TODO: Insert code here
|
||||||
@@ -321,13 +303,6 @@ bool Client::OnInputCommand(const Events::InputCommand & e)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::createNewPlayer(int i)
|
|
||||||
{
|
|
||||||
m_PlayerDefinitions[i].EntityID = m_World->CreateEntity();
|
|
||||||
ComponentWrapper transform = m_World->AttachComponent(m_PlayerDefinitions[i].EntityID, "Transform");
|
|
||||||
ComponentWrapper model = m_World->AttachComponent(m_PlayerDefinitions[i].EntityID, "Model");
|
|
||||||
model["Resource"] = "Models/Core/UnitSphere.obj";
|
|
||||||
}
|
|
||||||
|
|
||||||
void Client::identifyPacketLoss()
|
void Client::identifyPacketLoss()
|
||||||
{
|
{
|
||||||
@@ -347,3 +322,13 @@ bool Client::isConnected()
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EntityID Client::createPlayer()
|
||||||
|
{
|
||||||
|
EntityID entityID = m_World->CreateEntity();
|
||||||
|
ComponentWrapper transform = m_World->AttachComponent(entityID, "Transform");
|
||||||
|
ComponentWrapper model = m_World->AttachComponent(entityID, "Model");
|
||||||
|
model["Resource"] = "Models/Core/UnitSphere.obj";
|
||||||
|
ComponentWrapper player = m_World->AttachComponent(entityID, "Player");
|
||||||
|
return entityID;
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,24 +15,11 @@ void Server::Start(World* world, EventBroker* eventBroker)
|
|||||||
m_StopTimes[i] = std::clock();
|
m_StopTimes[i] = std::clock();
|
||||||
}
|
}
|
||||||
LOG_INFO("I am Server. BIP BOP\n");
|
LOG_INFO("I am Server. BIP BOP\n");
|
||||||
|
|
||||||
readFromClients();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::Update()
|
void Server::Update()
|
||||||
{
|
{
|
||||||
while (m_PlayersToCreate.size() > 0) {
|
readFromClients();
|
||||||
unsigned int i = m_PlayersToCreate.size() - 1;
|
|
||||||
unsigned int tempID = m_World->CreateEntity();
|
|
||||||
ComponentWrapper transform = m_World->AttachComponent(tempID, "Transform");
|
|
||||||
transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f);
|
|
||||||
ComponentWrapper model = m_World->AttachComponent(tempID, "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);
|
|
||||||
ComponentWrapper player = m_World->AttachComponent(tempID, "Player");
|
|
||||||
m_PlayerDefinitions[m_PlayersToCreate[i]].EntityID = tempID;
|
|
||||||
m_PlayersToCreate.pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::Close()
|
void Server::Close()
|
||||||
@@ -42,50 +29,37 @@ void Server::Close()
|
|||||||
|
|
||||||
void Server::readFromClients()
|
void Server::readFromClients()
|
||||||
{
|
{
|
||||||
char readBuffer[1024] = { 0 };
|
// m_ThreadIsRunning might be unnecessary but the
|
||||||
int bytesRead = 0;
|
// program crashed if it executed m_Socket.available()
|
||||||
// time for previouse message
|
// when closing the program.
|
||||||
std::clock_t previousePingMessage = std::clock();
|
|
||||||
std::clock_t previousSnapshotMessage = std::clock();
|
|
||||||
std::clock_t timOutTimer = std::clock();
|
|
||||||
// How often we send messages (milliseconds)
|
|
||||||
int intervalMs = 1000;
|
|
||||||
int snapshotInterval = 50;
|
|
||||||
int checkTimeOutInterval = 100;
|
|
||||||
|
|
||||||
while (m_ThreadIsRunning) {
|
if (m_Socket.available()) {
|
||||||
// m_ThreadIsRunning might be unnecessary but the
|
try {
|
||||||
// program crashed if it executed m_Socket.available()
|
bytesRead = receive(readBuffer, INPUTSIZE);
|
||||||
// when closing the program.
|
Packet packet(readBuffer, bytesRead);
|
||||||
|
parseMessageType(packet);
|
||||||
if (m_ThreadIsRunning && m_Socket.available()) {
|
} catch (const std::exception& err) {
|
||||||
try {
|
//LOG_ERROR("%i: Read from client crashed %s", m_PacketID, err.what());
|
||||||
bytesRead = receive(readBuffer, INPUTSIZE);
|
|
||||||
Packet packet(readBuffer, bytesRead);
|
|
||||||
parseMessageType(packet);
|
|
||||||
} catch (const std::exception& err) {
|
|
||||||
//LOG_ERROR("%i: Read from client crashed %s", m_PacketID, err.what());
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
std::clock_t currentTime = std::clock();
|
|
||||||
// Send snapshot
|
|
||||||
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
|
||||||
sendSnapshot();
|
|
||||||
previousSnapshotMessage = currentTime;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send pings each
|
}
|
||||||
if (intervalMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) {
|
std::clock_t currentTime = std::clock();
|
||||||
sendPing();
|
// Send snapshot
|
||||||
previousePingMessage = currentTime;
|
if (snapshotInterval < (1000 * (currentTime - previousSnapshotMessage) / (double)CLOCKS_PER_SEC)) {
|
||||||
}
|
sendSnapshot();
|
||||||
|
previousSnapshotMessage = currentTime;
|
||||||
|
}
|
||||||
|
|
||||||
// Time out logic
|
// Send pings each
|
||||||
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
if (intervalMs < (1000 * (currentTime - previousePingMessage) / (double)CLOCKS_PER_SEC)) {
|
||||||
checkForTimeOuts();
|
sendPing();
|
||||||
timOutTimer = currentTime;
|
previousePingMessage = currentTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Time out logic
|
||||||
|
if (checkTimeOutInterval < (1000 * (currentTime - timOutTimer) / (double)CLOCKS_PER_SEC)) {
|
||||||
|
checkForTimeOuts();
|
||||||
|
timOutTimer = currentTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,8 +265,7 @@ void Server::parseConnect(Packet& packet)
|
|||||||
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
for (int i = 0; i < MAXCONNECTIONS; i++) {
|
||||||
if (m_PlayerDefinitions[i].Endpoint.address() == boost::asio::ip::address()) {
|
if (m_PlayerDefinitions[i].Endpoint.address() == boost::asio::ip::address()) {
|
||||||
// Create new player
|
// Create new player
|
||||||
m_PlayersToCreate.push_back(i);
|
m_PlayerDefinitions[i].EntityID = createPlayer();
|
||||||
|
|
||||||
m_PlayerDefinitions[i].Endpoint = m_ReceiverEndpoint;
|
m_PlayerDefinitions[i].Endpoint = m_ReceiverEndpoint;
|
||||||
m_PlayerDefinitions[i].Name = packet.ReadString();
|
m_PlayerDefinitions[i].Name = packet.ReadString();
|
||||||
|
|
||||||
@@ -368,3 +341,15 @@ void Server::identifyPacketLoss()
|
|||||||
LOG_INFO("%i Packet(s) were lost...", difference);
|
LOG_INFO("%i Packet(s) were lost...", difference);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
EntityID Server::createPlayer()
|
||||||
|
{
|
||||||
|
EntityID entityID = m_World->CreateEntity();
|
||||||
|
ComponentWrapper transform = m_World->AttachComponent(entityID, "Transform");
|
||||||
|
transform["Position"] = glm::vec3(-1.5f, 0.f, 0.f);
|
||||||
|
ComponentWrapper model = m_World->AttachComponent(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);
|
||||||
|
ComponentWrapper player = m_World->AttachComponent(entityID, "Player");
|
||||||
|
return entityID;
|
||||||
|
}
|
||||||
|
|||||||
+4
-3
@@ -55,7 +55,8 @@ Game::Game(int argc, char* argv[])
|
|||||||
m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer);
|
m_SystemPipeline->AddSystem<EditorSystem>(m_Renderer);
|
||||||
// Invoke network
|
// Invoke network
|
||||||
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
|
if (m_Config->Get<bool>("Networking.StartNetwork", false)) {
|
||||||
boost::thread workerThread(&Game::networkFunction, this);
|
//boost::thread workerThread(&Game::networkFunction, this);
|
||||||
|
networkFunction();
|
||||||
}
|
}
|
||||||
m_LastTime = glfwGetTime();
|
m_LastTime = glfwGetTime();
|
||||||
|
|
||||||
@@ -153,7 +154,7 @@ void Game::networkFunction()
|
|||||||
// I don't think we are reaching this part of the code right now.
|
// I don't think we are reaching this part of the code right now.
|
||||||
// ~Game() is not called if the game is exited by closing console windows
|
// ~Game() is not called if the game is exited by closing console windows
|
||||||
// When server or client is done set it to false.
|
// When server or client is done set it to false.
|
||||||
m_IsClientOrServer = false;
|
//m_IsClientOrServer = false;
|
||||||
// Destroy it! (with fire)
|
// Destroy it! (with fire)
|
||||||
delete m_ClientOrServer;
|
//delete m_ClientOrServer;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user