Renamed some things, and removed Client::index

This commit is contained in:
Kayne Ruse
2013-12-01 18:36:18 +11:00
parent 1e0ed350fc
commit 865620b4a8
2 changed files with 19 additions and 15 deletions
+17 -10
View File
@@ -34,7 +34,7 @@ using namespace std;
//Declarations
//-------------------------
int ClientInformation::counter = 0;
int Client::counter = 0;
//-------------------------
//Define the ServerApplication
@@ -129,42 +129,49 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
network.Send(&packet.meta.srcAddress, &packet, sizeof(NetworkPacket));
break;
case NetworkPacket::Type::JOIN_REQUEST: {
//TODO: prevent duplicate logins from the same address?
//create the new client, filling it with the correct info
ClientInformation newClient;
newClient.index = ClientInformation::counter++;
Client newClient;
newClient.address = packet.meta.srcAddress;
//push the new client
clientInfo[newClient.index] = newClient;
clientMap[Client::counter] = newClient;
//send the client their info
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = newClient.index;
packet.clientInfo.index = Client::counter;
network.Send(&newClient.address, &packet, sizeof(NetworkPacket));
cout << "connect, total: " << clientInfo.size() << endl;
//finished this routine
Client::counter++;
cout << "connect, total: " << clientMap.size() << endl;
}
break;
case NetworkPacket::Type::DISCONNECT:
//disconnect the specified client
network.Send(&clientInfo[packet.clientInfo.index].address, &packet, sizeof(NetworkPacket));
clientInfo.erase(packet.clientInfo.index);
network.Send(&clientMap[packet.clientInfo.index].address, &packet, sizeof(NetworkPacket));
clientMap.erase(packet.clientInfo.index);
cout << "disconnect, total: " << clientInfo.size() << endl;
//remove players?
cout << "disconnect, total: " << clientMap.size() << endl;
break;
case NetworkPacket::Type::SYNCHRONIZE:
//TODO
break;
case NetworkPacket::Type::SHUTDOWN:
//end the server
running = false;
//disconnect all clients
packet.meta.type = NetworkPacket::Type::DISCONNECT;
for (auto& it : clientInfo) {
for (auto& it : clientMap) {
network.Send(&it.second.address, &packet, sizeof(NetworkPacket));
}