Changeset 7163 for code/trunk/src/libraries/network/Host.cc
- Timestamp:
- Aug 11, 2010, 8:55:13 AM (14 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/network/Host.cc
r5961 r7163 38 38 namespace orxonox { 39 39 40 SetConsoleCommandShortcut(Host, Chat);40 SetConsoleCommandShortcut(Host, Chat); 41 41 42 Host *Host::instance_=0; 42 // Host* Host::instance_=0; 43 uint32_t Host::clientID_s=0; 44 uint32_t Host::shipID_s=-1; 45 std::vector<Host*> Host::instances_s; 43 46 44 /**45 * @brief Constructor: assures that only one reference will be created and sets the pointer46 */47 Host::Host()48 {49 clientID_=0;50 assert(instance_==0);51 instance_=this;52 this->printRTTCC_ = createConsoleCommand( createFunctor(&Host::printRTT, this), "printRTT");53 CommandExecutor::addConsoleCommandShortcut( this->printRTTCC_ );54 }47 /** 48 * @brief Constructor: assures that only one reference will be created and sets the pointer 49 */ 50 Host::Host() 51 { 52 // assert(instance_==0); 53 instances_s.push_back(this); 54 this->printRTTCC_ = createConsoleCommand( createFunctor(&Host::printRTT, this), "printRTT" ); 55 CommandExecutor::addConsoleCommandShortcut( this->printRTTCC_ ); 56 this->bIsActive_ = false; 57 } 55 58 56 59 57 /** 58 * @brief Destructor: resets the instance pointer to 0 59 */ 60 Host::~Host() 61 { 62 instance_=0; 63 if( this->printRTTCC_ ) 64 delete this->printRTTCC_; 65 } 60 /** 61 * @brief Destructor: resets the instance pointer to 0 62 */ 63 Host::~Host() 64 { 65 assert( std::find( instances_s.begin(), instances_s.end(), this )!=instances_s.end() ); 66 instances_s.erase(std::find( instances_s.begin(), instances_s.end(), this )); 67 if( this->printRTTCC_ ) 68 delete this->printRTTCC_; 69 } 66 70 67 /** 68 * This function is used to add an enetpacket to be sent to another peer 69 * @param packet Packet to be added 70 * @param clientID ID of the client the packet should be sent to 71 * @return success? 72 */ 73 bool Host::addPacket(ENetPacket *packet, int clientID){ 74 if(instance_) 75 return instance_->queuePacket(packet, clientID); 76 else 77 return false; 78 } 71 /** 72 * This function is used to add an enetpacket to be sent to another peer 73 * @param packet Packet to be added 74 * @param clientID ID of the client the packet should be sent to 75 * @return success? 76 */ 77 bool Host::addPacket(ENetPacket *packet, int clientID) 78 { 79 bool result = true; 80 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it ) 81 { 82 if( (*it)->isActive() ) 83 { 84 if( !(*it)->queuePacket(packet, clientID) ) 85 result = false; 86 } 87 } 88 return result; 89 } 79 90 80 /** 81 * This function returns the ID of the player 82 * @return playerID 83 */ 84 unsigned int Host::getPlayerID(){ 85 if(!instance_) 86 return 0; 87 return instance_->clientID_; 88 } 91 bool Host::Chat(const std::string& message) 92 { 93 if(instances_s.size()==0) 94 { 95 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) 96 it->incomingChat(message, 0); 97 return true; 98 } 99 else 100 { 101 bool result = true; 102 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it ) 103 { 104 if( (*it)->isActive() ) 105 { 106 if( !(*it)->chat(message) ) 107 result = false; 108 } 109 } 110 return result; 111 } 112 } 89 113 90 bool Host::Chat(const std::string& message){ 91 if(!instance_) 114 bool Host::Broadcast(const std::string& message) 115 { 116 if(instances_s.size()==0) 117 { 118 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) 119 it->incomingChat(message, CLIENTID_UNKNOWN); 120 return true; 121 } 122 else 123 { 124 bool result = true; 125 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it ) 126 { 127 if( (*it)->isActive() ) 128 { 129 if( !(*it)->broadcast(message) ) 130 result = false; 131 } 132 } 133 return result; 134 } 135 } 136 137 bool Host::incomingChat(const std::string& message, unsigned int playerID) 92 138 { 93 139 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) 94 it->incomingChat(message, 0); 95 return true; 140 it->incomingChat(message, playerID); 141 142 bool result = true; 143 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it ) 144 { 145 if( (*it)->isActive() ) 146 { 147 if( !(*it)->processChat(message, playerID) ) 148 result = false; 149 } 150 } 151 return result; 96 152 } 97 return instance_->chat(message);98 }99 153 100 bool Host::Broadcast(const std::string& message){ 101 if(!instance_) 154 bool Host::isServer() 102 155 { 103 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) 104 it->incomingChat(message, CLIENTID_UNKNOWN); 105 return true; 156 for (std::vector<Host*>::iterator it=instances_s.begin(); it!=instances_s.end(); ++it ) 157 { 158 if( (*it)->isServer_() ) 159 return true; 160 } 161 return false; 106 162 } 107 else108 return instance_->broadcast(message);109 }110 111 bool Host::incomingChat(const std::string& message, unsigned int playerID){112 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)113 it->incomingChat(message, playerID);114 115 return instance_->processChat(message, playerID);116 }117 163 118 164 }//namespace orxonox
Note: See TracChangeset
for help on using the changeset viewer.