00001
00002
00003
00004
00005
00006
00007 #include <netLobby.h>
00008 #include <netThread.h>
00009 #include <engine.h>
00010 #include <string>
00011 #include <messages.h>
00012
00013 using boost::asio::ip::udp;
00014
00015 FastEcslent::NetLobby::NetLobby(Engine* eng):engine(eng) {
00016 init();
00017 }
00018
00019 void FastEcslent::NetLobby::run(){
00020 netThread = boost::thread(&FastEcslent::NetLobby::runThread, this);
00021 }
00022
00023 void FastEcslent::NetLobby::init() {
00024 netCommon = new NetCommon();
00025
00026 std::pair<boost::asio::ip::udp::endpoint, std::string> ippair = netCommon->getMyIP();
00027
00028 udp::endpoint myIP =ippair.first;
00029 myIPAddress = ippair.second;
00030
00031 listener = new Listener(myIP, engine->options.networkPort, engine->options.isServer);
00032 listener->init(myIPAddress);
00033 sender = new Sender(myIP, engine->options.networkPort, engine->options.isServer);
00034 sender->init();
00035
00036 quit = false;
00037 startTime = getCurrentTime();
00038 TICK_SLEEP_TIME = 10;
00039 sleepTime = new boost::posix_time::milliseconds(100);
00040 lobbySleepTime = new boost::posix_time::milliseconds(300);
00041 }
00042
00043 void FastEcslent::NetLobby::runThread(){
00044 listener->run();
00045 sender->run();
00046
00047 while (!quit){
00048 if (engine->options.isServer) {
00049 serverTick();
00050 boost::this_thread::sleep(*(lobbySleepTime));
00051 } else {
00052 clientTick();
00053 boost::this_thread::sleep(*(sleepTime));
00054 }
00055 }
00056 }
00057
00061 void FastEcslent::NetLobby::serverTick(){
00062 sendLobbyServerInfo();
00063 handleClientMessages();
00064 }
00065
00066 void FastEcslent::NetLobby::sendLobbyServerInfo(){
00067 if(!(engine->gfx->gimPtr->lobbyMgr))
00068 return;
00069
00070 Message *m = new Message();
00071 m->head.msgType = LOBBYSERVERTYPE;
00072 m->head.millisecondsFromStart = netCommon->getCurrentTimeLong();
00073 m->head.sizeOfStruct = LobbyServerSize;
00074
00075
00076 unsigned int messageSize = 0;
00077 unsigned int offset = 0;
00078
00079 for(std::map<std::string, OgreGFX::LobbyNode*>::iterator i= engine->gfx->gimPtr->lobbyMgr->nodeMap.begin();i!= engine->gfx->gimPtr->lobbyMgr->nodeMap.end();i++){
00080 if(i->second){
00081 LobbyServer *ls = fillLobbyClients(i->second->name, i->second->ip, i->second->port, i->second->side, i->second->player, i->second->slot, i->second->isHost);
00082 memcpy((void*)(m->data + offset), (void *)ls, LobbyServerSize);
00083 offset += LobbyServerSize;
00084 delete ls;
00085 messageSize++;
00086 }
00087 }
00088
00089 m->head.numberOfStructs = messageSize;
00090 sender->addMessage(m);
00091 }
00092
00093 FastEcslent::LobbyServer* FastEcslent::NetLobby::fillLobbyClients(const std::string &gameName, const std::string &ip, int port, int side, int player, int slot, bool isHost){
00094 LobbyServer *l = new LobbyServer();
00095 l->port = port;
00096
00097 char gName[64];
00098 sprintf(gName, "%63s", gameName.c_str());
00099 strcpy(l->name, gName);
00100
00101 l->ip = ip_string_to_int(ip);
00102 l->side = side;
00103 l->player = player;
00104 l->slot = slot;
00105 l->isHost = isHost?'1':'0';
00106 return l;
00107 }
00108
00109 void FastEcslent::NetLobby::joinRequest(Message *m){
00110 LobbyJoinRequest *req = new LobbyJoinRequest();
00111 memcpy((void *)req, (void*)(m->data), LobbyJoinRequestSize);
00112
00113 if(ip_int_to_string(req->serverIP) == myIPAddress){
00114 engine->gfx->gimPtr->lobbyMgr->handleJoinRequest(trim(std::string(req->clientName)), ip_int_to_string(req->clientIP), req->port);
00115 }
00116
00117 delete req;
00118 }
00119
00120 void FastEcslent::NetLobby::startByRemote(Message *m){
00121 LobbyGameStart *ls = new LobbyGameStart();
00122 memcpy((void *)ls, (void*)(m->data), LobbyGameStartSize);
00123
00124 engine->gfx->gimPtr->lobbyMgr->startGame(ip_int_to_string(ls->serverIP),ls->port);
00125
00126 delete ls;
00127 }
00128
00129 void FastEcslent::NetLobby::sendLobbyClientResponse(const std::string &clientIP, int port, int side, int player){
00130 Message *m = new Message();
00131 LobbyJoinResponse *l = new LobbyJoinResponse();
00132 l->port = engine->options.networkPort;
00133 l->side = side;
00134 l->player = player;
00135 l->clientIP = ip_string_to_int(clientIP);
00136 l->serverIP = ip_string_to_int(myIPAddress);
00137
00138 m->head.msgType = LOBBYJOINRESPONSETYPE;
00139 m->head.millisecondsFromStart = netCommon->getCurrentTimeLong();
00140 m->head.numberOfStructs = 1;
00141 m->head.sizeOfStruct = LobbyJoinResponseSize;
00142
00143 memcpy((void*)(m->data) , (void *)l, LobbyJoinResponseSize);
00144
00145 sender->addMessage(m);
00146
00147 delete l;
00148 }
00149
00150
00151 void FastEcslent::NetLobby::clientTick(){
00152 handleServerMessages();
00153 }
00154
00155 void FastEcslent::NetLobby::updateLobbyServer(Message *m){
00156 unsigned int offset = 0;
00157 for(int i=0;i<m->head.numberOfStructs;i++){
00158 LobbyServer *ls = new LobbyServer();
00159 memcpy((void *)ls, (void*)(m->data + offset), LobbyServerSize);
00160 offset += LobbyServerSize;
00161
00162 std::string gName = ls->name;
00163 std::string ip =ip_int_to_string(ls->ip);
00164 int port = ls->port;
00165 int side = ls->side;
00166 int player = ls->player;
00167 int slot = ls->slot;
00168 bool isHost = ls->isHost=='1'?true:false;
00169 gName = trim(gName);
00170
00171 if(engine->gfx->gimPtr->lobbyMgr){
00172 if(isHost){
00173 engine->gfx->gimPtr->lobbyMgr->addRemoteServer(gName, ip, port);
00174 }
00175
00176 engine->gfx->gimPtr->lobbyMgr->addNode(gName, ip, port, side, player, slot, isHost);
00177 }
00178
00179 delete ls;
00180 }
00181 }
00182
00183 void FastEcslent::NetLobby::handleClientMessages(){
00184 Message *m = listener->dequeMessage();
00185 while (m) {
00186 if((int) m->head.msgType == FastEcslent::LOBBYJOINREQUESTTYPE) {
00187 joinRequest(m);
00188 }else if ((int) m->head.msgType == FastEcslent::LOBBYEXITTYPE){
00189 clientExit(m);
00190 }else if ((int) m->head.msgType == FastEcslent::LOBBYSLOTSWAPTYPE){
00191 clientSwap(m);
00192 }
00193 delete m;
00194 m = listener->dequeMessage();
00195 }
00196 }
00197
00198 void FastEcslent::NetLobby::handleServerMessages(){
00199 Message *m = listener->dequeMessage();
00200 while (m) {
00201 if ((int)m->head.msgType == FastEcslent::LOBBYSERVERTYPE){
00202 updateLobbyServer(m);
00203 }else if ((int)m->head.msgType == FastEcslent::LOBBYJOINRESPONSETYPE){
00204 clientEnter(m);
00205 }else if ((int)m->head.msgType == FastEcslent::LOBBYGAMESTARTTYPE){
00206 startByRemote(m);
00207 }else if((int) m->head.msgType == FastEcslent::LOBBYEXITTYPE){
00208 serverExit(m);
00209 }
00210 delete m;
00211 m = listener->dequeMessage();
00212 }
00213 }
00214
00215 void FastEcslent::NetLobby::clientEnter(Message *m){
00216 LobbyJoinResponse *ljr = new LobbyJoinResponse();
00217 memcpy((void *)ljr, (void *)(m->data), LobbyJoinResponseSize);
00218 if(engine->gfx->gimPtr->lobbyMgr){
00219 engine->gfx->gimPtr->lobbyMgr->lobbyEnter(ip_int_to_string(ljr->serverIP), ljr->port, ljr->side, ljr->player);
00220 }
00221 delete ljr;
00222 }
00223
00224 void FastEcslent::NetLobby::clientExit(Message *m){
00225 LobbyExit *ljr = new LobbyExit();
00226 memcpy((void *)ljr, (void *)(m->data), LobbyExitSize);
00227 if(engine->gfx->gimPtr->lobbyMgr){
00228 std::pair<std::string, int> host = engine->gfx->gimPtr->lobbyMgr->getLobbyHost();
00229 if(ip_int_to_string(ljr->serverIP) == host.first && host.second == host.second){
00230 engine->gfx->gimPtr->lobbyMgr->lobbyExit(ip_int_to_string(ljr->clientIP), ljr->port);
00231 }
00232 }
00233 delete ljr;
00234 }
00235
00236 void FastEcslent::NetLobby::serverExit(Message *m){
00237 LobbyExit *ljr = new LobbyExit();
00238 memcpy((void *)ljr, (void *)(m->data), LobbyExitSize);
00239 if(engine->gfx->gimPtr->lobbyMgr){
00240 std::pair<std::string, int> host = engine->gfx->gimPtr->lobbyMgr->getLobbyHost();
00241 if(ip_int_to_string(ljr->serverIP) == host.first && host.second == host.second){
00242 engine->gfx->gimPtr->lobbyMgr->lobbyExit(ip_int_to_string(ljr->clientIP), ljr->port);
00243 }
00244 }
00245 delete ljr;
00246 }
00247
00248 void FastEcslent::NetLobby::clientSwap(Message *m){
00249 LobbySlotSwap *ls = new LobbySlotSwap();
00250 memcpy((void *)ls, (void *)(m->data), LobbySlotSwapSize);
00251 if(engine->gfx->gimPtr->lobbyMgr){
00252 if(ip_int_to_string(ls->serverIP) == myIPAddress && ls->port == engine->options.networkPort){
00253 engine->gfx->gimPtr->lobbyMgr->swapNodeSlot(ls->newpos, ls->oldpos, ls->newside, ls->newplayer);
00254 }
00255 }
00256 delete ls;
00257 }
00258
00259
00260 void FastEcslent::NetLobby::lobbyStart(){
00261 Message *m = new Message();
00262 LobbyGameStart *ls = new LobbyGameStart();
00263 ls->port = engine->options.networkPort;
00264 ls->serverIP = ip_string_to_int(myIPAddress);
00265
00266 m->head.msgType = LOBBYGAMESTARTTYPE;
00267 m->head.millisecondsFromStart = netCommon->getCurrentTimeLong();
00268 m->head.numberOfStructs = 1;
00269 m->head.sizeOfStruct = LobbyGameStartSize;
00270
00271 memcpy((void*)(m->data) , (void *)ls, LobbyGameStartSize);
00272
00273 sender->addMessage(m);
00274
00275 delete ls;
00276 }
00277
00278 void FastEcslent::NetLobby::lobbyJoinGame(const std::string &ipAddress, int port){
00279 Message *m = new Message();
00280 LobbyJoinRequest *l = new LobbyJoinRequest();
00281 l->port = port;
00282 l->serverIP = ip_string_to_int(ipAddress);
00283 l->clientIP = ip_string_to_int(myIPAddress);
00284
00285 char clientName[64];
00286 sprintf(clientName, "%63s", "clientName");
00287 strcpy(l->clientName, clientName);
00288
00289 m->head.msgType = LOBBYJOINREQUESTTYPE;
00290 m->head.millisecondsFromStart = netCommon->getCurrentTimeLong();
00291 m->head.numberOfStructs = 1;
00292 m->head.sizeOfStruct = LobbyJoinRequestSize;
00293
00294 memcpy((void*)(m->data) , (void *)l, LobbyJoinRequestSize);
00295
00296 sender->addMessage(m);
00297
00298 delete l;
00299 }
00300
00301 void FastEcslent::NetLobby::lobbyExitGame(const std::string &serverIP, int port){
00302 Message *m = new Message();
00303 LobbyExit *le = new LobbyExit();
00304
00305 le->port = port;
00306
00307 le->serverIP = ip_string_to_int(serverIP);
00308 le->clientIP = ip_string_to_int(myIPAddress);
00309
00310 m->head.msgType = LOBBYEXITTYPE;
00311 m->head.millisecondsFromStart = netCommon->getCurrentTimeLong();
00312 m->head.numberOfStructs = 1;
00313 m->head.sizeOfStruct = LobbyExitSize;
00314
00315 memcpy((void*)(m->data) , (void *)le, LobbyExitSize);
00316
00317 sender->addMessage(m);
00318
00319 delete le;
00320 }
00321
00322 void FastEcslent::NetLobby::lobbySlotSwapReq(const std::string &serverip, int port, int newpos, int oldpos, int newside, int newplayer){
00323 Message *m = new Message();
00324 LobbySlotSwap *le = new LobbySlotSwap();
00325
00326 le->serverIP = ip_string_to_int(serverip);
00327 le->port = port;
00328
00329 le->newpos = newpos;
00330 le->oldpos = oldpos;
00331
00332 le->newside = newside;
00333 le->newplayer = newplayer;
00334
00335 m->head.msgType = LOBBYSLOTSWAPTYPE;
00336 m->head.millisecondsFromStart = netCommon->getCurrentTimeLong();
00337 m->head.numberOfStructs = 1;
00338 m->head.sizeOfStruct = LobbySlotSwapSize;
00339
00340 memcpy((void*)(m->data) , (void *)le, LobbySlotSwapSize);
00341
00342 sender->addMessage(m);
00343
00344 delete le;
00345 }
00346 FastEcslent::NetLobby::~NetLobby(){
00347 delete netCommon;
00348 delete listener;
00349 delete sender;
00350 delete sleepTime;
00351 delete lobbySleepTime;
00352 }