Moved some code

This commit is contained in:
Kayne Ruse
2013-05-24 18:06:29 +10:00
parent e2fed23659
commit 0d3a69106f
5 changed files with 112 additions and 78 deletions
+63 -52
View File
@@ -16,12 +16,52 @@ void ServerApplication::Init() {
void ServerApplication::Proc() {
while(running) {
HandleInput();
UpdateWorld();
HandleOutput();
//handle input
while(netUtil.Receive()) {
PacketData packet;
memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(PacketData));
switch(packet.type) {
// case PacketList::NONE:
// //
// break;
case PacketList::PING:
Ping(&packet);
break;
// case PacketList::PONG:
// //
// break;
case PacketList::JOINREQUEST:
JoinRequest(&packet);
break;
// case PacketList::JOINCONFIRM:
// //
// break;
case PacketList::DISCONNECT:
Disconnect(packet.disconnect.playerID);
break;
// case PacketList::SYNCHRONIZE:
// //
// break;
// case PacketList::NEWPLAYER:
// //
// break;
// case PacketList::DELETEPLAYER:
// //
// break;
case PacketList::MOVEMENT:
Movement(&packet);
break;
}
}
//update the world
delta.Calculate();
for (auto it : clientMap) {
it.second.Update(delta.GetDelta());
}
//handle output...
//TODO...
//debug
// running = false;
SDL_Delay(10);
}
}
@@ -31,56 +71,17 @@ void ServerApplication::Quit() {
SDLNet_Quit();
}
void ServerApplication::HandleInput() {
//accept new connections
//accept updates from the clients
//read the updates from the clients into internal containers
PacketData packet;
while(netUtil.Receive()) {
memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(PacketData));
switch(packet.type) {
case PacketList::PING:
Ping(&packet);
break;
case PacketList::JOINREQUEST:
JoinRequest(&packet);
break;
case PacketList::DISCONNECT:
Disconnect(packet.disconnect.playerID);
break;
case PacketList::MOVEMENT:
Movement(&packet);
break;
}
}
}
void ServerApplication::UpdateWorld() {
//update internals ie.
// ai
// loot drops
delta.Calculate();
for (auto it : clientMap) {
it.second.Update(delta.GetDelta());
}
}
void ServerApplication::HandleOutput() {
//send all information to new connections
//selective updates to existing connectons
}
//-------------------------
//network commands
//-------------------------
void ServerApplication::Ping(PacketData* packet) {
//respond to pings with the server name
if (!packet) {
return;
}
//respond to pings with the server name
packet->type = PacketList::PONG;
sprintf(packet->pong.metadata, "%s",configUtil.CString("servername"));
snprintf(packet->pong.metadata,PACKET_STRING_SIZE, "%s",configUtil.CString("servername"));
netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(packet), sizeof(PacketData));
}
@@ -95,14 +96,16 @@ void ServerApplication::JoinRequest(PacketData* packet) {
clientMap[playerID].channel = netUtil.Bind(&netUtil.GetInPacket()->address, -1);
clientMap[playerID].handle = packet->joinRequest.handle;
clientMap[playerID].avatar = packet->joinRequest.avatar;
cout << "New player: " << playerID << endl;
//debug
#ifdef DEBUG
cout << "playerID: " << clientMap[playerID].playerID << endl;
cout << "channel: " << clientMap[playerID].channel << endl;
cout << "handle: " << clientMap[playerID].handle << endl;
cout << "avatar: " << clientMap[playerID].avatar << endl;
cout << "New player:" << endl;
cout << "\tplayerID: " << clientMap[playerID].playerID << endl;
cout << "\tchannel: " << clientMap[playerID].channel << endl;
cout << "\thandle: " << clientMap[playerID].handle << endl;
cout << "\tavatar: " << clientMap[playerID].avatar << endl;
#else
cout << "New player: " << playerID << endl;
#endif
//join confirm
@@ -110,16 +113,24 @@ void ServerApplication::JoinRequest(PacketData* packet) {
p.type = PacketList::JOINCONFIRM;
p.joinConfirm.playerID = clientMap[playerID].playerID;
netUtil.Send(clientMap[playerID].channel, &p, sizeof(PacketData));
//TODO: NewPlayer()
#ifdef DEBUG
cout << "current players: " << clientMap.size() << endl;
#endif
}
void ServerApplication::Disconnect(int playerID) {
//TODO: Delete player
if (clientMap.find(playerID) == clientMap.end()) {
return;
}
cout << "disconnecting: " << playerID << endl;
netUtil.Unbind(clientMap[playerID].channel);
clientMap.erase(playerID);
//TODO: Delete player
#ifdef DEBUG
cout << "current players: " << clientMap.size() << endl;
#endif