Changeset 12020 for code/branches
- Timestamp:
- May 30, 2018, 2:56:24 PM (6 years ago)
- Location:
- code/branches/PresentationFS18
- Files:
-
- 44 edited
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
code/branches/PresentationFS18
- Property svn:mergeinfo changed
/code/branches/Masterserver_FS18 (added) merged: 11816,11829,11842,11856,11858,11880,11889,11905,11907,11910,11929-11930,11937,11962-11963,11973,11983,11990
- Property svn:mergeinfo changed
-
code/branches/PresentationFS18/data/gui/layouts/MultiplayerMenu.layout
r7801 r12020 23 23 <Property Name="Text" Value="LAN"/> 24 24 <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> 25 <Property Name="UnifiedAreaRect" Value="{{0. 2,0},{0.87,0},{0.4,0},{0.97,0}}" />25 <Property Name="UnifiedAreaRect" Value="{{0.05,0},{0.87,0},{0.25,0},{0.97,0}}" /> 26 26 <Event Name="SelectStateChanged" Function="MultiplayerMenu.LanButton_clicked"/> 27 27 </Window> … … 29 29 <Property Name="Text" Value="Internet" /> 30 30 <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> 31 <Property Name="UnifiedAreaRect" Value="{{0. 6,0},{0.87,0},{0.8,0},{0.97,0}}" />31 <Property Name="UnifiedAreaRect" Value="{{0.4,0},{0.87,0},{0.6,0},{0.97,0}}" /> 32 32 <Event Name="SelectStateChanged" Function="MultiplayerMenu.InternetButton_clicked"/> 33 </Window> 34 <Window Type="MenuWidgets/Button" Name="orxonox/MultiplayerRefreshButton" > 35 <Property Name="Text" Value="Refresh" /> 36 <Property Name="UnifiedMaxSize" Value="{{1,0},{1,0}}" /> 37 <Property Name="UnifiedAreaRect" Value="{{0.75,0},{0.87,0},{0.95,0},{0.97,0}}" /> 38 <Event Name="Clicked" Function="MultiplayerMenu.showServerList" /> 33 39 </Window> 34 40 </Window> -
code/branches/PresentationFS18/data/overlays/lastTeamStandingHUD.oxo
r9348 r12020 2 2 <OverlayGroup name="lastTeamStandingHUD" scale = "1, 1"> 3 3 <LastTeamStandingInfos 4 position = "0. 14, 0.02"4 position = "0.5, 0.02" 5 5 pickpoint = "0.0, 0.0" 6 6 font = "ShareTechMono" … … 14 14 15 15 <OverlayText 16 position = "0. 02, 0.02"16 position = "0.38, 0.02" 17 17 pickpoint = "0.0, 0.0" 18 18 font = "ShareTechMono" … … 24 24 25 25 <OverlayText 26 position = "0. 02, 0.055"26 position = "0.38, 0.055" 27 27 pickpoint = "0.0, 0.0" 28 28 font = "ShareTechMono" … … 34 34 35 35 <LastTeamStandingInfos 36 position = "0. 14, 0.055"36 position = "0.5, 0.055" 37 37 pickpoint = "0.0, 0.0" 38 38 font = "ShareTechMono" -
code/branches/PresentationFS18/src/libraries/network/Client.cc
r11071 r12020 166 166 } 167 167 } 168 //assert(gs); <--- there might be the case that no data has to be sent, so its commented out now 169 // if(gs){ 170 // orxout(verbose, context::network) << "client tick: sending gs " << gs << endl; 171 // if( !gs->send() ) 172 // orxout(internal_warning, context::network) << "Problem adding partial gamestate to queue" << endl; 173 // // gs gets automatically deleted by enet callback 174 // } 168 175 169 FunctionCallManager::sendCalls(static_cast<Host*>(this)); 176 170 } 177 171 } 178 // sendPackets(); // flush the enet queue179 172 180 173 Connection::processQueue(); … … 185 178 isSynched_=true; 186 179 } 187 // GamestateManager::cleanup();; 188 // Connection::sendPackets(); 180 189 181 190 182 return; -
code/branches/PresentationFS18/src/libraries/network/Client.h
r11071 r12020 90 90 private: 91 91 Client(const Client& copy); // not used 92 virtual bool isServer_() override {return false;}92 virtual bool isServer_() override { return false; } 93 93 virtual void processPacket(packet::Packet* packet) override; 94 94 -
code/branches/PresentationFS18/src/libraries/network/ClientConnection.cc
r11071 r12020 105 105 { 106 106 // manually add server to list of peers 107 /*incomingEvent inEvent = */Connection::preprocessConnectEvent(event);108 // addPeer(inEvent.peerID); 107 Connection::preprocessConnectEvent(event); 108 109 109 // start communication thread 110 110 this->established_=true; … … 155 155 assert( this->server_ ); 156 156 assert( packet ); 157 // return Connection::addPacket( packet, NETWORK_PEER_ID_SERVER, channelID );158 157 // HACK: actually there should be a way to do this using addPacket and the correct peerID 159 158 return Connection::broadcastPacket(packet, channelID); … … 168 167 this->established_=false; 169 168 orxout(internal_error, context::network) << "Received disconnect Packet from Server!" << endl; 170 169 // server closed the connection 171 170 this->stopCommunicationThread(); 172 171 this->connectionClosed(); -
code/branches/PresentationFS18/src/libraries/network/ClientConnection.h
r11071 r12020 36 36 { 37 37 38 class _NetworkExport ClientConnection: public Connection {38 class _NetworkExport ClientConnection: public Connection { 39 39 public: 40 40 ClientConnection(); … … 44 44 void setPort( unsigned int port ); 45 45 46 // ENetEvent *getEvent();47 // check wheter the packet queue is empty48 // bool queueEmpty();49 46 // create a new listener thread 50 47 virtual bool establishConnection(); -
code/branches/PresentationFS18/src/libraries/network/ClientConnectionListener.cc
r11071 r12020 31 31 #include "core/CoreIncludes.h" 32 32 #include "core/GameMode.h" 33 // #include "ClientInformation.h"34 33 35 34 namespace orxonox … … 37 36 RegisterAbstractClass(ClientConnectionListener).inheritsFrom<Listable>(); 38 37 38 /** 39 * Constructor 40 * Register the object 41 */ 39 42 ClientConnectionListener::ClientConnectionListener() 40 43 { … … 42 45 } 43 46 47 /** 48 * Call clientConnected() on all ClientConnectionListeners. 49 * @param clientID The ID of the newly connected client 50 */ 44 51 void ClientConnectionListener::broadcastClientConnected(unsigned int clientID) 45 52 { … … 48 55 } 49 56 57 /** 58 * Call clientDisconnected() on all ClientConnectionListeners. 59 * @param clientID The ID of the newly disconnected client 60 */ 50 61 void ClientConnectionListener::broadcastClientDisconnected(unsigned int clientID) 51 62 { … … 53 64 listener->clientDisconnected(clientID); 54 65 } 55 56 // void ClientConnectionListener::getConnectedClients()57 // {58 // ClientInformation* client = ClientInformation::getBegin();59 // while (client)60 // {61 // this->clientConnected(client->getID());62 // client = client->next();63 // }64 // }65 66 } 66 67 -
code/branches/PresentationFS18/src/libraries/network/ClientConnectionListener.h
r9667 r12020 35 35 namespace orxonox 36 36 { 37 /** 38 * An abstract base class. Derived classes must implement clientConnected() and clientDisconnected(). 39 */ 37 40 class _NetworkExport ClientConnectionListener : virtual public Listable 38 41 { … … 46 49 virtual void clientConnected(unsigned int clientID) = 0; 47 50 virtual void clientDisconnected(unsigned int clientID) = 0; 48 49 protected:50 // void getConnectedClients();51 51 }; 52 52 } -
code/branches/PresentationFS18/src/libraries/network/Connection.cc
r11071 r12020 38 38 39 39 #include "packet/Packet.h" 40 #include "util/Output.h" 40 41 #include <util/Sleep.h> 41 42 … … 45 46 const unsigned int NETWORK_DISCONNECT_TIMEOUT = 500; 46 47 48 /** 49 * Constructor 50 * @param firstPeerId The initial value of nextPeerID_ 51 */ 47 52 Connection::Connection(uint32_t firstPeerID): 48 53 host_(nullptr), bCommunicationThreadRunning_(false), nextPeerID_(firstPeerID) 49 54 { 55 // Global initialization of ENet 50 56 enet_initialize(); 57 58 // Register enet_deinitialize to be executed when the program exits normally 51 59 atexit(enet_deinitialize); 60 61 // Create mutexes for incoming and outgoing events 52 62 this->incomingEventsMutex_ = new boost::mutex; 53 63 this->outgoingEventsMutex_ = new boost::mutex; 54 // this->overallMutex_ = new boost::mutex; 55 } 56 64 } 65 66 /** 67 * Destructor 68 */ 57 69 Connection::~Connection() 58 70 { 71 // Delete the mutexes 59 72 delete this->incomingEventsMutex_; 60 73 delete this->outgoingEventsMutex_; 61 74 } 62 75 76 /** 77 * Start the main communication thread. 78 */ 63 79 void Connection::startCommunicationThread() 64 80 { … … 67 83 } 68 84 85 /** 86 * Stop the main communication thread. 87 */ 69 88 void Connection::stopCommunicationThread() 70 89 { 71 90 this->bCommunicationThreadRunning_ = false; 72 if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) ) 73 { 74 // force thread to stop 91 // Wait for peaceful termination 92 if(!this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME)) 93 { 94 // Force thread to stop if the waiting time runs out. 75 95 this->communicationThread_->interrupt(); 76 96 } … … 78 98 } 79 99 100 /** 101 * Send an outgoing event of type 'disconnectPeer'. 102 * @param peerID The peer to which the event is sent 103 */ 80 104 void Connection::disconnectPeer(uint32_t peerID) 81 105 { 82 // this->overallMutex_->lock();83 106 outgoingEvent outEvent = { peerID, OutgoingEventType::disconnectPeer, nullptr, 0 }; 84 107 … … 86 109 this->outgoingEvents_.push_back(outEvent); 87 110 this->outgoingEventsMutex_->unlock(); 88 // this->overallMutex_->unlock(); 89 } 90 111 } 112 113 /** 114 * Send an outgoing event of type 'disconnectPeers'. 115 */ 91 116 void Connection::disconnectPeers() 92 117 { … … 98 123 } 99 124 125 /** 126 * Send a packet. 127 * @param packet Pointer to the packet to send 128 * @param peerID The peer to which the event is sent 129 * @param channelId The channel ID 130 */ 100 131 void Connection::addPacket(ENetPacket* packet, uint32_t peerID, uint8_t channelID) 101 132 { 102 // this->overallMutex_->lock();103 133 outgoingEvent outEvent = { peerID, OutgoingEventType::sendPacket, packet, channelID }; 104 134 … … 106 136 this->outgoingEvents_.push_back(outEvent); 107 137 this->outgoingEventsMutex_->unlock(); 108 // this->overallMutex_->unlock(); 109 } 110 138 } 139 140 /** 141 * Send a broadcast packet. 142 * @param packet Pointer to the packet to send 143 * @param channelId The channel ID 144 */ 111 145 void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID) 112 146 { 113 // this->overallMutex_->lock();114 147 outgoingEvent outEvent = { 0, OutgoingEventType::broadcastPacket, packet, channelID }; 115 148 … … 117 150 this->outgoingEvents_.push_back(outEvent); 118 151 this->outgoingEventsMutex_->unlock(); 119 // this->overallMutex_->unlock(); 120 } 121 122 152 } 153 154 155 /** 156 * Main communication thread 157 */ 123 158 void Connection::communicationThread() 124 159 { 125 160 ENetEvent event; 126 161 127 // this->overallMutex_->lock(); 128 while( bCommunicationThreadRunning_ ) 162 while(this->bCommunicationThreadRunning_) 129 163 { 130 164 // Receive all pending incoming Events (such as packets, connects and disconnects) 131 while( enet_host_check_events( this->host_, &event ) > 0)165 while(enet_host_check_events(this->host_, &event ) > 0) 132 166 { 133 processIncomingEvent(event);167 this->processIncomingEvent(event); 134 168 } 135 169 136 // this->overallMutex_->unlock(); 170 // Sleep for 1ms 137 171 msleep(1); 138 // this->overallMutex_->lock();139 172 140 173 // Send all waiting outgoing packets … … 142 175 uint32_t outgoingEventsCount = this->outgoingEvents_.size(); 143 176 this->outgoingEventsMutex_->unlock(); 144 while( outgoingEventsCount > 0 ) 177 178 while(outgoingEventsCount > 0) 145 179 { 146 // orxout(verbose, context::network) << "outgoing event" << endl;147 180 this->outgoingEventsMutex_->lock(); 148 181 outgoingEvent outEvent = this->outgoingEvents_.front(); … … 150 183 this->outgoingEventsMutex_->unlock(); 151 184 152 processOutgoingEvent(outEvent);185 this->processOutgoingEvent(outEvent); 153 186 154 187 this->outgoingEventsMutex_->lock(); … … 158 191 159 192 // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms) 160 if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0)193 if(enet_host_service(this->host_, &event, NETWORK_WAIT_TIMEOUT) > 0) 161 194 { 162 processIncomingEvent(event);195 this->processIncomingEvent(event); 163 196 } 164 197 } 165 // this->overallMutex_->unlock(); 166 } 167 198 } 199 200 /** 201 * Handle an incoming event. 202 * @param event The incoming event 203 */ 168 204 void Connection::processIncomingEvent(ENetEvent& event) 169 205 { 170 206 incomingEvent inEvent; 171 207 // preprocess event 172 switch( event.type)208 switch(event.type) 173 209 { 174 210 case ENET_EVENT_TYPE_CONNECT: … … 192 228 } 193 229 230 /** 231 * Send an event. 232 * @param event The event to send 233 */ 194 234 void Connection::processOutgoingEvent(outgoingEvent& event) 195 235 { 196 236 ENetPeer* peer; 197 switch( event.type)237 switch(event.type) 198 238 { 199 239 case OutgoingEventType::sendPacket: 200 240 // check whether the peer is still/already in the peer list 201 if( this->peerMap_.find(event.peerID) != this->peerMap_.end())241 if(this->peerMap_.find(event.peerID) != this->peerMap_.end()) 202 242 { 203 243 peer = this->peerMap_[event.peerID]; 204 enet_peer_send( peer, event.channelID, event.packet);244 enet_peer_send(peer, event.channelID, event.packet); 205 245 } 206 246 else 207 247 { 208 248 // peer probably already disconnected so just discard packet 209 assert(event.peerID<this->nextPeerID_);249 orxout(message) << "Trying to send packet to peer that is not in peer list. Ignoring packet." << endl; 210 250 enet_packet_destroy(event.packet); 211 251 } 212 252 break; 213 253 case OutgoingEventType::disconnectPeer: 214 if( this->peerMap_.find(event.peerID) != this->peerMap_.end())254 if(this->peerMap_.find(event.peerID) != this->peerMap_.end()) 215 255 { 216 256 peer = this->peerMap_[event.peerID]; … … 220 260 { 221 261 // peer probably already disconnected so just discard disconnect event 222 assert(event.peerID <this->nextPeerID_);262 assert(event.peerID < this->nextPeerID_); 223 263 } 224 264 break; 225 265 case OutgoingEventType::disconnectPeers: 226 disconnectPeersInternal();266 this->disconnectPeersInternal(); 227 267 break; 228 268 case OutgoingEventType::broadcastPacket: … … 234 274 } 235 275 236 237 276 void Connection::disconnectPeersInternal() 238 277 { … … 241 280 enet_peer_disconnect(mapEntry.second, 0); 242 281 } 243 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT /NETWORK_WAIT_TIMEOUT;282 uint32_t iterations = NETWORK_DISCONNECT_TIMEOUT / NETWORK_WAIT_TIMEOUT; 244 283 uint32_t i = 0; 245 284 while( this->peerMap_.size() && i++ < iterations ) … … 361 400 } 362 401 363 364 402 } -
code/branches/PresentationFS18/src/libraries/network/Connection.h
r11071 r12020 96 96 97 97 protected: 98 Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER +1);98 Connection(uint32_t firstPeerID = NETWORK_PEER_ID_SERVER + 1); 99 99 100 100 void startCommunicationThread(); … … 110 110 void processQueue(); 111 111 void waitOutgoingQueue(); // wait for the outgoing queue to become empty (everything processed by communication thread) 112 virtual void addPeer(uint32_t peerID) =0;113 virtual void removePeer(uint32_t peerID) =0;114 virtual void processPacket( packet::Packet* packet) =0;112 virtual void addPeer(uint32_t peerID) = 0; 113 virtual void removePeer(uint32_t peerID) = 0; 114 virtual void processPacket( packet::Packet* packet) = 0; 115 115 116 116 incomingEvent preprocessConnectEvent(ENetEvent& event); … … 124 124 125 125 ENetHost* host_; 126 126 127 private: 127 128 void communicationThread(); … … 130 131 bool bCommunicationThreadRunning_; 131 132 ENetAddress* bindAddress_; 133 134 // Queue for incoming events 132 135 std::deque<incomingEvent> incomingEvents_; 136 137 // Queue for outgoing events 133 138 std::deque<outgoingEvent> outgoingEvents_; 139 134 140 boost::mutex* incomingEventsMutex_; 135 141 boost::mutex* outgoingEventsMutex_; -
code/branches/PresentationFS18/src/libraries/network/FunctionCallManager.cc
r11784 r12020 42 42 void FunctionCallManager::addCall(uint32_t functionID, uint32_t objectID, uint32_t peerID, const MultiType& mt1, const MultiType& mt2, const MultiType& mt3, const MultiType& mt4, const MultiType& mt5) 43 43 { 44 if(sPeerMap_.find(peerID)==sPeerMap_.end()) 44 // If the peerID doesn't exist yet in the map... 45 if(sPeerMap_.find(peerID) == sPeerMap_.end()) 45 46 { 47 // ... add a new FunctionCalls packet for the peer 46 48 FunctionCallManager::sPeerMap_[peerID] = new packet::FunctionCalls; 47 49 FunctionCallManager::sPeerMap_[peerID]->setPeerID(peerID); 48 50 } 51 52 // Add a new function call to the peer 49 53 FunctionCallManager::sPeerMap_[peerID]->addCall(functionID, objectID, mt1, mt2, mt3, mt4, mt5); 50 54 } 51 55 52 // Send calls 53 56 /** 57 * Send all function calls in sPeerMap_ to a given host, then clear sPeerMap_ 58 * @param host The host to send the function calls to 59 */ 54 60 void FunctionCallManager::sendCalls(orxonox::Host* host) 55 61 { … … 62 68 } 63 69 70 /** 71 * Place an incoming function call in the queue for processing. 72 */ 64 73 void FunctionCallManager::bufferIncomingFunctionCall(const orxonox::FunctionCall& fctCall, uint32_t minGamestateID, uint32_t peerID) 65 74 { … … 67 76 } 68 77 78 /** 79 * Process queue of incoming function calls. 80 */ 69 81 void FunctionCallManager::processBufferedFunctionCalls() 70 82 { 71 83 std::vector<std::pair<FunctionCall, std::pair<uint32_t, uint32_t>>>::iterator it = FunctionCallManager::sIncomingFunctionCallBuffer_.begin(); 72 while( it !=FunctionCallManager::sIncomingFunctionCallBuffer_.end() )84 while( it != FunctionCallManager::sIncomingFunctionCallBuffer_.end() ) 73 85 { 74 86 OrxAssert( Host::getActiveInstance(), "No Host class existing" ); -
code/branches/PresentationFS18/src/libraries/network/FunctionCallManager.h
r11071 r12020 53 53 static void processBufferedFunctionCalls(); 54 54 55 // Maps peer IDs to function calls 55 56 static std::map<uint32_t, packet::FunctionCalls*> sPeerMap_; 57 56 58 static std::vector<std::pair<FunctionCall,std::pair<uint32_t, uint32_t>>> sIncomingFunctionCallBuffer_; 59 57 60 protected: 58 61 FunctionCallManager(); -
code/branches/PresentationFS18/src/libraries/network/GamestateHandler.cc
r11071 r12020 32 32 namespace orxonox { 33 33 34 // GamestateHandler *GamestateHandler::instance_=nullptr;35 36 34 GamestateHandler::GamestateHandler() 37 35 { -
code/branches/PresentationFS18/src/libraries/network/GamestateHandler.h
r8327 r12020 37 37 38 38 /** 39 @author Oliver Scheuss 39 * An interface for any class that wants to handle gamestates. 40 * @author Oliver Scheuss 40 41 */ 41 42 class _NetworkExport GamestateHandler … … 51 52 virtual bool addGamestate(packet::Gamestate* gs, unsigned int clientID) = 0; 52 53 virtual bool ackGamestate(unsigned int gamestateID, unsigned int clientID) = 0; 53 virtual uint32_t getLastReceivedGamestateID( unsigned int clientID ) =0;54 virtual uint32_t getCurrentGamestateID() =0;54 virtual uint32_t getLastReceivedGamestateID( unsigned int clientID ) = 0; 55 virtual uint32_t getCurrentGamestateID() = 0; 55 56 }; 56 57 -
code/branches/PresentationFS18/src/libraries/network/GamestateManager.cc
r11071 r12020 43 43 #include <cassert> 44 44 #include <queue> 45 // #include <boost/thread/mutex.hpp>46 45 47 46 #include "packet/Acknowledgement.h" … … 55 54 #include "util/Clock.h" 56 55 #include "util/OrxAssert.h" 57 // #include "TrafficControl.h"58 56 59 57 namespace orxonox … … 62 60 currentGamestate_(nullptr), id_(0) 63 61 { 64 // trafficControl_ = new TrafficControl();65 // threadMutex_ = new boost::mutex();66 // threadPool_ = new ThreadPool();67 62 } 68 63 69 64 GamestateManager::~GamestateManager() 70 65 { 71 if( this->currentGamestate_ ) 72 delete this->currentGamestate_; 73 for( const auto& mapEntry : gamestateQueue ) 74 delete mapEntry.second; 75 for( const auto& mapEntryPeer : peerMap_ ) 76 { 77 for( const auto& mapEntryGamestate : mapEntryPeer.second.gamestates ) 78 delete mapEntryGamestate.second; 79 } 80 // this->trafficControl_->destroy(); 81 // delete this->threadMutex_; 82 // delete this->threadPool_; 83 } 84 85 bool GamestateManager::update(){ 86 // cleanup(); 87 return getSnapshot(); 66 if(this->currentGamestate_) 67 { 68 delete this->currentGamestate_; 69 this->currentGamestate_ = nullptr; 70 } 71 72 for(const auto& gsPair : this->gamestateQueue) 73 { 74 delete gsPair.second; 75 } 76 77 for(const auto& peerPair : this->peerMap_) 78 { 79 for(const auto& gsPair : peerPair.second.gamestates) 80 { 81 delete gsPair.second; 82 } 83 } 84 } 85 86 bool GamestateManager::update() 87 { 88 return this->getSnapshot(); 88 89 } 89 90 … … 91 92 { 92 93 assert(gs); 93 std::map<unsigned int, packet::Gamestate*>::iterator it = gamestateQueue.find(clientID); 94 if(it!=gamestateQueue.end()) 94 // Search the queue for a gamestate for the client 95 std::map<unsigned int, packet::Gamestate*>::iterator it = this->gamestateQueue.find(clientID); 96 if(it != this->gamestateQueue.end()) 95 97 { 96 98 // delete obsolete gamestate 97 99 delete it->second; 98 100 } 99 gamestateQueue[clientID] = gs; 101 // update the client's gamestate 102 this->gamestateQueue[clientID] = gs; 103 100 104 return true; 101 105 } 102 106 107 /** 108 * Process the queued gamestates. 109 */ 103 110 bool GamestateManager::processGamestates() 104 111 { 105 if( this->gamestateQueue.empty() ) 112 // Queue is empty, nothing to do 113 if(this->gamestateQueue.empty()) 114 { 106 115 return true; 116 } 117 107 118 // now push only the most recent gamestates we received (ignore obsolete ones) 108 for(const auto& mapEntry :gamestateQueue)109 { 110 OrxVerify( processGamestate(mapEntry.second), "ERROR: could not process Gamestate");111 sendAck( mapEntry.second->getID(), mapEntry.second->getPeerID());112 delete mapEntry.second;119 for(const auto& gsPair : this->gamestateQueue) 120 { 121 OrxVerify(this->processGamestate(gsPair.second), "ERROR: could not process Gamestate"); 122 this->sendAck(gsPair.second->getID(), gsPair.second->getPeerID()); 123 delete gsPair.second; 113 124 } 114 125 // now clear the queue 115 gamestateQueue.clear(); 126 this->gamestateQueue.clear(); 127 116 128 //and call all queued callbacks 117 129 NetworkCallbackManager::callCallbacks(); 130 118 131 return true; 119 132 } 120 133 134 /** 135 * Send Acknowledgement packet. 136 * @param gamestateId The gamestate ID we want to acknowledge 137 * @param peerID The ID of the peer we want to send the Acknowledgement to 138 */ 121 139 bool GamestateManager::sendAck(unsigned int gamestateID, uint32_t peerID) 122 140 { 123 assert( gamestateID != ACKID_NACK);141 assert(gamestateID != ACKID_NACK); 124 142 packet::Acknowledgement *ack = new packet::Acknowledgement(gamestateID, peerID); 125 if( 143 if(!this->sendPacket(ack)) 126 144 { 127 145 orxout(internal_warning, context::network) << "could not ack gamestate: " << gamestateID << endl; … … 135 153 } 136 154 137 138 bool GamestateManager::getSnapshot(){ 139 if ( currentGamestate_ != nullptr ) 140 delete currentGamestate_; 155 /** 156 * Update the current gamestate. 157 */ 158 bool GamestateManager::getSnapshot() 159 { 160 // Delete current gamestate 161 if (this->currentGamestate_) 162 { 163 delete this->currentGamestate_; 164 this->currentGamestate_ = nullptr; 165 } 166 141 167 uint8_t gsMode; 142 if( GameMode::isMaster() ) 168 if(GameMode::isMaster()) 169 { 143 170 gsMode = packet::GAMESTATE_MODE_SERVER; 144 else 171 } 172 else 173 { 145 174 gsMode = packet::GAMESTATE_MODE_CLIENT; 175 } 176 146 177 uint32_t newID; 147 if( GameMode::isMaster() ) 148 newID = ++id_; 149 else 150 { 151 assert(peerMap_.size()!=0); 152 newID = peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID; 153 if( newID == GAMESTATEID_INITIAL ) 178 if(GameMode::isMaster()) 179 { 180 newID = ++this->id_; 181 } 182 else 183 { 184 assert(this->peerMap_.size() != 0); 185 newID = this->peerMap_[NETWORK_PEER_ID_SERVER].lastReceivedGamestateID; 186 if(newID == GAMESTATEID_INITIAL) 154 187 { 155 188 return false; … … 157 190 } 158 191 159 currentGamestate_ = new packet::Gamestate(); 160 161 if(!currentGamestate_->collectData(newID, gsMode)) 162 { //we have no data to send 163 delete currentGamestate_; 164 currentGamestate_=nullptr; 192 // Create a new gamestate 193 this->currentGamestate_ = new packet::Gamestate(); 194 if(!this->currentGamestate_->collectData(newID, gsMode)) 195 { 196 // we have no data to send 197 delete this->currentGamestate_; 198 this->currentGamestate_ = nullptr; 165 199 return false; 166 200 } 201 167 202 return true; 168 203 } 169 204 205 /** 206 * Return a vector with the gamestates of all peers. 207 */ 170 208 std::vector<packet::Gamestate*> GamestateManager::getGamestates() 171 209 { 172 if(!currentGamestate_) 210 // Current gamestate is empty 211 if(!this->currentGamestate_){ 173 212 return std::vector<packet::Gamestate*>(); 213 } 214 174 215 std::vector<packet::Gamestate*> peerGamestates; 175 176 for( const auto& mapEntry : peerMap_ ) 177 { 178 if( !mapEntry.second.isSynched ) 216 for(const auto& mapEntry : this->peerMap_) 217 { 218 if(!mapEntry.second.isSynched) 179 219 { 180 220 orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl; … … 198 238 peerGamestates.push_back(nullptr); // insert an empty gamestate* to be changed 199 239 finishGamestate( peerID, peerGamestates.back(), baseGamestate, currentGamestate_ ); 200 if( peerGamestates.back()==nullptr ) 240 if(peerGamestates.back() == nullptr) 241 { 201 242 // nothing to send to remove pointer from vector 202 243 peerGamestates.pop_back(); 203 //FunctorMember<GamestateManager>* functor = 204 // ExecutorMember<GamestateManager>* executor = createExecutor( createFunctor(&GamestateManager::finishGamestate, this) ); 205 // executor->setDefaultValues( cid, &clientGamestates.back(), client, currentGamestate_ ); 206 // (*static_cast<Executor*>(executor))(); 207 // this->threadPool_->passFunction( executor, true ); 208 // (*functor)( cid, &(clientGamestates.back()), client, currentGamestate_ ); 209 } 210 211 // threadPool_->synchronise(); 244 } 245 } 212 246 213 247 return peerGamestates; … … 215 249 216 250 217 void GamestateManager::finishGamestate( unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate ) { 218 //why are we searching the same client's gamestate id as we searched in 219 //Server::sendGameState? 251 void GamestateManager::finishGamestate(unsigned int peerID, packet::Gamestate*& destgamestate, packet::Gamestate* base, packet::Gamestate* gamestate) 252 { 220 253 // save the (undiffed) gamestate in the clients gamestate map 221 //chose wheather the next gamestate is the first or not 222 223 // packet::Gamestate *gs = gamestate->doSelection(clientID, 20000); 224 // packet::Gamestate* gs = new packet::Gamestate(*gamestate); 225 // packet::Gamestate* gs = gamestate; 254 // choose whether the next gamestate is the first or not 255 256 // Create a copy of the gamestate 226 257 packet::Gamestate *gs = new packet::Gamestate(*gamestate); //this is neccessary because the gamestate are being kept (to diff them later on) for each client seperately 227 // packet::Gamestate *gs = new packet::Gamestate(); 228 // gs->collectData( id_, 0x1 ); 229 // this->threadMutex_->lock(); 230 peerMap_[peerID].gamestates[gamestate->getID()]=gs; 231 // this->threadMutex_->unlock(); 258 this->peerMap_[peerID].gamestates[gamestate->getID()] = gs; 259 260 // Start the clock 232 261 Clock clock; 233 262 clock.capture(); … … 236 265 { 237 266 packet::Gamestate *diffed1 = gs->diffVariables(base); 238 if( diffed1->getDataSize() == 0)267 if(diffed1->getDataSize() == 0) 239 268 { 240 269 delete diffed1; … … 249 278 } 250 279 251 252 // OrxVerify(gs->compressData(), ""); 280 // Stop the clock 253 281 clock.capture(); 254 282 orxout(verbose_more, context::network) << "diff and compress time: " << clock.getDeltaTime() << endl; 255 // orxout(verbose_more, context::network) << "sending gamestate with id " << gs->getID(); 256 // if(gamestate->isDiffed()) 257 // orxout(verbose_more, context::network) << " and baseid " << gs->getBaseID() << endl; 258 // else 259 // orxout(verbose_more, context::network) << endl; 283 284 260 285 gs->setPeerID(peerID); 261 286 destgamestate = gs; … … 263 288 264 289 290 /** 291 * Acknowledge a received gamestate. 292 * @param gamestateID The ID of the gamestate to be acknowledged 293 * @param peerID The ID of the peer to send the Acknowledgement to 294 */ 265 295 bool GamestateManager::ackGamestate(unsigned int gamestateID, unsigned int peerID) 266 296 { 267 // ClientInformation *temp = ClientInformation::findClient(peerID); 268 // assert(temp); 297 // Search for the peer in the peer map 269 298 std::map<uint32_t, peerInfo>::iterator it = this->peerMap_.find(peerID); 270 assert(it!=this->peerMap_.end()); 299 assert(it != this->peerMap_.end()); 300 271 301 unsigned int curid = it->second.lastAckedGamestateID; 272 302 273 303 assert(gamestateID != ACKID_NACK); 274 // if(gamestateID == ACKID_NACK){ 275 // it->second.lastAckedGamestateID = GAMESTATEID_INITIAL; 276 // // temp->setGamestateID(GAMESTATEID_INITIAL); 277 // // now delete all saved gamestates for this client 278 // std::map<uint32_t, packet::Gamestate*>::iterator it2; 279 // for(it2 = it->second.gamestates.begin(); it2!=it->second.gamestates.end(); ++it2 ){ 280 // delete it2->second; 281 // } 282 // it->second.gamestates.clear(); 283 // return true; 284 // } 285 286 // assert(curid==GAMESTATEID_INITIAL || curid<=gamestateID); // this line is commented out because acknowledgements are unreliable and may arrive in distorted order 287 if( gamestateID <= curid && curid != GAMESTATEID_INITIAL ) 304 305 // The gamestate has already been acknowledged, nothing to do 306 if(gamestateID <= curid && curid != GAMESTATEID_INITIAL) 307 { 288 308 return true; 289 orxout(verbose, context::network) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << endl; 309 } 310 311 orxout(verbose, context::network) << "acking gamestate " << gamestateID << " for peerID: " << peerID << " curid: " << curid << endl; 290 312 std::map<uint32_t, packet::Gamestate*>::iterator it2; 291 for ( it2=it->second.gamestates.begin(); it2!=it->second.gamestates.end();)292 { 293 if( it2->second->getID() < gamestateID)313 for (it2 = it->second.gamestates.begin(); it2 != it->second.gamestates.end();) 314 { 315 if(it2->second->getID() < gamestateID) 294 316 { 295 317 delete it2->second; … … 297 319 } 298 320 else 321 { 299 322 ++it2; 323 } 300 324 } 301 325 302 // std::map<unsigned int, packet::Gamestate*>::iterator it; 303 // for(it = gamestateMap_[peerID].begin(); it!=gamestateMap_[peerID].end() && it->first<gamestateID; ){ 304 // delete it->second; 305 // gamestateMap_[peerID].erase(it++); 306 // } 326 // update the last acked gamestate 307 327 it->second.lastAckedGamestateID = gamestateID; 308 // temp->setGamestateID(gamestateID); 309 // TrafficControl::processAck(peerID, gamestateID); 328 310 329 return true; 311 330 } 312 331 332 /** 333 * Return the ID of the last received gamestate for a certain peer 334 * @param peerID The ID of the peer\ 335 */ 313 336 uint32_t GamestateManager::getLastReceivedGamestateID(unsigned int peerID) 314 337 { 315 assert( this->peerMap_.find(peerID)!=this->peerMap_.end() );316 if( this->peerMap_.find(peerID) != this->peerMap_.end() )338 if(this->peerMap_.find(peerID) != this->peerMap_.end()) 339 { 317 340 return this->peerMap_[peerID].lastReceivedGamestateID; 318 else 341 } 342 else 343 { 319 344 return GAMESTATEID_INITIAL; 345 } 320 346 } 321 347 322 348 /** 349 * Add a peer to the game. 350 * @param peerID The ID of the peer to add. 351 */ 323 352 void GamestateManager::addPeer(uint32_t peerID) 324 353 { 325 assert(peerMap_.find(peerID)==peerMap_.end()); 326 peerMap_[peerID].peerID = peerID; 327 peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL; 328 peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL; 329 if( GameMode::isMaster() ) 330 peerMap_[peerID].isSynched = false; 331 else 332 peerMap_[peerID].isSynched = true; 333 } 334 354 // Ensure that the peer doesn't already exist. 355 assert(this->peerMap_.find(peerID) == this->peerMap_.end()); 356 357 // Create the peerInfo struct for the peer 358 this->peerMap_[peerID].peerID = peerID; 359 this->peerMap_[peerID].lastReceivedGamestateID = GAMESTATEID_INITIAL; 360 this->peerMap_[peerID].lastAckedGamestateID = GAMESTATEID_INITIAL; 361 if(GameMode::isMaster()) 362 { 363 this->peerMap_[peerID].isSynched = false; 364 } 365 else 366 { 367 this->peerMap_[peerID].isSynched = true; 368 } 369 } 370 371 /** 372 * Remove a peer from the game. 373 * @param peerID The ID of the peer to be removed 374 */ 335 375 void GamestateManager::removePeer(uint32_t peerID) 336 376 { 337 assert(peerMap_.find(peerID)!=peerMap_.end()); 338 for( const auto& mapEntry : peerMap_[peerID].gamestates ) 377 // Make sure that the peer exists 378 assert(this->peerMap_.find(peerID) != this->peerMap_.end()); 379 380 for (const auto& mapEntry : this->peerMap_[peerID].gamestates) 339 381 { 340 382 delete mapEntry.second; 341 383 } 342 peerMap_.erase(peerMap_.find(peerID)); 343 } 344 345 346 // void GamestateManager::removeClient(ClientInformation* client){ 347 // assert(client); 348 // std::map<unsigned int, std::map<unsigned int, packet::Gamestate*>>::iterator clientMap = gamestateMap_.find(client->getID()); 349 // // first delete all remained gamestates 350 // std::map<unsigned int, packet::Gamestate*>::iterator it; 351 // for(it=clientMap->second.begin(); it!=clientMap->second.end(); it++) 352 // delete it->second; 353 // // now delete the clients gamestatemap 354 // gamestateMap_.erase(clientMap); 355 // } 356 384 this->peerMap_.erase(this->peerMap_.find(peerID)); 385 } 386 387 /** 388 * Process an incoming Gamestate packet. 389 * @param gs Pointer to the incoming Gamestate packet 390 */ 357 391 bool GamestateManager::processGamestate(packet::Gamestate *gs) 358 392 { 393 // Decompress if necessary 359 394 if(gs->isCompressed()) 360 395 { … … 362 397 } 363 398 assert(!gs->isDiffed()); 399 364 400 uint8_t gsMode; 365 if( GameMode::isMaster() ) 401 if(GameMode::isMaster()) 402 { 366 403 gsMode = packet::GAMESTATE_MODE_SERVER; 367 else 404 } 405 else 406 { 368 407 gsMode = packet::GAMESTATE_MODE_CLIENT; 369 if( gs->spreadData(gsMode) ) 408 } 409 410 if(gs->spreadData(gsMode)) 370 411 { 371 412 this->peerMap_[gs->getPeerID()].lastReceivedGamestateID = gs->getID(); … … 373 414 } 374 415 else 416 { 375 417 return false; 418 } 376 419 } 377 420 -
code/branches/PresentationFS18/src/libraries/network/GamestateManager.h
r11071 r12020 58 58 * - writing gamestates to universe 59 59 * - diffing gamestates 60 * 61 * Inherited by Host 62 * 60 63 * EN/DECODATION: 61 64 * a: last Gamestate a client has received 62 65 * b: new Gamestate 63 66 * x: diffed and compressed gamestate 64 * x =(a^b)65 * b =(a^x)66 * diff(a, diff(a,x))=x (hope this is correct)67 * x = (a ^ b) 68 * b = (a ^ x) 69 * diff(a, diff(a, x)) = x (hope this is correct) 67 70 * @author Oliver Scheuss 68 71 */ … … 75 78 uint32_t lastAckedGamestateID; //!< id of the last gamestate on which we received an ack from the peer 76 79 bool isSynched; 77 std::map< uint32_t, packet::Gamestate*> gamestates;80 std::map<uint32_t, packet::Gamestate*> gamestates; 78 81 }; 79 82 … … 86 89 virtual bool ackGamestate(unsigned int gamestateID, unsigned int peerID) override; 87 90 virtual uint32_t getLastReceivedGamestateID( unsigned int peerID ) override; 88 virtual uint32_t getCurrentGamestateID() override { if(currentGamestate_) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; }91 virtual uint32_t getCurrentGamestateID() override { if(currentGamestate_) return currentGamestate_->getID(); else return GAMESTATEID_INITIAL; } 89 92 90 93 bool processGamestates(); … … 98 101 void addPeer( uint32_t peerID ); 99 102 void setSynched( uint32_t peerID ) 100 { assert(peerMap_.find(peerID) !=peerMap_.end()); peerMap_[peerID].isSynched = true; }103 { assert(peerMap_.find(peerID) != peerMap_.end()); peerMap_[peerID].isSynched = true; } 101 104 void removePeer( uint32_t peerID ); 102 bool hasPeers(){ return this->peerMap_.size() !=0; }103 // void removeClient(ClientInformation *client); 105 bool hasPeers(){ return this->peerMap_.size() != 0; } 106 104 107 protected: 105 108 virtual bool sendPacket( packet::Packet* packet ) = 0; 109 106 110 private: 107 111 bool processGamestate(packet::Gamestate *gs); 108 112 109 // std::map<unsigned int, std::map<unsigned int, packet::Gamestate*>> gamestateMap_;110 113 std::map<unsigned int, packet::Gamestate*> gamestateQueue; 111 // std::map<unsigned int, uint32_t> lastProcessedGamestateID_; 114 115 // Each peerID maps to a peerInfo struct 112 116 std::map<uint32_t, peerInfo> peerMap_; 117 118 // always contains the latest gamestate 113 119 packet::Gamestate* currentGamestate_; 114 // TrafficControl *trafficControl_;115 120 unsigned int id_; 116 // boost::mutex* threadMutex_; 117 ThreadPool* /*thread*/Pool_; 121 ThreadPool* Pool_; 118 122 }; 119 123 -
code/branches/PresentationFS18/src/libraries/network/Host.cc
r11071 r12020 44 44 SetConsoleCommand(__CC_printRTT_group, __CC_printRTT_name, &Host::printRTT); 45 45 46 // Host* Host::instance_=nullptr; 47 uint32_t Host::clientID_s=0; 48 // uint32_t Host::shipID_s=-1; 46 uint32_t Host::clientID_s = 0; 49 47 std::vector<Host*> Host::instances_s; 50 48 … … 54 52 Host::Host() 55 53 { 56 // assert(instance_==nullptr);57 instances_s.push_back(this); 54 Host::instances_s.push_back(this); 55 58 56 ModifyConsoleCommand(__CC_printRTT_group, __CC_printRTT_name).setObject(this); 57 59 58 this->bIsActive_ = false; 60 59 } … … 66 65 Host::~Host() 67 66 { 68 assert( std::find( instances_s.begin(), instances_s.end(), this )!=instances_s.end());69 instances_s.erase(std::find( instances_s.begin(), instances_s.end(), this ));67 assert(std::find( instances_s.begin(), instances_s.end(), this ) != instances_s.end()); 68 Host::instances_s.erase(std::find( instances_s.begin(), instances_s.end(), this )); 70 69 ModifyConsoleCommand(__CC_printRTT_group, __CC_printRTT_name).setObject(nullptr); 71 70 } … … 80 79 void Host::addPacket(ENetPacket *packet, int clientID, uint8_t channelID) 81 80 { 82 for (Host* host : instances_s)81 for (Host* host : instances_s) 83 82 { 84 if ( host->isActive())83 if (host->isActive()) 85 84 { 86 85 host->queuePacket(packet, clientID, channelID); … … 97 96 void Host::sendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) 98 97 { 99 for(Host* host : instances_s) 100 if( host->isActive() ) 98 for (Host* host : instances_s) 99 { 100 if (host->isActive()) 101 { 101 102 host->doSendChat(message, sourceID, targetID); 103 } 104 } 102 105 } 103 106 … … 108 111 { 109 112 for (NetworkChatListener* listener : ObjectList<NetworkChatListener>()) 113 { 110 114 listener->incomingChat(message, sourceID); 115 } 111 116 } 112 117 … … 114 119 bool Host::isServer() 115 120 { 116 for (Host* host : instances_s)121 for (Host* host : Host::instances_s) 117 122 { 118 if ( host->isActive())123 if (host->isActive()) 119 124 { 120 if( host->isServer_() ) 125 if (host->isServer_()) 126 { 121 127 return true; 128 } 122 129 } 123 130 } … … 125 132 } 126 133 134 /** 135 * Singleton implementation. Return the first active instance. 136 */ 127 137 Host* Host::getActiveInstance() 128 138 { 129 139 std::vector<Host*>::iterator it = Host::instances_s.begin(); 130 while ( it != Host::instances_s.end())140 while (it != Host::instances_s.end()) 131 141 { 132 if( (*it)->isActive() ) 142 if ((*it)->isActive()) 143 { 133 144 return *it; 145 } 134 146 else 147 { 135 148 ++it; 149 } 136 150 } 137 151 return nullptr; -
code/branches/PresentationFS18/src/libraries/network/Host.h
r8858 r12020 40 40 const unsigned int CLIENTID_SERVER = 0; 41 41 const unsigned int NETWORK_FREQUENCY = 25; 42 const float NETWORK_PERIOD = 1.0f /NETWORK_FREQUENCY;42 const float NETWORK_PERIOD = 1.0f / NETWORK_FREQUENCY; 43 43 44 44 /** … … 55 55 56 56 private: 57 virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) =0;57 virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) = 0; 58 58 virtual bool isServer_()=0; 59 59 … … 63 63 void setActive( bool bActive ){ bIsActive_ = bActive; } 64 64 65 virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) =0;66 virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) =0;65 virtual void doSendChat(const std::string& message, unsigned int sourceID, unsigned int targetID) = 0; 66 virtual void doReceiveChat(const std::string& message, unsigned int sourceID, unsigned int targetID) = 0; 67 67 68 68 public: … … 74 74 static bool isServer(); 75 75 static void sendChat(const std::string& message, unsigned int sourceID, unsigned int targetID); 76 virtual void printRTT() =0;76 virtual void printRTT() = 0; 77 77 bool isActive(){ return bIsActive_; } 78 78 79 private: 79 80 static uint32_t clientID_s; -
code/branches/PresentationFS18/src/libraries/network/LANDiscoverable.cc
r11071 r12020 121 121 info.setClientNumber(this->clientNumber); 122 122 info.send(event.peer); 123 // ENetPacket* packet = enet_packet_create( LAN_DISCOVERY_ACK, strlen(LAN_DISCOVERY_ACK)+1, ENET_PACKET_FLAG_RELIABLE );124 // enet_peer_send(event.peer, 0, packet );125 123 enet_host_flush(this->host_); 126 124 } -
code/branches/PresentationFS18/src/libraries/network/LANDiscoverable.h
r10622 r12020 43 43 void setActivity( bool bActive ); 44 44 void update(); 45 void updateClientNumber(int clientNumber) {this->clientNumber = clientNumber;} 46 ; 45 void updateClientNumber(int clientNumber) { this->clientNumber = clientNumber; } 47 46 /** Function used for the configuration file parameter update */ 48 47 void setConfigValues(); -
code/branches/PresentationFS18/src/libraries/network/LANDiscovery.cc
r11071 r12020 61 61 address.port = LAN_DISCOVERY_PORT; 62 62 63 /* TODO: check for availability of each protocol */64 63 /* IPv4 */ 65 64 address.host = ENET_HOST_BROADCAST; … … 102 101 this->servers_.push_back(info); 103 102 } 104 // enet_address_get_host_ip(&event.peer->address, buffer, buflen );105 // serverIPs.push_back(std::string(buffer));106 103 break; 107 104 default: -
code/branches/PresentationFS18/src/libraries/network/MasterServer.cc
r11083 r12020 40 40 SetConsoleCommand( "ms-listservers", &MasterServer::listServers ); 41 41 SetConsoleCommand( "ms-delserver", &MasterServer::delServer ); 42 //SetConsoleCommand( "ms-serverinfo", &MasterServer::serverInfo );43 42 44 43 /* forward declaration so the linker doesn't complain */ … … 357 356 358 357 /* incoming data */ 359 case ENET_EVENT_TYPE_RECEIVE: eventData( event ); break; 358 case ENET_EVENT_TYPE_RECEIVE: 359 eventData( event ); break; 360 360 361 default: break; 361 362 } -
code/branches/PresentationFS18/src/libraries/network/MasterServerComm.cc
r11071 r12020 38 38 * the initialize method to facilitate debugging 39 39 */ 40 /* register object in orxonox */41 40 } 42 41 … … 48 47 return 1; 49 48 } 50 51 /* initialize the event holder */52 // this->event = (ENetEvent *)calloc( sizeof(ENetEvent), 1 );53 54 49 55 50 /* initiate the client */ … … 226 221 /* One could just use enet_host_service() instead. */ 227 222 enet_host_flush( this->client ); 228 229 /* free the packet */230 // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees231 // enet_packet_destroy( packet );232 223 233 224 /* all done. */ … … 248 239 /* One could just use enet_host_service() instead. */ 249 240 enet_host_flush( this->client ); 250 // PLEASE: never do this, because enet will free the packet once it's delivered. this will cause double frees251 // enet_packet_destroy( packet );252 241 253 242 /* all done. */ -
code/branches/PresentationFS18/src/libraries/network/Server.cc
r11103 r12020 55 55 #include "packet/Gamestate.h" 56 56 #include "packet/Welcome.h" 57 // #include "ClientInformation.h"58 57 #include "FunctionCallManager.h" 59 58 #include "GamestateManager.h" … … 69 68 Server::Server() 70 69 { 71 this->timeSinceLastUpdate_=0; 72 } 73 70 this->timeSinceLastUpdate_ = 0; 71 } 72 73 /** 74 * Constructor 75 * @param port Port to listen on 76 */ 74 77 Server::Server(int port) 75 78 { 76 this->setPort( port ); 77 this->timeSinceLastUpdate_=0; 78 } 79 /* 80 Server::Server(int port, const std::string name) 81 { 82 this->setPort( port ); 83 this->timeSinceLastUpdate_=0; 84 this->serverName_=name; 85 }*/ 79 this->setPort(port); 80 this->timeSinceLastUpdate_ = 0; 81 } 82 86 83 /** 87 84 * Constructor … … 91 88 Server::Server(int port, const std::string& bindAddress) 92 89 { 93 this->setPort( port);94 this->setBindAddress( bindAddress);95 this->timeSinceLastUpdate_ =0;90 this->setPort(port); 91 this->setBindAddress(bindAddress); 92 this->timeSinceLastUpdate_ = 0; 96 93 } 97 94 … … 156 153 LANDiscoverable::update(); 157 154 158 if ( GamestateManager::hasPeers())155 if (GamestateManager::hasPeers()) 159 156 { 160 157 // process incoming gamestates … … 163 160 164 161 // send function calls to clients 165 FunctionCallManager::sendCalls( static_cast<Host*>(this));166 167 // this steers our network frequency168 timeSinceLastUpdate_ +=time.getDeltaTime();169 if(timeSinceLastUpdate_ >=NETWORK_PERIOD)162 FunctionCallManager::sendCalls(static_cast<Host*>(this)); 163 164 // this steers our network frequency 165 timeSinceLastUpdate_ += time.getDeltaTime(); 166 if(timeSinceLastUpdate_ >= NETWORK_PERIOD) 170 167 { 171 timeSinceLastUpdate_ -= static_cast<unsigned int>( timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD;172 updateGamestate();168 timeSinceLastUpdate_ -= static_cast<unsigned int>(timeSinceLastUpdate_ / NETWORK_PERIOD) * NETWORK_PERIOD; 169 this->updateGamestate(); 173 170 } 174 // sendPackets(); // flush the enet queue175 171 } 176 172 } … … 182 178 183 179 /** 184 * @brief: returns ping time to client in milliseconds 185 */ 186 unsigned int Server::getRTT(unsigned int clientID) 187 { 188 // assert(ClientInformation::findClient(clientID)); 189 // return ClientInformation::findClient(clientID)->getRTT(); 190 // TODO: reimplement 191 return 0; 192 } 193 180 * Print ping time to client in milliseconds. 181 */ 194 182 void Server::printRTT() 195 183 { 196 // for( ClientInformation* temp=ClientInformation::getBegin(); temp!=nullptr; temp=temp->next() ) 197 // orxout(message) << "Round trip time to client with ID: " << temp->getID() << " is " << temp->getRTT() << " ms" << endl; 198 } 199 200 /** 201 * @brief: return packet loss ratio to client (scales from 0 to 1) 202 */ 203 float Server::getPacketLoss(unsigned int clientID) 204 { 205 // assert(ClientInformation::findClient(clientID)); 206 // return ClientInformation::findClient(clientID)->getPacketLoss(); 207 return 0.; 208 } 209 210 /** 211 * takes a new snapshot of the gamestate and sends it to the clients 212 */ 184 // TODO: Implement 185 } 186 187 /** 188 * Take a new snapshot of the gamestate and send it to the clients. 189 */ 213 190 void Server::updateGamestate() 214 191 { 215 if( this->clientIDs_.size()==0 ) 216 //no client connected 192 if(this->clientIDs_.size() == 0) 193 { 194 // no client connected 217 195 return; 196 } 218 197 GamestateManager::update(); 219 // orxout(verbose_more, context::network) << "Server: one gamestate update complete, goig to sendGameState" << endl; 220 //orxout(verbose_more, context::network) << "updated gamestate, sending it" << endl; 221 //if(clients->getGamestateID()!=GAMESTATEID_INITIAL) 222 sendGameStates(); 223 sendObjectDeletes(); 224 // orxout(verbose_more, context::network) << "Server: one sendGameState turn complete, repeat in next tick" << endl; 225 //orxout(verbose_more, context::network) << "sent gamestate" << endl; 226 } 227 228 /** 229 * sends the current gamestate to all peers 230 */ 198 this->sendGameStates(); 199 this->sendObjectDeletes(); 200 } 201 202 /** 203 * Send the current gamestate to all peers. 204 */ 231 205 bool Server::sendGameStates() 232 206 { 233 207 std::vector<packet::Gamestate*> gamestates = GamestateManager::getGamestates(); 234 for( packet::Gamestate* gamestate : gamestates)208 for(packet::Gamestate* gamestate : gamestates) 235 209 { 236 210 gamestate->send(static_cast<Host*>(this)); … … 240 214 241 215 216 /** 217 * Send 'DeleteObjects' packet 218 */ 242 219 bool Server::sendObjectDeletes() 243 220 { 244 // ClientInformation *temp = ClientInformation::getBegin(); 245 // if( temp == nullptr ) 246 //no client connected 247 if( this->clientIDs_.size()==0 ) 221 // no client connected 222 if(this->clientIDs_.size() == 0) 223 { 248 224 return true; 225 } 226 249 227 packet::DeleteObjects *del = new packet::DeleteObjects(); 250 228 if(!del->fetchIDs()) … … 253 231 return true; //everything ok (no deletes this tick) 254 232 } 255 // orxout(verbose, context::network) << "sending DeleteObjects" << endl; 256 // while(temp != nullptr){ 257 // if( !(temp->getSynched()) ) 258 // { 259 // orxout(verbose_more, context::network) << "Server: not sending gamestate" << endl; 260 // temp=temp->next(); 261 // continue; 262 // } 263 // int cid = temp->getID(); //get client id 264 // packet::DeleteObjects *cd = new packet::DeleteObjects(*del); 265 // assert(cd); 233 266 234 del->setPeerID(NETWORK_PEER_ID_BROADCAST); 267 if ( !del->send( static_cast<Host*>(this) ) ) 235 if (!del->send( static_cast<Host*>(this))) 236 { 268 237 orxout(internal_warning, context::network) << "Server: could not broadcast deleteObjects packet" << endl; 269 // temp=temp->next(); 270 // gs gets automatically deleted by enet callback 271 // } 272 // delete del; 238 } 239 273 240 return true; 274 241 } 275 242 276 243 /** 244 * Add a new peer to the server. 245 */ 277 246 void Server::addPeer(uint32_t peerID) 278 247 { 279 // static unsigned int newid=1;280 //281 // orxout(internal_info, context::network) << "Server: adding client" << endl;282 // ClientInformation *temp = ClientInformation::insertBack(new ClientInformation);283 // if(!temp)284 // {285 // orxout(internal_warning, context::network) << "Server: could not add client" << endl;286 // }287 // temp->setID(newid);288 // temp->setPeer(event->peer);289 290 248 // inform all the listeners 291 249 this->clientIDs_.push_back(peerID); … … 296 254 GamestateManager::addPeer(peerID); 297 255 298 // ++newid;299 300 256 orxout(internal_info, context::network) << "Server: added client id: " << peerID << endl; 301 257 302 createClient(peerID); 303 } 304 258 this->createClient(peerID); 259 } 260 261 /** 262 * Remove a peer from the server. 263 */ 305 264 void Server::removePeer(uint32_t peerID) 306 265 { 307 266 orxout(verbose, context::network) << "removing client from list" << endl; 308 // ClientInformation *client = ClientInformation::findClient(&event->peer->address); 309 // if(!client) 310 // return; 311 // else 312 // { 313 std::vector<uint32_t>::iterator it; 314 for( it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it ) 315 { 316 if( *it == peerID ) 317 { 318 this->clientIDs_.erase(it); 319 break; 320 } 321 } 322 WANDiscoverable::updateClientNumber(this->clientIDs_.size()); 323 LANDiscoverable::updateClientNumber(this->clientIDs_.size()); 324 325 ClientConnectionListener::broadcastClientDisconnected(peerID); 326 GamestateManager::removePeer(peerID); 327 //ServerConnection::disconnectClient( client ); 328 //ClientConnectionListener::broadcastClientDisconnected( client->getID() ); //this is done in ClientInformation now 329 // delete client; 330 // } 331 } 332 267 268 // erase the peer from the list 269 std::vector<uint32_t>::iterator it; 270 for(it=this->clientIDs_.begin(); it!=this->clientIDs_.end(); ++it) 271 { 272 if(*it == peerID) 273 { 274 this->clientIDs_.erase(it); 275 break; 276 } 277 } 278 279 WANDiscoverable::updateClientNumber(this->clientIDs_.size()); 280 LANDiscoverable::updateClientNumber(this->clientIDs_.size()); 281 282 // Send 'clientDisconnected' message 283 ClientConnectionListener::broadcastClientDisconnected(peerID); 284 285 GamestateManager::removePeer(peerID); 286 } 287 288 /** 289 * Process an incoming packet. 290 */ 333 291 void Server::processPacket(packet::Packet* packet) 334 292 { 335 if( packet->isReliable() ) 336 { 337 if( this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID() ) 293 if(packet->isReliable()) 294 { 295 if(this->getLastReceivedGamestateID(packet->getPeerID()) >= packet->getRequiredGamestateID()) 296 { 338 297 packet->process(static_cast<Host*>(this)); 298 } 339 299 else 300 { 340 301 this->packetQueue_.push_back(packet); 302 } 341 303 } 342 304 else 305 { 343 306 packet->process(static_cast<Host*>(this)); 344 } 345 346 307 } 308 } 309 310 /** 311 * Create a client. 312 */ 347 313 bool Server::createClient(int clientID) 348 314 { 349 // ClientInformation *temp = ClientInformation::findClient(clientID);350 // if(!temp)351 // {352 // orxout(internal_error, context::network) << "Server. could not create client with id: " << clientID << endl;353 // return false;354 // }355 // orxout(verbose, context::network) << "Con.Man: creating client id: " << temp->getID() << endl;356 315 357 316 // synchronise class ids 358 syncClassid(clientID);317 this->syncClassid(clientID); 359 318 360 319 // now synchronise functionIDs 361 320 packet::FunctionIDs *fIDs = new packet::FunctionIDs(); 362 321 fIDs->setPeerID(clientID); 363 bool b = fIDs->send( static_cast<Host*>(this));322 bool b = fIDs->send(static_cast<Host*>(this)); 364 323 assert(b); 365 324 366 // temp->setSynched(true);367 325 GamestateManager::setSynched(clientID); 368 326 327 // Send 'Welcome' packet to client 369 328 orxout(verbose, context::network) << "sending welcome" << endl; 370 329 packet::Welcome *w = new packet::Welcome(clientID); 371 330 w->setPeerID(clientID); 372 b = w->send( static_cast<Host*>(this));331 b = w->send(static_cast<Host*>(this)); 373 332 assert(b); 333 374 334 (void)b; // avoid compiler warning 375 // packet::Gamestate *g = new packet::Gamestate();376 // g->setPeerID(clientID);377 // b = g->collectData(0,packet::GAMESTATE_MODE_SERVER);378 // assert(b);379 // if(!b)380 // return false; //no data for the client381 // // b = g->compressData();382 // // assert(b);383 // b = g->send( static_cast<Host*>(this) );384 // assert(b);385 335 return true; 386 336 } 387 337 388 void Server::disconnectClient( uint32_t clientID ) 389 { 390 ServerConnection::disconnectClient( clientID ); 391 GamestateManager::removePeer( clientID ); 392 // inform all the listeners 393 // ClientConnectionListener::broadcastClientDisconnected(client->getID()); // this is done in ClientInformation now 338 /** 339 * Disconnect a client. 340 */ 341 void Server::disconnectClient(uint32_t clientID) 342 { 343 ServerConnection::disconnectClient(clientID); 344 GamestateManager::removePeer(clientID); 394 345 } 395 346 … … 403 354 { 404 355 // check if the target exists. just ignore the message otherwise 405 if (!this->isValidTarget(targetID)) // TODO: remove this if an invalid clientIDs don't trigger assertions anymore 356 if (!this->isValidTarget(targetID)) 357 { 406 358 return; 359 } 407 360 408 361 // send the message to the target 409 362 packet::Chat* packet = new packet::Chat(message, sourceID, targetID); 410 363 packet->setPeerID(targetID); 411 packet->send( static_cast<Host*>(this));364 packet->send(static_cast<Host*>(this)); 412 365 413 366 // if the target is (or includes) this host as well, call the parent function which passes the message to the listeners 414 367 if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == Host::getPlayerID()) 368 { 415 369 Host::doReceiveChat(message, sourceID, targetID); 370 } 416 371 } 417 372 … … 431 386 bool Server::isValidTarget(unsigned int targetID) 432 387 { 388 // Broadcast or server ID are okay 433 389 if (targetID == NETWORK_PEER_ID_BROADCAST || targetID == NETWORK_PEER_ID_SERVER) 390 { 434 391 return true; 435 436 for( uint32_t id : this->clientIDs_ ) 437 if( id == targetID ) 392 } 393 394 // IDs in the client list are okay also 395 for(uint32_t id : this->clientIDs_) 396 { 397 if(id == targetID) 398 { 438 399 return true; 400 } 401 } 439 402 440 403 return false; … … 443 406 void Server::syncClassid(unsigned int clientID) 444 407 { 445 int failures=0;446 408 packet::ClassID *classid = new packet::ClassID(); 447 409 classid->setPeerID(clientID); 448 while(!classid->send( static_cast<Host*>(this) ) && failures < 10){ 410 411 int failures = 0; 412 while(!classid->send(static_cast<Host*>(this)) && failures < 10)\ 413 { 449 414 failures++; 450 415 } -
code/branches/PresentationFS18/src/libraries/network/Server.h
r11071 r12020 38 38 #include "core/CorePrereqs.h" 39 39 #include "Host.h" 40 // #include "GamestateManager.h"41 40 #include "ServerConnection.h" 42 41 #include "LANDiscoverable.h" … … 62 61 void close(); 63 62 virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) override; 64 virtual bool sendPacket( packet::Packet* packet ) override { return packet->send( static_cast<Host*>(this) ); }63 virtual bool sendPacket( packet::Packet* packet ) override { return packet->send( static_cast<Host*>(this) ); } 65 64 void update(const Clock& time); 66 unsigned int getRTT(unsigned int clientID);67 65 virtual void printRTT() override; 68 float getPacketLoss(unsigned int clientID); 69 int getClientCount() { return this->clientIDs_.size();} 70 std::string getServerName() { return this->serverName_;} 66 int getClientCount() { return this->clientIDs_.size(); } 67 std::string getServerName() { return this->serverName_; } 71 68 72 69 protected: 73 70 void updateGamestate(); 74 71 private: 75 virtual bool isServer_() override {return true;}76 unsigned int playerID() {return 0;}72 virtual bool isServer_() override { return true; } 73 unsigned int playerID() { return 0; } 77 74 78 75 virtual void addPeer(uint32_t peerID) override; -
code/branches/PresentationFS18/src/libraries/network/ServerConnection.cc
r11071 r12020 36 36 #include "util/Output.h" 37 37 #include <util/Sleep.h> 38 // #include "ClientInformation.h"39 38 40 39 namespace orxonox 41 40 { 42 41 /** 42 * Constructor 43 */ 43 44 ServerConnection::ServerConnection(): 44 45 bListening_(false) 45 46 { 46 47 this->bindAddress_ = new ENetAddress(); 47 // memset(this->bindAddress_, 0, sizeof(ENetAddress));48 48 this->bindAddress_->host = ENET_HOST_ANY; 49 49 this->bindAddress_->port = NETWORK_PORT; … … 51 51 } 52 52 53 /** 54 * Destructor 55 */ 53 56 ServerConnection::~ServerConnection() 54 57 { 55 if ( this->bListening_ ) 56 closeListener(); 58 if (this->bListening_) 59 { 60 this->closeListener(); 61 } 57 62 delete this->bindAddress_; 58 63 } 59 64 60 void ServerConnection::setBindAddress( const std::string& bindAddress ) 65 /** 66 * Set address on which to listen. 67 * @param bindAddress The address on which to listen 68 */ 69 void ServerConnection::setBindAddress(const std::string& bindAddress) 61 70 { 62 if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0) 63 orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl; 71 if (enet_address_set_host(this->bindAddress_, bindAddress.c_str()) < 0) 72 { 73 orxout(internal_error, context::network) << "Could not resolve \"" << bindAddress << "\"." << endl; 74 } 64 75 } 65 76 66 void ServerConnection::setPort( unsigned int port ) { 77 /** 78 * Set port on which to listen on. 79 * @param port The port on which to listen on. 80 */ 81 void ServerConnection::setPort(unsigned int port) { 67 82 this->bindAddress_->port = port; 68 83 } … … 73 88 this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, NETWORK_CHANNEL_COUNT, 0, 0); 74 89 75 if ( this->host_ == nullptr)90 if (this->host_ == nullptr) 76 91 { 77 92 orxout(internal_error, context::network) << "ServerConnection: host_ == nullptr" << endl; … … 81 96 // enable compression 82 97 this->enableCompression(); 98 99 // ensure that either IPv4 or IPv6 succeeded 83 100 assert( this->host_->socket4 != ENET_SOCKET_NULL || this->host_->socket6 != ENET_SOCKET_NULL ); 101 84 102 if (this->host_->socket4 == ENET_SOCKET_NULL) 103 { 85 104 orxout(internal_warning, context::network) << "IPv4 Socket failed." << endl; 105 } 86 106 else if (this->host_->socket6 == ENET_SOCKET_NULL) 107 { 87 108 orxout(internal_warning, context::network) << "IPv6 Socket failed." << endl; 109 } 88 110 else 111 { 89 112 orxout(internal_info, context::network) << "Using IPv4 and IPv6 Sockets." << endl; 113 } 90 114 91 115 // start communication thread … … 95 119 } 96 120 121 /** 122 * Stop listening. 123 */ 97 124 bool ServerConnection::closeListener() 98 125 { 99 this->bListening_ =false;100 disconnectClients();126 this->bListening_ = false; 127 this->disconnectClients(); 101 128 Connection::stopCommunicationThread(); 102 129 enet_host_destroy(this->host_); … … 104 131 } 105 132 133 /** 134 * Add outgoing packet to queue. 135 * @param packet The packet to send 136 * @param clientID The ID of the recipient 137 * @param channelID The channel ID 138 */ 106 139 void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) 107 140 { 108 if ( clientID == NETWORK_PEER_ID_BROADCAST)141 if (clientID == NETWORK_PEER_ID_BROADCAST) 109 142 { 110 broadcastPacket(packet, channelID);143 this->broadcastPacket(packet, channelID); 111 144 } 112 145 else 113 146 { 114 // ClientInformation *temp = ClientInformation::findClient(clientID);115 // if(!temp){116 // orxout(internal_warning, context::network) << "C.Man: addPacket findClient failed" << endl;117 // }118 147 Connection::addPacket(packet, clientID, channelID); 119 148 } 120 149 } 121 150 122 // void ServerConnection::disconnectClient(ClientInformation *client) 123 // { 124 // Connection::disconnectPeer( client->getPeer() ); 125 // } 126 151 /** 152 * Terminate connection with a peer. 153 * @param clientID The peer with which to terminate the connection. 154 */ 127 155 void ServerConnection::disconnectClient(int clientID) 128 156 { 129 // ClientInformation *client = ClientInformation::findClient(clientID);130 // if(client)131 157 Connection::disconnectPeer(clientID); 132 158 } 133 159 160 /** 161 * Disconnect all peers. 162 */ 134 163 void ServerConnection::disconnectClients() 135 164 { … … 138 167 return; 139 168 } 140 141 142 // int ServerConnection::getClientID(ENetPeer* peer)143 // {144 // return getClientID(&(peer->address));145 // }146 147 // int ServerConnection::getClientID(ENetAddress* address)148 // {149 // return ClientInformation::findClient(address)->getID();150 // }151 //152 // ENetPeer *ServerConnection::getClientPeer(int clientID)153 // {154 // return ClientInformation::findClient(clientID)->getPeer();155 // }156 157 158 169 } -
code/branches/PresentationFS18/src/libraries/network/ServerConnection.h
r8327 r12020 47 47 { 48 48 49 class _NetworkExport ServerConnection : public Connection{ 49 class _NetworkExport ServerConnection : public Connection 50 { 50 51 public: 51 52 ~ServerConnection(); … … 57 58 bool closeListener(); 58 59 void addPacket(ENetPacket *packet, unsigned int ID, uint8_t channelID); 59 // virtual void disconnectClient(ClientInformation *client);60 60 void disconnectClient(int clientID); 61 61 62 protected: 62 63 ServerConnection(); … … 64 65 65 66 private: 66 // int getClientID(ENetPeer* peer);67 // int getClientID(ENetAddress* address);68 // ENetPeer* getClientPeer(int clientID);69 70 67 ENetAddress* bindAddress_; 71 72 68 bool bListening_; 73 69 -
code/branches/PresentationFS18/src/libraries/network/packet/Acknowledgement.cc
r11071 r12020 37 37 38 38 #define PACKET_FLAGS_ACK 0 39 // Offset to start of packet ID 39 40 #define _PACKETID 0 41 // Offset to start of ACK ID 40 42 #define _ACKID _PACKETID + sizeof(packet::Type) 41 43 44 /** 45 * Constructor 46 * Acknowledgement.data_ is 40 bits in size: 47 * [0, 7]: Packet Type 48 * [8, 39]: Acknowledgement ID 49 */ 42 50 Acknowledgement::Acknowledgement( unsigned int id, unsigned int peerID ) 43 51 : Packet() 44 52 { 45 flags_ = flags_ |PACKET_FLAGS_ACK;46 data_=new uint8_t[getSize() ];47 *(Type *)( data_ + _PACKETID) = Type::Acknowledgement;48 *(uint32_t *)( data_ + _ACKID) = id;49 peerID_=peerID;53 this->flags_ |= PACKET_FLAGS_ACK; 54 this->data_ = new uint8_t[ this->getSize() ]; 55 *(Type *)(this->data_ + _PACKETID) = Type::Acknowledgement; 56 *(uint32_t *)(this->data_ + _ACKID) = id; 57 this->peerID_ = peerID; 50 58 } 51 59 … … 59 67 } 60 68 61 unsigned int Acknowledgement::getSize() const {69 unsigned int Acknowledgement::getSize() const { 62 70 return _ACKID + sizeof(uint32_t); 63 71 } 64 72 65 bool Acknowledgement::process(orxonox::Host* host) {73 bool Acknowledgement::process(orxonox::Host* host) { 66 74 orxout(verbose_more, context::packets) << "processing ACK with ID: " << getAckID() << endl; 67 75 bool b = host->ackGamestate(getAckID(), peerID_); … … 70 78 } 71 79 72 unsigned int Acknowledgement::getAckID() {80 unsigned int Acknowledgement::getAckID() { 73 81 return *(uint32_t *)(data_ + _ACKID); 74 82 } -
code/branches/PresentationFS18/src/libraries/network/packet/Acknowledgement.h
r11071 r12020 34 34 35 35 namespace orxonox { 36 36 37 const unsigned int ACKID_NACK = 0; 38 37 39 namespace packet { 38 /** 39 @author 40 */ 40 41 41 class _NetworkExport Acknowledgement : public Packet 42 42 { -
code/branches/PresentationFS18/src/libraries/network/packet/Chat.cc
r11071 r12020 38 38 #define PACKET_FLAGS_CHAT PacketFlag::Reliable 39 39 40 /* Some lengths */40 /* Some lengths / offsets */ 41 41 #define _PACKETID 0 42 42 #define _SOURCEID _PACKETID + sizeof(Type) … … 49 49 { 50 50 /* Add chat flag to packet flags */ 51 flags_ = flags_ |PACKET_FLAGS_CHAT;51 this->flags_ |= PACKET_FLAGS_CHAT; 52 52 53 53 /* set message length to length of input string + 1 */ 54 messageLength_ = message.length()+1;54 this->messageLength_ = message.length() + 1; 55 55 56 56 /* allocate memory for the data */ 57 data_=new unsigned char[ getSize() ];57 this->data_ = new unsigned char[ getSize() ]; 58 58 59 59 *(Type *)(data_ + _PACKETID ) = Type::Chat; 60 60 *(unsigned int *)(data_ + _SOURCEID ) = sourceID; 61 61 *(unsigned int *)(data_ + _TARGETID ) = targetID; 62 *(unsigned int *)(data_ + _MESSAGELENGTH ) = messageLength_;62 *(unsigned int *)(data_ + _MESSAGELENGTH ) = this->messageLength_; 63 63 64 64 /* cast the hell out of the message string, and copy it into the 65 65 * data buffer. 66 66 */ 67 memcpy( data_+_MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())),messageLength_ );67 memcpy( this->data_ + _MESSAGE, static_cast<void*>(const_cast<char*>(message.c_str())), this->messageLength_ ); 68 68 } 69 69 … … 71 71 : Packet(data, clientID) 72 72 { 73 messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH );73 this->messageLength_ = *(uint32_t *)(data + _MESSAGELENGTH ); 74 74 } 75 75 … … 79 79 80 80 unsigned int Chat::getSize() const{ 81 return _MESSAGE + messageLength_;81 return _MESSAGE + this->messageLength_; 82 82 } 83 83 84 84 bool Chat::process(orxonox::Host* host){ 85 host->doReceiveChat(std::string((const char*)data_+_MESSAGE), *(uint32_t *)(data_+_SOURCEID), *(uint32_t *)(data_+_TARGETID)); 85 host->doReceiveChat(std::string((const char*)this->data_ + _MESSAGE), 86 *(uint32_t *)(this->data_+_SOURCEID), 87 *(uint32_t *)(this->data_+_TARGETID)); 86 88 delete this; 87 89 return true; 88 90 } 89 91 90 unsigned char *Chat::getMessage() {91 return data_ + _MESSAGE;92 unsigned char *Chat::getMessage() { 93 return this->data_ + _MESSAGE; 92 94 } 93 95 -
code/branches/PresentationFS18/src/libraries/network/packet/Chat.h
r11071 r12020 55 55 56 56 /* Get the length of the message (not the full size of the packet) */ 57 unsigned int getMessageLength() { returnmessageLength_; };57 unsigned int getMessageLength() { return this->messageLength_; }; 58 58 59 59 /* return message content */ -
code/branches/PresentationFS18/src/libraries/network/packet/ClassID.cc
r11071 r12020 46 46 47 47 48 ClassID::ClassID( ) : Packet(){48 ClassID::ClassID() : Packet() { 49 49 Identifier *id; 50 unsigned int nrOfClasses =0;51 unsigned int packetSize =2*sizeof(uint32_t); //space for the packetID and for the nrofclasses50 unsigned int nrOfClasses = 0; 51 unsigned int packetSize = 2 * sizeof(uint32_t); //space for the packetID and for the nrofclasses 52 52 uint32_t network_id; 53 flags_ = flags_ |PACKET_FLAGS_CLASSID;53 this->flags_ |= PACKET_FLAGS_CLASSID; 54 54 std::queue<std::pair<uint32_t, std::string>> tempQueue; 55 55 56 // calculate total needed size (for all strings and integers)57 for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()) {56 // calculate total needed size (for all strings and integers) 57 for(const auto& mapEntry : IdentifierManager::getInstance().getIdentifierByStringMap()) { 58 58 id = mapEntry.second; 59 59 if(id == nullptr || !id->hasFactory()) … … 64 64 tempQueue.push( std::pair<unsigned int, std::string>(network_id, classname) ); 65 65 ++nrOfClasses; 66 packetSize += (classname.size() +1)+sizeof(network_id)+sizeof(uint32_t);66 packetSize += (classname.size() + 1) + sizeof(network_id) + sizeof(uint32_t); 67 67 } 68 68 69 this->data_ =new uint8_t[ packetSize ];69 this->data_ = new uint8_t[ packetSize ]; 70 70 //set the appropriate packet id 71 71 assert(this->data_); 72 72 *(Type *)(this->data_ + _PACKETID ) = Type::ClassID; 73 73 74 uint8_t *temp =data_+sizeof(uint32_t);74 uint8_t *temp = this->data_ + sizeof(uint32_t); 75 75 // save the number of all classes 76 *(uint32_t*) temp = nrOfClasses;76 *(uint32_t*) temp = nrOfClasses; 77 77 temp += sizeof(uint32_t); 78 78 79 79 // now save all classids and classnames 80 80 std::pair<uint32_t, std::string> tempPair; 81 uint32_t tempsize = 2 *sizeof(uint32_t); // packetid and nrOfClasses81 uint32_t tempsize = 2 * sizeof(uint32_t); // packetid and nrOfClasses 82 82 while( !tempQueue.empty() ){ 83 83 tempPair = tempQueue.front(); 84 84 tempQueue.pop(); 85 *(uint32_t*) temp = tempPair.first;86 *(uint32_t*) (temp+sizeof(uint32_t)) = tempPair.second.size()+1;87 memcpy(temp +2*sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size()+1);88 temp +=2*sizeof(uint32_t)+tempPair.second.size()+1;89 tempsize +=2*sizeof(uint32_t)+tempPair.second.size()+1;85 *(uint32_t*) temp = tempPair.first; 86 *(uint32_t*) (temp+sizeof(uint32_t)) = tempPair.second.size() + 1; 87 memcpy(temp + 2 * sizeof(uint32_t), tempPair.second.c_str(), tempPair.second.size() + 1); 88 temp += 2 * sizeof(uint32_t) + tempPair.second.size() + 1; 89 tempsize += 2 * sizeof(uint32_t) + tempPair.second.size() + 1; 90 90 } 91 assert(tempsize ==packetSize);91 assert(tempsize == packetSize); 92 92 93 93 orxout(verbose_more, context::packets) << "classid packetSize is " << packetSize << endl; … … 104 104 } 105 105 106 uint32_t ClassID::getSize() const {107 uint8_t *temp = data_+sizeof(uint32_t); // packet identification106 uint32_t ClassID::getSize() const { 107 uint8_t *temp = this->data_ + sizeof(uint32_t); // packet identification 108 108 uint32_t totalsize = sizeof(uint32_t); // packet identification 109 uint32_t nrOfClasses = *(uint32_t*) temp;109 uint32_t nrOfClasses = *(uint32_t*) temp; 110 110 temp += sizeof(uint32_t); 111 111 totalsize += sizeof(uint32_t); // storage size for nr of all classes 112 112 113 for(unsigned int i=0; i <nrOfClasses; i++){114 totalsize += 2 *sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));115 temp += 2 *sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t));113 for(unsigned int i=0; i < nrOfClasses; i++) { 114 totalsize += 2 * sizeof(uint32_t) + *(uint32_t*) (temp + sizeof(uint32_t)); 115 temp += 2 * sizeof(uint32_t) + *(uint32_t*)(temp + sizeof(uint32_t)); 116 116 } 117 117 return totalsize; 118 118 } 119 119 120 121 bool ClassID::process(orxonox::Host* host){ 120 bool ClassID::process(orxonox::Host* host) { 122 121 int nrOfClasses; 123 uint8_t *temp = data_+sizeof(uint32_t); //skip the packetid122 uint8_t *temp = this->data_ + sizeof(uint32_t); //skip the packetid 124 123 uint32_t networkID; 125 124 uint32_t stringsize; 126 125 unsigned char *classname; 127 128 126 129 127 //clear the map of network ids … … 134 132 Identifier *id; 135 133 // read the total number of classes 136 nrOfClasses = *(uint32_t*) temp;134 nrOfClasses = *(uint32_t*) temp; 137 135 temp += sizeof(uint32_t); 138 136 139 for( int i =0; i<nrOfClasses; i++){140 networkID = *(uint32_t*) temp;141 stringsize = *(uint32_t*) (temp+sizeof(uint32_t));142 classname = temp +2*sizeof(uint32_t);143 id =ClassByString( std::string((const char*)classname) );137 for( int i = 0; i < nrOfClasses; i++) { 138 networkID = *(uint32_t*) temp; 139 stringsize = *(uint32_t*) (temp + sizeof(uint32_t)); 140 classname = temp + 2 * sizeof(uint32_t); 141 id = ClassByString( std::string((const char*) classname) ); 144 142 orxout(internal_info, context::packets) << "processing classid: " << networkID << " name: " << classname << " id: " << id << endl; 145 if(id ==nullptr){143 if(id == nullptr) { 146 144 orxout(user_error, context::packets) << "Received a bad classname" << endl; 147 145 abort(); 148 146 } 149 147 id->setNetworkID( networkID ); 150 temp += 2 *sizeof(uint32_t) + stringsize;148 temp += 2 * sizeof(uint32_t) + stringsize; 151 149 } 152 150 delete this; -
code/branches/PresentationFS18/src/libraries/network/packet/DeleteObjects.cc
r11071 r12020 45 45 : Packet() 46 46 { 47 flags_ = flags_ |PACKET_FLAG_DELETE;47 this->flags_ |= PACKET_FLAG_DELETE; 48 48 } 49 49 … … 60 60 { 61 61 unsigned int number = Synchronisable::getNumberOfDeletedObject(); 62 if(number ==0)62 if(number == 0) 63 63 return false; 64 64 orxout(verbose, context::packets) << "sending DeleteObjects: "; … … 83 83 { 84 84 assert(data_); 85 return _OBJECTIDS + *(uint32_t*) (data_+_QUANTITY)*sizeof(uint32_t);85 return _OBJECTIDS + *(uint32_t*) (this->data_ + _QUANTITY) * sizeof(uint32_t); 86 86 } 87 87 88 88 bool DeleteObjects::process(orxonox::Host* host) 89 89 { 90 for(unsigned int i =0; i<*(unsigned int *)(data_+_QUANTITY); i++)90 for(unsigned int i = 0; i < *(unsigned int *) (this->data_+_QUANTITY); i++) 91 91 { 92 92 orxout(verbose, context::packets) << "deleting object with id: " << *(uint32_t*)(data_+_OBJECTIDS+i*sizeof(uint32_t)) << endl; 93 Synchronisable::deleteObject( *(uint32_t*)( data_+_OBJECTIDS+i*sizeof(uint32_t)) );93 Synchronisable::deleteObject( *(uint32_t*)(this->data_ + _OBJECTIDS + i * sizeof(uint32_t)) ); 94 94 } 95 95 delete this; -
code/branches/PresentationFS18/src/libraries/network/packet/FunctionCalls.cc
r11071 r12020 63 63 assert(isDataENetAllocated()); 64 64 65 uint8_t* temp = data_ +sizeof(uint32_t); //skip packetid66 uint32_t nrOfCalls = *(uint32_t*) temp;65 uint8_t* temp = data_ + sizeof(uint32_t); //skip packetid 66 uint32_t nrOfCalls = *(uint32_t*) temp; 67 67 temp += sizeof(uint32_t); 68 this->minGamestateID_ = *(uint32_t*) temp;68 this->minGamestateID_ = *(uint32_t*) temp; 69 69 temp += sizeof(uint32_t); 70 for( unsigned int i = 0; i <nrOfCalls; i++ )70 for( unsigned int i = 0; i < nrOfCalls; i++ ) 71 71 { 72 72 FunctionCall fctCall; … … 95 95 this->minGamestateID_ = host->getCurrentGamestateID(); 96 96 assert(this->functionCalls_.size()); 97 data_=new uint8_t[ currentSize_ ];98 *(Type *)( data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID99 *(uint32_t*)( data_+sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls100 *(uint32_t*)( data_+2*sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_101 uint8_t* temp = data_+3*sizeof(uint32_t);97 this->data_ = new uint8_t[ currentSize_ ]; 98 *(Type *)(this->data_ + _PACKETID ) = Type::FunctionCalls; // Set the Packet ID 99 *(uint32_t*)(this->data_ + sizeof(uint32_t)) = this->functionCalls_.size(); // set nrOfCalls 100 *(uint32_t*)(this->data_ + 2 * sizeof(uint32_t)) = this->minGamestateID_; // set minGamestateID_ 101 uint8_t* temp = this->data_ + 3 * sizeof(uint32_t); 102 102 103 103 while( this->functionCalls_.size() ) … … 107 107 } 108 108 109 assert( temp ==data_+currentSize_ );109 assert( temp == this->data_ + currentSize_ ); 110 110 111 111 Packet::send(host); -
code/branches/PresentationFS18/src/libraries/network/packet/Gamestate.cc
r11083 r12020 46 46 #define GAMESTATE_START(data) (data + GamestateHeader::getSize()) 47 47 48 // #define PACKET_FLAG_GAMESTATE PacketFlag::Reliable49 48 #define PACKET_FLAG_GAMESTATE 0 50 49 … … 102 101 } 103 102 104 103 //AV: This takes all synchronisables and packs it in a GameState, to be sent over the network 105 104 bool Gamestate::collectData(int id, uint8_t mode) 106 105 { … … 128 127 for(it = ObjectList<Synchronisable>().begin(); it; ++it) 129 128 { 130 131 // tempsize=it->getSize(id, mode);132 129 133 130 tempsize = it->getData(mem, this->sizes_, id, mode); … … 173 170 } 174 171 175 172 //AV: This takes the Gamestate received from the network and "unpacks" it back to a list of Objects/Synchronisables, thus updating the data 176 173 bool Gamestate::spreadData(uint8_t mode) 177 174 { … … 196 193 else 197 194 { 198 // orxout(verbose, context::packets) << "not creating object of classid " << objectheader.getClassID() << endl;199 195 mem += objectheader.getDataSize() + ( objectheader.isDiffed() ? SynchronisableHeaderLight::getSize() : SynchronisableHeader::getSize() ); 200 196 } … … 202 198 else 203 199 { 204 // orxout(verbose, context::packets) << "updating object of classid " << objectheader.getClassID() << endl;205 200 OrxVerify(s->updateData(mem, mode), "ERROR: could not update Synchronisable with Gamestate data"); 206 201 } 207 202 } 208 203 assert((uintptr_t)(mem-data_) == GamestateHeader::getSize()+header_.getDataSize()); 209 210 // In debug mode, check first, whether there are no duplicate objectIDs211 #ifndef NDEBUG212 if(this->getID()%1000==1)213 {214 std::list<uint32_t> v1;215 for (Synchronisable* synchronisable : ObjectList<Synchronisable>())216 {217 if (synchronisable->getObjectID() == OBJECTID_UNKNOWN)218 {219 if (synchronisable->objectMode_ != 0x0)220 {221 orxout(user_error, context::packets) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << endl;222 orxout(user_error, context::packets) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << endl;223 orxout(user_error, context::packets) << "Objects class: " << synchronisable->getIdentifier()->getName() << endl;224 assert(false);225 }226 }227 else228 {229 for (uint32_t id : v1)230 {231 if (synchronisable->getObjectID() == id)232 {233 orxout(user_error, context::packets) << "Found duplicate objectIDs on the client!" << endl234 << "Are you sure you don't create a Sychnronisable objcect with 'new' \235 that doesn't have objectMode = 0x0?" << endl;236 assert(false);237 }238 }239 v1.push_back(synchronisable->getObjectID());240 }241 }242 }243 #endif244 204 return true; 245 205 } … … 276 236 } 277 237 278 238 //AV: This function takes the Gamestate and compresses it for transmission over the network 279 239 bool Gamestate::compressData() 280 240 { … … 313 273 } 314 274 315 275 //AV: This function takes the compressed Gamestate received from the network and decompresses it for further unpacking 316 276 bool Gamestate::decompressData() 317 277 { … … 374 334 if( memcmp( origDataPtr+objectOffset, baseDataPtr+objectOffset, objectHeader.getDataSize()) == 0 ) 375 335 { 376 // orxout(verbose, context::packets) << "skip object " << Synchronisable::getSynchronisable(objectHeader.getObjectID())->getIdentifier()->getName() << endl;377 336 origDataPtr += objectOffset + objectHeader.getDataSize(); // skip the whole object 378 337 baseDataPtr += objectOffset + objectHeader.getDataSize(); … … 430 389 inline void /*Gamestate::*/copyObject( uint8_t*& newData, uint8_t*& origData, uint8_t*& baseData, SynchronisableHeader& objectHeader, std::vector<uint32_t>::iterator& sizes ) 431 390 { 432 // orxout(verbose, context::packets) << "docopy" << endl;433 391 // Just copy over the whole Object 434 392 memcpy( newData, origData, objectHeader.getDataSize()+SynchronisableHeader::getSize() ); … … 437 395 newData += objectHeader.getDataSize()+SynchronisableHeader::getSize(); 438 396 origData += objectHeader.getDataSize()+SynchronisableHeader::getSize(); 439 // SynchronisableHeader baseHeader( baseData ); 440 // baseData += baseHeader.getDataSize()+SynchronisableHeader::getSize(); 441 // orxout(verbose, context::packets) << "copy " << h.getObjectID() << endl; 442 // orxout(verbose, context::packets) << "copy " << h.getObjectID() << ":"; 397 443 398 sizes += Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); 444 // for( unsigned int i = 0; i < Synchronisable::getSynchronisable(objectHeader.getObjectID())->getNrOfVariables(); ++i ) 445 // { 446 // // orxout(verbose, context::packets) << " " << *sizes; 447 // ++sizes; 448 // } 449 // orxout(verbose, context::packets) << endl; 399 450 400 } 451 401 … … 501 451 uint8_t *origDataEnd = origDataPtr + header_.getDataSize(); 502 452 uint8_t *baseDataEnd = baseDataPtr + base->header_.getDataSize(); 503 // uint32_t origLength = header_.getDataSize(); 504 // uint32_t baseLength = base->header_.getDataSize(); 453 505 454 506 455 // Allocate new space for diffed gamestate … … 595 544 596 545 597 /*Gamestate* Gamestate::diffData(Gamestate *base)598 {599 assert(this && base); assert(data_ && base->data_);600 assert(!header_.isCompressed() && !base->header_.isCompressed());601 assert(!header_.isDiffed());602 603 uint8_t *basep = GAMESTATE_START(base->data_);604 uint8_t *gs = GAMESTATE_START(this->data_);605 uint32_t dest_length = header_.getDataSize();606 607 if(dest_length==0)608 return nullptr;609 610 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];611 uint8_t *dest = GAMESTATE_START(ndata);612 613 rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );614 #ifndef NDEBUG615 uint8_t *dest2 = new uint8_t[dest_length];616 rawDiff( dest2, dest, basep, header_.getDataSize(), base->header_.getDataSize() );617 assert( memcmp( dest2, gs, dest_length) == 0 );618 delete dest2;619 #endif620 621 Gamestate *g = new Gamestate(ndata, getClientID());622 assert(g->header_);623 *(g->header_) = *header_;624 g->header_.setDiffed( true );625 g->header_.setBaseID( base->getID() );626 g->flags_=flags_;627 g->packetDirection_ = packetDirection_;628 assert(g->isDiffed());629 assert(!g->isCompressed());630 return g;631 }632 633 634 Gamestate* Gamestate::undiff(Gamestate *base)635 {636 assert(this && base); assert(data_ && base->data_);637 assert(!header_.isCompressed() && !base->header_.isCompressed());638 assert(header_.isDiffed());639 640 uint8_t *basep = GAMESTATE_START(base->data_);641 uint8_t *gs = GAMESTATE_START(this->data_);642 uint32_t dest_length = header_.getDataSize();643 644 if(dest_length==0)645 return nullptr;646 647 uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()];648 uint8_t *dest = ndata + GamestateHeader::getSize();649 650 rawDiff( dest, gs, basep, header_.getDataSize(), base->header_.getDataSize() );651 652 Gamestate *g = new Gamestate(ndata, getClientID());653 assert(g->header_);654 *(g->header_) = *header_;655 g->header_.setDiffed( false );656 g->flags_=flags_;657 g->packetDirection_ = packetDirection_;658 assert(!g->isDiffed());659 assert(!g->isCompressed());660 return g;661 }662 663 664 void Gamestate::rawDiff( uint8_t* newdata, uint8_t* data, uint8_t* basedata, uint32_t datalength, uint32_t baselength)665 {666 uint64_t* gd = (uint64_t*)data;667 uint64_t* bd = (uint64_t*)basedata;668 uint64_t* nd = (uint64_t*)newdata;669 670 unsigned int i;671 for( i=0; i<datalength/8; i++ )672 {673 if( i<baselength/8 )674 *(nd+i) = *(gd+i) ^ *(bd+i); // xor the data675 else676 *(nd+i) = *(gd+i); // just copy over the data677 }678 unsigned int j;679 // now process the rest (when datalength isn't a multiple of 4)680 for( j = 8*(datalength/8); j<datalength; j++ )681 {682 if( j<baselength )683 *(newdata+j) = *(data+j) ^ *(basedata+j); // xor684 else685 *(newdata+j) = *(data+j); // just copy686 }687 assert(j==datalength);688 }*/689 690 691 /*Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){692 assert(data_);693 std::list<obj>::iterator it;694 695 // allocate memory for new data696 uint8_t *gdata = new uint8_t[header_.getDataSize()+GamestateHeader::getSize()];697 // create a gamestate out of it698 Gamestate *gs = new Gamestate(gdata);699 uint8_t *newdata = gdata + GamestateHeader::getSize();700 uint8_t *origdata = GAMESTATE_START(data_);701 702 //copy the GamestateHeader703 assert(gs->header_);704 *(gs->header_) = *header_;705 706 uint32_t objectOffset;707 unsigned int objectsize, destsize=0;708 // TODO: Why is this variable not used?709 //Synchronisable *object;710 711 //call TrafficControl712 TrafficControl::getInstance()->processObjectList( clientID, header_.getID(), dataVector_ );713 714 //copy in the zeros715 // std::list<obj>::iterator itt;716 // orxout() << "myvector contains:";717 // for ( itt=dataVector_.begin() ; itt!=dataVector_.end(); itt++ )718 // orxout() << " " << (*itt).objID;719 // orxout() << endl;720 for(it=dataVector_.begin(); it!=dataVector_.end();){721 SynchronisableHeader oldobjectheader(origdata);722 SynchronisableHeader newobjectheader(newdata);723 if ( (*it).objSize == 0 )724 {725 ++it;726 continue;727 }728 objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();729 objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader730 if ( (*it).objID == oldobjectheader.getObjectID() ){731 memcpy(newdata, origdata, objectsize);732 ++it;733 }else{734 newobjectheader = oldobjectheader;735 memset(newdata+objectOffset, 0, objectsize-objectOffset);736 }737 newdata += objectsize;738 origdata += objectsize;739 destsize += objectsize;740 }741 #ifndef NDEBUG742 uint32_t origsize = destsize;743 while ( origsize < header_.getDataSize() )744 {745 SynchronisableHeader oldobjectheader(origdata);746 objectsize = oldobjectheader.getDataSize()+SynchronisableHeader::getSize();747 origdata += objectsize;748 origsize += objectsize;749 }750 assert(origsize==header_.getDataSize());751 assert(destsize!=0);752 #endif753 gs->header_.setDataSize( destsize );754 return gs;755 }*/756 757 758 546 uint32_t Gamestate::calcGamestateSize(uint32_t id, uint8_t mode) 759 547 { -
code/branches/PresentationFS18/src/libraries/network/packet/Gamestate.h
r11071 r12020 57 57 GamestateHeader(uint8_t* data) 58 58 { assert(data); data_ = data; *(Type*)data_ = Type::Gamestate; } 59 /*GamestateHeader(uint8_t* data, GamestateHeader* h)60 { assert(data); data_=data; memcpy(data_, h->data_, getSize()); }*/61 59 void setData(uint8_t* data) 62 60 { assert(data); data_ = data; *(Type*)data_ = Type::Gamestate; } … … 127 125 inline uint32_t getDataSize() const { return header_.getDataSize(); } 128 126 Gamestate* diffVariables(Gamestate *base); 129 // Gamestate* diffData(Gamestate *base); 130 // Gamestate *undiff(Gamestate *base); 131 // Gamestate* doSelection(unsigned int clientID, unsigned int targetSize); 127 132 128 bool compressData(); 133 129 bool decompressData(); … … 136 132 // Packet functions 137 133 private: 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 ); 134 140 135 virtual uint32_t getSize() const override; 141 136 virtual bool process(orxonox::Host* host) override; 142 137 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 ); 145 138 146 139 std::list<obj> dataVector_; 147 140 GamestateHeader header_; -
code/branches/PresentationFS18/src/libraries/network/packet/Packet.cc
r11071 r12020 129 129 } 130 130 131 /** 132 * Send the Packet. 133 * @param host The host which sends the packet 134 */ 131 135 bool Packet::send(orxonox::Host* host) 132 136 { 137 // Deny sending incoming packets 133 138 if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional ) 134 139 { … … 136 141 return false; 137 142 } 143 138 144 if(!enetPacket_) 139 145 { 140 if(!data_){ 146 // Deny sending empty packets 147 if(!data_) { 141 148 assert(0); 142 149 return false; … … 152 159 // without having a reference in the packetMap_ 153 160 Packet::packetMapMutex_.lock(); 154 packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;161 Packet::packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this; 155 162 Packet::packetMapMutex_.unlock(); 156 163 } … … 173 180 } 174 181 #endif 175 // ENetPacket *temp = enetPacket_; 176 // enetPacket_ = nullptr; // otherwise we have a double free because enet already handles the deallocation of the packet 182 183 // Send via reliable or standard channel respectively 177 184 if( this->flags_ & PacketFlag::Reliable ) 178 185 host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT); 179 186 else 180 187 host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE); 188 181 189 return true; 182 190 } 183 191 192 /** 193 * Given an ENetPacket, create an Orxonox packet 194 * @param packet The ENetPacket 195 * @param peerID The sender 196 */ 184 197 Packet *Packet::createPacket(ENetPacket* packet, uint32_t peerID) 185 198 { 186 199 uint8_t *data = packet->data; 187 // assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer());188 // unsigned int peerID = ClientInformation::findClient(&peer->address)->getID();189 // HACK190 // if( peerID==static_cast<unsigned int>(-2))191 // peerID = NETWORK_PEER_ID_SERVER;192 200 Packet *p = nullptr; 193 // orxout(verbose_ultra, context::packets) << "packet type: " << *(Type *)&data[_PACKETID] << endl;194 201 switch( *(Type *)(data + _PACKETID) ) 195 202 { 196 203 case Type::Acknowledgement: 197 // orxout(verbose_more, context::packets) << "ack" << endl; 198 p = new Acknowledgement( data, peerID ); 204 p = new Acknowledgement( data, peerID ); 199 205 break; 200 206 case Type::Chat: 201 // orxout(verbose_more, context::packets) << "chat" << endl;202 207 p = new Chat( data, peerID ); 203 208 break; 204 209 case Type::ClassID: 205 // orxout(verbose_more, context::packets) << "classid" << endl;206 210 p = new ClassID( data, peerID ); 207 211 break; 208 212 case Type::Gamestate: 209 // orxout(verbose_more, context::packets) << "gamestate" << endl;210 213 p = new Gamestate( data, peerID ); 211 214 break; 212 215 case Type::Welcome: 213 // orxout(verbose_more, context::packets) << "welcome" << endl;214 216 p = new Welcome( data, peerID ); 215 217 break; 216 218 case Type::DeleteObjects: 217 // orxout(verbose_more, context::packets) << "deleteobjects" << endl;218 219 p = new DeleteObjects( data, peerID ); 219 220 break; 220 221 case Type::FunctionCalls: 221 // orxout(verbose_more, context::packets) << "functionCalls" << endl;222 222 p = new FunctionCalls( data, peerID ); 223 223 break; 224 224 case Type::FunctionIDs: 225 // orxout(verbose_more, context::packets) << "functionIDs" << endl;226 225 p = new FunctionIDs( data, peerID ); 227 226 break; … … 247 246 // Get our Packet from a global map with all Packets created in the send() method of Packet. 248 247 Packet::packetMapMutex_.lock(); 249 std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket)); 248 249 std::map<size_t, Packet*>::iterator it = Packet::packetMap_.find(reinterpret_cast<size_t>(enetPacket)); 250 250 assert(it != packetMap_.end()); 251 251 252 // Make sure we don't delete it again in the destructor 252 253 it->second->enetPacket_ = nullptr; 253 254 delete it->second; 254 255 packetMap_.erase(it); 256 255 257 Packet::packetMapMutex_.unlock(); 256 // orxout(verbose_ultra, context::packets) << "PacketMap size: " << packetMap_.size() << endl;257 258 } 258 259 -
code/branches/PresentationFS18/src/libraries/network/packet/Packet.h
r11071 r12020 68 68 69 69 virtual unsigned char* getData(){ return data_; }; 70 virtual unsigned int getSize() const =0; 71 virtual bool process(orxonox::Host* host)=0; 70 virtual unsigned int getSize() const = 0; 71 72 // Invoke some sort of action associated with the packet 73 virtual bool process(orxonox::Host* host) = 0; 74 72 75 inline uint32_t getFlags() 73 76 { return flags_; } … … 82 85 83 86 virtual bool send(orxonox::Host* host); 87 84 88 protected: 85 89 Packet(); 86 90 Packet(uint8_t *data, unsigned int peerID); 87 // Packet(ENetPacket *packet, ENetPeer *peer);88 91 inline bool isDataENetAllocated() const 89 92 { return bDataENetAllocated_; } … … 100 103 data_ might no correlate with enetPacket_->data. */ 101 104 bool bDataENetAllocated_; 105 102 106 private: 107 // All Packets are contained in this map 103 108 static std::map<size_t, Packet *> packetMap_; 104 109 static boost::mutex packetMapMutex_; -
code/branches/PresentationFS18/src/libraries/network/packet/ServerInformation.cc
r11083 r12020 50 50 // Save Server Round Trip Time 51 51 this->serverRTT_ = event->peer->roundTripTime; 52 52 53 // Save Server Address, leave some space for scope ID 53 54 enet_address_get_host_ip(&event->peer->address, serverIP, 64); 55 54 56 this->serverIP_ = std::string(serverIP); 55 57 // Save ACK … … 57 59 char* ack = nullptr; 58 60 loadAndIncrease((char*&)ack, temp); 59 60 /* Fabian, what is this used for? it crashes the masterserver, hence commenting it */61 // written by Oli: this is just to make sure that loadAndIncrease really writes the whole ACK string into char* ack62 // assert(strcmp(ack, (const char*)LAN_DISCOVERY_ACK)==0);63 64 61 // Save Server Name 65 62 loadAndIncrease(this->serverName_, temp); … … 74 71 void ServerInformation::send(ENetPeer* peer) 75 72 { 76 std::string payload = this->serverName_ + Ogre::StringConverter::toString(this->clientNumber_);73 std::string payload = this->serverName_ + std::to_string(this->clientNumber_); 77 74 uint32_t size = returnSize(LAN_DISCOVERY_ACK) + returnSize(payload); 78 75 uint8_t* temp = new uint8_t[size]; -
code/branches/PresentationFS18/src/libraries/network/synchronisable/Synchronisable.cc
r11103 r12020 43 43 std::queue<uint32_t> Synchronisable::deletedObjects_; 44 44 45 uint8_t Synchronisable::state_=0x1; // dete mines wheter we are server (default) or client45 uint8_t Synchronisable::state_=0x1; // determines wheter we are server (default) or client 46 46 47 47 RegisterAbstractClass(Synchronisable).inheritsFrom<OrxonoxInterface>(); … … 53 53 Synchronisable::Synchronisable(Context* context) 54 54 { 55 55 RegisterObject(Synchronisable); 56 56 static uint32_t idCounter=0; 57 57 objectMode_=0x1; // by default do not send data to server … … 179 179 assert( Synchronisable::objectMap_.find(header.getObjectID()) == Synchronisable::objectMap_.end() ); 180 180 no->setObjectID(header.getObjectID()); 181 //no->contextID=header.getContextID(); //TODO: remove this182 181 no->setClassID(header.getClassID()); 183 182 assert(no->contextID_ == header.getContextID()); 184 //assert(no->classID_ == header.getClassID());185 183 orxout(verbose, context::network) << "fabricate objectID_: " << no->objectID_ << " classID_: " << no->classID_ << endl; 186 184 // update data and create object/entity... … … 189 187 if (b) 190 188 { 191 // b = no->create();192 189 assert(b); 193 190 } … … 273 270 274 271 orxout(verbose_more, context::network) << "getting data from objectID_: " << objectID_ << ", classID_: " << classID_ << endl; 275 // orxout(verbose, context::network) << "objectid: " << this->objectID_ << ":";276 272 // copy to location 277 273 for(SynchronisableVariableBase* variable : syncList_) 278 274 { 279 275 uint32_t varsize = variable->getData( mem, mode ); 280 // orxout(verbose, context::network) << " " << varsize;281 276 tempsize += varsize; 282 277 sizes.push_back(varsize); 283 278 ++test; 284 //tempsize += (*i)->getSize( mode );285 279 } 286 280 assert(tempsize!=0); // if this happens an empty object (with no variables) would be transmitted 287 // orxout(verbose, context::network) << endl;288 281 289 282 header.setObjectID( this->objectID_ ); … … 340 333 } 341 334 342 //orxout(verbose_more, context::network) << "Synchronisable: objectID_ " << syncHeader.getObjectID() << ", classID_ " << syncHeader.getClassID() << " size: " << syncHeader.getDataSize() << " synchronising data" << endl;343 335 if( !syncHeaderLight.isDiffed() ) 344 336 { … … 357 349 { 358 350 mem += SynchronisableHeaderLight::getSize(); 359 // orxout(debug_output, context::network) << "objectID: " << this->objectID_ << endl;360 351 while( mem < data+syncHeaderLight.getDataSize()+SynchronisableHeaderLight::getSize() ) 361 352 { 362 353 VariableID varID = *(VariableID*)mem; 363 // orxout(debug_output, context::network) << "varID: " << varID << endl;364 354 assert( varID < syncList_.size() ); 365 355 mem += sizeof(VariableID); … … 400 390 bool Synchronisable::doSync(/*int32_t id,*/ uint8_t mode) 401 391 { 402 // if(mode==0x0)403 // mode=state_;404 392 assert(mode!=0x0); 405 393 return ( (this->objectMode_ & mode)!=0 && (!syncList_.empty() ) ); -
code/branches/PresentationFS18/src/libraries/network/synchronisable/Synchronisable.h
r11071 r12020 134 134 { memcpy(data_, h.data_, getSize()); } 135 135 }; 136 137 // inline void operator=(SynchronisableHeaderLight& h1, SynchronisableHeader& h2)138 // {139 // memcpy(h1.data_, h2.data_, h1.getSize());140 // }141 136 142 137 /** -
code/branches/PresentationFS18/src/modules/overlays/hud/HUDWeaponSystem.cc
r11071 r12020 126 126 destroyHUDChilds(); 127 127 128 // TODO: 129 // On network clients, owner_->getWeaponSystem() returns a nullpointer 130 // This causes the overlay to not be shown on clients. 128 131 if (owner_->getWeaponSystem()) 129 132 { -
code/branches/PresentationFS18/src/orxonox/graphics/Model.cc
r11783 r12020 104 104 registerVariable(this->meshSrc_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMesh)); 105 105 registerVariable(this->renderQueueGroup_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedRenderQueueGroup)); 106 registerVariable(this->materialName_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial)); 106 //Commented out, since this causes texture load failures on client side 107 //registerVariable(this->materialName_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedMaterial)); 107 108 registerVariable(this->bCastShadows_, VariableDirection::ToClient, new NetworkCallback<Model>(this, &Model::changedShadows)); 108 109 } … … 125 126 this->detachOgreObject(this->mesh_.getEntity()); 126 127 127 this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); 128 // Refuse to set empty mesh sources 129 if (this->meshSrc_ != "") 130 this->mesh_.setMeshSource(this->getScene()->getSceneManager(), this->meshSrc_); 128 131 129 132 if (this->mesh_.getEntity())
Note: See TracChangeset
for help on using the changeset viewer.