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