Shadowrun: Awakened 29 September 2011 - Build 871
WorldServer.cpp
Go to the documentation of this file.
00001 #include "WorldServer.h"
00002 #include "ClientRegister.h"
00003 #include "MessagePump.h"
00004 #include "ReplyPacket.h"
00005 #include "JoinChannelPacket.h"
00006 
00007 namespace SraNetwork {
00008 
00009     //RakNet interfaces.
00010     static RakPeerInterface *m_rZoneInterface,*m_rClientInterface, *m_rChatInterface;
00011     static char public_key[cat::EasyHandshake::PUBLIC_KEY_BYTES];
00012     static char private_key[cat::EasyHandshake::PRIVATE_KEY_BYTES];
00013 
00014     WorldServer::WorldServer(void)
00015     {
00016     }
00017 
00018     WorldServer::~WorldServer(void)
00019     {
00020     }
00021 
00022 
00023     RakPeerInterface* WorldServer::getZoneInterface()
00024     {
00025         return m_rZoneInterface;
00026     }
00027 
00028     RakPeerInterface* WorldServer::getClientInterface()
00029     {
00030         return m_rClientInterface;
00031     }
00032     
00037     RakPeerInterface* WorldServer::getChatinterface(const int channelId)
00038     {
00039         //TODO: Support multiple chat server interfaces for
00040         // load balancing.
00041         return m_rChatInterface;
00042     }
00043 
00044     bool WorldServer::Initialize(void)
00045     {
00046         //Load key pair
00047         FILE* fp;
00048         fopen_s(&fp, "sra_public.key","r");
00049         if (!fp) 
00050         {
00051             printf("Public key not found!\n");
00052             return false;
00053         }
00054         fread(public_key, sizeof(public_key), 1, fp);
00055         fclose(fp);
00056 
00057         fopen_s(&fp, "sra_private.key", "r");
00058         if (!fp)
00059         {
00060             printf("Private key not found!\n");
00061             return false;
00062         }
00063         fread(private_key, sizeof(private_key), 1, fp);
00064         fclose(fp);
00065 
00066         return true;
00067     }
00068 
00069     void WorldServer::ChatServerConnectionThread()
00070     {
00071         printf("Listening for chat server ..... \n");
00072         m_rChatInterface = RakPeerInterface::GetInstance();
00073         SocketDescriptor chatSocket(SraNetwork::SERVER_CHAT_PORT, 0);
00074         m_rChatInterface->Startup(2, &chatSocket, 1);
00075         m_rChatInterface->SetMaximumIncomingConnections(2); //2 ?
00076 
00077         bool running = true;
00078         bool status = false;
00079 
00080         while (running)
00081         {
00082             Packet *packet = m_rChatInterface->Receive();
00083             if (packet) 
00084             {
00085                 if (packet->data[0] == ID_NEW_INCOMING_CONNECTION) 
00086                 {
00087                     printf("Chat server connected\n");
00088                     status = true;
00089                 }
00090                 running = false;
00091                 m_rChatInterface->DeallocatePacket(packet);
00092             }
00093             Sleep(10);
00094         }
00095         if (!status)
00096         {
00097             printf("Error while accepting chat server connection\n");
00098             m_rChatInterface->Shutdown(0);
00099             return;
00100         }
00101         running=true;
00102         while (running)
00103         {
00104             Packet *packet = m_rChatInterface->Receive();
00105             if (packet) 
00106             {
00107                 //We are processing the chat packets right here, because we have only
00108                 //one chat server, so no need for tasks here.....maybe later ^^
00109                 if ( packet->data[0] == ID_CONNECTION_LOST ) 
00110                 {
00111                     printf("Lost connection to chat server!\n");
00112                     running = false;
00113                 }  
00114                 else //if ( p->opCode == ID_CHAT_CHANNEL_REGISTER) 
00115                 {
00116                     // Now for our own packets. 
00117                     BitStream stream(packet->data, packet->length, false);
00118                     SraPacket sraPacket;
00119                     sraPacket.Deserialize(&stream);
00120                     switch (sraPacket.opCode)
00121                     {
00122                         case ID_CHAT_CHANNEL_REGISTER:
00123                         {
00124                             printf("Received a channel registration message from the chat server, forwarding it to the client!\n");
00125                             // We will only receive this packet if a 
00126                             // player has joined a channel succesfully
00127                             // So create a forward message with the server ip.
00128                             //If the channel join failed, send a reply to the client
00129                             JoinChannelPacket jp;
00130                             jp.Deserialize(&stream);
00131 
00133                             RakPeerInterface *clientI = SraNetwork::WorldServer::getClientInterface();
00134 
00135                             BitStream stream;
00136 
00137                             ReplyPacket rp;
00138                             rp.replyCode = ReplyPacket::OK;
00139                             rp.replyMessage = SraNetwork::CHAT_SERVER_ADDR;
00140                             rp.messageOpCode = SraNetwork::ID_CHAT_CHANNEL_REGISTER;
00141                             rp.Serialize(&stream);
00142                             clientI->Send( &stream, 
00143                             MEDIUM_PRIORITY, RELIABLE_ORDERED, 0, jp.clientAddress, false);
00144                         }
00145                         break;
00146                     }
00147                 }
00148                 //TODO: Check for other RakNet connection flags.
00149                 
00150                 m_rChatInterface->DeallocatePacket(packet);
00151             }
00152             Sleep(10);
00153         }
00154     }
00155 
00156 
00157     void WorldServer::ZoneServerConnectionThread()
00158     {
00159         printf("Listening for zones ....\n");
00160         m_rZoneInterface = RakPeerInterface::GetInstance();
00161         SocketDescriptor zoneSocket(SraNetwork::SERVER_ZONE_PORT, 0);
00162         m_rZoneInterface->Startup(2, &zoneSocket, 1);
00163         m_rZoneInterface->SetMaximumIncomingConnections(2);
00164         
00165         bool running = true;
00166         bool status = false;
00167         while (running)
00168         {
00169             Packet *packet = m_rZoneInterface->Receive();
00170             if (packet) 
00171             {
00172                 if (packet->data[0] == ID_NEW_INCOMING_CONNECTION) 
00173                 {
00174                     printf("Zone server connected\n");
00175                     status = true;
00176                 }
00177                 running = false;
00178                 m_rZoneInterface->DeallocatePacket(packet);
00179             }
00180             Sleep(10);
00181         }
00182         if (!status)
00183         {
00184             printf("Error while accepting zone server connection\n");
00185             m_rZoneInterface->Shutdown(0);
00186             return;
00187         }
00188         running=true;
00189         while (running)
00190         {
00191             Packet *packet = m_rZoneInterface->Receive();
00192             if (packet) 
00193             {
00194                 //We are processing the zone packets right here, because we have only
00195                 //one zone server, so no need for tasks here.....maybe later ^^
00196                 SraZonePacket* p = (SraZonePacket*)packet->data;
00197                 if ( packet->data[0] == ID_CONNECTION_LOST ) {
00198                     printf("Lost connection to zone server!\n");
00199                     running = false;
00200                 }  //TODO: Check for other RakNet connection flags.
00201                 else if ( p->toZoneID != -1) 
00202                 {
00203                     printf("Zone answer received, forwarding to client\n");
00204                     //Send ack to client
00205                     SraPacket2 answer;
00206                     ZeroMemory(&answer,sizeof(answer));
00207                     answer.opcode = ID_CONNECT_TO_SRV;
00208                     answer.pID = -1;
00209                     SystemAddress *sa = const_cast<SystemAddress*>(ClientRegister::getInstance()->getAddressFromClientID(p->clientID));
00210 
00211                     //We are now sending the address of the zone server and the port:
00212                     //TODO Reimpl this
00213                     /*std::string str;
00214                     str << SraNetwork::ZONE_SERVER_ADDR << ":" <<  p->toZoneID;
00215                     memcpy( answer.data, str.str().c_str(), strlen(str.str().c_str()));*/
00216 
00217                     //wcscpy_s( answer.data , str.str().c_str());
00218                     printf("Sending zone answer : %ls to clientID %d", answer.data, p->clientID);
00219                     m_rClientInterface->Send( (char*)&answer, sizeof(answer), HIGH_PRIORITY, RELIABLE_ORDERED, 0,*sa, false);
00220                 }
00221                 m_rZoneInterface->DeallocatePacket(packet);
00222             }
00223             Sleep(10);
00224         }
00225     }
00226 
00227     void WorldServer::ClientConnectionThread()
00228     {
00229         printf("Listening for clients ....\n");
00230         m_rClientInterface = RakPeerInterface::GetInstance();       
00231         //We are now using the RakNet security features:
00232         //if (!m_rClientInterface->InitializeSecurity(NULL, NULL))//InitializeSecurity(public_key,private_key)) 
00233         //{
00234         //  printf("Invalid keys!\n");
00235         //}
00236 
00237         SocketDescriptor clientSocket(SraNetwork::SERVER_CLIENT_PORT, 0);
00238         m_rClientInterface->Startup(SraNetwork::MAX_CLIENTS, &clientSocket, 1);
00239         m_rClientInterface->SetMaximumIncomingConnections(MAX_CLIENTS);
00240 
00241         //Root task:
00242         tbb::task& rootTask = *new( tbb::task::allocate_root() )tbb::empty_task;
00243         rootTask.set_ref_count( 2 );
00244         //int n = tbb::task_scheduler_init::default_num_threads();
00245         //tbb::task_scheduler_init init(3);
00246         //printf("Num Threads %d\n", n);
00247 
00248         while (1) 
00249         {
00250             Packet *packet = m_rClientInterface->Receive();
00251             if (packet) 
00252             {
00253                 // Add message to the queue
00254                 MessagePump::Instance->AddMessage( packet );
00255                 m_rClientInterface->DeallocatePacket(packet);
00256             }
00257             Sleep(10);
00258         }
00259     }
00260 
00261 }

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net