From ebd8c5e6fc29c709d5bae79933874a27f90a0130 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 24 Jun 2013 21:46:11 +1000 Subject: [PATCH] Synchronization works --- client/in_world.cpp | 29 ++++++++++++++++++++--------- client/in_world.hpp | 4 ++-- libs/common/packet.hpp | 16 ++++------------ rsc/config.cfg | 2 +- server/server_application.cpp | 33 ++++++++++++++++++++++++++------- server/server_application.hpp | 6 ++++-- 6 files changed, 57 insertions(+), 33 deletions(-) diff --git a/client/in_world.cpp b/client/in_world.cpp index 22841ab..ba9a833 100644 --- a/client/in_world.cpp +++ b/client/in_world.cpp @@ -39,12 +39,17 @@ InWorld::InWorld() { //debugging Packet::Packet p; p.meta.type = Packet::Type::PLAYER_NEW; - snprintf(p.playerNew.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); - snprintf(p.playerNew.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); - p.playerNew.position = {0, 50}; - p.playerNew.motion = {140, 0}; + snprintf(p.playerData.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); + snprintf(p.playerData.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); + p.playerData.position = {0, 50}; + p.playerData.motion = {140, 0}; netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet::Packet)); + + //request a sync + p.meta.type = Packet::Type::SYNCHRONIZE; + p.synchronize.clientIndex = infoMgr->GetClientIndex(); + netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet::Packet)); } InWorld::~InWorld() { @@ -182,13 +187,13 @@ int InWorld::HandlePacket(Packet::Packet p) { // // // break; case Packet::Type::PLAYER_NEW: - AddPlayer(p.playerNew); + AddPlayer(p.playerData); break; case Packet::Type::PLAYER_DELETE: RemovePlayer(p.playerDelete); break; case Packet::Type::PLAYER_UPDATE: - UpdatePlayer(p.playerUpdate); + UpdatePlayer(p.playerData); break; default: throw(runtime_error("Failed to recognize the packet type: " + itos(int(p.meta.type)))); @@ -222,7 +227,7 @@ void InWorld::HandleDisconnection(Packet::Disconnect& disconnect) { cout << "You have been disconnected" << endl; } -void InWorld::AddPlayer(Packet::PlayerNew& p) { +void InWorld::AddPlayer(Packet::PlayerData& p) { //sprite playerCharacters[p.playerIndex].GetSprite()->SetSurface(surfaceMgr->Get(p.avatar), 32, 48); @@ -238,6 +243,12 @@ void InWorld::RemovePlayer(Packet::PlayerDelete& p) { // } -void InWorld::UpdatePlayer(Packet::PlayerUpdate& p) { - // +void InWorld::UpdatePlayer(Packet::PlayerData& p) { + if (playerCharacters.find(p.playerIndex) == playerCharacters.end()) { + AddPlayer(p); + return; + } + + playerCharacters[p.playerIndex].SetPosition(p.position); + playerCharacters[p.playerIndex].SetMotion(p.motion); } diff --git a/client/in_world.hpp b/client/in_world.hpp index f5c681f..b58bc06 100644 --- a/client/in_world.hpp +++ b/client/in_world.hpp @@ -69,9 +69,9 @@ protected: void HandleDisconnection(Packet::Disconnect&); - void AddPlayer(Packet::PlayerNew&); + void AddPlayer(Packet::PlayerData&); void RemovePlayer(Packet::PlayerDelete&); - void UpdatePlayer(Packet::PlayerUpdate&); + void UpdatePlayer(Packet::PlayerData&); //services ConfigUtility* configUtil = Singleton::Get(); diff --git a/libs/common/packet.hpp b/libs/common/packet.hpp index d640f74..730f74b 100644 --- a/libs/common/packet.hpp +++ b/libs/common/packet.hpp @@ -90,9 +90,10 @@ struct Disconnect { struct Synchronize { Metadata meta; + int clientIndex; }; -struct PlayerNew { +struct PlayerData { Metadata meta; int playerIndex; int clientIndex; @@ -109,14 +110,6 @@ struct PlayerDelete { int clientIndex; }; -struct PlayerUpdate { - Metadata meta; - int playerIndex; - int clientIndex; - Vector2 position; - Vector2 motion; -}; - union Packet { Packet() { meta.type = Type::NONE; @@ -133,11 +126,10 @@ union Packet { JoinResponse joinResponse; Disconnect disconnect; - Synchronize sync; + Synchronize synchronize; - PlayerNew playerNew; + PlayerData playerData; PlayerDelete playerDelete; - PlayerUpdate playerUpdate; #ifdef DEBUG char buffer[1024]; diff --git a/rsc/config.cfg b/rsc/config.cfg index 6ef7cc5..0a83e92 100644 --- a/rsc/config.cfg +++ b/rsc/config.cfg @@ -16,5 +16,5 @@ interface = rsc/graphics/interface #debugging debug = true -avatar = elliot2.bmp +avatar = elliot handle = UserName diff --git a/server/server_application.cpp b/server/server_application.cpp index 0d1c37a..8b75fd5 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -174,11 +174,11 @@ int ServerApplication::HandlePacket(Packet::Packet p) { case Packet::Type::DISCONNECT: HandleDisconnection(p.disconnect); break; -// case Packet::Type::SYNCHRONIZE: -// // -// break; + case Packet::Type::SYNCHRONIZE: + SynchronizeEverything(p.synchronize); + break; case Packet::Type::PLAYER_NEW: - AddPlayer(p.playerNew); + AddPlayer(p.playerData); RelayPacket(p); break; case Packet::Type::PLAYER_DELETE: @@ -186,7 +186,7 @@ int ServerApplication::HandlePacket(Packet::Packet p) { RelayPacket(p); break; case Packet::Type::PLAYER_UPDATE: - UpdatePlayer(p.playerUpdate); + UpdatePlayer(p.playerData); RelayPacket(p); break; default: @@ -202,6 +202,25 @@ void ServerApplication::RelayPacket(Packet::Packet& p) { } } +void ServerApplication::SynchronizeEverything(Packet::Synchronize& sync) { + //send all known data to this client + //TODO multithreading? + + //all players + Packet::Packet p; + p.meta.type = Packet::Type::PLAYER_UPDATE; + for (auto& it : players) { + p.playerData.playerIndex = it.second.index; + p.playerData.clientIndex = it.second.clientIndex; + snprintf(p.playerData.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str()); + snprintf(p.playerData.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str()); + p.playerData.position = it.second.position; + p.playerData.motion = it.second.motion; + + netUtil->Send(&clients[sync.clientIndex].address, &p, sizeof(Packet::Packet)); + } +} + void ServerApplication::HandleBroadcast(Packet::BroadcastRequest& bcast) { //respond to a broadcast request with the server's data Packet::Packet p; @@ -248,7 +267,7 @@ void ServerApplication::HandleDisconnection(Packet::Disconnect& disconnect) { cout << "number of clients: " << clients.size() << endl; } -void ServerApplication::AddPlayer(Packet::PlayerNew& p) { +void ServerApplication::AddPlayer(Packet::PlayerData& p) { //add the player PlayerEntry newPlayer = { uniqueIndex++, @@ -273,6 +292,6 @@ void ServerApplication::RemovePlayer(Packet::PlayerDelete& p) { //TODO remove a player } -void ServerApplication::UpdatePlayer(Packet::PlayerUpdate& p) { +void ServerApplication::UpdatePlayer(Packet::PlayerData& p) { //TODO update a player } diff --git a/server/server_application.hpp b/server/server_application.hpp index cb0391b..3015500 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -58,13 +58,15 @@ private: int HandlePacket(Packet::Packet); void RelayPacket(Packet::Packet&); + void SynchronizeEverything(Packet::Synchronize&); + void HandleBroadcast(Packet::BroadcastRequest&); void HandleConnection(Packet::JoinRequest&); void HandleDisconnection(Packet::Disconnect&); - void AddPlayer(Packet::PlayerNew&); + void AddPlayer(Packet::PlayerData&); void RemovePlayer(Packet::PlayerDelete&); - void UpdatePlayer(Packet::PlayerUpdate&); + void UpdatePlayer(Packet::PlayerData&); //services ConfigUtility* configUtil = Singleton::Get();