Simplified the packet system

This commit is contained in:
Kayne Ruse
2013-06-24 22:59:06 +10:00
parent ebd8c5e6fc
commit 6d1ba24404
11 changed files with 163 additions and 215 deletions
+1 -1
View File
@@ -89,7 +89,7 @@ void ClientApplication::Init() {
flags); flags);
//initiate the remaining singletons //initiate the remaining singletons
netUtil->Open(0, sizeof(Packet::Packet)); netUtil->Open(0, sizeof(Packet));
} }
void ClientApplication::Proc() { void ClientApplication::Proc() {
+28 -28
View File
@@ -37,19 +37,19 @@ InWorld::InWorld() {
font.SetSurface(surfaceMgr->Get("font")); font.SetSurface(surfaceMgr->Get("font"));
//debugging //debugging
Packet::Packet p; Packet p;
p.meta.type = Packet::Type::PLAYER_NEW; p.meta.type = Packet::Type::PLAYER_NEW;
snprintf(p.playerData.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); snprintf(p.playerInfo.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
snprintf(p.playerData.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); snprintf(p.playerInfo.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar"));
p.playerData.position = {0, 50}; p.playerInfo.position = {0, 50};
p.playerData.motion = {140, 0}; p.playerInfo.motion = {60, 0};
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet::Packet)); netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
//request a sync //request a sync
p.meta.type = Packet::Type::SYNCHRONIZE; p.meta.type = Packet::Type::SYNCHRONIZE;
p.synchronize.clientIndex = infoMgr->GetClientIndex(); p.meta.clientIndex = infoMgr->GetClientIndex();
netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet::Packet)); netUtil->Send(GAME_CHANNEL, &p, sizeof(Packet));
} }
InWorld::~InWorld() { InWorld::~InWorld() {
@@ -154,7 +154,7 @@ void InWorld::KeyUp(SDL_KeyboardEvent const& key) {
//Utilities //Utilities
//------------------------- //-------------------------
int InWorld::HandlePacket(Packet::Packet p) { int InWorld::HandlePacket(Packet p) {
switch(p.meta.type) { switch(p.meta.type) {
case Packet::Type::NONE: case Packet::Type::NONE:
//DO NOTHING //DO NOTHING
@@ -163,7 +163,7 @@ int InWorld::HandlePacket(Packet::Packet p) {
case Packet::Type::PING: case Packet::Type::PING:
//quick pong //quick pong
p.meta.type = Packet::Type::PONG; p.meta.type = Packet::Type::PONG;
netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet)); netUtil->Send(&p.meta.address, &p, sizeof(Packet));
break; break;
case Packet::Type::PONG: case Packet::Type::PONG:
// //
@@ -181,19 +181,19 @@ int InWorld::HandlePacket(Packet::Packet p) {
// // // //
// break; // break;
case Packet::Type::DISCONNECT: case Packet::Type::DISCONNECT:
HandleDisconnection(p.disconnect); HandleDisconnection(p);
break; break;
// case Packet::Type::SYNCHRONIZE: // case Packet::Type::SYNCHRONIZE:
// // // //
// break; // break;
case Packet::Type::PLAYER_NEW: case Packet::Type::PLAYER_NEW:
AddPlayer(p.playerData); AddPlayer(p);
break; break;
case Packet::Type::PLAYER_DELETE: case Packet::Type::PLAYER_DELETE:
RemovePlayer(p.playerDelete); RemovePlayer(p);
break; break;
case Packet::Type::PLAYER_UPDATE: case Packet::Type::PLAYER_UPDATE:
UpdatePlayer(p.playerData); UpdatePlayer(p);
break; break;
default: default:
throw(runtime_error("Failed to recognize the packet type: " + itos(int(p.meta.type)))); throw(runtime_error("Failed to recognize the packet type: " + itos(int(p.meta.type))));
@@ -203,10 +203,10 @@ int InWorld::HandlePacket(Packet::Packet p) {
void InWorld::Disconnect() { void InWorld::Disconnect() {
//disconnect //disconnect
Packet::Packet p; Packet p;
p.meta.type = Packet::Type::DISCONNECT; p.meta.type = Packet::Type::DISCONNECT;
p.disconnect.clientIndex = infoMgr->GetClientIndex(); p.meta.clientIndex = infoMgr->GetClientIndex();
netUtil->Send(GAME_CHANNEL, reinterpret_cast<void*>(&p), sizeof(Packet::Packet)); netUtil->Send(GAME_CHANNEL, reinterpret_cast<void*>(&p), sizeof(Packet));
netUtil->Unbind(GAME_CHANNEL); netUtil->Unbind(GAME_CHANNEL);
//reset the client //reset the client
@@ -220,35 +220,35 @@ void InWorld::ExitGame() {
cout << "The game session has ended" << endl; cout << "The game session has ended" << endl;
} }
void InWorld::HandleDisconnection(Packet::Disconnect& disconnect) { void InWorld::HandleDisconnection(Packet& disconnect) {
Disconnect(); Disconnect();
SetNextScene(SceneList::MAINMENU); SetNextScene(SceneList::MAINMENU);
endQueueThread(); endQueueThread();
cout << "You have been disconnected" << endl; cout << "You have been disconnected" << endl;
} }
void InWorld::AddPlayer(Packet::PlayerData& p) { void InWorld::AddPlayer(Packet& p) {
//sprite //sprite
playerCharacters[p.playerIndex].GetSprite()->SetSurface(surfaceMgr->Get(p.avatar), 32, 48); playerCharacters[p.playerInfo.index].GetSprite()->SetSurface(surfaceMgr->Get(p.playerInfo.avatar), 32, 48);
//pos //pos
playerCharacters[p.playerIndex].SetPosition(p.position); playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position);
playerCharacters[p.playerIndex].SetMotion(p.motion); playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion);
//debugging //debugging
cout << "New player, index " << p.playerIndex << endl; cout << "New player, index " << p.playerInfo.index << endl;
} }
void InWorld::RemovePlayer(Packet::PlayerDelete& p) { void InWorld::RemovePlayer(Packet& p) {
// //
} }
void InWorld::UpdatePlayer(Packet::PlayerData& p) { void InWorld::UpdatePlayer(Packet& p) {
if (playerCharacters.find(p.playerIndex) == playerCharacters.end()) { if (playerCharacters.find(p.playerInfo.index) == playerCharacters.end()) {
AddPlayer(p); AddPlayer(p);
return; return;
} }
playerCharacters[p.playerIndex].SetPosition(p.position); playerCharacters[p.playerInfo.index].SetPosition(p.playerInfo.position);
playerCharacters[p.playerIndex].SetMotion(p.motion); playerCharacters[p.playerInfo.index].SetMotion(p.playerInfo.motion);
} }
+5 -5
View File
@@ -63,15 +63,15 @@ protected:
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//Utilities //Utilities
int HandlePacket(Packet::Packet); int HandlePacket(Packet);
void Disconnect(); void Disconnect();
void ExitGame(); void ExitGame();
void HandleDisconnection(Packet::Disconnect&); void HandleDisconnection(Packet&);
void AddPlayer(Packet::PlayerData&); void AddPlayer(Packet&);
void RemovePlayer(Packet::PlayerDelete&); void RemovePlayer(Packet&);
void UpdatePlayer(Packet::PlayerData&); void UpdatePlayer(Packet&);
//services //services
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get(); ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
+12 -12
View File
@@ -155,7 +155,7 @@ void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
//Utilities //Utilities
//------------------------- //-------------------------
int Lobby::HandlePacket(Packet::Packet p) { int Lobby::HandlePacket(Packet p) {
switch(p.meta.type) { switch(p.meta.type) {
case Packet::Type::NONE: case Packet::Type::NONE:
//DO NOTHING //DO NOTHING
@@ -164,7 +164,7 @@ int Lobby::HandlePacket(Packet::Packet p) {
case Packet::Type::PING: case Packet::Type::PING:
//quick pong //quick pong
p.meta.type = Packet::Type::PONG; p.meta.type = Packet::Type::PONG;
netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet)); netUtil->Send(&p.meta.address, &p, sizeof(Packet));
break; break;
case Packet::Type::PONG: case Packet::Type::PONG:
// //
@@ -173,13 +173,13 @@ int Lobby::HandlePacket(Packet::Packet p) {
// // // //
// break; // break;
case Packet::Type::BROADCAST_RESPONSE: case Packet::Type::BROADCAST_RESPONSE:
PushServer(p.broadcastResponse); PushServer(p);
break; break;
// case Packet::Type::JOIN_REQUEST: // case Packet::Type::JOIN_REQUEST:
// // // //
// break; // break;
case Packet::Type::JOIN_RESPONSE: case Packet::Type::JOIN_RESPONSE:
BeginGame(p.joinResponse); BeginGame(p);
break; break;
// case Packet::Type::DISCONNECT: // case Packet::Type::DISCONNECT:
// // // //
@@ -203,15 +203,15 @@ int Lobby::HandlePacket(Packet::Packet p) {
} }
void Lobby::BroadcastNetwork() { void Lobby::BroadcastNetwork() {
Packet::Packet p; Packet p;
p.meta.type = Packet::Type::BROADCAST_REQUEST; p.meta.type = Packet::Type::BROADCAST_REQUEST;
netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet::Packet)); netUtil->Send("255.255.255.255", configUtil->Int("server.port"), &p, sizeof(Packet));
serverList.clear(); serverList.clear();
} }
void Lobby::PushServer(Packet::BroadcastResponse& bcast) { void Lobby::PushServer(Packet& bcast) {
ServerEntry entry; ServerEntry entry;
entry.name = bcast.name; entry.name = bcast.serverInfo.name;
entry.address = bcast.meta.address; entry.address = bcast.meta.address;
serverList.push_back(entry); serverList.push_back(entry);
} }
@@ -221,14 +221,14 @@ void Lobby::ConnectToServer(ServerEntry* server) {
if (!server) { if (!server) {
throw(runtime_error("No server received")); throw(runtime_error("No server received"));
} }
Packet::Packet p; Packet p;
p.meta.type = Packet::Type::JOIN_REQUEST; p.meta.type = Packet::Type::JOIN_REQUEST;
netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet::Packet)); netUtil->Send(&server->address, reinterpret_cast<void*>(&p), sizeof(Packet));
} }
void Lobby::BeginGame(Packet::JoinResponse& response) { void Lobby::BeginGame(Packet& response) {
//should be downloading the resources here as well //should be downloading the resources here as well
infoMgr->SetClientIndex(response.clientIndex); infoMgr->SetClientIndex(response.meta.clientIndex);
netUtil->Bind(&response.meta.address, GAME_CHANNEL); netUtil->Bind(&response.meta.address, GAME_CHANNEL);
SetNextScene(SceneList::INWORLD); SetNextScene(SceneList::INWORLD);
} }
+3 -3
View File
@@ -63,11 +63,11 @@ protected:
void KeyUp(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&);
//utilities //utilities
int HandlePacket(Packet::Packet p); int HandlePacket(Packet);
void BroadcastNetwork(); void BroadcastNetwork();
void PushServer(Packet::BroadcastResponse&); void PushServer(Packet&);
void ConnectToServer(ServerEntry*); void ConnectToServer(ServerEntry*);
void BeginGame(Packet::JoinResponse&); void BeginGame(Packet&);
//services //services
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get(); ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
+8 -8
View File
@@ -32,7 +32,7 @@
static SDL_sem* lock = SDL_CreateSemaphore(1); static SDL_sem* lock = SDL_CreateSemaphore(1);
static SDL_Thread* queueThread = nullptr; static SDL_Thread* queueThread = nullptr;
static std::deque<Packet::Packet> queue; static std::deque<Packet> queue;
static bool running = false; static bool running = false;
@@ -41,8 +41,8 @@ static int networkQueue(void*) {
while(running) { while(running) {
SDL_SemWait(lock); SDL_SemWait(lock);
while(netUtil->Receive()) { while(netUtil->Receive()) {
Packet::Packet p; Packet p;
memcpy(&p, netUtil->GetInData(), sizeof(Packet::Packet)); memcpy(&p, netUtil->GetInData(), sizeof(Packet));
p.meta.address = netUtil->GetInPacket()->address; p.meta.address = netUtil->GetInPacket()->address;
queue.push_back(p); queue.push_back(p);
} }
@@ -80,19 +80,19 @@ void killQueueThread() {
queueThread = nullptr; queueThread = nullptr;
} }
Packet::Packet peekNetworkPacket() { Packet peekNetworkPacket() {
SDL_SemWait(lock); SDL_SemWait(lock);
Packet::Packet p; Packet p;
if (queue.size() > 0) { if (queue.size() > 0) {
Packet::Packet p = queue[0]; Packet p = queue[0];
} }
SDL_SemPost(lock); SDL_SemPost(lock);
return p; return p;
} }
Packet::Packet popNetworkPacket() { Packet popNetworkPacket() {
SDL_SemWait(lock); SDL_SemWait(lock);
Packet::Packet p; Packet p;
if (queue.size() > 0) { if (queue.size() > 0) {
p = queue[0]; p = queue[0];
queue.pop_front(); queue.pop_front();
+2 -2
View File
@@ -27,8 +27,8 @@
void beginQueueThread(); void beginQueueThread();
void endQueueThread(); void endQueueThread();
void killQueueThread(); void killQueueThread();
Packet::Packet peekNetworkPacket(); Packet peekNetworkPacket();
Packet::Packet popNetworkPacket(); Packet popNetworkPacket();
void flushNetworkQueue(); void flushNetworkQueue();
#endif #endif
+16 -69
View File
@@ -30,8 +30,8 @@
#pragma pack(push, 0) #pragma pack(push, 0)
namespace Packet { union Packet {
//the type of packet being sent
enum class Type { enum class Type {
NONE = 0, NONE = 0,
@@ -50,94 +50,41 @@ enum class Type {
PLAYER_UPDATE = 11, PLAYER_UPDATE = 11,
}; };
//metadata on the packet itself
struct Metadata { struct Metadata {
Type type; Type type;
IPaddress address; IPaddress address;
}; int clientIndex;
}meta;
struct Ping { //information about the server
Metadata meta; struct ServerInformation {
};
struct Pong {
Metadata meta;
};
struct BroadcastRequest {
Metadata meta;
};
struct BroadcastResponse {
Metadata meta; Metadata meta;
//TODO: version info
char name[PACKET_STRING_SIZE]; char name[PACKET_STRING_SIZE];
//TODO: version //TODO: player count
}; }serverInfo;
struct JoinRequest { //information about a specific player
struct PlayerInformation {
Metadata meta; Metadata meta;
}; int index;
struct JoinResponse {
Metadata meta;
int clientIndex;
//resource list
};
struct Disconnect {
Metadata meta;
int clientIndex;
};
struct Synchronize {
Metadata meta;
int clientIndex;
};
struct PlayerData {
Metadata meta;
int playerIndex;
int clientIndex;
char handle[PACKET_STRING_SIZE]; char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE]; char avatar[PACKET_STRING_SIZE];
Vector2 position; Vector2 position;
Vector2 motion; Vector2 motion;
//TODO Playerdata //TODO Playerdata
}; }playerInfo;
struct PlayerDelete { //zero the packet
Metadata meta;
int playerIndex;
int clientIndex;
};
union Packet {
Packet() { Packet() {
meta.type = Type::NONE; meta.type = Type::NONE;
meta.address.host = 0; meta.address.host = 0;
meta.address.port = 0; meta.address.port = 0;
meta.clientIndex = -1;
}; };
Metadata meta;
Ping ping;
Pong pong;
BroadcastRequest broadcastRequest;
BroadcastResponse broadcastResponse;
JoinRequest joinRequest;
JoinResponse joinResponse;
Disconnect disconnect;
Synchronize synchronize;
PlayerData playerData;
PlayerDelete playerDelete;
#ifdef DEBUG
char buffer[1024];
#endif
}; };
} //namespace Packet
#pragma pack(pop) #pragma pack(pop)
#endif #endif
+46 -43
View File
@@ -71,7 +71,7 @@ void ServerApplication::Init() {
} }
//initiate the remaining singletons //initiate the remaining singletons
netUtil->Open(configUtil->Int("server.port"), sizeof(Packet::Packet)); netUtil->Open(configUtil->Int("server.port"), sizeof(Packet));
//create the threads //create the threads
beginQueueThread(); beginQueueThread();
@@ -145,7 +145,7 @@ void ServerApplication::UpdateWorld(double delta) {
//Network loop //Network loop
//------------------------- //-------------------------
int ServerApplication::HandlePacket(Packet::Packet p) { int ServerApplication::HandlePacket(Packet p) {
switch(p.meta.type) { switch(p.meta.type) {
case Packet::Type::NONE: case Packet::Type::NONE:
//DO NOTHING //DO NOTHING
@@ -154,39 +154,39 @@ int ServerApplication::HandlePacket(Packet::Packet p) {
case Packet::Type::PING: case Packet::Type::PING:
//quick pong //quick pong
p.meta.type = Packet::Type::PONG; p.meta.type = Packet::Type::PONG;
netUtil->Send(&p.meta.address, &p, sizeof(Packet::Packet)); netUtil->Send(&p.meta.address, &p, sizeof(Packet));
break; break;
case Packet::Type::PONG: case Packet::Type::PONG:
// //
break; break;
case Packet::Type::BROADCAST_REQUEST: case Packet::Type::BROADCAST_REQUEST:
HandleBroadcast(p.broadcastRequest); HandleBroadcast(p);
break; break;
// case PacketType::BROADCAST_RESPONSE: // case PacketType::BROADCAST_RESPONSE:
// // // //
// break; // break;
case Packet::Type::JOIN_REQUEST: case Packet::Type::JOIN_REQUEST:
HandleConnection(p.joinRequest); HandleConnection(p);
break; break;
// case PacketType::JOIN_RESPONSE: // case PacketType::JOIN_RESPONSE:
// // // //
// break; // break;
case Packet::Type::DISCONNECT: case Packet::Type::DISCONNECT:
HandleDisconnection(p.disconnect); HandleDisconnection(p);
break; break;
case Packet::Type::SYNCHRONIZE: case Packet::Type::SYNCHRONIZE:
SynchronizeEverything(p.synchronize); SynchronizeEverything(p);
break; break;
case Packet::Type::PLAYER_NEW: case Packet::Type::PLAYER_NEW:
AddPlayer(p.playerData); AddPlayer(p);
RelayPacket(p); RelayPacket(p);
break; break;
case Packet::Type::PLAYER_DELETE: case Packet::Type::PLAYER_DELETE:
RemovePlayer(p.playerDelete); RemovePlayer(p);
RelayPacket(p); RelayPacket(p);
break; break;
case Packet::Type::PLAYER_UPDATE: case Packet::Type::PLAYER_UPDATE:
UpdatePlayer(p.playerData); UpdatePlayer(p);
RelayPacket(p); RelayPacket(p);
break; break;
default: default:
@@ -195,42 +195,43 @@ int ServerApplication::HandlePacket(Packet::Packet p) {
return 1; return 1;
} }
void ServerApplication::RelayPacket(Packet::Packet& p) { void ServerApplication::RelayPacket(Packet& p) {
//pump this packet to all clients //pump this packet to all clients
for (auto& it : clients) { for (auto& it : clients) {
netUtil->Send(&it.second.address, &p, sizeof(Packet::Packet)); netUtil->Send(&it.second.address, &p, sizeof(Packet));
} }
} }
void ServerApplication::SynchronizeEverything(Packet::Synchronize& sync) { void ServerApplication::SynchronizeEverything(Packet& sync) {
//send all known data to this client //send all known data to this client
//TODO multithreading? //TODO multithreading?
//all players //all players
Packet::Packet p; Packet p;
p.meta.type = Packet::Type::PLAYER_UPDATE; p.meta.type = Packet::Type::PLAYER_UPDATE;
for (auto& it : players) { for (auto& it : players) {
p.playerData.playerIndex = it.second.index; p.meta.clientIndex = it.second.clientIndex;
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)); p.playerInfo.index = it.second.index;
snprintf(p.playerInfo.handle, PACKET_STRING_SIZE, "%s", it.second.handle.c_str());
snprintf(p.playerInfo.avatar, PACKET_STRING_SIZE, "%s", it.second.avatar.c_str());
p.playerInfo.position = it.second.position;
p.playerInfo.motion = it.second.motion;
netUtil->Send(&clients[sync.meta.clientIndex].address, &p, sizeof(Packet));
} }
} }
void ServerApplication::HandleBroadcast(Packet::BroadcastRequest& bcast) { void ServerApplication::HandleBroadcast(Packet& bcast) {
//respond to a broadcast request with the server's data //respond to a broadcast request with the server's data
Packet::Packet p; Packet p;
p.meta.type = Packet::Type::BROADCAST_RESPONSE; p.meta.type = Packet::Type::BROADCAST_RESPONSE;
snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil->CString("server.name")); snprintf(p.serverInfo.name, PACKET_STRING_SIZE, "%s", configUtil->CString("server.name"));
//TODO version information //TODO version information
netUtil->Send(&bcast.meta.address, &p, sizeof(Packet::Packet)); netUtil->Send(&bcast.meta.address, &p, sizeof(Packet));
} }
void ServerApplication::HandleConnection(Packet::JoinRequest& request) { void ServerApplication::HandleConnection(Packet& request) {
//create the entries //create the entries
ClientEntry newClient = { ClientEntry newClient = {
uniqueIndex++, uniqueIndex++,
@@ -241,57 +242,59 @@ void ServerApplication::HandleConnection(Packet::JoinRequest& request) {
clients[newClient.index] = newClient; clients[newClient.index] = newClient;
//send the player their information //send the player their information
Packet::Packet p; Packet p;
p.meta.type = Packet::Type::JOIN_RESPONSE; p.meta.type = Packet::Type::JOIN_RESPONSE;
p.joinResponse.clientIndex = newClient.index; p.meta.clientIndex = newClient.index;
//TODO: resource list //TODO: resource list
netUtil->Send(&newClient.address, &p, sizeof(Packet::Packet)); netUtil->Send(&newClient.address, &p, sizeof(Packet));
//debugging //debugging
cout << "New connection: index " << newClient.index << endl; cout << "New connection: index " << newClient.index << endl;
cout << "number of clients: " << clients.size() << endl; cout << "number of clients: " << clients.size() << endl;
} }
void ServerApplication::HandleDisconnection(Packet::Disconnect& disconnect) { void ServerApplication::HandleDisconnection(Packet& disconnect) {
//disconnect a client (redundant message) //disconnect a client (redundant message)
netUtil->Send(&clients[disconnect.clientIndex].address, &disconnect, sizeof(Packet::Packet)); netUtil->Send(&clients[disconnect.meta.clientIndex].address, &disconnect, sizeof(Packet));
clients.erase(disconnect.clientIndex); clients.erase(disconnect.meta.clientIndex);
//TODO remove the player... //TODO remove the player...
//remove if(...) //remove if(...)
//remove the player from other clients
//debugging //debugging
cout << "Lost connection: index " << disconnect.clientIndex << endl; cout << "Lost connection: index " << disconnect.meta.clientIndex << endl;
cout << "number of clients: " << clients.size() << endl; cout << "number of clients: " << clients.size() << endl;
} }
void ServerApplication::AddPlayer(Packet::PlayerData& p) { void ServerApplication::AddPlayer(Packet& p) {
//add the player //add the player
PlayerEntry newPlayer = { PlayerEntry newPlayer = {
uniqueIndex++, uniqueIndex++,
p.clientIndex, p.meta.clientIndex,
p.handle, p.playerInfo.handle,
p.avatar, p.playerInfo.avatar,
p.position, p.playerInfo.position,
p.motion p.playerInfo.motion
}; };
players[newPlayer.index] = newPlayer; players[newPlayer.index] = newPlayer;
//prep for relay //prep for relay
p.playerIndex = newPlayer.index; p.playerInfo.index = newPlayer.index;
//debugging //debugging
cout << "New player " << newPlayer.handle << "Has joined the game" << endl; cout << "New player " << newPlayer.handle << " has joined the game" << endl;
cout << "Number of players: " << players.size() << endl; cout << "Number of players: " << players.size() << endl;
} }
void ServerApplication::RemovePlayer(Packet::PlayerDelete& p) { void ServerApplication::RemovePlayer(Packet& p) {
//TODO remove a player //TODO remove a player
} }
void ServerApplication::UpdatePlayer(Packet::PlayerData& p) { void ServerApplication::UpdatePlayer(Packet& p) {
//TODO update a player //TODO update a player
} }
+9 -9
View File
@@ -55,18 +55,18 @@ private:
void UpdateWorld(double delta); void UpdateWorld(double delta);
//network loop //network loop
int HandlePacket(Packet::Packet); int HandlePacket(Packet);
void RelayPacket(Packet::Packet&); void RelayPacket(Packet&);
void SynchronizeEverything(Packet::Synchronize&); void SynchronizeEverything(Packet&);
void HandleBroadcast(Packet::BroadcastRequest&); void HandleBroadcast(Packet&);
void HandleConnection(Packet::JoinRequest&); void HandleConnection(Packet&);
void HandleDisconnection(Packet::Disconnect&); void HandleDisconnection(Packet&);
void AddPlayer(Packet::PlayerData&); void AddPlayer(Packet&);
void RemovePlayer(Packet::PlayerDelete&); void RemovePlayer(Packet&);
void UpdatePlayer(Packet::PlayerData&); void UpdatePlayer(Packet&);
//services //services
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get(); ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
+2 -4
View File
@@ -1,12 +1,10 @@
#include "vector2.hpp" #include "packet.hpp"
#include <iostream> #include <iostream>
using namespace std; using namespace std;
int main(int, char**) { int main(int, char**) {
Vector2 v = {1, 1}; Packet p;
Vector2 a = 99 * v;
cout << a.x << ", " << a.y << endl;
return 0; return 0;
} }