Implemented the log on and log off systems

This is a pretty straight forward port of the old version, including the
incredibly hacky server list. But I just need to remember that this is a
prototype.
This commit is contained in:
Kayne Ruse
2013-11-23 17:53:36 +11:00
parent ca86dc5fb8
commit 6ccc874583
11 changed files with 220 additions and 68 deletions
+1 -42
View File
@@ -42,45 +42,4 @@ int main(int argc, char** argv) {
}
cout << "Clean exit" << endl;
return 0;
}
/*/
#include "thread_safe_queue.hpp"
#include "SDL/SDL.h"
#include <iostream>
using namespace std;
struct Object {
int value = 0;
};
int func(void* arg) {
ThreadSafeQueue<Object>& queue = *reinterpret_cast<ThreadSafeQueue<Object>*>(arg);
while(1) {
Object o = queue.PopFront();
if (o.value != 0) {
cout << o.value;
SDL_Delay(500);
cout << endl;
}
}
}
int main(int, char**) {
ThreadSafeQueue<Object> queue;
SDL_Thread* thread1 = SDL_CreateThread(func, reinterpret_cast<void*>(&queue));
SDL_Thread* thread2 = SDL_CreateThread(func, reinterpret_cast<void*>(&queue));
SDL_Thread* thread3 = SDL_CreateThread(func, reinterpret_cast<void*>(&queue));
while(1) {
SDL_Delay(1000);
Object o;
o.value = 3;
queue.PushBack(o);
}
return 0;
}
//*/
}
+37 -5
View File
@@ -34,7 +34,7 @@ using namespace std;
//Declarations
//-------------------------
//int ServerApplication::ClientEntry::indexCounter = 0;
int ClientInformation::counter = 0;
//-------------------------
//Define the network thread
@@ -164,14 +164,46 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
snprintf(packet.serverInfo.name, PACKET_STRING_SIZE, "%s", config["server.name"].c_str());
networkUtil.Send(&packet.meta.srcAddress, &packet, sizeof(NetworkPacket));
break;
case NetworkPacket::Type::JOIN_REQUEST:
//
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++;
newClient.address = packet.meta.srcAddress;
//push the new client
clientInfo[newClient.index] = newClient;
//send the client their info
packet.meta.type = NetworkPacket::Type::JOIN_RESPONSE;
packet.clientInfo.index = newClient.index;
networkUtil.Send(&newClient.address, &packet, sizeof(NetworkPacket));
cout << "connect, total: " << clientInfo.size() << endl;
}
break;
case NetworkPacket::Type::DISCONNECT:
//
//disconnect the specified client
networkUtil.Send(&clientInfo[packet.clientInfo.index].address, &packet, sizeof(NetworkPacket));
clientInfo.erase(packet.clientInfo.index);
cout << "disconnect, total: " << clientInfo.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) {
networkUtil.Send(&it.second.address, &packet, sizeof(NetworkPacket));
}
cout << "shutting down" << endl;
break;
//handle errors
+11
View File
@@ -36,6 +36,15 @@
#include "config_utility.hpp"
#include "world_room.hpp"
#include <map>
//hold the client info
struct ClientInformation {
static int counter;
int index;
IPaddress address;
};
//The main application class
class ServerApplication {
public:
@@ -63,6 +72,8 @@ private:
bool running = true;
ConfigUtility config;
WorldRoom worldRoom;
std::map<int, ClientInformation> clientInfo;
};
#endif