Changeset 8829 for code/branches/output/src/libraries/network
- Timestamp:
- Aug 7, 2011, 3:11:16 PM (13 years ago)
- Location:
- code/branches/output/src/libraries/network
- Files:
-
- 1 added
- 2 deleted
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/output/src/libraries/network/CMakeLists.txt
r8351 r8829 19 19 20 20 SET_SOURCE_FILES(NETWORK_SRC_FILES 21 ChatListener.cc22 21 Client.cc 23 22 ClientConnection.cc … … 45 44 46 45 SET_SOURCE_FILES(NETWORK_HDR_FILES 47 ChatListener.h48 46 Client.h 49 47 ClientConnection.h … … 61 59 WANDiscovery.h 62 60 MasterServerComm.h 61 NetworkChatListener.h 63 62 NetworkFunction.h 64 63 NetworkPrecompiledHeaders.h -
code/branches/output/src/libraries/network/Client.cc
r8807 r8829 116 116 } 117 117 118 bool Client::processChat(const std::string& message, unsigned int playerID)119 {120 // orxout(message) << "Player " << playerID << ": " << message << endl;121 return true;122 }123 124 118 void Client::printRTT() 125 119 { … … 128 122 129 123 /** 130 * This function implements the method of sending a chat message to the server124 * @brief Sends a chat message to the server. 131 125 * @param message message to be sent 132 * @return result(true/false) 126 * @param sourceID the ID of the sender 127 * @param targetID the ID of the receiver 133 128 */ 134 bool Client::chat(const std::string& message) 135 { 136 packet::Chat *m = new packet::Chat(message, Host::getPlayerID()); 137 return m->send(static_cast<Host*>(this)); 138 } 139 129 void Client::doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) 130 { 131 // send the message to the server 132 packet::Chat* packet = new packet::Chat(message, sourceID, targetID); 133 packet->send(static_cast<Host*>(this)); 134 } 135 136 /** 137 * @brief Gets called if a packet::Chat packet is received. Calls the parent function which passes the message to the listeners. 138 */ 139 void Client::doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) 140 { 141 // call the parent function which passes the message to the listeners 142 Host::doReceiveChat(message, sourceID, targetID); 143 } 140 144 141 145 /** … … 203 207 Game::getInstance().popState(); 204 208 } 205 209 206 210 void Client::processPacket(packet::Packet* packet) 207 211 { … … 216 220 packet->process(static_cast<Host*>(this)); 217 221 } 218 219 220 221 222 222 } -
code/branches/output/src/libraries/network/Client.h
r7801 r8829 73 73 Client(); 74 74 ~Client(); 75 75 76 76 static Client* getInstance(){ return singletonPtr_s; } // tolua_export 77 77 … … 81 81 void queuePacket(ENetPacket* packet, int clientID, uint8_t channelID); 82 82 virtual bool sendPacket( packet::Packet* packet ){ return packet->send( static_cast<Host*>(this) ); } 83 bool processChat(const std::string& message, unsigned int playerID); 84 virtual bool chat(const std::string& message); 85 virtual bool broadcast(const std::string& message) { return false; } 83 virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID); 84 virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID); 86 85 virtual void printRTT(); 87 86 -
code/branches/output/src/libraries/network/Host.cc
r8827 r8829 32 32 #include <string> 33 33 34 #include "core/CoreIncludes.h" 34 35 #include "core/ObjectList.h" 35 36 #include "core/command/ConsoleCommand.h" 36 #include " ChatListener.h"37 #include "NetworkChatListener.h" 37 38 38 39 namespace orxonox { … … 41 42 static const std::string __CC_printRTT_name = "printRTT"; 42 43 43 SetConsoleCommand("chat", &Host::Chat);44 44 SetConsoleCommand(__CC_printRTT_group, __CC_printRTT_name, &Host::printRTT); 45 45 … … 89 89 } 90 90 91 void Host::Chat(const std::string& message) 91 /** 92 * @brief Sends a chat message through the network. 93 * @param message message to be sent 94 * @param sourceID the ID of the sender 95 * @param targetID the ID of the receiver 96 */ 97 void Host::sendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) 92 98 { 93 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)94 it->incomingChat(message, 0);95 96 bool result = true;97 99 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it ) 98 {99 100 if( (*it)->isActive() ) 100 { 101 if( !(*it)->chat(message) ) 102 result = false; 103 } 104 } 105 // return result; 101 (*it)->doSendChat(message, sourceID, targetID); 106 102 } 107 103 108 bool Host::Broadcast(const std::string& message) 104 /** 105 * @brief Gets called if a packet::Chat packet is received. Passes the message to the listeners. 106 */ 107 void Host::doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) 109 108 { 110 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) 111 it->incomingChat(message, NETWORK_PEER_ID_BROADCAST); 112 113 bool result = true; 114 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it ) 115 { 116 if( (*it)->isActive() ) 117 { 118 if( !(*it)->broadcast(message) ) 119 result = false; 120 } 121 } 122 return result; 109 for (ObjectList<NetworkChatListener>::iterator it = ObjectList<NetworkChatListener>::begin(); it != ObjectList<NetworkChatListener>::end(); ++it) 110 it->incomingChat(message, sourceID); 123 111 } 124 112 125 bool Host::incomingChat(const std::string& message, unsigned int playerID)126 {127 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it)128 it->incomingChat(message, playerID);129 130 bool result = true;131 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it )132 {133 if( (*it)->isActive() )134 {135 if( !(*it)->processChat(message, playerID) )136 result = false;137 }138 }139 return result;140 }141 113 142 114 bool Host::isServer() … … 167 139 168 140 141 ////////////////////////////////////////////////////////////////////////// 142 // NetworkChatListener // 143 ////////////////////////////////////////////////////////////////////////// 144 145 NetworkChatListener::NetworkChatListener() 146 { 147 RegisterRootObject(NetworkChatListener); 148 } 149 169 150 }//namespace orxonox -
code/branches/output/src/libraries/network/Host.h
r8403 r8829 52 52 class _NetworkExport Host: public GamestateManager 53 53 { 54 friend class packet::Chat; 55 54 56 private: 55 //TODO add these functions or adequate56 //virtual bool processChat(packet::Chat *message, unsigned int clientID)=0;57 //virtual bool sendChat(packet::Chat *chat)=0;58 57 virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)=0; 59 virtual bool chat(const std::string& message)=0;60 virtual bool broadcast(const std::string& message)=0;61 virtual bool processChat(const std::string& message, unsigned int playerID)=0;62 58 virtual bool isServer_()=0; 63 64 65 59 66 60 protected: … … 68 62 virtual ~Host(); 69 63 void setActive( bool bActive ){ bIsActive_ = bActive; } 70 // static Host *instance_; 64 65 virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID)=0; 66 virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID)=0; 71 67 72 68 public: … … 74 70 static bool running(){ return instances_s.size(); } 75 71 static void addPacket(ENetPacket* packet, int clientID = NETWORK_PEER_ID_SERVER, uint8_t channelID = 0); 76 //static bool chat(std::string& message);77 // static bool receiveChat(packet::Chat *message, unsigned int clientID);78 72 static unsigned int getPlayerID(){ return clientID_s; } 79 73 static void setClientID(unsigned int id){ clientID_s = id; } 80 74 static bool isServer(); 81 static void Chat(const std::string& message); 82 static bool Broadcast(const std::string& message); 83 static bool incomingChat(const std::string& message, unsigned int playerID); 75 static void sendChat(const std::string& message, unsigned int sourceID, unsigned int targetID); 84 76 virtual void printRTT()=0; 85 77 bool isActive(){ return bIsActive_; } -
code/branches/output/src/libraries/network/NetworkPrereqs.h
r8351 r8829 118 118 namespace orxonox 119 119 { 120 class ChatListener;121 120 class Client; 122 121 class ClientConnection; … … 130 129 class GamestateManager; 131 130 class Host; 131 class NetworkChatListener; 132 132 class NetworkFunctionBase; 133 133 struct NetworkFunctionPointer; -
code/branches/output/src/libraries/network/Server.cc
r8807 r8829 56 56 #include "packet/Gamestate.h" 57 57 #include "packet/Welcome.h" 58 #include "ChatListener.h"59 58 // #include "ClientInformation.h" 60 59 #include "FunctionCallManager.h" … … 104 103 void Server::helper_ConnectToMasterserver() 105 104 { 106 // WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " " 105 // WANDiscovery::getInstance().msc.sendRequest( MSPROTO_GAME_SERVER " " 107 106 // MSPROTO_REGISTER_SERVER ); 108 107 } … … 116 115 orxout(verbose, context::network) << "opening server" << endl; 117 116 this->openListener(); 118 117 119 118 /* make discoverable on LAN */ 120 119 LANDiscoverable::setActivity(true); … … 123 122 WANDiscoverable::setActivity(true); 124 123 /* TODO this needs to be optional, we need a switch from the UI to 125 * enable/disable this 124 * enable/disable this 126 125 */ 127 126 // helper_ConnectToMasterserver(); … … 143 142 /* tell master server we're closing */ 144 143 orxout(internal_info, context::network) << "disconnecting." << endl; 145 WANDiscoverable::setActivity(false); 144 WANDiscoverable::setActivity(false); 146 145 orxout(internal_info, context::network) << "disconnecting done" << endl; 147 146 … … 150 149 } 151 150 152 bool Server::processChat(const std::string& message, unsigned int playerID)153 {154 // ClientInformation *temp = ClientInformation::getBegin();155 packet::Chat *chat;156 // while(temp){157 chat = new packet::Chat(message, playerID);158 chat->setPeerID(NETWORK_PEER_ID_BROADCAST);159 chat->send( static_cast<Host*>(this) );160 // orxout(internal_warning, context::network) << "could not send Chat message to client ID: " << temp->getID() << endl;161 // temp = temp->next();162 // }163 // orxout(message) << "Player " << playerID << ": " << message << endl;164 return true;165 }166 167 168 151 /* handle incoming data */ 169 152 int rephandler( char *addr, ENetEvent *ev ) 170 { 153 { 171 154 /* reply to pings */ 172 if( !strncmp( (char *)ev->packet->data, MSPROTO_PING_GAMESERVER, 155 if( !strncmp( (char *)ev->packet->data, MSPROTO_PING_GAMESERVER, 173 156 MSPROTO_PING_GAMESERVER_LEN ) ) 174 157 //this->msc.sendRequest( MSPROTO_ACK ); 175 158 /* NOTE implement this after pollForReply 176 * reimplementation 159 * reimplementation 177 160 */ 178 161 return 0; … … 183 166 184 167 void Server::helper_HandleMasterServerRequests() 185 { 186 /* poll the master server for replies and see whether something 168 { 169 /* poll the master server for replies and see whether something 187 170 * has to be done or changed. 188 171 */ … … 202 185 // receive and process incoming discovery packets 203 186 LANDiscoverable::update(); 204 187 205 188 // receive and process requests from master server 206 189 /* todo */ … … 330 313 { 331 314 // static unsigned int newid=1; 332 // 315 // 333 316 // orxout(internal_info, context::network) << "Server: adding client" << endl; 334 317 // ClientInformation *temp = ClientInformation::insertBack(new ClientInformation); … … 375 358 // } 376 359 } 377 360 378 361 void Server::processPacket(packet::Packet* packet) 379 362 { … … 411 394 // temp->setSynched(true); 412 395 GamestateManager::setSynched(clientID); 413 396 414 397 orxout(verbose, context::network) << "sending welcome" << endl; 415 398 packet::Welcome *w = new packet::Welcome(clientID); … … 438 421 } 439 422 440 bool Server::chat(const std::string& message) 441 { 442 return this->sendChat(message, Host::getPlayerID()); 443 } 444 445 bool Server::broadcast(const std::string& message) 446 { 447 return this->sendChat(message, NETWORK_PEER_ID_BROADCAST); 448 } 449 450 bool Server::sendChat(const std::string& message, unsigned int clientID) 451 { 452 // ClientInformation *temp = ClientInformation::getBegin(); 453 packet::Chat *chat; 454 // while(temp) 455 { 456 chat = new packet::Chat(message, clientID); 457 chat->setPeerID(NETWORK_PEER_ID_BROADCAST); 458 chat->send( static_cast<Host*>(this) ); 459 // orxout(internal_warning, context::network) << "could not send Chat message to client ID: " << temp->getID() << endl; 460 // temp = temp->next(); 461 } 462 // orxout(message) << "Player " << Host::getPlayerID() << ": " << message << endl; 463 for (ObjectList<ChatListener>::iterator it = ObjectList<ChatListener>::begin(); it != ObjectList<ChatListener>::end(); ++it) 464 it->incomingChat(message, clientID); 465 466 return true; 423 /** 424 * @brief Sends a chat message to the given target ID. 425 * @param message message to be sent 426 * @param sourceID the ID of the sender 427 * @param targetID the ID of the receiver 428 */ 429 void Server::doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) 430 { 431 // check if the target exists. just ignore the message otherwise 432 if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore 433 return; 434 435 // send the message to the target 436 packet::Chat* packet = new packet::Chat(message, sourceID, targetID); 437 packet->setPeerID(targetID); 438 packet->send( static_cast<Host*>(this) ); 439 440 // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners 441 if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID()) 442 Host::doReceiveChat(message, sourceID, targetID); 443 } 444 445 /** 446 * @brief Gets called if a packet::Chat packet is received. Forwards the packet to the target 447 * and calls the parent function if necessary. 448 */ 449 void Server::doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) 450 { 451 this->doSendChat(message, sourceID, targetID); 452 } 453 454 /** 455 * @brief Returns true if the target ID is in the list of clients (or if it 456 * corresponds to the broadcast or the server ID). 457 */ 458 bool Server::isValidTarget(unsigned int targetID) 459 { 460 if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER) 461 return true; 462 463 std::vector<uint32_t>::iterator it; 464 for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it ) 465 if( *it == targetID ) 466 return true; 467 468 return false; 467 469 } 468 470 -
code/branches/output/src/libraries/network/Server.h
r8351 r8829 68 68 void open(); 69 69 void close(); 70 bool processChat(const std::string& message, unsigned int playerID);71 70 void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID); 72 71 virtual bool sendPacket( packet::Packet* packet ){ return packet->send( static_cast<Host*>(this) ); } … … 89 88 bool sendGameStates(); 90 89 bool sendObjectDeletes(); 91 virtual bool chat(const std::string& message);92 virtual bool broadcast(const std::string& message);93 bool sendChat(const std::string& message, unsigned int clientID);90 bool isValidTarget(unsigned int targetID); 91 virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID); 92 virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID); 94 93 void syncClassid(unsigned int clientID); 95 94 -
code/branches/output/src/libraries/network/packet/Chat.cc
r7801 r8829 39 39 40 40 /* Some lengths */ 41 #define _PACKETID 0 42 const int _PLAYERID = _PACKETID + sizeof(Type::Value); 43 #define _MESSAGELENGTH _PLAYERID + sizeof(uint32_t) 44 #define _MESSAGE _MESSAGELENGTH + sizeof(uint32_t) 41 #define _PACKETID 0 42 #define _SOURCEID _PACKETID + sizeof(Type::Value) 43 #define _TARGETID _SOURCEID + sizeof(uint32_t) 44 #define _MESSAGELENGTH _TARGETID + sizeof(uint32_t) 45 #define _MESSAGE _MESSAGELENGTH + sizeof(uint32_t) 45 46 46 Chat::Chat( const std::string& message, unsigned int playerID )47 Chat::Chat( const std::string& message, unsigned int sourceID, unsigned int targetID ) 47 48 : Packet() 48 49 { … … 57 58 58 59 *(Type::Value *)(data_ + _PACKETID ) = Type::Chat; 59 *(unsigned int *)(data_ + _PLAYERID ) = playerID; 60 *(unsigned int *)(data_ + _SOURCEID ) = sourceID; 61 *(unsigned int *)(data_ + _TARGETID ) = targetID; 60 62 *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_; 61 63 … … 81 83 82 84 bool Chat::process(orxonox::Host* host){ 83 bool b = host->incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID));85 host->doReceiveChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_SOURCEID), *(uint32_t *)(data_+_TARGETID)); 84 86 delete this; 85 return b;87 return true; 86 88 } 87 89 -
code/branches/output/src/libraries/network/packet/Chat.h
r7801 r8829 42 42 public: 43 43 /* constructors */ 44 Chat( const std::string& message, unsigned int playerID );44 Chat( const std::string& message, unsigned int sourceID, unsigned int targetID ); 45 45 Chat( uint8_t* data, unsigned int clientID ); 46 46
Note: See TracChangeset
for help on using the changeset viewer.