Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

gne.cpp

Go to the documentation of this file.
00001 //Mutexar någonstans??
00002 
00003 //Lägg in så att updatePlayersFromServer görs automatiskt
00004 
00005 //sätta dit eller ta bort LOG_ERRORS??
00006 
00007 //använd timestamplarna till nått kanske? eller ta bort dom..
00008 
00009 //Om man dissar med en klient så borde man kunna ansluta igen med samma..
00010 
00011 //lägg in lite if isrunning för å se så att servern är igång..
00012 
00013 //Stäng inte ned listen tråden.. låt den vara uppe och svara nej på nya anslutningar istället?
00014 
00015 //Fixa stopserver och disconnect så dom funkar bra.. kolla även i disconnect i Connection.
00016 
00017 // kolla 333:55  bör ej vara exit(-1)
00018 
00019 
00020 // kolla bara så jag inte har lyckats göra så att idlist[i] = i.. effektivisera isf..
00021 
00022 #include "gne.h"
00023 
00024 #define TIMEOUT_TS 10000
00025 
00026 Gne::Gne(){
00027         msgQueue = NULL;
00028         self = NULL;
00029         freeSlotsThreads = NULL;
00030         playerIdSlots = NULL;
00031         connection = NULL;
00032         clients = NULL;
00033 
00034         this->isrunning = false;
00035         this->playerlist = new PlayerList();
00036         this->self = new Player();
00037         if(isserver)
00038                 this->self->setID(0);
00039         this->msgQueue = new MessageQueue(MAX_MSG);
00040         this->eventQueue = new EventQueue();
00041         this->playerInfoSelfIsSet = false;
00042 }
00043 
00044 
00045 Gne::~Gne(){
00046         //connection->disconnect();
00047 
00048         TerminateThread(hThreadListen, 0);
00049         TerminateThread(hThreadClient, 0);
00050         if(isserver){
00051                 for(int i = 0; i<this->getMaxPlayers(); i++)
00052                         if(!freeSlotsThreads[i])
00053                                 TerminateThread(hThreadTCP[i], 0);
00054         }
00062         /*
00063         if(msgQueue){
00064                 //memset(msgQueue, '\0', sizeof(msgQueue));     
00065                 delete(msgQueue);
00066         }
00067         if(eventQueue){
00068                 //memset(eventQueue, '\0', sizeof(eventQueue)); 
00069                 delete(eventQueue);
00070         }
00071         if(playerlist){
00072                 //memset(playerlist, '\0', sizeof(playerlist)); 
00073                 delete(playerlist);
00074         }
00075         if(self){
00076                 //memset(self, '\0', sizeof(self));     
00077                 delete(self);
00078         }
00079         if(freeSlotsThreads){
00080                 memset(freeSlotsThreads, '\0', sizeof(freeSlotsThreads));
00081                 free(freeSlotsThreads);
00082         }
00083         if(playerIdSlots){
00084                 memset(playerIdSlots, '\0', sizeof(playerIdSlots));
00085                 free(playerIdSlots);
00086         }
00087         if(clients){    
00088                 memset(clients, '\0', sizeof(clients));
00089                 free(clients);
00090         }
00091         if(idlist){
00092                 memset(idlist, '\0', sizeof(idlist));
00093                 free(idlist);
00094         }*/
00095 }
00096 
00097 
00098 int Gne::init(bool isserver){
00099         if(isrunning)
00100                 return(-1);
00101         this->isserver = isserver;
00102         this->port = STD_PORT;
00103 
00104         //LOG_FILE("Network.txt");
00105         LOG_SUCCESS(("Gne initiated."));
00106         LOG_SUCCESS(("Port set to %d.", port));
00107 
00108         return(0);
00109 }
00110 
00111 
00112 bool Gne::isServer(){
00113         return(this->isserver);
00114 }
00115 
00116 
00117 bool Gne::isRunning(){
00118         return(this->isrunning);
00119 }
00120 
00121 int Gne::setMaxPlayers(int n){
00122         if(isrunning)
00123                 return(-1);
00124         playerlist->setMaxPlayers(n);
00125         hThreadTCP = (HANDLE *)malloc(n*sizeof(HANDLE));
00126         freeSlotsThreads = (bool *)malloc(n*sizeof(bool));
00127         clients = (Connection **)malloc(n*sizeof(Connection));
00128         idlist = (int *)malloc(n*sizeof(int));
00129         for(int i = 0; i < n; i++){
00130                 freeSlotsThreads[i] = true;
00131                 hThreadTCP[i] = NULL;
00132         }
00133         
00134         LOG_SUCCESS(("Max players set to %d.", n));
00135         return(n);
00136 }
00137 
00138 
00139 int Gne::getMaxPlayers(){
00140         return(playerlist->getMaxPlayers());
00141 }
00142 
00143 
00144 int Gne::getNumPlayers(){
00145         return(playerlist->getNumPlayers()+1);
00146 }
00147 
00148 
00149 int Gne::addPlayer(Player *p){
00150         int ret = playerlist->addPlayer(p);
00151         this->eventQueue->addLast(new GE_PlayerJoined(p->getID()));
00152         return(ret);    
00153 }
00154 
00155 
00156 int Gne::removePlayerById(int playerid)
00157 {
00158         if(!isrunning)
00159                 return(-1);
00160         return(playerlist->removePlayerById(playerid)); 
00161 }
00162 
00163 
00164 Player* Gne::getPlayerById(int playerid){
00165         if(this->self->getID() == playerid)
00166                 return(self);
00167         return(playerlist->getPlayerById(playerid));
00168 }
00169 
00170 
00171 
00172 int Gne::setPlayerInfoSelf(char *name){
00173         if(strlen(name) > 25){
00174                 LOG_ERROR(("Name have to be shorter then 26 characters."));
00175                 return(-1);
00176         }
00177         playerInfoSelfIsSet = true;
00178         if(isserver){
00179                 this->self->setID(0);
00180                 this->self->setName(name);
00181                 return(0);
00182         }
00183         this->self->setName(name);
00184         return(0);
00185 }
00186 
00187 
00188 void Gne::setPlayerIdSelf(int id){
00189         this->self->setID(id);
00190 }
00191 
00192 
00193 char *Gne::getPlayerNameSelf(){
00194         if(!playerInfoSelfIsSet)
00195                 return(NULL);
00196         return(this->self->getName());
00197 }
00198 
00199 
00200 int Gne::getPlayerIdSelf(){
00201         if(!playerInfoSelfIsSet)
00202                 return(-1);
00203         return(this->self->getID());
00204 }
00205 
00206 
00207 
00208 int Gne::getNextPlayerID(){
00209         return(this->playerlist->getFreeId());
00210 }
00211 
00212 
00213 
00214 bool Gne::isMessagePending(){
00215         return(!this->msgQueue->isEmpty());
00216 }
00217 
00218 
00219 msgstruct Gne::getNextMessage(){
00220         return(msgQueue->getNextMessage());
00221         
00222 }
00223 
00224 
00225 
00226 int Gne::setPort(int port){
00227         if(port<1024){
00228                 LOG_ERROR(("Port %d is invalid, choose a port > 1024.", port));
00229                 return(-1);
00230         }
00231         LOG_SUCCESS(("Port changed to %d.", port));
00232         return(this->port = port);
00233 }
00234 
00235 
00236 int Gne::getPort(){
00237         return(this->port);
00238 }
00239 
00240 
00241 int Gne::getFreeThread(){
00242         int n = this->getMaxPlayers();
00243         for(int i=0; i<n; i++)
00244                 if(freeSlotsThreads[i]){
00245                         freeSlotsThreads[i] = false;
00246                         return(i);
00247                 }
00248         return(-1);
00249 }
00250 
00251 
00252 
00253 void Gne::setFreeThread(int nr){
00254         freeSlotsThreads[nr] = true;
00255 }
00256 
00257 
00258 int Gne::startServer(){
00259         if(!isserver)
00260                 return(-1);
00261 
00262         hThreadListen = CreateThread(
00263                 NULL, 
00264                 0,
00265                 listen,
00266                 (void *)this,
00267                 0,
00268                 &hThreadListenID);
00269         if(hThreadListen == NULL){
00270                 LOG_ERROR(("Could not create the server thread."));
00271                 return(-1);
00272         }
00273         isrunning = true;       
00274         return(0);
00275 }
00276 
00277 
00278 int Gne::stopServer(){
00279         if(!isServer()&&!isRunning())
00280                 return(-1);
00281         
00282         /*
00283         this->disconnect();
00284         TerminateThread(hThreadListen, 0);
00285         TerminateThread(hThreadClient, 0);
00286         for(int i = 0; i<this->getMaxPlayers(); i++)
00287                 if(!freeSlotsThreads[i])
00288                         TerminateThread(hThreadTCP[i], 0);
00289 
00290   */
00291         this->connection->disconnect();
00292         TerminateThread(hThreadListen, 0);
00293         TerminateThread(hThreadClient, 0);
00294         for(int i = 0; i<this->getMaxPlayers(); i++)
00295                 if(!freeSlotsThreads[i])
00296                         TerminateThread(hThreadTCP[i], 0);
00297         isrunning = false;
00298         return(0);
00299 }
00300 
00301 
00302 
00303 int Gne::joinServer(char *address){
00304         if(isserver)
00305                 return(-1);
00306         connection = new Connection(this->getPort());
00307         int b = connection->connectByIp(address);
00308         if(b == -1){
00309                 LOG_ERROR(("Could not connect to %s.", address));
00310                 return(-1);
00311         }
00312         LOG_SUCCESS(("Connection to %s succeded.", address));
00313         packetConnect pc = createPacketConnect(VERSION_ID);
00314         setTimeStamp(&pc.header);
00315         setPacketFrom(&pc.header, self->getID());
00316         //Request to join game
00317         b = connection->sendData((char *)&pc, sizeof(packetConnect));
00318         if(b == -1){
00319                 connection->disconnect();
00320                 LOG_ERROR(("Could not send data to server."));
00321                 return(-1);
00322         }
00323         //look at the response
00324         packetConnectionResponse pktRes;
00325         b = connection->recieveData((char *)&pktRes, sizeof(pktRes), 0);
00326         if(b == -1||pktRes.header.pktType!=PKT_CONNECTION_RESPONSE){
00327                 connection->disconnect();
00328                 LOG_ERROR(("error when recieving data."));
00329                 return(-1);
00330         }
00331         if(pktRes.acceptionFlag == 0)
00332         {
00333                 connection->disconnect();
00334                 LOG_ERROR(("Your client was not accepted."));
00335                 return(-1);
00336         }
00337         else{
00338                 packetPlayerInfo ppi;
00339                 ppi = createPacketPlayerInfo(this->getPlayerNameSelf(), 0);
00340                 setTimeStamp(&ppi.header);
00341                 setPacketFrom(&ppi.header, self->getID());
00342                 b = connection->sendData((char *)&ppi, sizeof(ppi));
00343                 if(b == -1){
00344                                 LOG_ERROR(("Could not send data to server."));
00345                                 connection->disconnect();
00346                                 return(-1);
00347                 }
00348                 b = connection->recieveData((char *)&ppi, sizeof(ppi), 0);
00349                 if(b == -1||ppi.header.pktType!=PKT_PLAYER_INFO){
00350                         LOG_ERROR(("Did not playerinfo from the server."));
00351                         connection->disconnect();
00352                         return(-1);
00353                 }
00354                 this->setPlayerIdSelf(ppi.id);
00355                 Player *serv = new Player();
00356                 serv->setID(0);
00357                 serv->setName(ppi.name);
00358                 playerlist->addPlayer(serv);
00359                 this->eventQueue->addLast(new GE_PlayerJoined(0));
00360                 isrunning = true;
00361                 
00362                 //tråd som lyssnar på meddelanden från servern.
00363                 this->hThreadClient = CreateThread(
00364                         NULL, 
00365                         0,
00366                         clientThread,
00367                         (void *)this,
00368                         0,
00369                         &hThreadClientID);
00370                 if(this->hThreadClient = NULL){
00371                         LOG_ERROR(("Could not spawn a new thread with the server connection."));
00372                         return(-1);
00373                 }
00374 
00375         }
00376         return(0);
00377 }
00378 
00379 
00380 DWORD WINAPI Gne::listen(LPVOID arg){
00381         Gne *gne = (Gne *)arg;
00382         Connection *conn = gne->connection;
00383         Connection *newConn;
00384         int b;
00385         bool first = true;
00386         packetConnectionResponse pktRes;
00387         packetConnect pkt;
00388         packetConnectionResponse pktOk;
00389         packetPlayerInfo ppi;
00390 
00391         while(gne->playerlist->isNotFull()){
00392                 try{
00393                         if(first){
00394                                 conn = new Connection(gne->getPort());
00395                                 first = false;
00396                         }
00397                         else
00398                                 conn = newConn;
00399                 }
00400                 catch(char *err){
00401                         LOG_ERROR(("%s", err));
00402                         exit(-1); //eller kasta vidare exception så man kan gå ner i menyn lr nått...
00403                 }
00404                 
00405                 newConn = conn->waitForConnection();
00406                 if(newConn == NULL)
00407                 {       
00408                         LOG_ERROR(("Could not wait for connection."));
00409                         exit(-1); //eller kasta vidare exception så man kan gå ner i menyn lr nått...
00410 
00411                 }
00412                 b = conn->recieveData((char *)&pkt, sizeof(packetConnect), 0);
00413                 if(b == -1){
00414                         LOG_ERROR(("Could not recieve data."));
00415                 }
00416                 if(pkt.header.pktType!=PKT_CONNECTION_REQUEST){
00417                         LOG_ERROR(("Non connectionpacket - connection refused."));
00418                         conn->disconnect();             
00419                 }
00420                 else if(pkt.vid!=VERSION_ID){
00421                         LOG_ERROR(("Version missmatch - connection refused."));
00422                         pktRes = createPacketConnectionResponse(0);
00423                         setTimeStamp(&pktOk.header);
00424                         setPacketFrom(&pktOk.header, gne->self->getID());
00425                         b = conn->sendData((char *)&pktRes, sizeof(pktRes));
00426                         if(b == -1){
00427                                 LOG_ERROR(("Could not send response to the refused client."));
00428                         }
00429                         LOG_ERROR(("A connecting client had wrong version of the network protocol."));
00430                         conn->disconnect();
00431                 }
00432                 else{
00433                         pktRes = createPacketConnectionResponse(1);
00434                         setTimeStamp(&pktRes.header);
00435                         setPacketFrom(&pktRes.header, gne->self->getID());
00436                         b = conn->sendData((char *)&pktRes, sizeof(pktRes));
00437                         if(b == -1){
00438                                 LOG_ERROR(("Could not send data to client."));
00439                                 conn->disconnect();
00440                         }
00441                         else{                           
00442                                 conn->recieveData((char *)&ppi, sizeof(ppi), 0);
00443                                 if(b == -1||ppi.header.pktType!=PKT_PLAYER_INFO){
00444                                         LOG_ERROR(("Could not get playerinfo."));
00445                                         conn->disconnect();
00446                                 }
00447                                 else{                                   
00448                                         Player *p = new Player();
00449                                         p->setName(ppi.name);
00450                                         int id = gne->getNextPlayerID();
00451                                         p->setID(id);
00452                                         gne->addPlayer(p);
00453                                         ppi.id = id;
00454                                         //skickar egen info samt klientens playerid
00455                                         strcpy(ppi.name, gne->self->getName());
00456 
00457                                         setTimeStamp(&ppi.header);
00458                                         setPacketFrom(&ppi.header, gne->self->getID());
00459                                         b = conn->sendData((char *)&ppi, sizeof(ppi));
00460                                         if(b == -1){
00461                                                 LOG_ERROR(("Could not send data to client."));
00462                                                 //skicka nått event?
00463                                                 gne->removePlayerById(id);
00464                                                 conn->disconnect();
00465                                         }
00466                                         else{
00467                                                 //Använd samma för att skapa en udp-tråd.
00468                                                 int freeHandler = gne->getFreeThread();
00469                                                 gne->latestThread = freeHandler;
00470                                                 gne->latestPlayerID = id;
00471                                                 gne->clients[freeHandler] = conn;
00472                                                 gne->idlist[freeHandler] = id;
00473 
00474                                                 gne->hThreadTCP[freeHandler] = CreateThread(
00475                                                         NULL, 
00476                                                         0,
00477                                                         aPlayer,
00478                                                         (void *)arg,
00479                                                         0,
00480                                                         NULL);
00481                                                 if(gne->hThreadTCP[freeHandler] = NULL){
00482                                                         LOG_ERROR(("Could not spawn thread for the new client."));
00483                                                         return(-1);
00484                                                 }
00485                                                 //Finns det något sätt att vänta på tråden?
00486                                                 if(gne->sendPlayerToAll(p, freeHandler) == -1){
00487                                                         LOG_ERROR(("Could not send out a clients info."));
00488                                                         //släng ut spelaren??
00489                                                 }
00490                                         }
00491                                 }       
00492                         }
00493                 }
00494         }
00495         return(0);
00496 }
00497 
00498 
00499 
00500 DWORD WINAPI Gne::aPlayer(LPVOID arg){
00501         Gne *gne = (Gne *)arg;
00502         int mySock = gne->latestThread;
00503         int myPlayerID = gne->latestPlayerID;
00504         Connection *conn = gne->clients[mySock];
00505         Player *p;
00506         int size;
00507         int i;
00508         packetHeader chk;
00509         packetChatMsg pcm;
00510         packetRequestAllPlayersInfo prapi;
00511         packetDisconnect pd;
00512         packetPlayerPosition ppp;
00513         packetAttack pa;
00514         packetPlayerState pps;
00515         packetPlayerScore ppsc;
00516         packetPlayerAction ppa;
00517         packetPlayerKilled ppd;
00518         packetPlayerExtras ppe;
00519         packetDealExtras pde;
00520         packetPlayerName ppn;
00521 
00522         while(1){
00523                 size = conn->recieveData((char *)&chk, sizeof(chk), MSG_PEEK);
00524                 if(size == -1){
00525                         conn->disconnect();
00526                         gne->setFreeThread(mySock);
00527                         gne->removePlayerById(myPlayerID);
00528                         gne->eventQueue->addLast(new GE_PlayerDisconnected(myPlayerID));
00529                         LOG_ERROR(("Connection to a client lost."));
00530                         ExitThread(-1);
00531                 }
00532                 switch(chk.pktType){
00533                 case(PKT_CHAT_MSG):
00534                         size = conn->recieveData((char *)&pcm, sizeof(pcm), 0);
00535                         gne->msgQueue->newMessage(pcm.msg);
00536                         gne->sendChatMsg(pcm.msg);
00537                         break;
00538                 case(PKT_REQUEST_PLAYERS):
00539                         size = conn->recieveData((char *)&prapi, sizeof(prapi), 0);
00540                         for(i = 1; i < gne->playerlist->getNumPlayers(); i++){
00541                                 if((p = gne->playerlist->getPlayerById(i))!=NULL)
00542                                         gne->sendPlayer(conn, p);
00543                         }
00544                         break;
00545                 case(PKT_DISCONNECT):           
00546                         size = conn->recieveData((char *)&pd, sizeof(pd), 0);
00547                         setTimeStamp(&pd.header);
00548                         size = gne->sendDisconnectToAll(pd);
00549                         conn->disconnect();
00550                         gne->removePlayerById(pd.id);
00551                         gne->setFreeThread(mySock);
00552                         gne->eventQueue->addLast(new GE_PlayerDisconnected(pd.id));
00553                         ExitThread(0);
00554                         break;
00555                 case(PKT_PLAYER_POSITION):
00556                         size = conn->recieveData((char *)&ppp, sizeof(ppp), 0);
00557                         gne->sendPlayerPosToAll(ppp, ppp.header.from);
00558                         gne->eventQueue->addLast(new GE_PlayerPos(ppp.id, ppp.rotation, ppp.move, ppp.x, ppp.y, ppp.angle));
00559                         break;
00560                 case(PKT_ATTACK):
00561                         size = conn->recieveData((char *)&pa, sizeof(pa), 0);
00562                         gne->sendAttackNT(pa.id, pa.attack, pa.x, pa.y, pa.z, pa.dx, pa.dy, pa.dz, pa.header.from);
00563                         gne->eventQueue->addLast(new GE_Attack(pa.id, pa.attack, pa.x, pa.y, pa.z, pa.dx, pa.dy, pa.dz));
00564                         break;
00565                 case(PKT_PLAYER_STATE):
00566                         size = conn->recieveData((char *)&pps, sizeof(pps), 0);                 
00567                         gne->setPlayerState(pps.id, pps.state);
00568                         gne->eventQueue->addLast(new GE_State(pps.id, pps.state));
00569                         break;
00570                 case(PKT_PLAYER_SCORE):
00571                         size = conn->recieveData((char *)&ppsc, sizeof(ppsc), 0);                       
00572                         gne->setPlayerScore(ppsc.id, ppsc.frags, ppsc.kills, ppsc.deaths);
00573                         gne->eventQueue->addLast(new GE_PlayerScore(ppsc.id, ppsc.frags, ppsc.kills, ppsc.deaths));
00574                         break;
00575                 case(PKT_PLAYER_ACTION):
00576                         size = conn->recieveData((char *)&ppa, sizeof(ppa), 0);
00577                         gne->setPlayerAction(ppa.id, ppa.action);
00578                         //gne->eventQueue->addLast(new GE_PlayerAction(ppa.id, ppa.action));
00579                         break;
00580                 case(PKT_PLAYER_KILLED):
00581                         size = conn->recieveData((char *)&ppd, sizeof(pps), 0);                 
00582                         gne->setPlayerDeath(ppd.id, ppd.killer);
00583                         //gne->eventQueue->addLast(new GE_PlayerDeath(ppd.id, ppd.killer));
00584                         break;
00585                 case(PKT_PLAYER_EXTRAS):
00586                         size = conn->recieveData((char *)&ppe, sizeof(ppe), 0);                 
00587                         gne->setPlayerExtras(ppe.id, ppe.extras, ppe.value);
00588                         //gne->eventQueue->addLast(new GE_PlayerExtras(ppe.id, ppe.extras, ppe.value));
00589                         break;
00590                 case(PKT_DEAL_EXTRAS):
00591                         size = conn->recieveData((char *)&pde, sizeof(pde), 0);                 
00592                         gne->dealExtras(pde.detype, pde.x, pde.y, pde.flag);
00593                         //gne->eventQueue->addLast(new GE_PlayerExtras(ppe.id, ppe.extras, ppe.value));
00594                         break;
00595                 case(PKT_PLAYER_NAME):
00596                         size = conn->recieveData((char *)&ppn, sizeof(ppn), 0);
00597                         gne->setPlayerName(ppn.id, ppn.name, ppn.flag);
00598                         if(ppn.id != gne->getPlayerIdSelf())
00599                                 gne->playerlist->setPlayerName(ppn.id, ppn.name);
00600                         break;
00601                 default:
00602                         conn->recieveData(NULL, size, 0);
00603                         break;
00604                 }
00605         }
00606         return(NULL);
00607 }
00608 
00609 
00610 DWORD WINAPI Gne::clientThread(LPVOID arg){
00611         Gne *gne = (Gne *)arg;
00612         Connection *conn = gne->connection;
00613         int size;
00614         packetHeader chk;
00615         Player *p;
00616         packetChatMsg pcm;
00617         packetPlayerInfo ppi;
00618         packetDisconnect pd;
00619         packetPlayerPosition ppp;
00620         packetAttack pa;
00621         packetPlayerState pps;
00622         packetPlayerAction ppa;
00623         packetPlayerScore ppsc;
00624         packetPlayerKilled ppd;
00625         packetPlayerExtras ppe;
00626         packetDealExtras pde;
00627         packetPlayerName ppn;
00628         packetInfo pi;
00629 
00630         while(1){
00631                 size = conn->recieveData((char *)&chk, sizeof(chk), MSG_PEEK);
00632                 if(size == -1){
00633                         conn->disconnect();
00634                         gne->playerlist->removeAllPlayers();
00635                         gne->eventQueue->addLast(new GE_PlayerDisconnected(0));
00636                         LOG_ERROR(("Connection lost."));
00637                         ExitThread(-1);
00638                 }
00639 
00640                 switch(chk.pktType){
00641                 case(PKT_CHAT_MSG):
00642                         size = conn->recieveData((char *)&pcm, sizeof(pcm), 0);
00643                         if(size == -1){
00644                                 LOG_ERROR(("Error recieving chat msg."));
00645                                 ExitThread(-1);
00646                         }
00647                         else
00648                                 gne->msgQueue->newMessage(pcm.msg);
00649                         break;
00650                 case(PKT_PLAYER_INFO):
00651                         size = conn->recieveData((char *)&ppi, sizeof(ppi), 0);
00652                         if(ppi.id!=gne->self->getID()){
00653                                 p = new Player();
00654                                 p->setID(ppi.id);
00655                                 p->setName(ppi.name);
00656                                 gne->addPlayer(p);
00657                         }
00658                         break;
00659                 case(PKT_DISCONNECT):
00660                         size = conn->recieveData((char *)&pd, sizeof(pd), 0);
00661                         if(pd.id == gne->self->getID()){
00662                                 gne->connection->disconnect();
00663                                 delete(gne->connection);
00664                                 gne->playerlist->removeAllPlayers();
00665                                 gne->eventQueue->addLast(new GE_PlayerDisconnected(pd.id));
00666                                 ExitThread(0);
00667                         }
00668                         if(pd.id == 0){
00669                                 gne->disconnect();
00670                         }
00671                         else{
00672                                 gne->removePlayerById(pd.id);
00673                                 gne->eventQueue->addLast(new GE_PlayerDisconnected(pd.id));
00674                         }
00675                         break;
00676                 case(PKT_PLAYER_POSITION):
00677                         size = conn->recieveData((char *)&ppp, sizeof(ppp), 0);
00678                         gne->eventQueue->addLast(new GE_PlayerPos(ppp.id, ppp.rotation, ppp.move, ppp.x, ppp.y, ppp.angle));
00679                         break;
00680                 case(PKT_ATTACK):                       
00681                         size = conn->recieveData((char *)&pa, sizeof(pa), 0);
00682                         gne->eventQueue->addLast(new GE_Attack(pa.id, pa.attack, pa.x, pa.y, pa.z, pa.dx, pa.dy, pa.dz));
00683                         break;
00684                 case(PKT_PLAYER_STATE):                 
00685                         size = conn->recieveData((char *)&pps, sizeof(pps), 0);
00686                         gne->eventQueue->addLast(new GE_State(pps.id, pps.state));
00687                         break;
00688                 case(PKT_PLAYER_SCORE):
00689                         size = conn->recieveData((char *)&ppsc, sizeof(ppsc), 0);                       
00690                         gne->eventQueue->addLast(new GE_PlayerScore(ppsc.id, ppsc.frags, ppsc.kills, ppsc.deaths));
00691                         break;
00692                 case(PKT_PLAYER_ACTION):
00693                         size = conn->recieveData((char *)&ppa, sizeof(ppa), 0);
00694                         gne->eventQueue->addLast(new GE_PlayerAction(ppa.id, ppa.action));
00695                         break;
00696                 case(PKT_PLAYER_KILLED):
00697                         size = conn->recieveData((char *)&ppd, sizeof(pps), 0);
00698                         gne->eventQueue->addLast(new GE_PlayerDeath(ppd.id, ppd.killer));
00699                         break;
00700                 case(PKT_PLAYER_EXTRAS):
00701                         size = conn->recieveData((char *)&ppe, sizeof(ppe), 0);                 
00702                         gne->eventQueue->addLast(new GE_PlayerExtras(ppe.id, ppe.extras, ppe.value));
00703                         break;
00704                 case(PKT_DEAL_EXTRAS):
00705                         size = conn->recieveData((char *)&pde, sizeof(pde), 0);                 
00706                         gne->eventQueue->addLast(new GE_DealExtras(pde.detype, pde.x, pde.y, pde.flag));
00707                         break;
00708                 case(PKT_PLAYER_NAME):
00709                         size = conn->recieveData((char *)&ppn, sizeof(ppn), 0);
00710                         if(ppn.id != gne->getPlayerIdSelf())
00711                                 gne->playerlist->setPlayerName(ppn.id, ppn.name);
00712                         gne->eventQueue->addLast(new GE_SetName(ppn.id, ppn.name, ppn.flag));
00713                         break;
00714                 case(PKT_INFO):
00715                         size = conn->recieveData((char *)&pi, sizeof(pi), 0);
00716                         gne->eventQueue->addLast(new GE_Info(pi.id, pi.valA, pi.valB, pi.valC));
00717                         break;
00718                 default:
00719                         conn->recieveData(NULL, chk.pktLen, 0);
00720                         break;
00721                 }
00722         }
00723         return(NULL);
00724 }
00725 
00726 
00727 
00728 int Gne::disconnect(){
00729         packetDisconnect pd;
00730         if(!isrunning){
00731                 LOG_ERROR(("Could not disconnect: not connected."));
00732                 return(-1);
00733         }
00734         if(!isserver){
00735         /*      pd = createPacketDisconnect(this->self->getID());
00736                 setTimeStamp(&pd.header);
00737                 setPacketFrom(&pd.header, self->getID());
00738                 this->connection->sendData((char *)&pd, sizeof(pd));*/
00739                 this->connection->disconnect();
00740         }
00741         else{
00742                 pd = createPacketDisconnect(this->self->getID());
00743                 setTimeStamp(&pd.header);
00744                 setPacketFrom(&pd.header, self->getID());
00745                 sendDisconnectToAll(pd);
00746                 if(connection)
00747                         connection->disconnect();
00748                 //kan ta ett tag tills alla är disconnected.
00749         }
00750         return(0);
00751 }
00752 
00753 
00754 
00755 int Gne::sendChatMsg(char *msg){
00756         int b;
00757         packetChatMsg pkt;
00758         pkt = createPacketChatMsg(this->self->getID() ,msg);
00759         setTimeStamp(&pkt.header);
00760         setPacketFrom(&pkt.header, self->getID());
00761         if(!isserver){
00762                 b = this->connection->sendData((char *)&pkt, sizeof(pkt));
00763         }
00764         else{
00765                 for(int i = 0; i < playerlist->getNumPlayers(); i++){
00766                         if(!freeSlotsThreads[i]){
00767                                 b = this->clients[i]->sendData((char *)&pkt, sizeof(pkt));
00768                         }
00769                 }
00770                 this->msgQueue->newMessage(0, msg);
00771         }
00772         if(b == -1)
00773         {
00774                 LOG_ERROR(("Error sending chat message."));
00775                 return(-1);
00776         }
00777         return(b);
00778 }
00779 
00780 
00781 int Gne::sendChatMsg(msgstruct msg){
00782         int b;
00783         packetChatMsg pkt;
00784         pkt = createPacketChatMsg(msg.playerid ,msg.msg);
00785         setTimeStamp(&pkt.header);
00786         setPacketFrom(&pkt.header, self->getID());
00787         if(!isserver){
00788                 b = this->connection->sendData((char *)&pkt, sizeof(pkt));
00789         }
00790         else{
00791                 for(int i = 0; i < playerlist->getNumPlayers(); i++){
00792                         if(!freeSlotsThreads[i]){
00793                                 b = this->clients[i]->sendData((char *)&pkt, sizeof(pkt));
00794                                 if(b == -1)
00795                                 {
00796                                         LOG_ERROR(("Error sending chat message."));
00797                                         return(-1);
00798                                 }
00799                         }
00800                 }
00801         }
00802         return(b);
00803 }
00804 
00805 
00806 
00807 int Gne::sendDisconnectToAll(packetDisconnect pkt){
00808         int b;
00809         if(!isserver)
00810                 return(-1); 
00811         setTimeStamp(&pkt.header);
00812         setPacketFrom(&pkt.header, self->getID());
00813 
00814         //for(int i = 0; i < this->nextPlayerID; i++){
00815         for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
00816                 if(!freeSlotsThreads[i]){
00817                         b = this->clients[i]->sendData((char *)&pkt, sizeof(pkt));
00818                         if(b == -1)
00819                         {
00820                                 LOG_ERROR(("Error sending a disconnect to a client."));
00821                                 return(-1);
00822                         }
00823                 }
00824         }
00825         return(b);
00826 }
00827 
00828 
00829 
00830 
00831 //server calls
00832 int Gne::sendPlayer(Connection *conn, Player *p){
00833         packetPlayerInfo pkt = createPacketPlayerInfo(p->getName(), p->getID());
00834         setTimeStamp(&pkt.header);
00835         setPacketFrom(&pkt.header, self->getID());
00836         int size = conn->sendData((char *)&pkt, sizeof(pkt));
00837         if(size == -1){
00838                 LOG_ERROR(("sendPlayer() error:: error sending player information."));
00839         }
00840         return(size);
00841 }
00842 
00843 
00844 
00845 //exception är fulhack.. se i listen.
00846 int Gne::sendPlayerToAll(Player *p, int exception){
00847         int b;
00848         int num = this->playerlist->getNumPlayers();
00849         for(int i = 0; i < num; i++){
00850                 if(!freeSlotsThreads[i]&&(i!=exception)){
00851                         b = sendPlayer(this->clients[i], p);
00852                         if(b==-1){
00853                                 LOG_ERROR(("sendPlayerToAll() error:: this is a error follows from the sendPlayer() error."));
00854                                 return(-1);
00855                         }
00856                 }
00857         }
00858         return(b);
00859 }
00860 
00861 
00862 int Gne::updatePlayersFromServer(){
00863         if(isserver)
00864                 return(-1);
00865         packetRequestAllPlayersInfo prapi;
00866         prapi = createPacketRequestAllPlayersInfo();
00867         setTimeStamp(&prapi.header);
00868         setPacketFrom(&prapi.header, self->getID());
00869         int size = this->connection->sendData((char *)&prapi, sizeof(prapi));
00870         if(size == -1){
00871                 return(-1);
00872         }
00873         
00874         return(0);
00875 }
00876 
00877 
00878 void Gne::setPlayerPos(int id, int rotation, int move, float x, float y, float angle){
00879         packetPlayerPosition pkt;
00880         pkt = createPacketPlayerPosition(id, rotation, move, x, y, angle, false);
00881         setTimeStamp(&pkt.header);
00882         setPacketFrom(&pkt.header, self->getID());
00883         if(!isserver){
00884                 this->connection->sendData((char *)&pkt, sizeof(pkt));
00885         }
00886         else{
00887                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
00888                 if(!freeSlotsThreads[i]){
00889                         this->clients[i]->sendData((char *)&pkt, sizeof(pkt));
00890                         }
00891                 }
00892         }
00893 }
00894 
00895 
00896 
00897 
00898 void Gne::sendPlayerPosToAll(packetPlayerPosition pkt, int nt){
00899         if(!isserver)
00900                 return;
00901         setTimeStamp(&pkt.header);
00902         setPacketFrom(&pkt.header, self->getID());
00903         for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
00904                 if(!freeSlotsThreads[i]&&idlist[i]!=nt){
00905                         this->clients[i]->sendData((char *)&pkt, sizeof(pkt));
00906                 }
00907         }
00908 
00909 }
00910 
00911 
00912 
00913 void Gne::sendAttackNT(int playerid, int type, float x, float y, float z, float dx, float dy, float dz, int nt){
00914         if(!isrunning)
00915                 return;
00916         packetAttack pa;
00917         pa = createPacketAttack(playerid, type, x, y, z, dx, dy, dz);
00918         setTimeStamp(&pa.header);
00919         setPacketFrom(&pa.header, self->getID());
00920         if(!isserver){
00921                 this->connection->sendData((char *)&pa, sizeof(pa));
00922 
00923         }
00924         else{
00925                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
00926                         if(!freeSlotsThreads[i]&&idlist[i]!=nt){
00927                                         this->clients[i]->sendData((char *)&pa, sizeof(pa));
00928                         }
00929                 }
00930         }
00931 }
00932 
00933 
00934 void Gne::sendInfo(int playerid, int valA, int valB, int valC){
00935         if(!isrunning)
00936                 return;
00937         packetInfo pi;
00938         pi = createPacketInfo(playerid, valA, valB, valC);
00939         setTimeStamp(&pi.header);
00940         setPacketFrom(&pi.header, self->getID());
00941         for(int i=0; i<this->playerlist->getNumPlayers(); i++){
00942                 if(idlist[i]==playerid)
00943                         this->clients[i]->sendData((char *)&pi, sizeof(pi));
00944         }
00945 }
00946 
00947 
00948 
00949 
00950 void Gne::sendAttack(int playerid, int type, float x, float y, float z, float dx, float dy, float dz){
00951         sendAttackNT(playerid, type, x, y, z, dx, dy, dz, 0);
00952 }
00953 
00954 
00955 void Gne::setPlayerState(int playerid, int state){
00956         if(!isrunning)
00957                 return;
00958         packetPlayerState pps;
00959         pps = createPacketPlayerState(playerid, state);
00960         setTimeStamp(&pps.header);
00961         setPacketFrom(&pps.header, self->getID());
00962         if(!isserver){
00963                 this->connection->sendData((char *)&pps, sizeof(pps));
00964         }
00965         else{
00966                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
00967                         if(!freeSlotsThreads[i]){
00968                                         this->clients[i]->sendData((char *)&pps, sizeof(pps));
00969                         }
00970                 }
00971         }
00972 }
00973 
00974 
00975 void Gne::setPlayerScore(int playerID, int frags, int kills, int deaths){
00976         if(!isrunning)
00977                 return;
00978         packetPlayerScore ppsc;
00979         ppsc = createPacketPlayerScore(playerID, kills, frags, deaths);
00980         setTimeStamp(&ppsc.header);
00981         setPacketFrom(&ppsc.header, self->getID());
00982         if(!isserver){
00983                 this->connection->sendData((char *)&ppsc, sizeof(ppsc));
00984         }
00985         else{
00986                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
00987                         if(!freeSlotsThreads[i]){
00988                                         this->clients[i]->sendData((char *)&ppsc, sizeof(ppsc));
00989                         }
00990                 }
00991         }
00992 }
00993 
00994 
00995 void Gne::setPlayerActionNT(int playerID, int action, int nt){
00996         if(!isrunning)
00997                 return;
00998         packetPlayerAction ppa;
00999         ppa = createPacketPlayerAction(playerID, action);
01000         setTimeStamp(&ppa.header);
01001         setPacketFrom(&ppa.header, self->getID());
01002         if(!isserver){
01003                 this->connection->sendData((char *)&ppa, sizeof(ppa));
01004 
01005         }
01006         else{
01007                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
01008                         if(!freeSlotsThreads[i]/*&&idlist[i]!=nt*/){
01009                                         this->clients[i]->sendData((char *)&ppa, sizeof(ppa));
01010                         }
01011                 }
01012                 this->eventQueue->addLast(new GE_PlayerAction(playerID, action));
01013         }
01014 }
01015 
01016 
01017 void Gne::setPlayerAction(int playerID, int action){
01018         setPlayerActionNT(playerID, action, 0);
01019 }
01020 
01021 
01022 
01023 void Gne::setPlayerDeath(int playerID, int killerID){
01024         if(!isrunning)
01025                 return;
01026         packetPlayerKilled ppd;
01027         ppd = createPacketPlayerKilled(playerID, killerID);
01028         setTimeStamp(&ppd.header);
01029         setPacketFrom(&ppd.header, self->getID());
01030         if(!isserver){
01031                 this->connection->sendData((char *)&ppd, sizeof(ppd));
01032         }
01033         else{
01034                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
01035                         if(!freeSlotsThreads[i]){
01036                                         this->clients[i]->sendData((char *)&ppd, sizeof(ppd));
01037                         }
01038                 }
01039                 this->eventQueue->addLast(new GE_PlayerDeath(playerID, killerID));
01040         }
01041 }
01042 
01043 
01044 void Gne::setPlayerExtras(int playerID, int extras, int value){
01045         if(!isrunning)
01046                 return;
01047         packetPlayerExtras ppe;
01048         ppe = createPacketPlayerExtras(playerID, extras, value);
01049         setTimeStamp(&ppe.header);
01050         setPacketFrom(&ppe.header, self->getID());
01051         if(!isserver){
01052                 this->connection->sendData((char *)&ppe, sizeof(ppe));
01053         }
01054         else{
01055                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
01056                         if(!freeSlotsThreads[i]){
01057                                         this->clients[i]->sendData((char *)&ppe, sizeof(ppe));
01058                         }
01059                 }
01060                 this->eventQueue->addLast(new GE_PlayerExtras(playerID, extras, value));
01061         }
01062 }
01063 
01064 
01065 void Gne::dealExtras(int type, float x, float y, int flag){
01066         if(!isrunning)
01067                 return;
01068         packetDealExtras pde;
01069         pde = createPacketDealExtras(type, x, y, flag);
01070         setTimeStamp(&pde.header);
01071         setPacketFrom(&pde.header, self->getID());
01072         if(!isserver){
01073                 this->connection->sendData((char *)&pde, sizeof(pde));
01074         }
01075         else{
01076                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
01077                         if(!freeSlotsThreads[i]){
01078                                         this->clients[i]->sendData((char *)&pde, sizeof(pde));
01079                         }
01080                 }
01081                 this->eventQueue->addLast(new GE_DealExtras(type, x, y, flag));
01082         }
01083 }
01084 
01085 
01086 
01087 int Gne::setPlayerName(int playerID, const char *name, int flag){
01088         if(!isrunning)
01089                 return(-1);
01090         if(strlen(name) > 25){
01091                 LOG_ERROR(("Name have to be shorter then 26 characters."));
01092                 return(-1);
01093         }
01094 
01095         packetPlayerName ppn;
01096         ppn = createPacketPlayerName(playerID, name, flag);
01097         setTimeStamp(&ppn.header);
01098         setPacketFrom(&ppn.header, getPlayerIdSelf());
01099         if(!isserver){
01100                 this->connection->sendData((char *)&ppn, sizeof(ppn));
01101         }
01102         else{
01103                 for(int i = 0; i < this->playerlist->getNumPlayers(); i++){
01104                         if(!freeSlotsThreads[i]){
01105                                         this->clients[i]->sendData((char *)&ppn, sizeof(ppn));
01106                         }
01107                 }
01108                 this->eventQueue->addLast(new GE_SetName(playerID, name, flag));
01109         }       
01110 
01111         if(playerID == self->getID()){
01112                 this->self->setName(name);
01113                 return(0);
01114         }
01115         return(0);
01116 
01117 }
01118 
01119 
01120 int Gne::setName(const char *name, int flag){
01121         return(setPlayerName(this->getPlayerIdSelf(), name, flag));
01122 }
01123 
01124 
01125 int Gne::nextEventPending(){
01126         return(this->eventQueue->getType());
01127 }
01128 
01129 
01130 void *Gne::getNextEvent(){
01131         return(this->eventQueue->getNextEvent());
01132 }

Generated on Sun Jun 5 15:47:03 2005 for Defacto by  doxygen 1.4.3