Started working on receiving information about other players

This commit is contained in:
Kayne Ruse
2013-05-24 19:21:50 +10:00
parent 0d3a69106f
commit 4c228e0e36
8 changed files with 112 additions and 13 deletions
+41 -1
View File
@@ -20,6 +20,7 @@ InGame::InGame(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nU
} }
InGame::~InGame() { InGame::~InGame() {
//placeholder disconnect
PacketData p; PacketData p;
p.type = PacketList::DISCONNECT; p.type = PacketList::DISCONNECT;
p.disconnect.playerID = *playerID; p.disconnect.playerID = *playerID;
@@ -44,7 +45,46 @@ void InGame::FrameEnd() {
} }
void InGame::Update() { void InGame::Update() {
// Receive();
}
void InGame::Receive() {
PacketData packet;
while(netUtil->Receive()) {
memcpy(&packet, netUtil->GetInData(), sizeof(PacketData));
switch(packet.type) {
// case PacketList::NONE:
// //
// break;
// case PacketList::PING:
// //
// break;
// case PacketList::PONG:
// //
// break;
// case PacketList::JOINREQUEST:
// //
// break;
// case PacketList::JOINCONFIRM:
// //
// break;
// case PacketList::DISCONNECT:
// //
// break;
// case PacketList::SYNCHRONIZE:
// //
// break;
case PacketList::NEWPLAYER:
cout << "NEWPLAYER triggered" << endl;
break;
// case PacketList::DELETEPLAYER:
// //
// break;
// case PacketList::MOVEMENT:
// //
// break;
}
}
} }
void InGame::Render(SDL_Surface* const screen) { void InGame::Render(SDL_Surface* const screen) {
+1
View File
@@ -19,6 +19,7 @@ protected:
virtual void FrameStart(); virtual void FrameStart();
virtual void FrameEnd(); virtual void FrameEnd();
virtual void Update(); virtual void Update();
virtual void Receive();
virtual void Render(SDL_Surface* const); virtual void Render(SDL_Surface* const);
//Event handlers //Event handlers
+9 -5
View File
@@ -63,7 +63,6 @@ void Lobby::Update() {
} }
void Lobby::Receive() { void Lobby::Receive() {
//dump to the console
PacketData packet; PacketData packet;
while(netUtil->Receive()) { while(netUtil->Receive()) {
memcpy(&packet, netUtil->GetInData(), sizeof(PacketData)); memcpy(&packet, netUtil->GetInData(), sizeof(PacketData));
@@ -80,12 +79,13 @@ void Lobby::Receive() {
// case PacketList::JOINREQUEST: // case PacketList::JOINREQUEST:
// // // //
// break; // break;
case PacketList::JOINCONFIRM: case PacketList::JOINCONFIRM: {
PacketData p; PacketData p;
memcpy(&p, netUtil->GetInData(), sizeof(PacketData)); memcpy(&p, netUtil->GetInData(), sizeof(PacketData));
*playerID = p.joinConfirm.playerID; *playerID = p.joinConfirm.playerID;
netUtil->Bind(&netUtil->GetInPacket()->address, 0); netUtil->Bind(&netUtil->GetInPacket()->address, 0);
SetNextScene(SceneList::INGAME); SetNextScene(SceneList::INGAME);
}
break; break;
// case PacketList::DISCONNECT: // case PacketList::DISCONNECT:
// // // //
@@ -93,9 +93,12 @@ void Lobby::Receive() {
// case PacketList::SYNCHRONIZE: // case PacketList::SYNCHRONIZE:
// // // //
// break; // break;
// case PacketList::NEWPLAYER: #ifdef DEBUG
// // //this might not be the end of the world; it only happens when the game ping is too low
// break; case PacketList::NEWPLAYER:
cout << "WARNING: new player triggered unexpectedly" << endl;
break;
#endif
// case PacketList::DELETEPLAYER: // case PacketList::DELETEPLAYER:
// // // //
// break; // break;
@@ -195,6 +198,7 @@ void Lobby::PushServer(PacketData* packet) {
void Lobby::JoinRequest(ServerData* server) { void Lobby::JoinRequest(ServerData* server) {
if (!server) { if (!server) {
//CAN receive null
return; return;
} }
PacketData p; PacketData p;
+1
View File
@@ -69,6 +69,7 @@ struct NewPlayer {
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;
}; };
struct DeletePlayer { struct DeletePlayer {
+22
View File
@@ -50,3 +50,25 @@ Receive:
player update: player update:
PlayerManager.Update(message) PlayerManager.Update(message)
end end
-------------------------
--send info about the specified client to all clients
--use this for new connections, movement, etc.
SendClientData(int playerID):
for (clientMap):
Send(it.channel, clientMap[playerID])
end
end
NewClientData := SendClientData
--send all info about the server to the specified client
--send this to new connections
SynchronizeClient(int playerID):
for (clientMap):
Send(clientMap[playerID].channel, it)
end
end
+1 -2
View File
@@ -25,8 +25,7 @@ struct ClientData {
std::string avatar; std::string avatar;
enum class Command { enum class Command {
NORMAL, NORMAL,
SYNCHRONIZE, CHANGED,
PING,
}command = Command::NORMAL; }command = Command::NORMAL;
}; };
+32 -3
View File
@@ -78,7 +78,7 @@ void ServerApplication::Quit() {
void ServerApplication::Ping(PacketData* packet) { void ServerApplication::Ping(PacketData* packet) {
//respond to pings with the server name //respond to pings with the server name
if (!packet) { if (!packet) {
return; throw(runtime_error("Ping() received null"));
} }
packet->type = PacketList::PONG; packet->type = PacketList::PONG;
snprintf(packet->pong.metadata,PACKET_STRING_SIZE, "%s",configUtil.CString("servername")); snprintf(packet->pong.metadata,PACKET_STRING_SIZE, "%s",configUtil.CString("servername"));
@@ -114,7 +114,8 @@ void ServerApplication::JoinRequest(PacketData* packet) {
p.joinConfirm.playerID = clientMap[playerID].playerID; p.joinConfirm.playerID = clientMap[playerID].playerID;
netUtil.Send(clientMap[playerID].channel, &p, sizeof(PacketData)); netUtil.Send(clientMap[playerID].channel, &p, sizeof(PacketData));
//TODO: NewPlayer() //send it out to the clients
NewClientData(playerID);
#ifdef DEBUG #ifdef DEBUG
cout << "current players: " << clientMap.size() << endl; cout << "current players: " << clientMap.size() << endl;
@@ -138,12 +139,40 @@ void ServerApplication::Disconnect(int playerID) {
void ServerApplication::Movement(PacketData* packet) { void ServerApplication::Movement(PacketData* packet) {
if (!packet) { if (!packet) {
return; throw(runtime_error("Movement() received null"));
} }
clientMap[packet->movement.playerID].position = packet->movement.position; clientMap[packet->movement.playerID].position = packet->movement.position;
clientMap[packet->movement.playerID].motion = packet->movement.motion; clientMap[packet->movement.playerID].motion = packet->movement.motion;
//simple relay //simple relay
//TODO: SendClientData(packet->movement.playerID);
for (auto it : clientMap) { for (auto it : clientMap) {
netUtil.Send(it.second.channel, packet, sizeof(PacketData)); netUtil.Send(it.second.channel, packet, sizeof(PacketData));
} }
} }
void ServerApplication::NewClientData(int playerID) {
if (clientMap.find(playerID) == clientMap.end()) {
throw(runtime_error("NewClientData() Failed to find the client"));
}
//create the packet to send
PacketData p;
p.type = PacketList::NEWPLAYER;
p.newPlayer.playerID = clientMap[playerID].playerID;
snprintf(p.newPlayer.handle, PACKET_STRING_SIZE, "%s", clientMap[playerID].handle.c_str());
snprintf(p.newPlayer.avatar, PACKET_STRING_SIZE, "%s", clientMap[playerID].avatar.c_str());
p.newPlayer.position = clientMap[playerID].position;
p.newPlayer.motion = clientMap[playerID].motion;
//send the packet
for (auto it : clientMap) {
netUtil.Send(it.second.channel, &p, sizeof(PacketData));
}
}
void ServerApplication::SendClientData(int playerID) {
//TODO
}
void ServerApplication::SynchronizeClient(int playerID) {
//TODO
}
+5 -2
View File
@@ -27,14 +27,17 @@ private:
void Disconnect(int playerID); void Disconnect(int playerID);
void Movement(PacketData*); void Movement(PacketData*);
bool running = false; void NewClientData(int playerID);
Delta delta; void SendClientData(int playerID);
void SynchronizeClient(int playerID);
//globals //globals
ConfigUtility configUtil; ConfigUtility configUtil;
UDPNetworkUtility netUtil; UDPNetworkUtility netUtil;
//members //members
bool running = false;
Delta delta;
std::map<int, ClientData> clientMap; std::map<int, ClientData> clientMap;
int maxClients = SDLNET_MAX_UDPCHANNELS; int maxClients = SDLNET_MAX_UDPCHANNELS;
int uniqueIndex = 0; int uniqueIndex = 0;