1 // ============================================================== 2 // This file is part of Glest (www.glest.org) 3 // 4 // Copyright (C) 2001-2008 Marti�o Figueroa 5 // 6 // You can redistribute this code and/or modify it under 7 // the terms of the GNU General Public License as published 8 // by the Free Software Foundation; either version 2 of the 9 // License, or (at your option) any later version 10 // ============================================================== 11 12 #include "connection_slot.h" 13 14 #include <stdexcept> 15 16 #include "conversion.h" 17 #include "game_util.h" 18 #include "config.h" 19 #include "server_interface.h" 20 #include "network_message.h" 21 #include "leak_dumper.h" 22 23 using namespace std; 24 using namespace Shared::Util; 25 26 namespace Glest{ namespace Game{ 27 28 // ===================================================== 29 // class ClientConnection 30 // ===================================================== 31 ConnectionSlot(ServerInterface * serverInterface,int playerIndex)32ConnectionSlot::ConnectionSlot(ServerInterface* serverInterface, int playerIndex){ 33 this->serverInterface= serverInterface; 34 this->playerIndex= playerIndex; 35 socket= NULL; 36 ready= false; 37 } 38 ~ConnectionSlot()39ConnectionSlot::~ConnectionSlot(){ 40 close(); 41 } 42 update()43void ConnectionSlot::update(){ 44 if(socket==NULL){ 45 socket= serverInterface->getServerSocket()->accept(); 46 47 //send intro message when connected 48 if(socket!=NULL){ 49 NetworkMessageIntro networkMessageIntro(getNetworkVersionString(), socket->getHostName(), playerIndex); 50 sendMessage(&networkMessageIntro); 51 } 52 } 53 else{ 54 if(socket->isConnected()){ 55 NetworkMessageType networkMessageType= getNextMessageType(); 56 57 //process incoming commands 58 switch(networkMessageType){ 59 60 case nmtInvalid: 61 case nmtText: 62 break; 63 64 //command list 65 case nmtCommandList:{ 66 NetworkMessageCommandList networkMessageCommandList; 67 if(receiveMessage(&networkMessageCommandList)){ 68 for(int i= 0; i<networkMessageCommandList.getCommandCount(); ++i){ 69 serverInterface->requestCommand(networkMessageCommandList.getCommand(i)); 70 } 71 } 72 } 73 break; 74 75 //process intro messages 76 case nmtIntro:{ 77 NetworkMessageIntro networkMessageIntro; 78 if(receiveMessage(&networkMessageIntro)){ 79 name= networkMessageIntro.getName(); 80 } 81 } 82 break; 83 84 default: 85 throw runtime_error("Unexpected message in connection slot: " + intToStr(networkMessageType)); 86 } 87 } 88 else{ 89 close(); 90 } 91 } 92 } 93 close()94void ConnectionSlot::close(){ 95 delete socket; 96 socket= NULL; 97 } 98 99 }}//end namespace 100