Renamed some things, and removed Client::index
This commit is contained in:
@@ -34,7 +34,7 @@ using namespace std;
|
|||||||
//Declarations
|
//Declarations
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
int ClientInformation::counter = 0;
|
int Client::counter = 0;
|
||||||
|
|
||||||
//-------------------------
|
//-------------------------
|
||||||
//Define the ServerApplication
|
//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());
|
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
|
||||||
network.Send(&packet.meta.srcAddress, &packet, sizeof(NetworkPacket));
|
network.Send(&packet.meta.srcAddress, &packet, sizeof(NetworkPacket));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NetworkPacket::Type::JOIN_REQUEST: {
|
case NetworkPacket::Type::JOIN_REQUEST: {
|
||||||
//TODO: prevent duplicate logins from the same address?
|
//TODO: prevent duplicate logins from the same address?
|
||||||
|
|
||||||
//create the new client, filling it with the correct info
|
//create the new client, filling it with the correct info
|
||||||
ClientInformation newClient;
|
Client newClient;
|
||||||
newClient.index = ClientInformation::counter++;
|
|
||||||
newClient.address = packet.meta.srcAddress;
|
newClient.address = packet.meta.srcAddress;
|
||||||
|
|
||||||
//push the new client
|
//push the new client
|
||||||
clientInfo[newClient.index] = newClient;
|
clientMap[Client::counter] = newClient;
|
||||||
|
|
||||||
//send the client their info
|
//send the client their info
|
||||||
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
|
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
|
||||||
packet.clientInfo.index = newClient.index;
|
packet.clientInfo.index = Client::counter;
|
||||||
network.Send(&newClient.address, &packet, sizeof(NetworkPacket));
|
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;
|
break;
|
||||||
|
|
||||||
case NetworkPacket::Type::DISCONNECT:
|
case NetworkPacket::Type::DISCONNECT:
|
||||||
//disconnect the specified client
|
//disconnect the specified client
|
||||||
network.Send(&clientInfo[packet.clientInfo.index].address, &packet, sizeof(NetworkPacket));
|
network.Send(&clientMap[packet.clientInfo.index].address, &packet, sizeof(NetworkPacket));
|
||||||
clientInfo.erase(packet.clientInfo.index);
|
clientMap.erase(packet.clientInfo.index);
|
||||||
|
|
||||||
cout << "disconnect, total: " << clientInfo.size() << endl;
|
//remove players?
|
||||||
|
|
||||||
|
cout << "disconnect, total: " << clientMap.size() << endl;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NetworkPacket::Type::SYNCHRONIZE:
|
case NetworkPacket::Type::SYNCHRONIZE:
|
||||||
//TODO
|
//TODO
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case NetworkPacket::Type::SHUTDOWN:
|
case NetworkPacket::Type::SHUTDOWN:
|
||||||
//end the server
|
//end the server
|
||||||
running = false;
|
running = false;
|
||||||
|
|
||||||
//disconnect all clients
|
//disconnect all clients
|
||||||
packet.meta.type = NetworkPacket::Type::DISCONNECT;
|
packet.meta.type = NetworkPacket::Type::DISCONNECT;
|
||||||
for (auto& it : clientInfo) {
|
for (auto& it : clientMap) {
|
||||||
network.Send(&it.second.address, &packet, sizeof(NetworkPacket));
|
network.Send(&it.second.address, &packet, sizeof(NetworkPacket));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -35,12 +35,10 @@
|
|||||||
|
|
||||||
//STL
|
//STL
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <queue>
|
|
||||||
|
|
||||||
//hold the client info
|
//hold the client info
|
||||||
struct ClientInformation {
|
struct Client {
|
||||||
static int counter;
|
static int counter;
|
||||||
int index;
|
|
||||||
IPaddress address;
|
IPaddress address;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -60,7 +58,6 @@ private:
|
|||||||
|
|
||||||
//networking
|
//networking
|
||||||
UDPNetworkUtility network;
|
UDPNetworkUtility network;
|
||||||
std::queue<NetworkPacket> networkQueue;
|
|
||||||
|
|
||||||
//database
|
//database
|
||||||
sqlite3* database = nullptr;
|
sqlite3* database = nullptr;
|
||||||
@@ -69,7 +66,7 @@ private:
|
|||||||
bool running = true;
|
bool running = true;
|
||||||
ConfigUtility config;
|
ConfigUtility config;
|
||||||
|
|
||||||
std::map<int, ClientInformation> clientInfo;
|
std::map<int, Client> clientMap;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user