Added some multiplayer code to the server

This commit is contained in:
Kayne Ruse
2013-06-24 13:31:19 +10:00
parent 23782ff4e3
commit 2235f307e7
5 changed files with 100 additions and 27 deletions
-1
View File
@@ -27,7 +27,6 @@
struct ClientEntry { struct ClientEntry {
int index; int index;
IPaddress address; IPaddress address;
int playerIndex;
}; };
#endif #endif
+25 -4
View File
@@ -45,7 +45,9 @@ enum class Type {
SYNCHRONIZE = 8, SYNCHRONIZE = 8,
PLAYER = 9, PLAYER_NEW = 9,
PLAYER_DELETE = 10,
PLAYER_UPDATE = 11,
}; };
struct Metadata { struct Metadata {
@@ -90,11 +92,28 @@ struct Synchronize {
Metadata meta; Metadata meta;
}; };
struct Player { struct PlayerNew {
Metadata meta; Metadata meta;
//player data int playerIndex;
int clientIndex;
char handle[PACKET_STRING_SIZE];
char avatar[PACKET_STRING_SIZE];
//TODO Playerdata
}; };
struct PlayerDelete {
Metadata meta;
int playerIndex;
int clientIndex;
};
struct PlayerUpdate {
Metadata meta;
int playerIndex;
int clientIndex;
Vector2 position;
Vector2 motion;
};
union Packet { union Packet {
Packet() { Packet() {
@@ -114,7 +133,9 @@ union Packet {
Synchronize sync; Synchronize sync;
Player player; PlayerNew playerNew;
PlayerDelete playerDelete;
PlayerUpdate playerUpdate;
#ifdef DEBUG #ifdef DEBUG
char buffer[1024]; char buffer[1024];
+1
View File
@@ -29,6 +29,7 @@
struct ServerEntry { struct ServerEntry {
std::string name; std::string name;
IPaddress address; IPaddress address;
//TODO: player count
}; };
#endif #endif
+66 -21
View File
@@ -165,24 +165,34 @@ int ServerApplication::HandlePacket(Packet::Packet p) {
case Packet::Type::DISCONNECT: case Packet::Type::DISCONNECT:
HandleDisconnection(p.disconnect); HandleDisconnection(p.disconnect);
break; break;
// case PacketType::SYNCHRONIZE: // case Packet::Type::SYNCHRONIZE:
// //
// break;
// case PacketType::PLAYER_NEW:
// //
// break;
// case PacketType::PLAYER_DELETE:
// //
// break;
// case PacketType::PLAYER_MOVE:
// // // //
// break; // break;
case Packet::Type::PLAYER_NEW:
AddPlayer(p.playerNew);
RelayPacket(p);
break;
case Packet::Type::PLAYER_DELETE:
DeletePlayer(p.playerDelete);
RelayPacket(p);
break;
case Packet::Type::PLAYER_UPDATE:
UpdatePlayer(p.playerUpdate);
RelayPacket(p);
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))));
} }
return 1; return 1;
} }
void ServerApplication::RelayPacket(Packet::Packet& p) {
//pump this packet to all clients
for (auto it : clients) {
netUtil->Send(&it.second.address, &p, sizeof(Packet::Packet));
}
}
void ServerApplication::HandleBroadcast(Packet::BroadcastRequest& bcast) { void ServerApplication::HandleBroadcast(Packet::BroadcastRequest& 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::Packet p;
@@ -193,21 +203,26 @@ void ServerApplication::HandleBroadcast(Packet::BroadcastRequest& bcast) {
} }
void ServerApplication::HandleConnection(Packet::JoinRequest& request) { void ServerApplication::HandleConnection(Packet::JoinRequest& request) {
//create the containers //create the entries
ClientEntry client = {uniqueIndex++, request.meta.address}; ClientEntry newClient = {
uniqueIndex++,
request.meta.address
};
//push this information //push this information
clients[client.index] = client; clients[newClient.index] = newClient;
//send the player their information //send the player their information
Packet::Packet p; Packet::Packet p;
p.meta.type = Packet::Type::JOIN_RESPONSE; p.meta.type = Packet::Type::JOIN_RESPONSE;
p.joinResponse.clientIndex = client.index; p.joinResponse.clientIndex = newClient.index;
//TODO: resource list
netUtil->Send(&client.address, &p, sizeof(Packet::Packet));
//pretty //TODO: resource list
cout << "New connection: index " << client.index << endl;
netUtil->Send(&newClient.address, &p, sizeof(Packet::Packet));
//debugging
cout << "New connection: index " << newClient.index << endl;
cout << "number of clients: " << clients.size() << endl; cout << "number of clients: " << clients.size() << endl;
} }
@@ -216,9 +231,39 @@ void ServerApplication::HandleDisconnection(Packet::Disconnect& disconnect) {
netUtil->Send(&clients[disconnect.clientIndex].address, &disconnect, sizeof(Packet::Packet)); netUtil->Send(&clients[disconnect.clientIndex].address, &disconnect, sizeof(Packet::Packet));
clients.erase(disconnect.clientIndex); clients.erase(disconnect.clientIndex);
//remove the player... //TODO remove the player...
//remove if(...)
//pretty //debugging
cout << "Lost connection: index " << disconnect.clientIndex << endl; cout << "Lost connection: index " << disconnect.clientIndex << endl;
cout << "number of clients: " << clients.size() << endl; cout << "number of clients: " << clients.size() << endl;
} }
void ServerApplication::AddPlayer(Packet::PlayerNew& playerNew) {
//add the player
PlayerEntry newPlayer = {
uniqueIndex++,
playerNew.clientIndex,
playerNew.handle,
playerNew.avatar,
{0,0},
{0,0}
};
players[newPlayer.index] = newPlayer;
//prep for relay
playerNew.playerIndex = newPlayer.index;
//debugging
cout << "New player " << newPlayer.handle << "Has joined the game" << endl;
cout << "Number of players: " << players.size() << endl;
}
void ServerApplication::DeletePlayer(Packet::PlayerDelete& playerDelete) {
//TODO remove a player
}
void ServerApplication::UpdatePlayer(Packet::PlayerUpdate& playerUpdate) {
//TODO update a player
}
+8 -1
View File
@@ -38,6 +38,7 @@
#include <map> #include <map>
#include <string> #include <string>
#include <algorithm>
class ServerApplication { class ServerApplication {
public: public:
@@ -54,11 +55,17 @@ private:
void UpdateWorld(double delta); void UpdateWorld(double delta);
//network loop //network loop
int HandlePacket(Packet::Packet p); int HandlePacket(Packet::Packet);
void RelayPacket(Packet::Packet&);
void HandleBroadcast(Packet::BroadcastRequest&); void HandleBroadcast(Packet::BroadcastRequest&);
void HandleConnection(Packet::JoinRequest&); void HandleConnection(Packet::JoinRequest&);
void HandleDisconnection(Packet::Disconnect&); void HandleDisconnection(Packet::Disconnect&);
void AddPlayer(Packet::PlayerNew&);
void DeletePlayer(Packet::PlayerDelete&);
void UpdatePlayer(Packet::PlayerUpdate&);
//services //services
ConfigUtility* configUtil = Singleton<ConfigUtility>::Get(); ConfigUtility* configUtil = Singleton<ConfigUtility>::Get();
UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get(); UDPNetworkUtility* netUtil = Singleton<UDPNetworkUtility>::Get();