Changeset 7788 for code/branches
- Timestamp:
- Dec 21, 2010, 6:09:09 PM (14 years ago)
- Location:
- code/branches/presentation2
- Files:
-
- 52 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/presentation2
- Property svn:mergeinfo changed
/code/branches/network3 (added) merged: 7333,7336-7337,7344 /code/branches/network4 (added) merged: 7497,7718,7753-7755 /code/branches/network5 (added) merged: 7757-7759,7772-7778,7780-7781
- Property svn:mergeinfo changed
-
code/branches/presentation2/src/libraries/network/CMakeLists.txt
r7769 r7788 28 28 FunctionCallManager.cc 29 29 GamestateManager.cc 30 GamestateClient.cc30 #GamestateClient.cc 31 31 GamestateHandler.cc 32 32 LANDiscoverable.cc … … 53 53 FunctionCall.h 54 54 FunctionCallManager.h 55 GamestateClient.h55 #GamestateClient.h 56 56 GamestateHandler.h 57 57 GamestateManager.h -
code/branches/presentation2/src/libraries/network/Client.cc
r7495 r7788 64 64 */ 65 65 Client::Client(): 66 gamestate(0),67 66 isSynched_(false), 68 67 gameStateFailure_(false), … … 85 84 { 86 85 Synchronisable::setClient(true); 87 this->gamestate = new GamestateClient();88 86 if( ClientConnection::establishConnection() ) 89 87 { 90 88 Host::setActive(true); 89 GamestateManager::addPeer(NETWORK_PEER_ID_SERVER); 91 90 return true; 92 91 } … … 101 100 bool Client::closeConnection() 102 101 { 103 assert(this->gamestate);104 delete this->gamestate;105 this->gamestate = 0;106 102 Host::setActive(false); 103 GamestateManager::removePeer(NETWORK_PEER_ID_SERVER); 107 104 return ClientConnection::closeConnection(); 108 105 } … … 114 111 } 115 112 116 bool Client::queuePacket(ENetPacket *packet, int clientID) 117 { 118 bool b = ClientConnection::addPacket(packet); 119 assert(b); 120 return b; 113 void Client::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) 114 { 115 ClientConnection::addPacket(packet, channelID); 121 116 } 122 117 … … 140 135 { 141 136 packet::Chat *m = new packet::Chat(message, Host::getPlayerID()); 142 return m->send( );137 return m->send(static_cast<Host*>(this)); 143 138 } 144 139 … … 159 154 { 160 155 COUT(4) << "popping partial gamestate: " << std::endl; 161 packet::Gamestate *gs = gamestate->getGamestate(); 156 // packet::Gamestate *gs = GamestateClient::getGamestate(); 157 GamestateManager::update(); 158 std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates(); 159 std::vector<packet::Gamestate*>::iterator it; 160 for( it = gamestates.begin(); it != gamestates.end(); ++it ) 161 { 162 (*it)->send( static_cast<Host*>(this) ); 163 } 162 164 //assert(gs); <--- there might be the case that no data has to be sent, so its commented out now 163 if(gs){164 COUT(4) << "client tick: sending gs " << gs << std::endl;165 if( !gs->send() )166 COUT(3) << "Problem adding partial gamestate to queue" << std::endl;167 // gs gets automatically deleted by enet callback168 }169 FunctionCallManager::sendCalls( );165 // if(gs){ 166 // COUT(4) << "client tick: sending gs " << gs << std::endl; 167 // if( !gs->send() ) 168 // COUT(2) << "Problem adding partial gamestate to queue" << std::endl; 169 // // gs gets automatically deleted by enet callback 170 // } 171 FunctionCallManager::sendCalls(static_cast<Host*>(this)); 170 172 } 171 173 } 172 sendPackets(); // flush the enet queue174 // sendPackets(); // flush the enet queue 173 175 174 176 Connection::processQueue(); 175 if( gamestate->processGamestates())177 if(GamestateManager::processGamestates()) 176 178 { 177 179 FunctionCallManager::processBufferedFunctionCalls(); … … 179 181 isSynched_=true; 180 182 } 181 gamestate->cleanup();182 Connection::sendPackets();183 // GamestateManager::cleanup();; 184 // Connection::sendPackets(); 183 185 184 186 return; … … 200 202 Game::getInstance().popState(); 201 203 } 204 205 void Client::processPacket(packet::Packet* packet) 206 { 207 if( packet->isReliable() ) 208 { 209 if( this->getLastProcessedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() ) 210 packet->process(static_cast<Host*>(this)); 211 else 212 this->packetQueue_.push_back(packet); 213 } 214 else 215 packet->process(static_cast<Host*>(this)); 216 } 217 202 218 203 219 -
code/branches/presentation2/src/libraries/network/Client.h
r7163 r7788 45 45 46 46 #include <string> 47 #include <deque> 47 48 48 49 #include "util/UtilPrereqs.h" 49 50 #include "util/Singleton.h" 50 51 #include "ClientConnection.h" 51 #include "GamestateClient.h"52 // #include "GamestateClient.h" 52 53 #include "Host.h" 53 54 #include "LANDiscovery.h" … … 78 79 void setDestination( const std::string& serverAddress, unsigned int port ); // tolua_export 79 80 bool closeConnection(); 80 bool queuePacket(ENetPacket *packet, int clientID); 81 void queuePacket(ENetPacket* packet, int clientID, uint8_t channelID); 82 virtual bool sendPacket( packet::Packet* packet ){ return packet->send( static_cast<Host*>(this) ); } 81 83 bool processChat(const std::string& message, unsigned int playerID); 82 84 virtual bool chat(const std::string& message); … … 90 92 Client(const Client& copy); // not used 91 93 virtual bool isServer_(){return false;} 94 void processPacket(packet::Packet* packet); 92 95 93 96 static Client* singletonPtr_s; 94 GamestateClient* gamestate;95 97 bool isSynched_; 98 std::deque<packet::Packet*> packetQueue_; 96 99 97 100 bool gameStateFailure_; -
code/branches/presentation2/src/libraries/network/ClientConnection.cc
r7459 r7788 39 39 const unsigned int NETWORK_CLIENT_CONNECTION_TIMEOUT = 3000; //millisecs 40 40 const unsigned int NETWORK_CLIENT_MAX_CONNECTIONS = 1; 41 const unsigned int NETWORK_CLIENT_CHANNELS = 1;42 41 43 42 … … 71 70 ENetEvent event; 72 71 73 this->host_ = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, 0, 0, 0); 72 // create host 73 this->host_ = enet_host_create(NULL, NETWORK_CLIENT_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); 74 74 75 if ( this->host_ == NULL ) 75 76 { … … 78 79 return false; 79 80 } 81 82 // enable compression 83 this->enableCompression(); 84 80 85 assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); 81 86 if (this->host_->socket4 == ENET_SOCKET_NULL) … … 86 91 COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl; 87 92 88 this->server_ = enet_host_connect(this->host_, serverAddress_, NETWORK_C LIENT_CHANNELS, 0);93 this->server_ = enet_host_connect(this->host_, serverAddress_, NETWORK_CHANNEL_COUNT, 0); 89 94 if ( this->server_==NULL ) 90 95 { … … 99 104 { 100 105 this->established_=true; 106 Connection::startCommunicationThread(); 101 107 return true; 102 108 } … … 112 118 return true; 113 119 this->established_ = false; 120 Connection::stopCommunicationThread(); 114 121 enet_peer_disconnect(this->server_, 0); 115 122 for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++) … … 138 145 139 146 140 bool ClientConnection::addPacket(ENetPacket *packet) {147 void ClientConnection::addPacket(ENetPacket *packet, uint8_t channelID) { 141 148 assert( this->server_ ); 142 149 assert( packet ); 143 return Connection::addPacket( packet, this->server_ );150 return Connection::addPacket( packet, this->server_, channelID ); 144 151 } 145 152 … … 153 160 COUT(1) << "Received disconnect Packet from Server!" << endl; 154 161 // server closed the connection 162 this->stopCommunicationThread(); 155 163 this->connectionClosed(); 156 164 } -
code/branches/presentation2/src/libraries/network/ClientConnection.h
r6417 r7788 44 44 void setPort( unsigned int port ); 45 45 46 ENetEvent *getEvent();46 // ENetEvent *getEvent(); 47 47 // check wheter the packet queue is empty 48 bool queueEmpty();48 // bool queueEmpty(); 49 49 // create a new listener thread 50 50 virtual bool establishConnection(); 51 51 virtual bool closeConnection(); 52 52 // add a packet to queue for the server 53 bool addPacket(ENetPacket *packet);53 void addPacket(ENetPacket *packet, uint8_t channelID); 54 54 inline bool isConnected(){ return this->established_; } 55 55 protected: -
code/branches/presentation2/src/libraries/network/ClientInformation.cc
r7459 r7788 56 56 preve=0; 57 57 nexte=0; 58 partialGamestateID_=GAMESTATEID_INITIAL-1;59 58 synched_=false; 60 59 } … … 133 132 } 134 133 135 bool ClientInformation::setPartialGamestateID(int id){136 if(!this)137 return false;138 partialGamestateID_=id;139 return true;140 }141 142 134 unsigned int ClientInformation::getID() { 143 135 if(!this) … … 154 146 } 155 147 156 int ClientInformation::getFailures(){157 return failures_;158 }159 void ClientInformation::addFailure(){160 failures_++;161 }162 void ClientInformation::resetFailures(){163 failures_=0;164 }165 166 148 uint32_t ClientInformation::getRTT(){ 167 149 return this->peer_->roundTripTime; … … 175 157 if(this) 176 158 return gamestateID_; 177 else178 return static_cast<unsigned int>(-1);179 }180 181 unsigned int ClientInformation::getPartialGamestateID() {182 if(this)183 return partialGamestateID_;184 159 else 185 160 return static_cast<unsigned int>(-1); -
code/branches/presentation2/src/libraries/network/ClientInformation.h
r5781 r7788 66 66 bool setPeer(ENetPeer *peer); 67 67 bool setGamestateID(int id); 68 bool setPartialGamestateID(int id);69 68 inline void setShipID(unsigned int id){ShipID_=id;} 70 69 … … 73 72 unsigned int getID(); 74 73 unsigned int getGamestateID(); 75 unsigned int getPartialGamestateID();76 74 ENetPeer *getPeer(); 77 75 78 int getFailures();79 void addFailure();80 void resetFailures();81 76 uint32_t getRTT(); 82 77 double getPacketLoss(); … … 106 101 unsigned int clientID_; 107 102 unsigned int gamestateID_; 108 unsigned int partialGamestateID_;109 103 unsigned int ShipID_; // this is the unique objectID 110 104 bool synched_; 111 unsigned short failures_;112 105 113 106 }; -
code/branches/presentation2/src/libraries/network/Connection.cc
r7163 r7788 30 30 31 31 #include <cassert> 32 #include <deque> 32 33 #define WIN32_LEAN_AND_MEAN 33 34 #include <enet/enet.h> 35 #include <boost/thread.hpp> 36 #include <boost/thread/mutex.hpp> 37 #include <boost/date_time.hpp> 38 34 39 #include "packet/Packet.h" 35 40 36 41 namespace orxonox 37 42 { 38 // Connection *Connection::instance_=0;43 const boost::posix_time::millisec NETWORK_COMMUNICATION_THREAD_WAIT_TIME(20); 39 44 40 45 Connection::Connection(): 41 host_(0) 42 { 43 // assert(instance_==0); 44 // Connection::instance_=this; 46 host_(0), bCommunicationThreadRunning_(false) 47 { 45 48 enet_initialize(); 46 49 atexit(enet_deinitialize); 47 } 48 49 Connection::~Connection(){ 50 // Connection::instance_=0; 51 } 52 53 int Connection::service(ENetEvent* event) { 54 return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT ); 55 } 56 57 void Connection::disconnectPeer(ENetPeer *peer) { 58 enet_peer_disconnect(peer, 0); 59 } 60 61 bool Connection::addPacket(ENetPacket *packet, ENetPeer *peer) { 62 if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0) 63 return false; 64 else 65 return true; 66 } 67 68 bool Connection::sendPackets() { 69 if ( /*!Connection::instance_ || */this->host_==NULL ) 70 return false; 71 enet_host_flush(this->host_); 72 return true; 73 } 74 75 void Connection::processQueue() { 50 this->incomingEventsMutex_ = new boost::mutex; 51 this->outgoingEventsMutex_ = new boost::mutex; 52 } 53 54 Connection::~Connection() 55 { 56 delete this->incomingEventsMutex_; 57 delete this->outgoingEventsMutex_; 58 } 59 60 void Connection::startCommunicationThread() 61 { 62 this->bCommunicationThreadRunning_ = true; 63 this->communicationThread_ = new boost::thread(&Connection::communicationThread, this); 64 } 65 66 void Connection::stopCommunicationThread() 67 { 68 this->bCommunicationThreadRunning_ = false; 69 if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) ) 70 { 71 // force thread to stop 72 this->communicationThread_->interrupt(); 73 } 74 delete this->communicationThread_; 75 } 76 77 78 // int Connection::service(ENetEvent* event) { 79 // return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT ); 80 // } 81 82 void Connection::disconnectPeer(ENetPeer *peer) 83 { 84 assert(peer); 85 outgoingEvent outEvent = { peer, outgoingEventType::disconnectPeer, (ENetPacket*)10, 15 }; 86 87 this->outgoingEventsMutex_->lock(); 88 this->outgoingEvents_.push_back(outEvent); 89 this->outgoingEventsMutex_->unlock(); 90 } 91 92 void Connection::addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID) 93 { 94 assert(peer); 95 outgoingEvent outEvent = { peer, outgoingEventType::sendPacket, packet, channelID }; 96 97 this->outgoingEventsMutex_->lock(); 98 this->outgoingEvents_.push_back(outEvent); 99 this->outgoingEventsMutex_->unlock(); 100 } 101 102 void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID) 103 { 104 outgoingEvent outEvent = { (ENetPeer*)15, outgoingEventType::broadcastPacket, packet, channelID }; 105 106 this->outgoingEventsMutex_->lock(); 107 this->outgoingEvents_.push_back(outEvent); 108 this->outgoingEventsMutex_->unlock(); 109 } 110 111 112 void Connection::communicationThread() 113 { 114 COUT(0) << "starting communication thread" << endl; 76 115 ENetEvent event; 77 78 assert(this->host_); 79 80 while( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 ) 116 117 while( bCommunicationThreadRunning_ ) 81 118 { 82 switch(event.type){ 119 // Receive all pending incoming Events (such as packets, connects and disconnects) 120 while( enet_host_check_events( this->host_, &event ) > 0 ) 121 { 122 // COUT(0) << "incoming event" << endl; 123 // received an event 124 this->incomingEventsMutex_->lock(); 125 this->incomingEvents_.push_back(event); 126 this->incomingEventsMutex_->unlock(); 127 } 128 129 // Send all waiting outgoing packets 130 this->outgoingEventsMutex_->lock(); 131 uint32_t outgoingEventsCount = this->outgoingEvents_.size(); 132 this->outgoingEventsMutex_->unlock(); 133 while( outgoingEventsCount > 0 ) 134 { 135 // COUT(0) << "outgoing event" << endl; 136 this->outgoingEventsMutex_->lock(); 137 outgoingEvent outEvent = this->outgoingEvents_.front(); 138 this->outgoingEvents_.pop_front(); 139 this->outgoingEventsMutex_->unlock(); 140 141 switch( outEvent.type ) 142 { 143 case outgoingEventType::sendPacket: 144 enet_peer_send( outEvent.peer, outEvent.channelID, outEvent.packet ); 145 break; 146 case outgoingEventType::disconnectPeer: 147 enet_peer_disconnect(outEvent.peer, 0); 148 break; 149 case outgoingEventType::broadcastPacket: 150 enet_host_broadcast( this->host_, outEvent.channelID, outEvent.packet ); 151 break; 152 default: 153 assert(0); 154 } 155 this->outgoingEventsMutex_->lock(); 156 outgoingEventsCount = this->outgoingEvents_.size(); 157 this->outgoingEventsMutex_->unlock(); 158 } 159 160 // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms) 161 if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 ) 162 { 163 // COUT(0) << "incoming event after wait" << endl; 164 //received an event 165 this->incomingEventsMutex_->lock(); 166 this->incomingEvents_.push_back(event); 167 this->incomingEventsMutex_->unlock(); 168 } 169 } 170 } 171 172 void Connection::processQueue() 173 { 174 ENetEvent event; 175 176 this->incomingEventsMutex_->lock(); 177 uint32_t incomingEventsCount = this->incomingEvents_.size(); 178 this->incomingEventsMutex_->unlock(); 179 while( incomingEventsCount > 0 ) 180 { 181 packet::Packet* p; 182 this->incomingEventsMutex_->lock(); 183 event = this->incomingEvents_.front(); 184 this->incomingEvents_.pop_front(); 185 this->incomingEventsMutex_->unlock(); 186 187 switch(event.type) 188 { 83 189 // log handling ================ 84 190 case ENET_EVENT_TYPE_CONNECT: … … 89 195 break; 90 196 case ENET_EVENT_TYPE_RECEIVE: 91 processPacket( &event ); 197 // COUT(0) << "ENET_EVENT_TYPE_RECEIVE" << endl; 198 p = createPacket( &event ); 199 processPacket(p); 92 200 break; 93 201 case ENET_EVENT_TYPE_NONE: 94 202 break; 95 203 } 204 205 this->incomingEventsMutex_->lock(); 206 incomingEventsCount = this->incomingEvents_.size(); 207 this->incomingEventsMutex_->unlock(); 96 208 } 97 209 } 98 210 99 bool Connection::processPacket(ENetEvent* event) { 211 packet::Packet* Connection::createPacket(ENetEvent* event) 212 { 100 213 packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer); 101 return p->process(); 102 } 214 return p; 215 // return p->process(); 216 } 217 218 void Connection::enableCompression() 219 { 220 enet_host_compress_with_range_coder( this->host_ ); 221 } 222 103 223 104 224 } -
code/branches/presentation2/src/libraries/network/Connection.h
r7163 r7788 43 43 #include "NetworkPrereqs.h" 44 44 45 #include <deque> 46 47 namespace boost 48 { 49 class thread; 50 class mutex; 51 } 52 45 53 namespace orxonox 46 54 { 47 const unsigned int NETWORK_PORT = 55556; 48 const unsigned int NETWORK_MAX_CONNECTIONS = 50; 49 const unsigned int NETWORK_WAIT_TIMEOUT = 0; 50 const unsigned int NETWORK_DEFAULT_CHANNEL = 0; 51 const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5; 52 53 class _NetworkExport Connection{ 55 const unsigned int NETWORK_PORT = 55556; 56 const unsigned int NETWORK_MAX_CONNECTIONS = 50; 57 const unsigned int NETWORK_WAIT_TIMEOUT = 1; 58 const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5; 59 60 namespace outgoingEventType 61 { 62 enum Value 63 { 64 sendPacket = 1, 65 disconnectPeer = 2, 66 broadcastPacket = 3 67 }; 68 69 } 70 71 struct _NetworkExport outgoingEvent 72 { 73 ENetPeer* peer; 74 outgoingEventType::Value type; 75 ENetPacket* packet; 76 ENetChannelID channelID; 77 }; 78 79 class _NetworkExport Connection 80 { 54 81 public: 55 82 virtual ~Connection(); 56 83 57 static bool addPacket(ENetPacket *packet, ENetPeer *peer);58 bool sendPackets();59 ENetHost* getHost(){ return this->host_; }84 void addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID); 85 void broadcastPacket(ENetPacket* packet, uint8_t channelID); 86 // ENetHost* getHost(){ return this->host_; } 60 87 61 88 protected: … … 63 90 // static Connection* getInstance(){ return Connection::instance_; } 64 91 65 int service(ENetEvent* event); 92 // int service(ENetEvent* event); 93 void startCommunicationThread(); 94 void stopCommunicationThread(); 95 void communicationThread(); 66 96 virtual void disconnectPeer(ENetPeer *peer); 97 98 void enableCompression(); 67 99 68 100 void processQueue(); 69 101 virtual void addPeer(ENetEvent* event)=0; 70 102 virtual void removePeer(ENetEvent* event)=0; 71 virtual bool processPacket(ENetEvent* event); 103 virtual void processPacket( packet::Packet* packet)=0; 104 virtual packet::Packet* createPacket(ENetEvent* event); 72 105 73 ENetHost *host_;106 ENetHost* host_; 74 107 private: 75 ENetAddress *bindAddress_; 108 boost::thread* communicationThread_; 109 bool bCommunicationThreadRunning_; 110 ENetAddress* bindAddress_; 111 std::deque<ENetEvent> incomingEvents_; 112 std::deque<outgoingEvent> outgoingEvents_; 113 boost::mutex* incomingEventsMutex_; 114 boost::mutex* outgoingEventsMutex_; 76 115 77 116 // static Connection *instance_; -
code/branches/presentation2/src/libraries/network/FunctionCallManager.cc
r7495 r7788 30 30 #include "packet/FunctionCalls.h" 31 31 #include "core/GameMode.h" 32 #include "GamestateHandler.h" 32 33 33 34 namespace orxonox { 34 35 35 std::map<uint32_t, packet::FunctionCalls*> FunctionCallManager::s ClientMap_;36 std::vector< FunctionCall> FunctionCallManager::sIncomingFunctionCallBuffer_;36 std::map<uint32_t, packet::FunctionCalls*> FunctionCallManager::sPeerMap_; 37 std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t> > > FunctionCallManager::sIncomingFunctionCallBuffer_; 37 38 38 39 // Static calls 39 40 40 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID)41 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID) 41 42 { 42 if(s ClientMap_.find(clientID)==sClientMap_.end())43 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 43 44 { 44 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;45 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);45 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 46 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 46 47 } 47 FunctionCallManager::s ClientMap_[clientID]->addCallStatic(functionID);48 FunctionCallManager::sPeerMap_[peerID]->addCallStatic(functionID); 48 49 } 49 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1)50 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1) 50 51 { 51 if(s ClientMap_.find(clientID)==sClientMap_.end())52 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 52 53 { 53 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;54 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);54 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 55 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 55 56 } 56 FunctionCallManager:: s ClientMap_[clientID]->addCallStatic(functionID, &mt1);57 FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1); 57 58 } 58 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2)59 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2) 59 60 { 60 if(s ClientMap_.find(clientID)==sClientMap_.end())61 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 61 62 { 62 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;63 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);63 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 64 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 64 65 } 65 FunctionCallManager:: s ClientMap_[clientID]->addCallStatic(functionID, &mt1, &mt2);66 FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1, &mt2); 66 67 } 67 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)68 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3) 68 69 { 69 if(s ClientMap_.find(clientID)==sClientMap_.end())70 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 70 71 { 71 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;72 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);72 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 73 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 73 74 } 74 FunctionCallManager:: s ClientMap_[clientID]->addCallStatic(functionID, &mt1, &mt2, &mt3);75 FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1, &mt2, &mt3); 75 76 } 76 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)77 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4) 77 78 { 78 if(s ClientMap_.find(clientID)==sClientMap_.end())79 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 79 80 { 80 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;81 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);81 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 82 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 82 83 } 83 FunctionCallManager:: s ClientMap_[clientID]->addCallStatic(functionID, &mt1, &mt2, &mt3, &mt4);84 FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1, &mt2, &mt3, &mt4); 84 85 } 85 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)86 void FunctionCallManager::addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5) 86 87 { 87 if(s ClientMap_.find(clientID)==sClientMap_.end())88 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 88 89 { 89 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;90 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);90 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 91 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 91 92 } 92 FunctionCallManager:: s ClientMap_[clientID]->addCallStatic(functionID, &mt1, &mt2, &mt3, &mt4, &mt5);93 FunctionCallManager:: sPeerMap_[peerID]->addCallStatic(functionID, &mt1, &mt2, &mt3, &mt4, &mt5); 93 94 } 94 95 … … 96 97 // MemberCalls 97 98 98 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID)99 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID) 99 100 { 100 if(s ClientMap_.find(clientID)==sClientMap_.end())101 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 101 102 { 102 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;103 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);103 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 104 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 104 105 } 105 FunctionCallManager::s ClientMap_[clientID]->addCallMember(functionID, objectID);106 FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID); 106 107 } 107 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1)108 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1) 108 109 { 109 if(s ClientMap_.find(clientID)==sClientMap_.end())110 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 110 111 { 111 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;112 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);112 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 113 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 113 114 } 114 FunctionCallManager::s ClientMap_[clientID]->addCallMember(functionID, objectID, &mt1);115 FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1); 115 116 } 116 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2)117 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2) 117 118 { 118 if(s ClientMap_.find(clientID)==sClientMap_.end())119 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 119 120 { 120 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;121 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);121 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 122 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 122 123 } 123 FunctionCallManager::s ClientMap_[clientID]->addCallMember(functionID, objectID, &mt1, &mt2);124 FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1, &mt2); 124 125 } 125 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3)126 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3) 126 127 { 127 if(s ClientMap_.find(clientID)==sClientMap_.end())128 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 128 129 { 129 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;130 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);130 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 131 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 131 132 } 132 FunctionCallManager::s ClientMap_[clientID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3);133 FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3); 133 134 } 134 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4)135 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4) 135 136 { 136 if(s ClientMap_.find(clientID)==sClientMap_.end())137 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 137 138 { 138 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;139 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);139 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 140 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 140 141 } 141 FunctionCallManager::s ClientMap_[clientID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3, &mt4);142 FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3, &mt4); 142 143 } 143 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5)144 void FunctionCallManager::addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5) 144 145 { 145 if(s ClientMap_.find(clientID)==sClientMap_.end())146 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 146 147 { 147 FunctionCallManager::s ClientMap_[clientID] = new packet::FunctionCalls;148 FunctionCallManager::s ClientMap_[clientID]->setClientID(clientID);148 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 149 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 149 150 } 150 FunctionCallManager::s ClientMap_[clientID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3, &mt4, &mt5);151 FunctionCallManager::sPeerMap_[peerID]->addCallMember(functionID, objectID, &mt1, &mt2, &mt3, &mt4, &mt5); 151 152 } 152 153 153 154 // Send calls 154 155 155 void FunctionCallManager::sendCalls( )156 void FunctionCallManager::sendCalls(orxonox::Host* host) 156 157 { 157 158 std::map<uint32_t, packet::FunctionCalls*>::iterator it; 158 for (it = FunctionCallManager::s ClientMap_.begin(); it != FunctionCallManager::sClientMap_.end(); ++it )159 for (it = FunctionCallManager::sPeerMap_.begin(); it != FunctionCallManager::sPeerMap_.end(); ++it ) 159 160 { 160 assert(!FunctionCallManager::s ClientMap_.empty());161 it->second->send( );161 assert(!FunctionCallManager::sPeerMap_.empty()); 162 it->second->send(host); 162 163 } 163 FunctionCallManager::s ClientMap_.clear();164 FunctionCallManager::sPeerMap_.clear(); 164 165 } 165 166 166 void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall )167 void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall, uint32_t minGamestateID, uint32_t peerID) 167 168 { 168 if( !GameMode::isMaster() ) 169 FunctionCallManager::sIncomingFunctionCallBuffer_.push_back( fctCall ); 169 FunctionCallManager::sIncomingFunctionCallBuffer_.push_back( std::make_pair(fctCall, std::make_pair(minGamestateID, peerID))); 170 170 } 171 171 172 172 void FunctionCallManager::processBufferedFunctionCalls() 173 173 { 174 std::vector< FunctionCall>::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin();174 std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t> > >::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin(); 175 175 while( it!=FunctionCallManager::sIncomingFunctionCallBuffer_.end() ) 176 176 { 177 if( it-> execute() )177 if( it->first.execute() ) 178 178 FunctionCallManager::sIncomingFunctionCallBuffer_.erase(it); 179 179 else 180 { 180 181 ++it; 182 } 181 183 } 182 184 } -
code/branches/presentation2/src/libraries/network/FunctionCallManager.h
r7495 r7788 34 34 #include <map> 35 35 #include <vector> 36 #include <utility> 36 37 #include "util/UtilPrereqs.h" 37 38 #include "FunctionCall.h" … … 45 46 { 46 47 public: 47 static void addCallStatic(uint32_t functionID, uint32_t clientID);48 static void addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1);49 static void addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2);50 static void addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3);51 static void addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4);52 static void addCallStatic(uint32_t functionID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5);48 static void addCallStatic(uint32_t functionID, uint32_t peerID); 49 static void addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1); 50 static void addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2); 51 static void addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3); 52 static void addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4); 53 static void addCallStatic(uint32_t functionID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5); 53 54 54 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID);55 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1);56 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2);57 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3);58 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4);59 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t clientID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5);55 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID); 56 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1); 57 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2); 58 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3); 59 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4); 60 static void addCallMember(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5); 60 61 61 static void sendCalls( );62 static void sendCalls(orxonox::Host* host); 62 63 63 static void bufferIncomingFunctionCall( const FunctionCall& fctCall );64 static void bufferIncomingFunctionCall( const FunctionCall& fctCall, uint32_t minGamestateID, uint32_t peerID ); 64 65 static void processBufferedFunctionCalls(); 65 66 66 static std::map<uint32_t, packet::FunctionCalls*> sClientMap_;67 static std::vector< FunctionCall>sIncomingFunctionCallBuffer_;67 static std::map<uint32_t, packet::FunctionCalls*> sPeerMap_; 68 static std::vector<std::pair<FunctionCall,std::pair<uint32_t, uint32_t> > > sIncomingFunctionCallBuffer_; 68 69 protected: 69 70 FunctionCallManager(); -
code/branches/presentation2/src/libraries/network/GamestateClient.cc
r7401 r7788 39 39 namespace orxonox 40 40 { 41 struct _NetworkExport GameStateItem{ 41 struct _NetworkExport GameStateItem 42 { 42 43 packet::Gamestate *state; 43 44 unsigned int id; 44 45 }; 45 46 46 GamestateClient::GamestateClient() { 47 GamestateClient::GamestateClient() 48 { 47 49 COUT(5) << "this: " << this << std::endl; 48 last _diff_=0;49 last _gamestate_=GAMESTATEID_INITIAL-1;50 lastAckedGamestateID_=GAMESTATEID_INITIAL-1; 51 lastProcessedGamestateID_=GAMESTATEID_INITIAL-1; 50 52 tempGamestate_=NULL; 51 53 } 52 54 53 GamestateClient::~GamestateClient() { 55 GamestateClient::~GamestateClient() 56 { 54 57 std::map<unsigned int, packet::Gamestate *>::iterator it; 55 58 for ( it = this->gamestateMap_.begin(); it != this->gamestateMap_.end(); ++it ) … … 59 62 } 60 63 61 bool GamestateClient::ack(unsigned int gamestateID, unsigned int clientID){ 64 bool GamestateClient::ackGamestate(unsigned int gamestateID, unsigned int clientID) 65 { 62 66 return true; 63 67 } 64 68 65 bool GamestateClient::add(packet::Gamestate *gs, unsigned int clientID){ 66 if(tempGamestate_!=NULL){ 69 bool GamestateClient::addGamestate(packet::Gamestate *gs, unsigned int clientID) 70 { 71 if(tempGamestate_!=NULL) 72 { 67 73 //delete the obsolete gamestate 68 74 if(tempGamestate_->getID()>gs->getID()) … … 74 80 } 75 81 76 bool GamestateClient::processGamestates(){ 82 bool GamestateClient::processGamestates() 83 { 77 84 if(tempGamestate_==NULL) 78 85 return false; … … 85 92 NetworkCallbackManager::callCallbacks(); 86 93 87 if (!processed){ 94 if (!processed) 95 { 96 assert(0); 88 97 sendAck(0); 89 98 return false; … … 92 101 tempGamestate_=NULL; 93 102 gamestateMap_[processed->getID()]=processed; 103 lastProcessedGamestateID_ = processed->getID(); 94 104 if(isDiffed) 95 last _diff_ = processed->getBaseID();105 lastAckedGamestateID_ = processed->getBaseID(); 96 106 id = processed->getID(); 97 107 sendAck(id); … … 105 115 * @return iterator pointing to the next object in the list 106 116 */ 107 void GamestateClient::removeObject(ObjectListIterator<Synchronisable> &it) { 117 void GamestateClient::removeObject(ObjectListIterator<Synchronisable> &it) 118 { 108 119 ObjectListIterator<Synchronisable> temp=it; 109 120 ++it; … … 111 122 } 112 123 113 packet::Gamestate *GamestateClient::getGamestate(){ 124 packet::Gamestate *GamestateClient::getGamestate() 125 { 114 126 packet::Gamestate *gs = new packet::Gamestate(); 115 if(!gs->collectData(0,0x2)){ 127 if(!gs->collectData(this->getLastProcessedGamestateID(NETWORK_PEER_ID_SERVER), 0x2)) 128 { 116 129 delete gs; 117 130 return 0; … … 120 133 } 121 134 122 void GamestateClient::cleanup(){ 135 void GamestateClient::cleanup() 136 { 123 137 std::map<unsigned int, packet::Gamestate*>::iterator temp, it = gamestateMap_.begin(); 124 while(it!=gamestateMap_.end()){ 125 if(it->first>=last_diff_) 138 while(it!=gamestateMap_.end()) 139 { 140 if(it->first>=lastAckedGamestateID_) 126 141 break; 127 142 // otherwise delete that stuff … … 133 148 } 134 149 135 void GamestateClient::printGamestateMap(){ 150 void GamestateClient::printGamestateMap() 151 { 136 152 std::map<unsigned int, packet::Gamestate*>::iterator it; 137 153 COUT(4) << "gamestates: "; 138 for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++){ 154 for(it=gamestateMap_.begin(); it!=gamestateMap_.end(); it++) 155 { 139 156 COUT(4) << it->first << ':' << it->second << '|'; 140 157 } … … 143 160 } 144 161 145 bool GamestateClient::sendAck(unsigned int gamestateID){ 162 bool GamestateClient::sendAck(unsigned int gamestateID) 163 { 146 164 packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, 0); 147 if(!ack->send()){ 165 if(!ack->send()) 166 { 148 167 COUT(3) << "could not ack gamestate: " << gamestateID << std::endl; 149 168 return false; 150 169 } 151 else{ 170 else 171 { 152 172 COUT(5) << "acked a gamestate: " << gamestateID << std::endl; 153 173 return true; … … 155 175 } 156 176 157 packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs){ 177 packet::Gamestate *GamestateClient::processGamestate(packet::Gamestate *gs) 178 { 158 179 if(gs->isCompressed()) 159 180 { -
code/branches/presentation2/src/libraries/network/GamestateClient.h
r5781 r7788 57 57 ~GamestateClient(); 58 58 59 bool add(packet::Gamestate *gs, unsigned int clientID); 60 bool ack(unsigned int gamestateID, unsigned int clientID); 59 virtual bool addGamestate(packet::Gamestate *gs, unsigned int clientID); 60 virtual bool ackGamestate(unsigned int gamestateID, unsigned int clientID); 61 virtual uint32_t getLastProcessedGamestateID(unsigned int clientID=0) { return this->lastProcessedGamestateID_; } 62 virtual uint32_t getCurrentGamestateID(){ return this->lastProcessedGamestateID_; } 61 63 62 64 bool processGamestates(); … … 69 71 bool sendAck(unsigned int gamestateID); 70 72 71 unsigned int last _diff_;72 unsigned int last _gamestate_;73 unsigned int lastAckedGamestateID_; 74 unsigned int lastProcessedGamestateID_; 73 75 std::map<unsigned int, packet::Gamestate *> gamestateMap_; 74 76 packet::Gamestate *tempGamestate_; // we save the received gamestates here during processQueue -
code/branches/presentation2/src/libraries/network/GamestateHandler.cc
r6417 r7788 32 32 namespace orxonox { 33 33 34 GamestateHandler *GamestateHandler::instance_=0;34 // GamestateHandler *GamestateHandler::instance_=0; 35 35 36 36 GamestateHandler::GamestateHandler() 37 37 { 38 assert(instance_==0);39 instance_=this;40 38 } 41 39 … … 43 41 GamestateHandler::~GamestateHandler() 44 42 { 45 instance_=0;46 43 } 47 44 -
code/branches/presentation2/src/libraries/network/GamestateHandler.h
r6073 r7788 32 32 #include "NetworkPrereqs.h" 33 33 34 #include <cassert> 35 34 36 namespace orxonox { 35 37 … … 37 39 @author Oliver Scheuss 38 40 */ 39 class _NetworkExport GamestateHandler{ 41 class _NetworkExport GamestateHandler 42 { 40 43 private: 41 virtual bool add(packet::Gamestate *gs, unsigned int clientID)=0;42 virtual bool ack(unsigned int gamestateID, unsigned int clientID)=0;43 44 static GamestateHandler *instance_;45 44 46 45 … … 50 49 51 50 public: 52 static bool addGamestate(packet::Gamestate *gs, unsigned int clientID){ return instance_->add(gs, clientID); } 53 static bool ackGamestate(unsigned int gamestateID, unsigned int clientID){ return instance_->ack(gamestateID, clientID); } 51 virtual bool addGamestate(packet::Gamestate* gs, unsigned int clientID) = 0; 52 virtual bool ackGamestate(unsigned int gamestateID, unsigned int clientID) = 0; 53 virtual uint32_t getLastProcessedGamestateID( unsigned int clientID )=0; 54 virtual uint32_t getCurrentGamestateID()=0; 54 55 }; 55 56 -
code/branches/presentation2/src/libraries/network/GamestateManager.cc
r7284 r7788 43 43 #include <cassert> 44 44 #include <queue> 45 #include "util/Clock.h"46 45 // #include <boost/thread/mutex.hpp> 47 46 48 #include "util/Debug.h"49 #include "core/ThreadPool.h"50 #include "core/command/Executor.h"51 #include "ClientInformation.h"52 47 #include "packet/Acknowledgement.h" 53 48 #include "packet/Gamestate.h" 54 49 #include "synchronisable/NetworkCallbackManager.h" 55 #include "TrafficControl.h" 50 51 #include "core/ThreadPool.h" 52 #include "core/command/Executor.h" 53 #include "core/GameMode.h" 54 #include "util/Debug.h" 55 #include "util/Clock.h" 56 // #include "TrafficControl.h" 56 57 57 58 namespace orxonox 58 59 { 59 60 GamestateManager::GamestateManager() : 60 reference(0), id_(0)61 { 62 trafficControl_ = new TrafficControl();61 currentGamestate_(0), id_(0) 62 { 63 // trafficControl_ = new TrafficControl(); 63 64 // threadMutex_ = new boost::mutex(); 64 65 // threadPool_ = new ThreadPool(); … … 67 68 GamestateManager::~GamestateManager() 68 69 { 69 if( this-> reference)70 delete this-> reference;std::map<unsigned int, packet::Gamestate*>::iterator it;70 if( this->currentGamestate_ ) 71 delete this->currentGamestate_;std::map<unsigned int, packet::Gamestate*>::iterator it; 71 72 for( it = gamestateQueue.begin(); it != gamestateQueue.end(); ++it ) 72 73 delete it->second; 73 std::map<u nsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator it1;74 std::map<u nsigned int, packet::Gamestate*>::iterator it2;75 for( it1 = gamestateMap_.begin(); it1 != gamestateMap_.end(); ++it1)76 { 77 for( it2 = it1->second.begin(); it2 != it1->second.end(); ++it2)78 delete it2->second;79 } 80 this->trafficControl_->destroy();74 std::map<uint32_t, peerInfo>::iterator peerIt; 75 std::map<uint32_t, packet::Gamestate*>::iterator gamestateIt; 76 for( peerIt = peerMap_.begin(); peerIt != peerMap_.end(); ++peerIt ) 77 { 78 for( gamestateIt = peerIt->second.gamestates.begin(); gamestateIt != peerIt->second.gamestates.end(); ++gamestateIt ) 79 delete gamestateIt->second; 80 } 81 // this->trafficControl_->destroy(); 81 82 // delete this->threadMutex_; 82 83 // delete this->threadPool_; … … 88 89 } 89 90 90 bool GamestateManager::add(packet::Gamestate *gs, unsigned int clientID){ 91 bool GamestateManager::addGamestate(packet::Gamestate *gs, unsigned int clientID) 92 { 91 93 assert(gs); 92 94 std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID); … … 99 101 } 100 102 101 bool GamestateManager::processGamestates(){ 103 bool GamestateManager::processGamestates() 104 { 102 105 if( this->gamestateQueue.empty() ) 103 106 return true; … … 107 110 bool b = processGamestate(it->second); 108 111 assert(b); 112 sendAck( it->second->getID(), it->second->getPeerID() ); 109 113 delete it->second; 110 114 } … … 115 119 return true; 116 120 } 121 122 bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID) 123 { 124 packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID); 125 if( !this->sendPacket(ack)) 126 { 127 COUT(3) << "could not ack gamestate: " << gamestateID << std::endl; 128 return false; 129 } 130 else 131 { 132 COUT(5) << "acked a gamestate: " << gamestateID << std::endl; 133 return true; 134 } 135 } 117 136 118 137 119 138 bool GamestateManager::getSnapshot(){ 120 if ( reference != 0 ) 121 delete reference; 122 reference = new packet::Gamestate(); 123 if(!reference->collectData(++id_, 0x1)){ //we have no data to send 124 delete reference; 125 reference=0; 139 if ( currentGamestate_ != 0 ) 140 delete currentGamestate_; 141 currentGamestate_ = new packet::Gamestate(); 142 uint8_t gsMode; 143 if( GameMode::isMaster() ) 144 gsMode = packet::GAMESTATE_MODE_SERVER; 145 else 146 gsMode = packet::GAMESTATE_MODE_CLIENT; 147 uint32_t newID; 148 if( GameMode::isMaster() ) 149 newID = ++id_; 150 else 151 newID = peerMap_[NETWORK_PEER_ID_SERVER].lastProcessedGamestateID; 152 153 if(!currentGamestate_->collectData(newID, gsMode)){ //we have no data to send 154 delete currentGamestate_; 155 currentGamestate_=0; 126 156 } 127 157 return true; 128 158 } 129 159 130 void GamestateManager::sendGamestates() 131 { 132 ClientInformation *temp = ClientInformation::getBegin(); 133 std::queue<packet::Gamestate*> clientGamestates; 134 while(temp != NULL){ 135 if( !(temp->getSynched()) ){ 160 std::vector<packet::Gamestate*> GamestateManager::getGamestates() 161 { 162 if(!currentGamestate_) 163 return std::vector<packet::Gamestate*>(); 164 std::vector<packet::Gamestate*> peerGamestates; 165 166 std::map<uint32_t, peerInfo>::iterator peerIt; 167 for( peerIt=peerMap_.begin(); peerIt!=peerMap_.end(); ++peerIt ) 168 { 169 if( !peerIt->second.isSynched ) 170 { 136 171 COUT(5) << "Server: not sending gamestate" << std::endl; 137 temp=temp->next();138 if(!temp)139 break;140 172 continue; 141 173 } 142 COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss()<< std::endl;174 COUT(4) << "client id: " << peerIt->first << std::endl; 143 175 COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; 144 int cid = temp->getID(); //get client id145 146 unsigned int gID = temp->getGamestateID();147 if(!reference) 148 return;149 150 packet::Gamestate *client=0;151 if(gID != GAMESTATEID_INITIAL){152 assert(gamestateMap_.find(cid)!=gamestateMap_.end());153 std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateMap_[cid].find(gID);154 if(it!=gamestateMap_[cid].end())155 {156 client = it->second; 157 }158 }159 160 clientGamestates.push(0);161 finishGamestate( cid, clientGamestates.back(), client, reference);176 int peerID = peerIt->first; //get client id 177 178 unsigned int lastAckedGamestateID = peerIt->second.lastAckedGamestateID; 179 180 packet::Gamestate* baseGamestate=0; 181 if(lastAckedGamestateID != GAMESTATEID_INITIAL) 182 { 183 assert(peerMap_.find(peerID)!=peerMap_.end()); 184 std::map<uint32_t, packet::Gamestate*>::iterator it = peerMap_[peerID].gamestates.find(lastAckedGamestateID); 185 assert(it!=peerMap_[peerID].gamestates.end()); 186 baseGamestate = it->second; 187 } 188 189 peerGamestates.push_back(0); // insert an empty gamestate* to change 190 finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ ); 191 if( peerGamestates.back()==0 ) 192 // nothing to send to remove pointer from vector 193 peerGamestates.pop_back(); 162 194 //FunctorMember<GamestateManager>* functor = 163 195 // ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) ); 164 // executor->setDefaultValues( cid, &clientGamestates.back(), client, reference);196 // executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ ); 165 197 // (*static_cast<Executor*>(executor))(); 166 198 // this->threadPool_->passFunction( executor, true ); 167 // (*functor)( cid, &(clientGamestates.back()), client, reference ); 168 169 temp = temp->next(); 199 // (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ ); 170 200 } 171 201 172 202 // threadPool_->synchronise(); 173 203 174 while( !clientGamestates.empty() ) 175 { 176 if(clientGamestates.front()) 177 clientGamestates.front()->send(); 178 clientGamestates.pop(); 179 } 180 } 181 182 183 void GamestateManager::finishGamestate( unsigned int clientID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) { 204 return peerGamestates; 205 } 206 207 208 void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) { 184 209 //why are we searching the same client's gamestate id as we searched in 185 210 //Server::sendGameState? … … 194 219 // gs->collectData( id_, 0x1 ); 195 220 // this->threadMutex_->lock(); 196 gamestateMap_[clientID][gamestate->getID()]=gs;221 peerMap_[peerID].gamestates[gamestate->getID()]=gs; 197 222 // this->threadMutex_->unlock(); 198 199 223 Clock clock; 224 clock.capture(); 200 225 201 226 if(base) … … 216 241 217 242 218 bool b = gs->compressData();219 assert(b);220 221 COUT(0) << "difftime: " << clock.getDeltaTime() << endl;243 // bool b = gs->compressData(); 244 // assert(b); 245 clock.capture(); 246 COUT(4) << "diff and compress time: " << clock.getDeltaTime() << endl; 222 247 // COUT(5) << "sending gamestate with id " << gs->getID(); 223 248 // if(gamestate->isDiffed()) … … 225 250 // else 226 251 // COUT(5) << endl; 227 gs->set ClientID(clientID);252 gs->setPeerID(peerID); 228 253 destgamestate = gs; 229 254 } 230 255 231 256 232 bool GamestateManager::ack(unsigned int gamestateID, unsigned int clientID) { 233 ClientInformation *temp = ClientInformation::findClient(clientID); 234 assert(temp); 235 unsigned int curid = temp->getGamestateID(); 257 bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID) 258 { 259 // ClientInformation *temp = ClientInformation::findClient(peerID); 260 // assert(temp); 261 std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID); 262 assert(it!=this->peerMap_.end()); 263 unsigned int curid = it->second.lastAckedGamestateID; 236 264 237 265 if(gamestateID == ACKID_NACK){ 238 temp->setGamestateID(GAMESTATEID_INITIAL); 266 it->second.lastAckedGamestateID = GAMESTATEID_INITIAL; 267 // temp->setGamestateID(GAMESTATEID_INITIAL); 239 268 // now delete all saved gamestates for this client 240 std::map<unsigned int, packet::Gamestate*>::iterator it; 241 for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end(); ){ 242 delete it->second; 243 244 gamestateMap_[clientID].erase(it++); 245 } 269 std::map<uint32_t, packet::Gamestate*>::iterator it2; 270 for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){ 271 delete it2->second; 272 } 273 it->second.gamestates.clear(); 246 274 return true; 247 275 } 248 276 249 assert(curid==GAMESTATEID_INITIAL || curid<gamestateID); 250 COUT(5) << "acking gamestate " << gamestateID << " for clientid: " << clientID << " curid: " << curid << std::endl; 251 std::map<unsigned int, packet::Gamestate*>::iterator it; 252 for(it = gamestateMap_[clientID].begin(); it!=gamestateMap_[clientID].end() && it->first<gamestateID; ){ 253 delete it->second; 254 gamestateMap_[clientID].erase(it++); 255 } 256 temp->setGamestateID(gamestateID); 257 TrafficControl::processAck(clientID, gamestateID); 277 assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID); 278 COUT(5) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << std::endl; 279 std::map<uint32_t, packet::Gamestate*>::iterator it2; 280 for( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ) 281 { 282 if( it2->second->getID() < gamestateID ) 283 { 284 delete it2->second; 285 it->second.gamestates.erase(it2++); 286 } 287 else 288 ++it2; 289 } 290 291 // std::map<unsigned int, packet::Gamestate*>::iterator it; 292 // for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){ 293 // delete it->second; 294 // gamestateMap_[peerID].erase(it++); 295 // } 296 it->second.lastAckedGamestateID = gamestateID; 297 // temp->setGamestateID(gamestateID); 298 // TrafficControl::processAck(peerID, gamestateID); 258 299 return true; 259 300 } 260 261 void GamestateManager::removeClient(ClientInformation* client){ 262 assert(client); 263 std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); 264 // first delete all remained gamestates 265 std::map<unsigned int, packet::Gamestate*>::iterator it; 266 for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) 267 delete it->second; 268 // now delete the clients gamestatemap 269 gamestateMap_.erase(clientMap); 270 } 271 272 bool GamestateManager::processGamestate(packet::Gamestate *gs){ 301 302 uint32_t GamestateManager::getLastProcessedGamestateID(unsigned int peerID) 303 { 304 assert( this->peerMap_.find(peerID)!=this->peerMap_.end() ); 305 if( this->peerMap_.find(peerID) != this->peerMap_.end() ) 306 return this->peerMap_[peerID].lastProcessedGamestateID; 307 else 308 return GAMESTATEID_INITIAL; 309 } 310 311 312 void GamestateManager::addPeer(uint32_t peerID) 313 { 314 assert(peerMap_.find(peerID)==peerMap_.end()); 315 peerMap_[peerID].peerID = peerID; 316 peerMap_[peerID].lastProcessedGamestateID = GAMESTATEID_INITIAL; 317 peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL; 318 if( GameMode::isMaster() ) 319 peerMap_[peerID].isSynched = false; 320 else 321 peerMap_[peerID].isSynched = true; 322 } 323 324 void GamestateManager::removePeer(uint32_t peerID) 325 { 326 assert(peerMap_.find(peerID)!=peerMap_.end()); 327 std::map<uint32_t, packet::Gamestate*>::iterator peerIt; 328 for( peerIt = peerMap_[peerID].gamestates.begin(); peerIt!=peerMap_[peerID].gamestates.end(); ++peerIt ) 329 { 330 delete peerIt->second; 331 } 332 peerMap_.erase(peerMap_.find(peerID)); 333 } 334 335 336 // void GamestateManager::removeClient(ClientInformation* client){ 337 // assert(client); 338 // std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> >::iterator clientMap = gamestateMap_.find(client->getID()); 339 // // first delete all remained gamestates 340 // std::map<unsigned int, packet::Gamestate*>::iterator it; 341 // for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) 342 // delete it->second; 343 // // now delete the clients gamestatemap 344 // gamestateMap_.erase(clientMap); 345 // } 346 347 bool GamestateManager::processGamestate(packet::Gamestate *gs) 348 { 273 349 if(gs->isCompressed()) 274 350 { … … 277 353 } 278 354 assert(!gs->isDiffed()); 279 return gs->spreadData(0x1); 355 uint8_t gsMode; 356 if( GameMode::isMaster() ) 357 gsMode = packet::GAMESTATE_MODE_SERVER; 358 else 359 gsMode = packet::GAMESTATE_MODE_CLIENT; 360 if( gs->spreadData(gsMode) ) 361 { 362 this->peerMap_[gs->getPeerID()].lastProcessedGamestateID = gs->getID(); 363 return true; 364 } 365 else 366 return false; 280 367 } 281 368 -
code/branches/presentation2/src/libraries/network/GamestateManager.h
r7163 r7788 46 46 #include "GamestateHandler.h" 47 47 #include "core/CorePrereqs.h" 48 #include "packet/Gamestate.h" 49 #include <boost/concept_check.hpp> 48 50 49 51 namespace orxonox … … 66 68 * @author Oliver Scheuss 67 69 */ 68 class _NetworkExport GamestateManager: public GamestateHandler{ 70 class _NetworkExport GamestateManager: public GamestateHandler 71 { 72 struct peerInfo 73 { 74 uint32_t peerID; 75 uint32_t lastProcessedGamestateID; 76 uint32_t lastAckedGamestateID; 77 bool isSynched; 78 std::map< uint32_t, packet::Gamestate* > gamestates; 79 }; 80 69 81 public: 82 70 83 GamestateManager(); 71 84 ~GamestateManager(); 72 85 73 bool add(packet::Gamestate *gs, unsigned int clientID); 86 virtual bool addGamestate(packet::Gamestate *gs, unsigned int peerID); 87 virtual bool ackGamestate(unsigned int gamestateID, unsigned int peerID); 88 virtual uint32_t getLastProcessedGamestateID( unsigned int peerID ); 89 virtual uint32_t getCurrentGamestateID(){ return currentGamestate_->getID(); } 90 74 91 bool processGamestates(); 92 bool sendAck(unsigned int gamestateID, uint32_t peerID); 75 93 bool update(); 76 void sendGamestates(); 77 // packet::Gamestate *popGameState(unsigned int clientID); 78 void finishGamestate( unsigned int clientID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ); 94 std::vector<packet::Gamestate*> getGamestates(); 95 void finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ); 79 96 80 97 bool getSnapshot(); 81 98 82 bool ack(unsigned int gamestateID, unsigned int clientID); 83 void removeClient(ClientInformation *client); 99 void addPeer( uint32_t peerID ); 100 void setSynched( uint32_t peerID ) 101 { assert(peerMap_.find(peerID)!=peerMap_.end()); peerMap_[peerID].isSynched = true; } 102 void removePeer( uint32_t peerID ); 103 // void removeClient(ClientInformation *client); 104 protected: 105 virtual bool sendPacket( packet::Packet* packet ) = 0; 84 106 private: 85 107 bool processGamestate(packet::Gamestate *gs); 86 108 87 std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> > gamestateMap_;109 // std::map<unsigned int, std::map<unsigned int, packet::Gamestate*> > gamestateMap_; 88 110 std::map<unsigned int, packet::Gamestate*> gamestateQueue; 89 packet::Gamestate *reference; 90 TrafficControl *trafficControl_; 111 // std::map<unsigned int, uint32_t> lastProcessedGamestateID_; 112 std::map<uint32_t, peerInfo> peerMap_; 113 packet::Gamestate* currentGamestate_; 114 // TrafficControl *trafficControl_; 91 115 unsigned int id_; 92 116 // boost::mutex* threadMutex_; -
code/branches/presentation2/src/libraries/network/Host.cc
r7284 r7788 45 45 // Host* Host::instance_=0; 46 46 uint32_t Host::clientID_s=0; 47 uint32_t Host::shipID_s=-1;47 // uint32_t Host::shipID_s=-1; 48 48 std::vector<Host*> Host::instances_s; 49 49 … … 76 76 * @return success? 77 77 */ 78 bool Host::addPacket(ENetPacket *packet, int clientID)78 void Host::addPacket(ENetPacket *packet, int clientID, uint8_t channelID) 79 79 { 80 bool result = true;81 80 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it ) 82 81 { 83 82 if( (*it)->isActive() ) 84 83 { 85 if( !(*it)->queuePacket(packet, clientID) ) 86 result = false; 84 (*it)->queuePacket(packet, clientID, channelID); 87 85 } 88 86 } 89 return result;90 87 } 91 88 -
code/branches/presentation2/src/libraries/network/Host.h
r7284 r7788 31 31 32 32 #include "NetworkPrereqs.h" 33 #include "GamestateManager.h" 33 34 #include "core/CorePrereqs.h" 34 35 … … 49 50 * @author Oliver Scheuss 50 51 */ 51 class _NetworkExport Host{ 52 class _NetworkExport Host: public GamestateManager 53 { 52 54 private: 53 55 //TODO add these functions or adequate 54 56 //virtual bool processChat(packet::Chat *message, unsigned int clientID)=0; 55 57 //virtual bool sendChat(packet::Chat *chat)=0; 56 virtual bool queuePacket(ENetPacket *packet, int clientID)=0;58 virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)=0; 57 59 virtual bool chat(const std::string& message)=0; 58 60 virtual bool broadcast(const std::string& message)=0; … … 71 73 // static Host* getInstance(){ return instance_; } 72 74 static bool running(){ return instances_s.size(); } 73 static bool addPacket(ENetPacket *packet, int clientID=0);75 static void addPacket(ENetPacket* packet, int clientID = NETWORK_PEER_ID_SERVER, uint8_t channelID = 0); 74 76 //static bool chat(std::string& message); 75 77 // static bool receiveChat(packet::Chat *message, unsigned int clientID); 76 78 static unsigned int getPlayerID(){ return clientID_s; } 77 static unsigned int getShipID(){return shipID_s;}78 79 static void setClientID(unsigned int id){ clientID_s = id; } 79 static void setShipID(unsigned int id){ shipID_s = id; }80 80 static bool isServer(); 81 81 static void Chat(const std::string& message); … … 86 86 private: 87 87 static uint32_t clientID_s; 88 static uint32_t shipID_s;89 88 static std::vector<Host*> instances_s; 90 89 bool bIsActive_; -
code/branches/presentation2/src/libraries/network/MasterServerComm.cc
r7769 r7788 90 90 91 91 /* Wait up to 2 seconds for the connection attempt to succeed. */ 92 if (enet_host_service (this->client, this->event, 2000) > 0 &&92 if (enet_host_service (this->client, this->event, 500) > 0 && 93 93 this->event->type == ENET_EVENT_TYPE_CONNECT ) 94 94 COUT(3) << "Connection to master server succeeded.\n"; -
code/branches/presentation2/src/libraries/network/NetworkPrereqs.h
r7490 r7788 64 64 namespace orxonox 65 65 { 66 static const unsigned int GAMESTATEID_INITIAL = static_cast<unsigned int>(-1);67 static const unsigned int CLIENTID_UNKNOWN = static_cast<unsigned int>(-2);66 static const unsigned int GAMESTATEID_INITIAL = static_cast<unsigned int>(-1); 67 static const unsigned int CLIENTID_UNKNOWN = static_cast<unsigned int>(-2); 68 68 extern const char* LAN_DISCOVERY_MESSAGE; 69 69 extern const char* LAN_DISCOVERY_ACK; 70 static const unsigned int LAN_DISCOVERY_PORT = 55557; 70 static const unsigned int LAN_DISCOVERY_PORT = 55557; 71 static const unsigned int NETWORK_PEER_ID_SERVER = 0; 72 static const unsigned int NETWORK_CHANNEL_DEFAULT = 0; 73 static const unsigned int NETWORK_CHANNEL_UNRELIABLE = 1; 74 static const unsigned int NETWORK_CHANNEL_COUNT = 2; 71 75 } 72 76 … … 83 87 enum Value 84 88 { 85 Reliable = 1,86 Unsequence = 2,87 NoAllocate = 489 Reliable = 1, 90 Unsequenced = 2, 91 NoAllocate = 4 88 92 }; 89 93 } … … 97 101 // from ENet 98 102 struct _ENetPeer; 99 typedef _ENetPeer ENetPeer;103 typedef _ENetPeer ENetPeer; 100 104 struct _ENetPacket; 101 typedef _ENetPacket ENetPacket;105 typedef _ENetPacket ENetPacket; 102 106 struct _ENetEvent; 103 typedef _ENetEvent ENetEvent;107 typedef _ENetEvent ENetEvent; 104 108 struct _ENetHost; 105 typedef _ENetHost ENetHost;109 typedef _ENetHost ENetHost; 106 110 struct _ENetAddress; 107 typedef _ENetAddress ENetAddress; 111 typedef _ENetAddress ENetAddress; 112 typedef uint8_t ENetChannelID; 108 113 109 114 namespace orxonox … … 161 166 } 162 167 168 namespace boost 169 { 170 class mutex; 171 class thread; 172 } 173 163 174 #endif /* _NetworkPrereqs_H__ */ -
code/branches/presentation2/src/libraries/network/Server.cc
r7764 r7788 156 156 while(temp){ 157 157 chat = new packet::Chat(message, playerID); 158 chat->set ClientID(temp->getID());159 if(!chat->send( ))158 chat->setPeerID(temp->getID()); 159 if(!chat->send( static_cast<Host*>(this) )) 160 160 COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; 161 161 temp = temp->next(); … … 211 211 // process incoming gamestates 212 212 GamestateManager::processGamestates(); 213 FunctionCallManager::processBufferedFunctionCalls(); 213 214 214 215 // send function calls to clients 215 FunctionCallManager::sendCalls( );216 FunctionCallManager::sendCalls( static_cast<Host*>(this) ); 216 217 217 218 //this steers our network frequency … … 222 223 updateGamestate(); 223 224 } 224 sendPackets(); // flush the enet queue225 } 226 } 227 228 bool Server::queuePacket(ENetPacket *packet, int clientID)229 { 230 return ServerConnection::addPacket(packet, clientID);225 // sendPackets(); // flush the enet queue 226 } 227 } 228 229 void Server::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) 230 { 231 ServerConnection::addPacket(packet, clientID, channelID); 231 232 } 232 233 … … 264 265 return; 265 266 GamestateManager::update(); 266 COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl;267 // COUT(5) << "Server: one gamestate update complete, goig to sendGameState" << std::endl; 267 268 //std::cout << "updated gamestate, sending it" << std::endl; 268 269 //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) 269 sendGameState ();270 sendGameStates(); 270 271 sendObjectDeletes(); 271 COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl;272 // COUT(5) << "Server: one sendGameState turn complete, repeat in next tick" << std::endl; 272 273 //std::cout << "sent gamestate" << std::endl; 273 274 } 274 275 275 bool Server::processPacket( ENetPacket *packet, ENetPeer *peer ){ 276 packet::Packet *p = packet::Packet::createPacket(packet, peer); 277 return p->process(); 278 } 279 280 /** 281 * sends the gamestate 282 */ 283 bool Server::sendGameState() 284 { 285 // COUT(5) << "Server: starting function sendGameState" << std::endl; 286 // ClientInformation *temp = ClientInformation::getBegin(); 287 // bool added=false; 288 // while(temp != NULL){ 289 // if( !(temp->getSynched()) ){ 290 // COUT(5) << "Server: not sending gamestate" << std::endl; 291 // temp=temp->next(); 292 // if(!temp) 293 // break; 294 // continue; 295 // } 296 // COUT(4) << "client id: " << temp->getID() << " RTT: " << temp->getRTT() << " loss: " << temp->getPacketLoss() << std::endl; 297 // COUT(5) << "Server: doing gamestate gamestate preparation" << std::endl; 298 // int cid = temp->getID(); //get client id 299 // packet::Gamestate *gs = GamestateManager::popGameState(cid); 300 // if(gs==NULL){ 301 // COUT(2) << "Server: could not generate gamestate (NULL from compress)" << std::endl; 302 // temp = temp->next(); 303 // continue; 304 // } 305 // //std::cout << "adding gamestate" << std::endl; 306 // gs->setClientID(cid); 307 // if ( !gs->send() ){ 308 // COUT(3) << "Server: packet with client id (cid): " << cid << " not sended: " << temp->getFailures() << std::endl; 309 // temp->addFailure(); 310 // }else 311 // temp->resetFailures(); 312 // added=true; 313 // temp=temp->next(); 314 // // gs gets automatically deleted by enet callback 315 // } 316 GamestateManager::sendGamestates(); 276 /** 277 * sends the current gamestate to all peers 278 */ 279 bool Server::sendGameStates() 280 { 281 std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates(); 282 std::vector<packet::Gamestate*>::iterator it; 283 for( it = gamestates.begin(); it != gamestates.end(); ++it ) 284 { 285 (*it)->send(static_cast<Host*>(this)); 286 } 317 287 return true; 318 288 } 289 319 290 320 291 bool Server::sendObjectDeletes() … … 341 312 packet::DeleteObjects *cd = new packet::DeleteObjects(*del); 342 313 assert(cd); 343 cd->set ClientID(cid);344 if ( !cd->send( ) )345 COUT(3) << "Server: packet with client id (cid): " << cid << " not sended : " << temp->getFailures()<< std::endl;314 cd->setPeerID(cid); 315 if ( !cd->send( static_cast<Host*>(this) ) ) 316 COUT(3) << "Server: packet with client id (cid): " << cid << " not sended" << std::endl; 346 317 temp=temp->next(); 347 318 // gs gets automatically deleted by enet callback … … 367 338 // inform all the listeners 368 339 ClientConnectionListener::broadcastClientConnected(newid); 340 GamestateManager::addPeer(newid); 369 341 370 342 ++newid; … … 382 354 else 383 355 { 356 GamestateManager::removePeer(client->getID()); 384 357 //ServerConnection::disconnectClient( client ); 385 358 //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now … … 387 360 } 388 361 } 362 363 void Server::processPacket(packet::Packet* packet) 364 { 365 if( packet->isReliable() ) 366 { 367 if( this->getLastProcessedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() ) 368 packet->process(static_cast<Host*>(this)); 369 else 370 this->packetQueue_.push_back(packet); 371 } 372 else 373 packet->process(static_cast<Host*>(this)); 374 } 375 389 376 390 377 bool Server::createClient(int clientID) … … 396 383 return false; 397 384 } 398 COUT( 5) << "Con.Man: creating client id: " << temp->getID() << std::endl;385 COUT(4) << "Con.Man: creating client id: " << temp->getID() << std::endl; 399 386 400 387 // synchronise class ids … … 403 390 // now synchronise functionIDs 404 391 packet::FunctionIDs *fIDs = new packet::FunctionIDs(); 405 fIDs->set ClientID(clientID);406 bool b = fIDs->send( );392 fIDs->setPeerID(clientID); 393 bool b = fIDs->send( static_cast<Host*>(this) ); 407 394 assert(b); 408 395 409 396 temp->setSynched(true); 397 GamestateManager::setSynched(clientID); 398 410 399 COUT(4) << "sending welcome" << std::endl; 411 400 packet::Welcome *w = new packet::Welcome(temp->getID(), temp->getShipID()); 412 w->set ClientID(temp->getID());413 b = w->send( );401 w->setPeerID(temp->getID()); 402 b = w->send( static_cast<Host*>(this) ); 414 403 assert(b); 415 404 packet::Gamestate *g = new packet::Gamestate(); 416 g->setClientID(temp->getID()); 417 b = g->collectData(0,0x1); 405 g->setPeerID(temp->getID()); 406 b = g->collectData(0,packet::GAMESTATE_MODE_SERVER); 407 assert(b); 418 408 if(!b) 419 409 return false; //no data for the client 420 b = g->compressData();421 assert(b);422 b = g->send( );410 // b = g->compressData(); 411 // assert(b); 412 b = g->send( static_cast<Host*>(this) ); 423 413 assert(b); 424 414 return true; … … 428 418 { 429 419 ServerConnection::disconnectClient( client ); 430 GamestateManager::remove Client(client);420 GamestateManager::removePeer(client->getID()); 431 421 // inform all the listeners 432 422 // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now … … 450 440 { 451 441 chat = new packet::Chat(message, clientID); 452 chat->set ClientID(temp->getID());453 if(!chat->send( ))442 chat->setPeerID(temp->getID()); 443 if(!chat->send( static_cast<Host*>(this) )) 454 444 COUT(3) << "could not send Chat message to client ID: " << temp->getID() << std::endl; 455 445 temp = temp->next(); … … 466 456 int failures=0; 467 457 packet::ClassID *classid = new packet::ClassID(); 468 classid->set ClientID(clientID);469 while(!classid->send( ) && failures < 10){458 classid->setPeerID(clientID); 459 while(!classid->send( static_cast<Host*>(this) ) && failures < 10){ 470 460 failures++; 471 461 } -
code/branches/presentation2/src/libraries/network/Server.h
r7764 r7788 32 32 #include "NetworkPrereqs.h" 33 33 34 #include <deque> 35 34 36 #include "util/UtilPrereqs.h" 35 37 #include "core/CorePrereqs.h" 36 38 #include "Host.h" 37 #include "GamestateManager.h"39 // #include "GamestateManager.h" 38 40 #include "ServerConnection.h" 39 41 #include "LANDiscoverable.h" … … 49 51 * It implements all functions necessary for a Server 50 52 */ 51 class _NetworkExport Server : public Host, public ServerConnection, public GamestateManager, publicLANDiscoverable{53 class _NetworkExport Server : public Host, public ServerConnection, public LANDiscoverable{ 52 54 public: 53 55 Server(); … … 64 66 void close(); 65 67 bool processChat(const std::string& message, unsigned int playerID); 66 bool queuePacket(ENetPacket *packet, int clientID); 68 void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID); 69 virtual bool sendPacket( packet::Packet* packet ){ return packet->send( static_cast<Host*>(this) ); } 67 70 void update(const Clock& time); 68 71 unsigned int getRTT(unsigned int clientID); … … 73 76 private: 74 77 virtual bool isServer_(){return true;} 75 unsigned int shipID(){return 0;}76 78 unsigned int playerID(){return 0;} 77 79 78 80 void addPeer(ENetEvent *event); 79 81 void removePeer(ENetEvent *event); 82 void processPacket(packet::Packet* packet); 80 83 81 84 bool createClient(int clientID); 82 85 void disconnectClient( ClientInformation *client); 83 bool processPacket( ENetPacket *packet, ENetPeer *peer ); 84 bool sendGameState(); 86 bool sendGameStates(); 85 87 bool sendObjectDeletes(); 86 88 virtual bool chat(const std::string& message); … … 90 92 91 93 float timeSinceLastUpdate_; 94 std::deque<packet::Packet*> packetQueue_; 92 95 }; 93 96 -
code/branches/presentation2/src/libraries/network/ServerConnection.cc
r7459 r7788 49 49 } 50 50 51 ServerConnection::~ServerConnection(){ 51 ServerConnection::~ServerConnection() 52 { 52 53 if ( this->bListening_ ) 53 54 closeListener(); … … 55 56 } 56 57 57 void ServerConnection::setBindAddress( const std::string& bindAddress ) { 58 void ServerConnection::setBindAddress( const std::string& bindAddress ) 59 { 58 60 if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0) 59 61 COUT(1) << "Error: Could not resolve \"" << bindAddress << "\"." << std::endl; … … 64 66 } 65 67 66 bool ServerConnection::openListener() { 67 this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0, 0); 68 bool ServerConnection::openListener() 69 { 70 // create host 71 this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); 72 68 73 if ( this->host_ == NULL ) 69 74 { … … 71 76 return false; 72 77 } 78 79 // enable compression 80 this->enableCompression(); 73 81 assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); 74 82 if (this->host_->socket4 == ENET_SOCKET_NULL) … … 78 86 else 79 87 COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl; 88 89 // start communication thread 90 Connection::startCommunicationThread(); 80 91 81 92 return true; 82 93 } 83 94 84 bool ServerConnection::closeListener() { 95 bool ServerConnection::closeListener() 96 { 85 97 this->bListening_=false; 86 98 disconnectClients(); 99 Connection::stopCommunicationThread(); 87 100 enet_host_destroy(this->host_); 88 101 return true; 89 102 } 90 103 91 bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) { 104 void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) 105 { 92 106 if ( clientID == CLIENTID_UNKNOWN ) 93 107 { 94 return addPacketAll(packet);108 broadcastPacket(packet, channelID); 95 109 } 96 110 else … … 99 113 if(!temp){ 100 114 COUT(3) << "C.Man: addPacket findClient failed" << std::endl; 101 return false;102 115 } 103 return Connection::addPacket(packet, temp->getPeer());116 Connection::addPacket(packet, temp->getPeer(), channelID); 104 117 } 105 }106 107 bool ServerConnection::addPacketAll(ENetPacket *packet) {108 // if ( !Connection::getInstance() )109 // return false;110 enet_host_broadcast( Connection::getHost(), 0, packet);111 return true;112 118 } 113 119 … … 117 123 } 118 124 119 void ServerConnection::disconnectClient(int clientID){ 125 void ServerConnection::disconnectClient(int clientID) 126 { 120 127 ClientInformation *client = ClientInformation::findClient(clientID); 121 128 if(client) … … 123 130 } 124 131 125 void ServerConnection::disconnectClients() {126 ENetEvent event;132 void ServerConnection::disconnectClients() 133 { 127 134 ClientInformation *temp = ClientInformation::getBegin(); 128 while(temp!=0){ 135 while(temp!=0) 136 { 129 137 ServerConnection::disconnectClient( temp ); 130 138 temp = temp->next(); 131 }132 temp = ClientInformation::getBegin();133 while( temp!=0 ){134 if( service( &event ) )135 {136 switch (event.type)137 {138 case ENET_EVENT_TYPE_NONE: break;139 case ENET_EVENT_TYPE_CONNECT: break;140 case ENET_EVENT_TYPE_RECEIVE:141 enet_packet_destroy(event.packet);142 break;143 case ENET_EVENT_TYPE_DISCONNECT:144 removePeer( &event );145 temp = ClientInformation::getBegin();146 break;147 }148 }149 139 } 150 140 return; … … 152 142 153 143 154 int ServerConnection::getClientID(ENetPeer* peer) { 144 int ServerConnection::getClientID(ENetPeer* peer) 145 { 155 146 return getClientID(&(peer->address)); 156 147 } 157 148 158 int ServerConnection::getClientID(ENetAddress* address) { 149 int ServerConnection::getClientID(ENetAddress* address) 150 { 159 151 return ClientInformation::findClient(address)->getID(); 160 152 } 161 153 162 ENetPeer *ServerConnection::getClientPeer(int clientID) { 154 ENetPeer *ServerConnection::getClientPeer(int clientID) 155 { 163 156 return ClientInformation::findClient(clientID)->getPeer(); 164 157 } -
code/branches/presentation2/src/libraries/network/ServerConnection.h
r7163 r7788 56 56 bool openListener(); 57 57 bool closeListener(); 58 bool addPacket(ENetPacket *packet, unsigned int ID); 59 bool addPacketAll(ENetPacket *packet); 58 void addPacket(ENetPacket *packet, unsigned int ID, uint8_t channelID); 60 59 virtual void disconnectClient(ClientInformation *client); 61 60 void disconnectClient(int clientID); -
code/branches/presentation2/src/libraries/network/WANDiscovery.cc
r7763 r7788 71 71 * has changed. 72 72 */ 73 SetConfigValue( msaddress, " localhost");73 SetConfigValue( msaddress, "orxonox.net"); 74 74 } 75 75 -
code/branches/presentation2/src/libraries/network/packet/Acknowledgement.cc
r6417 r7788 31 31 #include "util/Debug.h" 32 32 #include "network/GamestateHandler.h" 33 #include "network/Host.h" 33 34 34 35 namespace orxonox { … … 39 40 #define _ACKID _PACKETID + sizeof(packet::Type::Value) 40 41 41 Acknowledgement::Acknowledgement( unsigned int id, unsigned int clientID )42 Acknowledgement::Acknowledgement( unsigned int id, unsigned int peerID ) 42 43 : Packet() 43 44 { … … 46 47 *(Type::Value *)(data_ + _PACKETID ) = Type::Acknowledgement; 47 48 *(uint32_t *)(data_ + _ACKID ) = id; 48 clientID_=clientID;49 peerID_=peerID; 49 50 } 50 51 51 Acknowledgement::Acknowledgement( uint8_t *data, unsigned int clientID )52 : Packet(data, clientID)52 Acknowledgement::Acknowledgement( uint8_t *data, unsigned int peerID ) 53 : Packet(data, peerID) 53 54 { 54 55 } … … 62 63 } 63 64 64 bool Acknowledgement::process( ){65 bool Acknowledgement::process(orxonox::Host* host){ 65 66 COUT(5) << "processing ACK with ID: " << getAckID() << endl; 66 bool b = GamestateHandler::ackGamestate(getAckID(), clientID_);67 bool b = host->ackGamestate(getAckID(), peerID_); 67 68 delete this; 68 69 return b; -
code/branches/presentation2/src/libraries/network/packet/Acknowledgement.h
r6073 r7788 42 42 { 43 43 public: 44 Acknowledgement( unsigned int id, unsigned int clientID );45 Acknowledgement( uint8_t* data, unsigned int clientID );44 Acknowledgement( unsigned int id, unsigned int peerID ); 45 Acknowledgement( uint8_t* data, unsigned int peerID ); 46 46 ~Acknowledgement(); 47 47 48 48 inline unsigned int getSize() const; 49 bool process();49 virtual bool process(orxonox::Host* host); 50 50 51 51 unsigned int getAckID(); -
code/branches/presentation2/src/libraries/network/packet/Chat.cc
r7163 r7788 80 80 } 81 81 82 bool Chat::process( ){83 bool b = Host::incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID));82 bool Chat::process(orxonox::Host* host){ 83 bool b = host->incomingChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_PLAYERID)); 84 84 delete this; 85 85 return b; -
code/branches/presentation2/src/libraries/network/packet/Chat.h
r7163 r7788 52 52 53 53 /* process chat message packet and remove it afterwards */ 54 bool process();54 virtual bool process(orxonox::Host* host); 55 55 56 56 /* Get the length of the message (not the full size of the packet) */ -
code/branches/presentation2/src/libraries/network/packet/ClassID.cc
r7163 r7788 120 120 121 121 122 bool ClassID::process( ){122 bool ClassID::process(orxonox::Host* host){ 123 123 int nrOfClasses; 124 124 uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid -
code/branches/presentation2/src/libraries/network/packet/ClassID.h
r6417 r7788 48 48 49 49 uint32_t getSize() const; 50 bool process();50 virtual bool process(orxonox::Host* host); 51 51 52 52 private: -
code/branches/presentation2/src/libraries/network/packet/DeleteObjects.cc
r6417 r7788 57 57 } 58 58 59 bool DeleteObjects::fetchIDs(){ 59 bool DeleteObjects::fetchIDs() 60 { 60 61 unsigned int number = Synchronisable::getNumberOfDeletedObject(); 61 62 if(number==0) … … 79 80 } 80 81 81 unsigned int DeleteObjects::getSize() const{ 82 unsigned int DeleteObjects::getSize() const 83 { 82 84 assert(data_); 83 85 return _OBJECTIDS + *(uint32_t*)(data_+_QUANTITY)*sizeof(uint32_t); 84 86 } 85 87 86 bool DeleteObjects::process(){ 87 for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++){ 88 bool DeleteObjects::process(orxonox::Host* host) 89 { 90 for(unsigned int i=0; i<*(unsigned int *)(data_+_QUANTITY); i++) 91 { 88 92 COUT(4) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << std::endl; 89 93 Synchronisable::deleteObject( *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) ); -
code/branches/presentation2/src/libraries/network/packet/DeleteObjects.h
r6073 r7788 32 32 #include "Packet.h" 33 33 34 namespace orxonox { 35 namespace packet { 34 namespace orxonox 35 { 36 namespace packet 37 { 36 38 /** 37 39 @author … … 47 49 48 50 inline unsigned int getSize() const; 49 bool process();51 virtual bool process(orxonox::Host* host); 50 52 51 53 private: -
code/branches/presentation2/src/libraries/network/packet/FunctionCalls.cc
r7495 r7788 32 32 #include "network/FunctionCall.h" 33 33 #include "network/FunctionCallManager.h" 34 #include "network/GamestateHandler.h" 34 35 35 36 namespace orxonox { … … 40 41 const unsigned int FUNCTIONCALLS_MEM_ALLOCATION = 1000; 41 42 42 FunctionCalls::FunctionCalls() 43 : Packet()43 FunctionCalls::FunctionCalls(): 44 Packet(), minGamestateID_(GAMESTATEID_INITIAL) 44 45 { 45 46 flags_ = flags_ | PACKET_FLAGS_FUNCTIONCALLS; 46 currentSize_ = 2*sizeof(uint32_t); // for packetid and nrOfCalls47 currentSize_ = 3*sizeof(uint32_t); // for packetid, nrOfCalls and minGamestateID_ 47 48 } 48 49 49 FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID ) 50 : Packet(data, clientID)50 FunctionCalls::FunctionCalls( uint8_t* data, unsigned int clientID ): 51 Packet(data, clientID), minGamestateID_(GAMESTATEID_INITIAL) 51 52 { 52 53 } … … 57 58 58 59 59 bool FunctionCalls::process(){ 60 bool FunctionCalls::process(orxonox::Host* host) 61 { 60 62 assert(isDataENetAllocated()); 61 63 62 64 uint8_t* temp = data_+sizeof(uint32_t); //skip packetid 63 65 uint32_t nrOfCalls = *(uint32_t*)temp; 66 temp += sizeof(uint32_t); 67 this->minGamestateID_ = *(uint32_t*)temp; 64 68 temp += sizeof(uint32_t); 65 69 for( unsigned int i = 0; i<nrOfCalls; i++ ) … … 68 72 fctCall.loadData(temp); 69 73 if( !fctCall.execute() ) 70 FunctionCallManager::bufferIncomingFunctionCall( fctCall ); 74 { 75 FunctionCallManager::bufferIncomingFunctionCall( fctCall, minGamestateID_, this->getPeerID() ); 76 } 71 77 } 72 78 … … 75 81 } 76 82 77 void FunctionCalls::addCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){ 83 void FunctionCalls::addCallStatic( uint32_t networkID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5) 84 { 78 85 assert(!isDataENetAllocated()); 79 86 … … 83 90 } 84 91 85 void FunctionCalls::addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5){ 92 void FunctionCalls::addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1, const MultiType* mt2, const MultiType* mt3, const MultiType* mt4, const MultiType* mt5) 93 { 86 94 assert(!isDataENetAllocated()); 87 95 … … 91 99 } 92 100 93 bool FunctionCalls::send( )101 bool FunctionCalls::send(orxonox::Host* host) 94 102 { 103 this->minGamestateID_ = host->getCurrentGamestateID(); 95 104 assert(this->functionCalls_.size()); 96 105 data_=new uint8_t[ currentSize_ ]; 97 106 *(Type::Value *)(data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID 98 *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls to 0 99 uint8_t* temp = data_+2*sizeof(uint32_t); 107 *(uint32_t*)(data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls 108 *(uint32_t*)(data_+2*sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_ 109 uint8_t* temp = data_+3*sizeof(uint32_t); 100 110 101 111 while( this->functionCalls_.size() ) … … 107 117 assert( temp==data_+currentSize_ ); 108 118 109 Packet::send( );119 Packet::send(host); 110 120 return true; 111 121 } -
code/branches/presentation2/src/libraries/network/packet/FunctionCalls.h
r7490 r7788 54 54 inline unsigned int getSize() const 55 55 { assert(!this->isDataENetAllocated()); return currentSize_; } 56 bool process();56 virtual bool process(orxonox::Host* host); 57 57 58 58 void addCallStatic( uint32_t networkID, const MultiType* mt1=0, const MultiType* mt2=0, const MultiType* mt3=0, const MultiType* mt4=0, const MultiType* mt5=0); 59 59 void addCallMember( uint32_t networkID, uint32_t objectID, const MultiType* mt1=0, const MultiType* mt2=0, const MultiType* mt3=0, const MultiType* mt4=0, const MultiType* mt5=0); 60 virtual bool send( );60 virtual bool send(orxonox::Host* host); 61 61 private: 62 62 std::queue<orxonox::FunctionCall> functionCalls_; 63 63 unsigned int clientID_; 64 uint32_t minGamestateID_; 64 65 uint32_t currentSize_; 65 66 }; -
code/branches/presentation2/src/libraries/network/packet/FunctionIDs.cc
r6417 r7788 46 46 47 47 48 FunctionIDs::FunctionIDs( ) : Packet(){ 48 FunctionIDs::FunctionIDs( ) : Packet() 49 { 49 50 unsigned int nrOfFunctions=0; 50 51 unsigned int packetSize=2*sizeof(uint32_t); //space for the packetID and for the nroffunctions … … 55 56 //calculate total needed size (for all strings and integers) 56 57 ObjectList<NetworkFunctionBase>::iterator it; 57 for(it = ObjectList<NetworkFunctionBase>::begin(); it; ++it){ 58 for(it = ObjectList<NetworkFunctionBase>::begin(); it; ++it) 59 { 58 60 const std::string& functionname = it->getName(); 59 61 networkID = it->getNetworkID(); … … 76 78 // now save all classids and classnames 77 79 std::pair<uint32_t, std::string> tempPair; 78 while( !tempQueue.empty() ){ 80 while( !tempQueue.empty() ) 81 { 79 82 tempPair = tempQueue.front(); 80 83 tempQueue.pop(); … … 98 101 } 99 102 100 uint32_t FunctionIDs::getSize() const{ 103 uint32_t FunctionIDs::getSize() const 104 { 101 105 assert(this->data_); 102 106 uint8_t *temp = data_+sizeof(uint32_t); // packet identification … … 114 118 115 119 116 bool FunctionIDs::process(){ 120 bool FunctionIDs::process(orxonox::Host* host) 121 { 117 122 int nrOfFunctions; 118 123 uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid … … 127 132 temp += sizeof(uint32_t); 128 133 129 for( int i=0; i<nrOfFunctions; i++){ 134 for( int i=0; i<nrOfFunctions; i++) 135 { 130 136 networkID = *(uint32_t*)temp; 131 137 stringsize = *(uint32_t*)(temp+sizeof(uint32_t)); -
code/branches/presentation2/src/libraries/network/packet/FunctionIDs.h
r6417 r7788 47 47 ~FunctionIDs(); 48 48 49 uint32_t getSize() const;50 bool process();49 virtual uint32_t getSize() const; 50 virtual bool process(orxonox::Host* host); 51 51 52 52 private: -
code/branches/presentation2/src/libraries/network/packet/Gamestate.cc
r7163 r7788 36 36 #include "network/synchronisable/Synchronisable.h" 37 37 #include "network/GamestateHandler.h" 38 #include "network/Host.h" 38 39 39 40 namespace orxonox { … … 43 44 #define GAMESTATE_START(data) (data + GamestateHeader::getSize()) 44 45 45 #define PACKET_FLAG_GAMESTATE PacketFlag::Reliable 46 // #define PACKET_FLAG_GAMESTATE PacketFlag::Reliable 47 #define PACKET_FLAG_GAMESTATE 0 46 48 47 49 inline bool memzero( uint8_t* data, uint32_t datalength) … … 65 67 66 68 Gamestate::Gamestate(): 67 header_( 0)69 header_() 68 70 { 69 71 flags_ = flags_ | PACKET_FLAG_GAMESTATE; … … 72 74 73 75 Gamestate::Gamestate(uint8_t *data, unsigned int clientID): 74 Packet(data, clientID) 76 Packet(data, clientID), header_(data) 75 77 { 76 78 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 77 header_ = new GamestateHeader(data_); 78 } 79 80 81 Gamestate::Gamestate(uint8_t *data)79 } 80 81 82 Gamestate::Gamestate(uint8_t *data): 83 header_(data) 82 84 { 83 85 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 84 86 data_ = data; 85 header_ = new GamestateHeader(data_);86 87 } 87 88 88 89 89 90 Gamestate::Gamestate(const Gamestate& g) : 90 Packet( *(Packet*)&g), nrOfVariables_(0)91 Packet( *(Packet*)&g ), header_(this->data_), nrOfVariables_(0) 91 92 { 92 93 flags_ = flags_ | PACKET_FLAG_GAMESTATE; 93 header_ = new GamestateHeader(data_);94 94 sizes_ = g.sizes_; 95 95 } … … 98 98 Gamestate::~Gamestate() 99 99 { 100 if( header_ )101 delete header_;102 100 } 103 101 … … 105 103 bool Gamestate::collectData(int id, uint8_t mode) 106 104 { 107 assert(this->header_==0); // make sure the header didn't exist before108 105 uint32_t tempsize=0, currentsize=0; 109 106 assert(data_==0); … … 120 117 } 121 118 122 // create the header object 123 assert( header_ == 0 ); 124 header_ = new GamestateHeader(data_); 119 // tell the gamestate header where to store the data 120 header_.setData(this->data_); 125 121 126 122 //start collect data synchronisable by synchronisable … … 142 138 assert(0); // if we don't use multithreading this part shouldn't be neccessary 143 139 // start allocate additional memory 144 COUT(3) << "G .St.Man: need additional memory" << std::endl;140 COUT(3) << "Gamestate: need additional memory" << std::endl; 145 141 ObjectList<Synchronisable>::iterator temp = it; 146 142 uint32_t addsize=tempsize; … … 161 157 162 158 //start write gamestate header 163 header_ ->setDataSize( currentsize );164 header_ ->setID( id );165 header_ ->setBaseID( GAMESTATEID_INITIAL );166 header_ ->setDiffed( false );167 header_ ->setComplete( true );168 header_ ->setCompressed( false );159 header_.setDataSize( currentsize ); 160 header_.setID( id ); 161 header_.setBaseID( GAMESTATEID_INITIAL ); 162 header_.setDiffed( false ); 163 header_.setComplete( true ); 164 header_.setCompressed( false ); 169 165 //stop write gamestate header 170 166 171 COUT(5) << "G .ST.Man: Gamestate size: " << currentsize << std::endl;172 COUT(5) << "G .ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl;167 COUT(5) << "Gamestate: Gamestate size: " << currentsize << std::endl; 168 COUT(5) << "Gamestate: 'estimated' (and corrected) Gamestate size: " << size << std::endl; 173 169 return true; 174 170 } … … 177 173 bool Gamestate::spreadData(uint8_t mode) 178 174 { 179 COUT( 4) << "processing gamestate with id " << header_->getID() << endl;175 COUT(5) << "processing gamestate with id " << header_.getID() << endl; 180 176 assert(data_); 181 assert(!header_ ->isCompressed());177 assert(!header_.isCompressed()); 182 178 uint8_t *mem=data_+GamestateHeader::getSize(); 183 179 Synchronisable *s; 184 180 185 181 // update the data of the objects we received 186 while(mem < data_+GamestateHeader::getSize()+header_ ->getDataSize())182 while(mem < data_+GamestateHeader::getSize()+header_.getDataSize()) 187 183 { 188 184 SynchronisableHeader objectheader(mem); … … 197 193 else 198 194 { 195 // COUT(4) << "not creating object of classid " << objectheader.getClassID() << endl; 199 196 mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() ); 200 197 } … … 202 199 else 203 200 { 201 // COUT(4) << "updating object of classid " << objectheader.getClassID() << endl; 204 202 bool b = s->updateData(mem, mode); 205 203 assert(b); 206 204 } 207 205 } 206 assert(mem-data_ == GamestateHeader::getSize()+header_.getDataSize()); 207 208 208 // In debug mode, check first, whether there are no duplicate objectIDs 209 209 #ifndef NDEBUG … … 249 249 { 250 250 assert(data_); 251 if(header_ ->isCompressed())252 return header_ ->getCompSize()+GamestateHeader::getSize();251 if(header_.isCompressed()) 252 return header_.getCompSize()+GamestateHeader::getSize(); 253 253 else 254 254 { 255 return header_ ->getDataSize()+GamestateHeader::getSize();255 return header_.getDataSize()+GamestateHeader::getSize(); 256 256 } 257 257 } … … 271 271 272 272 273 bool Gamestate::process( )274 { 275 return GamestateHandler::addGamestate(this, getClientID());273 bool Gamestate::process(orxonox::Host* host) 274 { 275 return host->addGamestate(this, getPeerID()); 276 276 } 277 277 … … 280 280 { 281 281 assert(data_); 282 assert(!header_ ->isCompressed());283 uLongf buffer = (uLongf)(((header_ ->getDataSize() + 12)*1.01)+1);282 assert(!header_.isCompressed()); 283 uLongf buffer = (uLongf)(((header_.getDataSize() + 12)*1.01)+1); 284 284 if(buffer==0) 285 285 return false; … … 289 289 uint8_t *source = data_ + GamestateHeader::getSize(); 290 290 int retval; 291 retval = compress( dest, &buffer, source, (uLong)(header_ ->getDataSize()) );291 retval = compress( dest, &buffer, source, (uLong)(header_.getDataSize()) ); 292 292 switch ( retval ) 293 293 { … … 299 299 300 300 //copy and modify header 301 GamestateHeader *temp = header_; 302 header_ = new GamestateHeader(ndata, temp); 301 GamestateHeader *temp = new GamestateHeader(data_); 302 header_.setData(ndata); 303 header_ = *temp; 303 304 delete temp; 304 305 //delete old data … … 306 307 //save new data 307 308 data_ = ndata; 308 header_ ->setCompSize( buffer );309 header_ ->setCompressed( true );310 COUT( 0) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl;309 header_.setCompSize( buffer ); 310 header_.setCompressed( true ); 311 COUT(4) << "gamestate compress datasize: " << header_.getDataSize() << " compsize: " << header_.getCompSize() << std::endl; 311 312 return true; 312 313 } … … 316 317 { 317 318 assert(data_); 318 assert(header_ ->isCompressed());319 COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_ ->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl;320 uint32_t datasize = header_ ->getDataSize();321 uint32_t compsize = header_ ->getCompSize();319 assert(header_.isCompressed()); 320 COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_.getID() << ", baseid: " << header_.getBaseID() << ", datasize: " << header_.getDataSize() << ", compsize: " << header_.getCompSize() << std::endl; 321 uint32_t datasize = header_.getDataSize(); 322 uint32_t compsize = header_.getCompSize(); 322 323 uint32_t bufsize; 323 324 bufsize = datasize; … … 338 339 339 340 //copy over the header 340 GamestateHeader *temp = header_; 341 header_ = new GamestateHeader( data_, header_ ); 341 GamestateHeader* temp = new GamestateHeader( data_ ); 342 header_.setData(ndata); 343 header_ = *temp; 342 344 delete temp; 343 345 … … 357 359 //set new pointers 358 360 data_ = ndata; 359 header_ ->setCompressed( false );360 assert(header_ ->getDataSize()==datasize);361 assert(header_ ->getCompSize()==compsize);361 header_.setCompressed( false ); 362 assert(header_.getDataSize()==datasize); 363 assert(header_.getCompSize()==compsize); 362 364 return true; 363 365 } 364 366 365 367 368 inline void /*Gamestate::*/diffObject( uint8_t*& newDataPtr, uint8_t*& origDataPtr, uint8_t*& baseDataPtr, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ) 369 { 370 assert( objectHeader.getDataSize() == SynchronisableHeader(baseDataPtr).getDataSize() ); 371 372 uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData 373 // Check whether the whole object stayed the same 374 if( memcmp( origDataPtr+objectOffset, baseDataPtr+objectOffset, objectHeader.getDataSize()) == 0 ) 375 { 376 // COUT(4) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl; 377 origDataPtr += objectOffset + objectHeader.getDataSize(); // skip the whole object 378 baseDataPtr += objectOffset + objectHeader.getDataSize(); 379 sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); 380 } 381 else 382 { 383 // Now start to diff the Object 384 SynchronisableHeaderLight newObjectHeader(newDataPtr); 385 newObjectHeader = objectHeader; // copy over the objectheader 386 VariableID variableID = 0; 387 uint32_t diffedObjectOffset = SynchronisableHeaderLight::getSize(); 388 // iterate through all variables 389 while( objectOffset < objectHeader.getDataSize()+SynchronisableHeader::getSize() ) 390 { 391 // check whether variable changed and write id and copy over variable to the new stream 392 // otherwise skip variable 393 uint32_t varSize = *sizes; 394 assert( varSize == Synchronisable::getSynchronisable(objectHeader.getObjectID())->getVarSize(variableID) ); 395 if ( varSize != 0 ) 396 { 397 if ( memcmp(origDataPtr+objectOffset, baseDataPtr+objectOffset, varSize) != 0 ) 398 { 399 *(VariableID*)(newDataPtr+diffedObjectOffset) = variableID; // copy over the variableID 400 diffedObjectOffset += sizeof(VariableID); 401 memcpy( newDataPtr+diffedObjectOffset, origDataPtr+objectOffset, varSize ); 402 diffedObjectOffset += varSize; 403 objectOffset += varSize; 404 } 405 else 406 { 407 objectOffset += varSize; 408 } 409 } 410 411 ++variableID; 412 ++sizes; 413 } 414 415 // if there are variables from this object with 0 size left in sizes 416 if( Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() != variableID ) 417 sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables() - variableID; 418 419 newObjectHeader.setDiffed(true); 420 newObjectHeader.setDataSize(diffedObjectOffset-SynchronisableHeaderLight::getSize()); 421 assert(objectOffset == objectHeader.getDataSize()+SynchronisableHeader::getSize()); 422 assert(newObjectHeader.getDataSize()>0); 423 424 origDataPtr += objectOffset; 425 baseDataPtr += objectOffset; 426 newDataPtr += diffedObjectOffset; 427 } 428 } 429 430 inline void /*Gamestate::*/copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ) 431 { 432 // COUT(4) << "docopy" << endl; 433 // Just copy over the whole Object 434 memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() ); 435 SynchronisableHeader(newData).setDiffed(false); 436 437 newData += objectHeader.getDataSize()+SynchronisableHeader::getSize(); 438 origData += objectHeader.getDataSize()+SynchronisableHeader::getSize(); 439 // SynchronisableHeader baseHeader( baseData ); 440 // baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize(); 441 // COUT(4) << "copy " << h.getObjectID() << endl; 442 // COUT(4) << "copy " << h.getObjectID() << ":"; 443 sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); 444 // for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i ) 445 // { 446 // // COUT(4) << " " << *sizes; 447 // ++sizes; 448 // } 449 // COUT(4) << endl; 450 } 451 452 inline bool findObject(uint8_t*& dataPtr, uint8_t* endPtr, SynchronisableHeader& objectHeader) 453 { 454 // Some assertions to make sure the dataPtr is valid (pointing to a SynchronisableHeader) 455 { 456 SynchronisableHeader htemp2(dataPtr); 457 assert(htemp2.getClassID()<500); 458 assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 459 assert(htemp2.isDiffed()==false); 460 } 461 uint32_t objectID = objectHeader.getObjectID(); 462 while ( dataPtr < endPtr ) 463 { 464 SynchronisableHeader htemp(dataPtr); 465 assert( htemp.getDataSize()!=0 ); 466 if ( htemp.getObjectID() == objectID ) 467 { 468 assert( objectHeader.getClassID() == htemp.getClassID() ); 469 assert( objectHeader.getCreatorID() == htemp.getCreatorID() ); 470 return true; 471 } 472 { 473 if( dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize() < endPtr ) 474 { 475 SynchronisableHeader htemp2(dataPtr+htemp.getDataSize()+SynchronisableHeader::getSize()); 476 assert(htemp2.getClassID()<500); 477 assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 478 assert(htemp2.isDiffed()==false); 479 } 480 } 481 dataPtr += htemp.getDataSize()+SynchronisableHeader::getSize(); 482 483 } 484 assert(dataPtr == endPtr); 485 486 return false; 487 } 488 366 489 Gamestate* Gamestate::diffVariables(Gamestate *base) 367 490 { 368 491 assert(this && base); assert(data_ && base->data_); 369 assert(!header_->isCompressed() && !base->header_->isCompressed()); 370 assert(!header_->isDiffed()); 492 assert(!header_.isCompressed() && !base->header_.isCompressed()); 493 assert(!header_.isDiffed()); 494 assert( header_.getDataSize() && base->header_.getDataSize() ); 371 495 372 496 373 497 // *** first do a raw diff of the two gamestates 374 498 375 uint8_t *baseData = GAMESTATE_START(base->data_);376 uint8_t *origData = GAMESTATE_START(this->data_);377 uint 32_t origLength = header_->getDataSize();378 uint 32_t baseLength = base->header_->getDataSize();379 380 assert( origLength && baseLength);381 382 uint8_t *nData = new uint8_t[origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_]; // this is the maximum size needed in the worst case383 uint 8_t *dest = GAMESTATE_START(nData);384 385 uint 32_t baseOffset = 0; //offset in the diffed stream386 uint32_t origOffset = 0; //offset in the new stream with removed 0's 387 std::vector<uint32_t>::iterator sizes = this->sizes_.begin();388 389 while( orig Offset < origLength)499 uint8_t *baseDataPtr = GAMESTATE_START(base->data_); 500 uint8_t *origDataPtr = GAMESTATE_START(this->data_); 501 uint8_t *origDataEnd = origDataPtr + header_.getDataSize(); 502 uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize(); 503 // uint32_t origLength = header_.getDataSize(); 504 // uint32_t baseLength = base->header_.getDataSize(); 505 506 // Allocate new space for diffed gamestate 507 uint32_t newDataSize = header_.getDataSize() + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_; 508 uint8_t *newData = new uint8_t[newDataSize]; // this is the maximum size needed in the worst case 509 uint8_t *destDataPtr = GAMESTATE_START(newData); 510 511 std::vector<uint32_t>::iterator sizesIt = this->sizes_.begin(); 512 513 while( origDataPtr < origDataEnd ) 390 514 { 391 515 //iterate through all objects 392 516 393 SynchronisableHeader h(origData+origOffset);517 SynchronisableHeader origHeader(origDataPtr); 394 518 395 519 // Find (if possible) the current object in the datastream of the old gamestate 396 520 // Start at the current offset position 397 if(baseOffset >= baseLength) 398 baseOffset = 0; 399 uint8_t* temp = baseData + baseOffset; 400 uint32_t objectID = h.getObjectID(); 401 assert(temp < baseData+baseLength); 402 assert(dest < nData + origLength + GamestateHeader::getSize() + sizeof(uint32_t)*this->nrOfVariables_); 403 assert(sizes != this->sizes_.end()); 404 while ( temp < baseData+baseLength ) 405 { 406 SynchronisableHeader htemp(temp); 407 assert( htemp.getDataSize()!=0 ); 408 if ( htemp.getObjectID() == objectID ) 409 { 410 assert( h.getClassID() == htemp.getClassID() ); 411 goto DODIFF; 412 } 413 // { 414 // SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize()); 415 // if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength ) 416 // { 417 // assert(htemp2.getClassID()<500); 418 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 419 // assert(htemp2.isDiffed()==false); 420 // } 421 // } 422 temp += htemp.getDataSize()+SynchronisableHeader::getSize(); 521 if(baseDataPtr == baseDataEnd) 522 baseDataPtr = GAMESTATE_START(base->data_); 523 uint8_t* oldBaseDataPtr = baseDataPtr; 524 525 assert(baseDataPtr < baseDataEnd); 526 assert(destDataPtr < newData + newDataSize); 527 assert(sizesIt != this->sizes_.end()); 528 529 assert(Synchronisable::getSynchronisable(origHeader.getObjectID())); 530 assert(ClassByID(origHeader.getClassID())); 531 assert(origHeader.getDataSize() < 500); 532 533 bool diffedObject = false; 534 if( findObject(baseDataPtr, baseDataEnd, origHeader) ) 535 { 536 SynchronisableHeader baseHeader(baseDataPtr); 537 assert(Synchronisable::getSynchronisable(baseHeader.getObjectID())); 538 assert(ClassByID(baseHeader.getClassID())); 539 assert(baseHeader.getDataSize() < 500); 540 if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() ) 541 { 542 // COUT(4) << "diffing object in order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 543 diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 544 diffedObject = true; 545 } 546 else 547 { 548 // COUT(4) << "copy object because of different data sizes (1): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 549 copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 550 assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); 551 } 423 552 424 553 } 425 // If not found start looking at the beginning 426 assert( temp==baseData+baseLength ); 427 temp = baseData; 428 // { 429 // SynchronisableHeader htemp2(temp); 430 // if( temp < baseData+baseLength ) 431 // { 432 // assert(htemp2.getClassID()<500); 433 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 434 // assert(htemp2.isDiffed()==false); 435 // } 436 // } 437 while ( temp < baseData+baseOffset ) 438 { 439 SynchronisableHeader htemp(temp); 440 if ( htemp.getObjectID() == objectID ) 441 { 442 assert( h.getClassID() == htemp.getClassID() ); 443 goto DODIFF; 444 } 445 // { 446 // SynchronisableHeader htemp2(temp+htemp.getDataSize()+SynchronisableHeader::getSize()); 447 // if( temp+htemp.getDataSize()+SynchronisableHeader::getSize() < baseData+baseLength ) 448 // { 449 // assert(htemp2.getClassID()<500); 450 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 451 // assert(htemp2.isDiffed()==false); 452 // } 453 // } 454 temp += htemp.getDataSize()+SynchronisableHeader::getSize(); 455 } 456 // Object is new, thus never transmitted -> just copy over 457 goto DOCOPY; 458 459 460 DODIFF: 461 { 462 // COUT(4) << "dodiff" << endl; 463 // if(baseOffset==0) 464 // { 465 // assert(origOffset==0); 466 // } 467 uint32_t objectOffset = SynchronisableHeader::getSize(); // offset inside the object in the origData and baseData 468 // Check whether the whole object stayed the same 469 if( memcmp( origData+origOffset+objectOffset, temp+objectOffset, h.getDataSize()) == 0 ) 470 { 471 // COUT(4) << "skip object" << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl; 472 origOffset += objectOffset+ h.getDataSize(); // skip the whole object 473 baseOffset = temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData; 474 sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); 554 else 555 { 556 assert( baseDataPtr == baseDataEnd ); 557 baseDataPtr = GAMESTATE_START(base->data_); 558 if( findObject(baseDataPtr, oldBaseDataPtr, origHeader) ) 559 { 560 SynchronisableHeader baseHeader(baseDataPtr); 561 assert(Synchronisable::getSynchronisable(baseHeader.getObjectID())); 562 assert(ClassByID(baseHeader.getClassID())); 563 assert(baseHeader.getDataSize() < 500); 564 if( SynchronisableHeader(baseDataPtr).getDataSize()==origHeader.getDataSize() ) 565 { 566 // COUT(4) << "diffing object out of order: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 567 diffObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 568 diffedObject = true; 569 } 570 else 571 { 572 // COUT(4) << "copy object because of different data sizes (2): " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 573 copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 574 assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); 575 } 475 576 } 476 577 else 477 578 { 478 // if( Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() == "Bot" ) 479 // COUT(0) << "blub" << endl; 480 // COUT(4) << "object diff: " << Synchronisable::getSynchronisable(h.getObjectID())->getIdentifier()->getName() << endl; 481 // COUT(4) << "diff " << h.getObjectID() << ":"; 482 // Now start to diff the Object 483 SynchronisableHeaderLight h2(dest); 484 h2 = h; // copy over the objectheader 485 VariableID variableID = 0; 486 uint32_t newObjectOffset = SynchronisableHeaderLight::getSize(); 487 // iterate through all variables 488 while( objectOffset < h.getDataSize()+SynchronisableHeader::getSize() ) 489 { 490 // check whether variable changed and write id and copy over variable to the new stream 491 // otherwise skip variable 492 assert(sizes != this->sizes_.end()); 493 uint32_t varSize = *sizes; 494 assert( varSize == Synchronisable::getSynchronisable(h.getObjectID())->getVarSize(variableID) ); 495 if ( varSize != 0 ) 496 { 497 if ( memcmp(origData+origOffset+objectOffset, temp+objectOffset, varSize) != 0 ) 498 { 499 // COUT(4) << "copy variable" << endl; 500 *(VariableID*)(dest+newObjectOffset) = variableID; // copy over the variableID 501 newObjectOffset += sizeof(VariableID); 502 memcpy( dest+newObjectOffset, origData+origOffset+objectOffset, varSize ); 503 newObjectOffset += varSize; 504 objectOffset += varSize; 505 } 506 else 507 { 508 // COUT(4) << "skip variable" << endl; 509 objectOffset += varSize; 510 } 511 } 512 // else 513 // COUT(4) << "varsize 0" << endl; 514 515 ++variableID; 516 ++sizes; 517 } 518 519 if( Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() != variableID ) 520 sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables() - variableID; 521 // COUT(4) << endl; 522 h2.setDiffed(true); 523 h2.setDataSize(newObjectOffset-SynchronisableHeaderLight::getSize()); 524 assert(objectOffset == h.getDataSize()+SynchronisableHeader::getSize()); 525 origOffset += objectOffset; 526 // baseOffset += temp + h.getDataSize()+SynchronisableHeader::getSize() - baseData; 527 //baseOffset += objectOffset; 528 // SynchronisableHeader htemp(temp); 529 // baseOffset += SynchronisableHeader::getSize() + htemp.getDataSize(); 530 // { 531 // SynchronisableHeader htemp2( baseData+(temp-baseData+objectOffset) ); 532 // if( baseData+(temp-baseData+objectOffset) < baseData+baseLength ) 533 // { 534 // assert(htemp2.getClassID()<500); 535 // assert(htemp2.getDataSize()!=0 && htemp2.getDataSize()<1000); 536 // assert(htemp2.isDiffed()==false); 537 // } 538 // } 539 baseOffset = temp-baseData + objectOffset; 540 dest += newObjectOffset; 541 } 542 543 continue; 544 } 545 546 DOCOPY: 547 { 548 // COUT(4) << "docopy" << endl; 549 // Just copy over the whole Object 550 memcpy( dest, origData+origOffset, h.getDataSize()+SynchronisableHeader::getSize() ); 551 dest += h.getDataSize()+SynchronisableHeader::getSize(); 552 origOffset += h.getDataSize()+SynchronisableHeader::getSize(); 553 assert( Synchronisable::getSynchronisable(h.getObjectID()) ); 554 // COUT(4) << "copy " << h.getObjectID() << endl; 555 // COUT(4) << "copy " << h.getObjectID() << ":"; 556 //sizes += Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); 557 for( unsigned int i = 0; i < Synchronisable::getSynchronisable(h.getObjectID())->getNrOfVariables(); ++i ) 558 { 559 // COUT(4) << " " << *sizes; 560 ++sizes; 561 } 562 // COUT(4) << endl; 563 assert(sizes != this->sizes_.end() || origOffset>=origLength); 564 continue; 565 } 566 } 567 568 569 Gamestate *g = new Gamestate(nData, getClientID()); 570 assert(g->header_); 571 *(g->header_) = *header_; 572 g->header_->setBaseID( base->getID() ); 573 g->header_->setDataSize(dest - nData - GamestateHeader::getSize()); 579 // COUT(4) << "copy object: " << Synchronisable::getSynchronisable(origHeader.getObjectID())->getIdentifier()->getName() << endl; 580 assert(baseDataPtr == oldBaseDataPtr); 581 copyObject(destDataPtr, origDataPtr, baseDataPtr, origHeader, sizesIt); 582 assert(sizesIt != this->sizes_.end() || origDataPtr==origDataEnd); 583 } 584 } 585 } 586 assert(sizesIt==this->sizes_.end()); 587 588 589 Gamestate *g = new Gamestate(newData, getPeerID()); 590 (g->header_) = header_; 591 g->header_.setBaseID( base->getID() ); 592 g->header_.setDataSize(destDataPtr - newData - GamestateHeader::getSize()); 574 593 g->flags_=flags_; 575 594 g->packetDirection_ = packetDirection_; … … 579 598 580 599 581 Gamestate* Gamestate::diffData(Gamestate *base)600 /*Gamestate* Gamestate::diffData(Gamestate *base) 582 601 { 583 602 assert(this && base); assert(data_ && base->data_); 584 assert(!header_ ->isCompressed() && !base->header_->isCompressed());585 assert(!header_ ->isDiffed());603 assert(!header_.isCompressed() && !base->header_.isCompressed()); 604 assert(!header_.isDiffed()); 586 605 587 606 uint8_t *basep = GAMESTATE_START(base->data_); 588 607 uint8_t *gs = GAMESTATE_START(this->data_); 589 uint32_t dest_length = header_ ->getDataSize();608 uint32_t dest_length = header_.getDataSize(); 590 609 591 610 if(dest_length==0) … … 595 614 uint8_t *dest = GAMESTATE_START(ndata); 596 615 597 rawDiff( dest, gs, basep, header_ ->getDataSize(), base->header_->getDataSize() );616 rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); 598 617 #ifndef NDEBUG 599 618 uint8_t *dest2 = new uint8_t[dest_length]; 600 rawDiff( dest2, dest, basep, header_ ->getDataSize(), base->header_->getDataSize() );619 rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() ); 601 620 assert( memcmp( dest2, gs, dest_length) == 0 ); 602 621 delete dest2; … … 606 625 assert(g->header_); 607 626 *(g->header_) = *header_; 608 g->header_ ->setDiffed( true );609 g->header_ ->setBaseID( base->getID() );627 g->header_.setDiffed( true ); 628 g->header_.setBaseID( base->getID() ); 610 629 g->flags_=flags_; 611 630 g->packetDirection_ = packetDirection_; … … 619 638 { 620 639 assert(this && base); assert(data_ && base->data_); 621 assert(!header_ ->isCompressed() && !base->header_->isCompressed());622 assert(header_ ->isDiffed());640 assert(!header_.isCompressed() && !base->header_.isCompressed()); 641 assert(header_.isDiffed()); 623 642 624 643 uint8_t *basep = GAMESTATE_START(base->data_); 625 644 uint8_t *gs = GAMESTATE_START(this->data_); 626 uint32_t dest_length = header_ ->getDataSize();645 uint32_t dest_length = header_.getDataSize(); 627 646 628 647 if(dest_length==0) … … 632 651 uint8_t *dest = ndata + GamestateHeader::getSize(); 633 652 634 rawDiff( dest, gs, basep, header_ ->getDataSize(), base->header_->getDataSize() );653 rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() ); 635 654 636 655 Gamestate *g = new Gamestate(ndata, getClientID()); 637 656 assert(g->header_); 638 657 *(g->header_) = *header_; 639 g->header_ ->setDiffed( false );658 g->header_.setDiffed( false ); 640 659 g->flags_=flags_; 641 660 g->packetDirection_ = packetDirection_; … … 670 689 } 671 690 assert(j==datalength); 672 } 673 674 675 Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){691 }*/ 692 693 694 /*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ 676 695 assert(data_); 677 696 std::list<obj>::iterator it; 678 697 679 698 // allocate memory for new data 680 uint8_t *gdata = new uint8_t[header_ ->getDataSize()+GamestateHeader::getSize()];699 uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()]; 681 700 // create a gamestate out of it 682 701 Gamestate *gs = new Gamestate(gdata); … … 694 713 695 714 //call TrafficControl 696 TrafficControl::getInstance()->processObjectList( clientID, header_ ->getID(), dataVector_ );715 TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ ); 697 716 698 717 //copy in the zeros … … 725 744 #ifndef NDEBUG 726 745 uint32_t origsize = destsize; 727 while ( origsize < header_ ->getDataSize() )746 while ( origsize < header_.getDataSize() ) 728 747 { 729 748 SynchronisableHeader oldobjectheader(origdata); … … 732 751 origsize += objectsize; 733 752 } 734 assert(origsize==header_ ->getDataSize());753 assert(origsize==header_.getDataSize()); 735 754 assert(destsize!=0); 736 755 #endif 737 gs->header_ ->setDataSize( destsize );756 gs->header_.setDataSize( destsize ); 738 757 return gs; 739 } 740 741 742 uint32_t Gamestate::calcGamestateSize( int32_t id, uint8_t mode)758 }*/ 759 760 761 uint32_t Gamestate::calcGamestateSize(uint32_t id, uint8_t mode) 743 762 { 744 763 uint32_t size = 0; -
code/branches/presentation2/src/libraries/network/packet/Gamestate.h
r7163 r7788 42 42 #include "Packet.h" 43 43 44 namespace orxonox { 44 namespace orxonox 45 { 45 46 46 namespace packet { 47 namespace packet 48 { 49 50 static const uint8_t GAMESTATE_MODE_SERVER = 0x1; 51 static const uint8_t GAMESTATE_MODE_CLIENT = 0x2; 47 52 48 class _NetworkExport GamestateHeader{ 53 class _NetworkExport GamestateHeader 54 { 49 55 public: 50 GamestateHeader(uint8_t *data){ assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } 51 GamestateHeader(uint8_t *data, GamestateHeader* h) 52 { assert(data); data_=data; memcpy(data_, h->data_, getSize()); } 56 GamestateHeader(){ data_=0; } 57 GamestateHeader(uint8_t* data) 58 { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } 59 /*GamestateHeader(uint8_t* data, GamestateHeader* h) 60 { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/ 61 void setData(uint8_t* data) 62 { assert(data); data_ = data; *(uint32_t*)data_ = Type::Gamestate; } 53 63 static inline uint32_t getSize() 54 { return 21; }64 { return 21; } 55 65 56 inline int32_t getID() const57 { assert(data_); return *(int32_t*)(data_+4); }58 inline void setID( int32_t id)59 { assert(data_); *(int32_t*)(data_+4) = id; }66 inline uint32_t getID() const 67 { assert(data_); return *(uint32_t*)(data_+4); } 68 inline void setID(uint32_t id) 69 { assert(data_); *(uint32_t*)(data_+4) = id; } 60 70 61 inline int32_t getBaseID() const62 { assert(data_); return *(int32_t*)(data_+8); }63 inline void setBaseID( int32_t id)64 { assert(data_); *(int32_t*)(data_+8) = id; }71 inline uint32_t getBaseID() const 72 { assert(data_); return *(uint32_t*)(data_+8); } 73 inline void setBaseID(uint32_t id) 74 { assert(data_); *(uint32_t*)(data_+8) = id; } 65 75 66 76 inline uint32_t getDataSize() const 67 { assert(data_); return *(uint32_t*)(data_+12); }77 { assert(data_); return *(uint32_t*)(data_+12); } 68 78 inline void setDataSize(uint32_t size) 69 { assert(data_); *(uint32_t*)(data_+12) = size; }79 { assert(data_); *(uint32_t*)(data_+12) = size; } 70 80 71 81 inline uint32_t getCompSize() const … … 75 85 76 86 inline bool isDiffed() const 77 { assert(data_); return *(int8_t*)(data_+20) & 0x1; }87 { assert(data_); return *(int8_t*)(data_+20) & 0x1; } 78 88 inline void setDiffed(bool b) 79 { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); }89 { assert(data_); *(int8_t*)(data_+20) = (b<<0) | (*(int8_t*)(data_+20) & 0x6 ); } 80 90 81 91 inline bool isComplete() const 82 { assert(data_); return *(int8_t*)(data_+20) & 0x2; }92 { assert(data_); return *(int8_t*)(data_+20) & 0x2; } 83 93 inline void setComplete(bool b) 84 { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); }94 { assert(data_); *(int8_t*)(data_+20) = (b<<1) | (*(int8_t*)(data_+20) & 0x5 ); } 85 95 86 96 inline bool isCompressed() const 87 { assert(data_); return *(int8_t*)(data_+20) & 0x4; }97 { assert(data_); return *(int8_t*)(data_+20) & 0x4; } 88 98 inline void setCompressed(bool b) 89 { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); }99 { assert(data_); *(int8_t*)(data_+20) = (b<<2) | (*(int8_t*)(data_+20) & 0x3 ); } 90 100 91 101 inline void operator=(GamestateHeader& h) 92 { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); }102 { assert(data_); assert(h.data_); memcpy( data_, h.data_, getSize()); } 93 103 private: 94 uint8_t *data_;104 uint8_t* data_; 95 105 96 106 }; … … 99 109 @author Oliver Scheuss 100 110 */ 101 class _NetworkExport Gamestate: public Packet{ 111 class _NetworkExport Gamestate: public Packet 112 { 102 113 public: 103 114 Gamestate(); … … 110 121 bool collectData(int id, uint8_t mode=0x0); 111 122 bool spreadData( uint8_t mode=0x0); 112 inline int32_t getID() const { return header_->getID(); }113 inline bool isDiffed() const { return header_ ->isDiffed(); }114 inline bool isCompressed() const { return header_ ->isCompressed(); }115 inline int32_t getBaseID() const { return header_ ->getBaseID(); }116 inline uint32_t getDataSize() const { return header_ ->getDataSize(); }123 inline uint32_t getID() const { return header_.getID(); } 124 inline bool isDiffed() const { return header_.isDiffed(); } 125 inline bool isCompressed() const { return header_.isCompressed(); } 126 inline int32_t getBaseID() const { return header_.getBaseID(); } 127 inline uint32_t getDataSize() const { return header_.getDataSize(); } 117 128 Gamestate* diffVariables(Gamestate *base); 118 Gamestate* diffData(Gamestate *base);119 Gamestate *undiff(Gamestate *base);120 Gamestate* doSelection(unsigned int clientID, unsigned int targetSize);129 // Gamestate* diffData(Gamestate *base); 130 // Gamestate *undiff(Gamestate *base); 131 // Gamestate* doSelection(unsigned int clientID, unsigned int targetSize); 121 132 bool compressData(); 122 133 bool decompressData(); … … 125 136 // Packet functions 126 137 private: 127 void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength);128 inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 );138 // void rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength); 139 // inline uint32_t findObject( const SynchronisableHeader& header, uint8_t* mem, uint32_t dataLength, uint32_t startPosition = 0 ); 129 140 virtual uint32_t getSize() const; 130 virtual inline bool process(); 131 uint32_t calcGamestateSize(int32_t id, uint8_t mode=0x0); 141 virtual bool process(orxonox::Host* host); 142 uint32_t calcGamestateSize(uint32_t id, uint8_t mode=0x0); 143 // inline void diffObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ); 144 // inline void copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ); 132 145 133 146 std::list<obj> dataVector_; 134 GamestateHeader *header_;147 GamestateHeader header_; 135 148 std::vector<uint32_t> sizes_; 136 149 uint32_t nrOfVariables_; -
code/branches/presentation2/src/libraries/network/packet/Packet.cc
r7163 r7788 35 35 #include <enet/enet.h> 36 36 #include <boost/static_assert.hpp> 37 #include <boost/thread/mutex.hpp> 37 38 38 39 #include "util/Debug.h" … … 53 54 54 55 // Make sure we assume the right values 55 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable) == static_cast<int>(ENET_PACKET_FLAG_RELIABLE));56 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequence ) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));57 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate) == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE));56 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable) == static_cast<int>(ENET_PACKET_FLAG_RELIABLE)); 57 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequenced) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED)); 58 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate) == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE)); 58 59 59 60 #define PACKET_FLAG_DEFAULT PacketFlag::NoAllocate … … 61 62 62 63 std::map<size_t, Packet *> Packet::packetMap_; 64 boost::mutex Packet::packetMapMutex_; 63 65 64 66 Packet::Packet() … … 66 68 flags_ = PACKET_FLAG_DEFAULT; 67 69 packetDirection_ = Direction::Outgoing; 68 clientID_=0;70 peerID_=0; 69 71 data_=0; 70 72 enetPacket_=0; … … 72 74 } 73 75 74 Packet::Packet(uint8_t *data, unsigned int clientID)76 Packet::Packet(uint8_t *data, unsigned int peerID) 75 77 { 76 78 flags_ = PACKET_FLAG_DEFAULT; 77 79 packetDirection_ = Direction::Incoming; 78 clientID_=clientID;80 peerID_=peerID; 79 81 data_=data; 80 82 enetPacket_=0; … … 87 89 flags_=p.flags_; 88 90 packetDirection_ = p.packetDirection_; 89 clientID_ = p.clientID_;91 peerID_ = p.peerID_; 90 92 if(p.data_){ 91 93 data_ = new uint8_t[p.getSize()]; … … 123 125 } 124 126 125 bool Packet::send( ){127 bool Packet::send(orxonox::Host* host){ 126 128 if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional ){ 127 129 assert(0); … … 142 144 // Assures we don't create a packet and destroy it right after in another thread 143 145 // without having a reference in the packetMap_ 146 Packet::packetMapMutex_.lock(); 144 147 packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this; 148 Packet::packetMapMutex_.unlock(); 145 149 } 146 150 } … … 164 168 // ENetPacket *temp = enetPacket_; 165 169 // enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet 166 if(!Host::addPacket( enetPacket_, clientID_)) 167 enet_packet_destroy(this->enetPacket_); // if we could not add the packet to the enet queue delete it manually 170 if( this->flags_ & PacketFlag::Reliable ) 171 host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT); 172 else 173 host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE); 168 174 return true; 169 175 } … … 172 178 uint8_t *data = packet->data; 173 179 assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer()); 174 unsigned int clientID = ClientInformation::findClient(&peer->address)->getID(); 180 unsigned int peerID = ClientInformation::findClient(&peer->address)->getID(); 181 // HACK 182 if( peerID==static_cast<unsigned int>(-2)) 183 peerID = NETWORK_PEER_ID_SERVER; 175 184 Packet *p = 0; 176 COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl;185 // COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl; 177 186 switch( *(Type::Value *)(data + _PACKETID) ) 178 187 { 179 188 case Type::Acknowledgement: 180 COUT(5) << "ack" << std::endl;181 p = new Acknowledgement( data, clientID );189 // COUT(5) << "ack" << std::endl; 190 p = new Acknowledgement( data, peerID ); 182 191 break; 183 192 case Type::Chat: 184 COUT(5) << "chat" << std::endl;185 p = new Chat( data, clientID );193 // COUT(5) << "chat" << std::endl; 194 p = new Chat( data, peerID ); 186 195 break; 187 196 case Type::ClassID: 188 COUT(5) << "classid" << std::endl;189 p = new ClassID( data, clientID );197 // COUT(5) << "classid" << std::endl; 198 p = new ClassID( data, peerID ); 190 199 break; 191 200 case Type::Gamestate: 192 COUT(5) << "gamestate" << std::endl; 193 // TODO: remove brackets 194 p = new Gamestate( data, clientID ); 201 // COUT(5) << "gamestate" << std::endl; 202 p = new Gamestate( data, peerID ); 195 203 break; 196 204 case Type::Welcome: 197 COUT(5) << "welcome" << std::endl;198 p = new Welcome( data, clientID );205 // COUT(5) << "welcome" << std::endl; 206 p = new Welcome( data, peerID ); 199 207 break; 200 208 case Type::DeleteObjects: 201 COUT(5) << "deleteobjects" << std::endl;202 p = new DeleteObjects( data, clientID );209 // COUT(5) << "deleteobjects" << std::endl; 210 p = new DeleteObjects( data, peerID ); 203 211 break; 204 212 case Type::FunctionCalls: 205 COUT(5) << "functionCalls" << std::endl;206 p = new FunctionCalls( data, clientID );213 // COUT(5) << "functionCalls" << std::endl; 214 p = new FunctionCalls( data, peerID ); 207 215 break; 208 216 case Type::FunctionIDs: 209 COUT(5) << "functionIDs" << std::endl;210 p = new FunctionIDs( data, clientID );217 // COUT(5) << "functionIDs" << std::endl; 218 p = new FunctionIDs( data, peerID ); 211 219 break; 212 220 default: 213 assert(0); //TODO: repair this221 assert(0); 214 222 break; 215 223 } … … 229 237 void Packet::deletePacket(ENetPacket *enetPacket){ 230 238 // Get our Packet from a global map with all Packets created in the send() method of Packet. 239 Packet::packetMapMutex_.lock(); 231 240 std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket)); 232 241 assert(it != packetMap_.end()); … … 235 244 delete it->second; 236 245 packetMap_.erase(it); 237 COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl; 246 Packet::packetMapMutex_.unlock(); 247 // COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl; 238 248 } 239 249 -
code/branches/presentation2/src/libraries/network/packet/Packet.h
r7490 r7788 68 68 virtual unsigned char *getData(){ return data_; }; 69 69 virtual unsigned int getSize() const =0; 70 virtual bool process( )=0;70 virtual bool process(orxonox::Host* host)=0; 71 71 inline uint32_t getFlags() 72 72 { return flags_; } 73 inline int getClientID() 74 { return clientID_; } 75 inline void setClientID( int id ) 76 { clientID_ = id; } 73 inline int getPeerID() 74 { return peerID_; } 75 inline void setPeerID( int id ) 76 { peerID_ = id; } 77 inline bool isReliable() 78 { return this->flags_ & PacketFlag::Reliable; } 79 inline uint32_t getRequiredGamestateID() 80 { return this->requiredGamestateID_; } 77 81 78 virtual bool send( );82 virtual bool send(orxonox::Host* host); 79 83 protected: 80 84 Packet(); 81 Packet(uint8_t *data, unsigned int clientID);85 Packet(uint8_t *data, unsigned int peerID); 82 86 // Packet(ENetPacket *packet, ENetPeer *peer); 83 87 inline bool isDataENetAllocated() const … … 85 89 86 90 uint32_t flags_; 87 unsigned int clientID_; 91 unsigned int peerID_; 92 uint32_t requiredGamestateID_; 88 93 Direction::Value packetDirection_; 89 94 /** Pointer to the data. Be careful when deleting it because it might … … 96 101 private: 97 102 static std::map<size_t, Packet *> packetMap_; 103 static boost::mutex packetMapMutex_; 98 104 ENetPacket *enetPacket_; 99 105 }; -
code/branches/presentation2/src/libraries/network/packet/Welcome.cc
r5781 r7788 73 73 } 74 74 75 bool Welcome::process( ){75 bool Welcome::process(orxonox::Host* host){ 76 76 uint32_t clientID; 77 77 clientID = *(uint32_t *)(data_ + _CLIENTID ); 78 78 assert(*(uint32_t *)(data_ + _ENDIANTEST ) == 0xFEDC4321); 79 Host::setClientID(clientID);79 host->setClientID(clientID); 80 80 COUT(3) << "Welcome set clientId: " << clientID << endl; 81 81 Synchronisable::setClient(true); -
code/branches/presentation2/src/libraries/network/packet/Welcome.h
r6073 r7788 47 47 uint8_t *getData(); 48 48 inline unsigned int getSize() const; 49 bool process();49 virtual bool process(orxonox::Host* host); 50 50 51 51 private: -
code/branches/presentation2/src/libraries/network/synchronisable/Synchronisable.cc
r7401 r7788 123 123 { 124 124 SynchronisableHeader header(mem); 125 assert( !header.isDiffed() ); 125 if( header.isDiffed() ) 126 { 127 mem += header.getDataSize() + header.getSize(); 128 return 0; 129 } 130 // assert( !header.isDiffed() ); 126 131 127 132 COUT(4) << "fabricating object with id: " << header.getObjectID() << std::endl; … … 217 222 * length of varx: size saved int syncvarlist 218 223 * @param mem pointer to allocated memory with enough size 219 * @param sizes FIXME - add doc!224 * @param sizes vector containing sizes of all objects in gamestate (to be appended) 220 225 * @param id gamestateid of the gamestate to be saved (important for priorities) 221 226 * @param mode defines the direction in which the data will be send/received 222 227 * 0x1: server->client 223 * 0x2: client->server (not recommended)228 * 0x2: client->server 224 229 * 0x3: bidirectional 225 230 * @return true: if !doSync or if everything was successfully saved … … 265 270 //tempsize += (*i)->getSize( mode ); 266 271 } 272 assert(tempsize!=0); // if this happens an empty object (with no variables) would be transmitted 267 273 // COUT(4) << endl; 268 274 … … 316 322 mem += SynchronisableHeader::getSize(); 317 323 std::vector<SynchronisableVariableBase *>::iterator i; 318 for(i=syncList_.begin(); i!=syncList_.end(); i++)324 for(i=syncList_.begin(); i!=syncList_.end(); ++i) 319 325 { 320 326 assert( mem <= data+syncHeader2.getDataSize()+SynchronisableHeader::getSize() ); // always make sure we don't exceed the datasize in our stream -
code/branches/presentation2/src/libraries/network/synchronisable/Synchronisable.h
r7163 r7788 65 65 }; 66 66 } 67 68 typedef uint8_t VariableID;69 70 /**71 * @brief: stores information about a Synchronisable72 *73 * This class stores the information about a Synchronisable (objectID_, classID_, creatorID_, dataSize)74 * in an emulated bitset.75 * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream76 * Bit 32 is a bool and defines whether the variables are stored in diff mode77 * Byte 5 to 8: objectID_78 * Byte 9 to 12: classID_79 * Byte 13 to 16: creatorID_80 */81 class _NetworkExport SynchronisableHeader{82 friend class SynchronisableHeaderLight;83 private:84 uint8_t* data_;85 public:86 SynchronisableHeader(uint8_t* data)87 { data_ = data; }88 inline static uint32_t getSize()89 { return 14; }90 inline uint16_t getDataSize() const91 { return (*(uint16_t*)data_) & 0x7FFF; } //only use the first 15 bits92 inline void setDataSize(uint16_t size)93 { *(uint16_t*)(data_) = (size & 0x7FFF) | (*(uint16_t*)(data_) & 0x8000 ); }94 inline bool isDiffed() const95 { return ( (*(uint16_t*)data_) & 0x8000 ) == 0x8000; }96 inline void setDiffed( bool b)97 { *(uint16_t*)(data_) = (b << 15) | (*(uint16_t*)(data_) & 0x7FFF ); }98 inline uint32_t getObjectID() const99 { return *(uint32_t*)(data_+2); }100 inline void setObjectID(uint32_t objectID_)101 { *(uint32_t*)(data_+2) = objectID_; }102 inline uint32_t getClassID() const103 { return *(uint32_t*)(data_+6); }104 inline void setClassID(uint32_t classID_)105 { *(uint32_t*)(data_+6) = classID_; }106 inline uint32_t getCreatorID() const107 { return *(uint32_t*)(data_+10); }108 inline void setCreatorID(uint32_t creatorID_)109 { *(uint32_t*)(data_+10) = creatorID_; }110 inline void operator=(SynchronisableHeader& h)111 { memcpy(data_, h.data_, getSize()); }112 };113 67 114 68 /** … … 121 75 * Byte 5 to 8: objectID_ 122 76 */ 123 class _NetworkExport SynchronisableHeaderLight{ 124 private: 77 class _NetworkExport SynchronisableHeaderLight 78 { 79 protected: 125 80 uint8_t* data_; 126 81 public: … … 141 96 inline void setObjectID(uint32_t objectID_) 142 97 { *(uint32_t*)(data_+2) = objectID_; } 98 inline void operator=(SynchronisableHeaderLight& h) 99 { memcpy(data_, h.data_, SynchronisableHeaderLight::getSize()); } 100 }; 101 102 typedef uint8_t VariableID; 103 104 /** 105 * @brief: stores information about a Synchronisable 106 * 107 * This class stores the information about a Synchronisable (objectID_, classID_, creatorID_, dataSize) 108 * in an emulated bitset. 109 * Bit 1 to 31 store the size of the Data the synchronisable consumes in the stream 110 * Bit 32 is a bool and defines whether the variables are stored in diff mode 111 * Byte 5 to 8: objectID_ 112 * Byte 9 to 12: classID_ 113 * Byte 13 to 16: creatorID_ 114 */ 115 class _NetworkExport SynchronisableHeader: public SynchronisableHeaderLight 116 { 117 public: 118 SynchronisableHeader(uint8_t* data): SynchronisableHeaderLight(data) 119 {} 120 inline static uint32_t getSize() 121 { return SynchronisableHeaderLight::getSize()+8; } 122 inline uint32_t getClassID() const 123 { return *(uint32_t*)(data_+SynchronisableHeaderLight::getSize()); } 124 inline void setClassID(uint32_t classID_) 125 { *(uint32_t*)(data_+SynchronisableHeaderLight::getSize()) = classID_; } 126 inline uint32_t getCreatorID() const 127 { return *(uint32_t*)(data_+SynchronisableHeaderLight::getSize()+4); } 128 inline void setCreatorID(uint32_t creatorID_) 129 { *(uint32_t*)(data_+SynchronisableHeaderLight::getSize()+4) = creatorID_; } 143 130 inline void operator=(SynchronisableHeader& h) 144 131 { memcpy(data_, h.data_, getSize()); } 145 132 }; 133 134 // inline void operator=(SynchronisableHeaderLight& h1, SynchronisableHeader& h2) 135 // { 136 // memcpy(h1.data_, h2.data_, h1.getSize()); 137 // } 146 138 147 139 /** -
code/branches/presentation2/src/libraries/util/SignalHandler.cc
r7457 r7788 51 51 #include <X11/Xutil.h> 52 52 #include <X11/keysym.h> 53 #include <sys/prctl.h> 53 54 54 55 namespace orxonox … … 137 138 COUT(0) << "Received signal " << sigName.c_str() << std::endl << "Try to write backtrace to file orxonox_crash.log" << std::endl; 138 139 139 int sigPipe[2]; 140 if ( pipe(sigPipe) == -1 ) 141 { 142 perror("pipe failed!\n"); 143 exit(EXIT_FAILURE); 144 } 145 146 int sigPid = fork(); 147 148 if ( sigPid == -1 ) 149 { 150 perror("fork failed!\n"); 151 exit(EXIT_FAILURE); 152 } 153 154 // gdb will be attached to this process 155 if ( sigPid == 0 ) 156 { 157 getInstance().dontCatch(); 158 // wait for message from parent when it has attached gdb 159 int someData; 160 161 read( sigPipe[0], &someData, sizeof(someData) ); 162 163 if ( someData != 0x12345678 ) 164 { 165 COUT(0) << "something went wrong :(" << std::endl; 166 } 167 168 return; 169 } 170 140 141 // First start GDB which will be attached to this process later on 142 171 143 int gdbIn[2]; 172 144 int gdbOut[2]; 173 145 int gdbErr[2]; 174 146 175 147 if ( pipe(gdbIn) == -1 || pipe(gdbOut) == -1 || pipe(gdbErr) == -1 ) 176 148 { 177 149 perror("pipe failed!\n"); 178 kill( sigPid, SIGTERM );179 waitpid( sigPid, NULL, 0 );180 150 exit(EXIT_FAILURE); 181 151 } 182 152 183 153 int gdbPid = fork(); 184 154 // this process will run gdb 185 155 186 156 if ( gdbPid == -1 ) 187 157 { 188 158 perror("fork failed\n"); 189 kill( sigPid, SIGTERM );190 waitpid( sigPid, NULL, 0 );191 159 exit(EXIT_FAILURE); 192 160 } 193 161 194 162 if ( gdbPid == 0 ) 195 163 { 196 164 // start gdb 197 165 198 166 close(gdbIn[1]); 199 167 close(gdbOut[0]); 200 168 close(gdbErr[0]); 201 169 202 170 dup2( gdbIn[0], STDIN_FILENO ); 203 171 dup2( gdbOut[1], STDOUT_FILENO ); 204 172 dup2( gdbErr[1], STDERR_FILENO ); 205 173 206 174 execlp( "sh", "sh", "-c", "gdb", static_cast<void*>(NULL)); 175 } 176 177 178 // Now start a fork of this process on which GDB will be attached on 179 180 int sigPipe[2]; 181 if ( pipe(sigPipe) == -1 ) 182 { 183 perror("pipe failed!\n"); 184 kill( gdbPid, SIGTERM ); 185 waitpid( gdbPid, NULL, 0 ); 186 exit(EXIT_FAILURE); 187 } 188 189 int sigPid = fork(); 190 191 if ( sigPid == -1 ) 192 { 193 perror("fork failed!\n"); 194 kill( gdbPid, SIGTERM ); 195 waitpid( gdbPid, NULL, 0 ); 196 exit(EXIT_FAILURE); 197 } 198 199 // gdb will be attached to this process 200 if ( sigPid == 0 ) 201 { 202 getInstance().dontCatch(); 203 204 // make sure gdb is allowed to attach to our PID even if there are some system restrictions 205 #ifdef PR_SET_PTRACER 206 if( prctl(PR_SET_PTRACER, gdbPid, 0, 0, 0) == -1 ) 207 COUT(0) << "could not set proper permissions for GDB to attach to process..." << endl; 208 #endif 209 210 // wait for message from parent when it has attached gdb 211 int someData; 212 213 if( read( sigPipe[0], &someData, sizeof(someData) ) != sizeof(someData) ) 214 COUT(0) << "something went wrong :(" << std::endl; 215 216 if ( someData != 0x12345678 ) 217 { 218 COUT(0) << "something went wrong :(" << std::endl; 219 } 220 221 return; 207 222 } 208 223 -
code/branches/presentation2/src/modules/weapons/MuzzleFlash.cc
r7284 r7788 42 42 this->setScale(0.1f); 43 43 44 this->delayTimer_.setTimer(0.1f, false, createExecutor(createFunctor(&MuzzleFlash::destroy, this))); 44 if( GameMode::isMaster() ) 45 this->delayTimer_.setTimer(0.1f, false, createExecutor(createFunctor(&MuzzleFlash::destroy, this))); 45 46 } 46 47 } -
code/branches/presentation2/src/orxonox/controllers/NewHumanController.cc
r7284 r7788 375 375 { 376 376 // CCOUT(0) << "testing object as target" << endl; 377 if (itr->movable->isInScene() && itr->movable->getMovableType() == "Entity" /*&& itr->distance > 500*/)377 if (itr->movable->isInScene() && itr->movable->getMovableType() == "Entity" && itr->distance > 200) 378 378 { 379 379 // Try to cast the user pointer -
code/branches/presentation2/src/orxonox/gametypes/Gametype.cc
r7284 r7788 79 79 else 80 80 this->scoreboard_ = 0; 81 82 /* HACK HACK HACK */ 83 this->dedicatedAddBots_ = createConsoleCommand( "dedicatedAddBots", createExecutor( createFunctor(&Gametype::addBots, this) ) ); 84 this->dedicatedKillBots_ = createConsoleCommand( "dedicatedKillBots", createExecutor( createFunctor(&Gametype::killBots, this) ) ); 85 /* HACK HACK HACK */ 81 86 } 82 87 … … 86 91 { 87 92 this->gtinfo_->destroy(); 93 if( this->dedicatedAddBots_ ) 94 delete this->dedicatedAddBots_; 95 if( this->dedicatedKillBots_ ) 96 delete this->dedicatedKillBots_; 88 97 } 89 98 } -
code/branches/presentation2/src/orxonox/gametypes/Gametype.h
r7284 r7788 184 184 // Config Values 185 185 std::string scoreboardTemplate_; 186 187 /* HACK HACK HACK */ 188 ConsoleCommand* dedicatedAddBots_; 189 ConsoleCommand* dedicatedKillBots_; 190 /* HACK HACK HACK */ 186 191 }; 187 192 }
Note: See TracChangeset
for help on using the changeset viewer.