Renamed some things, and removed Client::index
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
@@ -35,12 +35,10 @@
|
||||
|
||||
//STL
|
||||
#include <map>
|
||||
#include <queue>
|
||||
|
||||
//hold the client info
|
||||
struct ClientInformation {
|
||||
struct Client {
|
||||
static int counter;
|
||||
int index;
|
||||
IPaddress address;
|
||||
};
|
||||
|
||||
@@ -60,7 +58,6 @@ private:
|
||||
|
||||
//networking
|
||||
UDPNetworkUtility network;
|
||||
std::queue<NetworkPacket> networkQueue;
|
||||
|
||||
//database
|
||||
sqlite3* database = nullptr;
|
||||
@@ -69,7 +66,7 @@ private:
|
||||
bool running = true;
|
||||
ConfigUtility config;
|
||||
|
||||
std::map<int, ClientInformation> clientInfo;
|
||||
std::map<int, Client> clientMap;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user