Changeset 7772
- Timestamp:
- Dec 17, 2010, 10:41:24 AM (14 years ago)
- Location:
- code/branches/network5/src/libraries/network
- Files:
-
- 15 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network5/src/libraries/network/Client.cc
r7759 r7772 114 114 } 115 115 116 bool Client::queuePacket(ENetPacket *packet, int clientID) 117 { 118 bool b = ClientConnection::addPacket(packet); 119 assert(b); 120 return b; 116 void Client::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) 117 { 118 ClientConnection::addPacket(packet, channelID); 121 119 } 122 120 … … 170 168 } 171 169 } 172 sendPackets(); // flush the enet queue170 // sendPackets(); // flush the enet queue 173 171 174 172 Connection::processQueue(); … … 180 178 } 181 179 gamestate->cleanup(); 182 Connection::sendPackets();180 // Connection::sendPackets(); 183 181 184 182 return; -
code/branches/network5/src/libraries/network/Client.h
r7163 r7772 78 78 void setDestination( const std::string& serverAddress, unsigned int port ); // tolua_export 79 79 bool closeConnection(); 80 bool queuePacket(ENetPacket *packet, int clientID);80 void queuePacket(ENetPacket* packet, int clientID, uint8_t channelID); 81 81 bool processChat(const std::string& message, unsigned int playerID); 82 82 virtual bool chat(const std::string& message); -
code/branches/network5/src/libraries/network/ClientConnection.cc
r7459 r7772 99 99 { 100 100 this->established_=true; 101 Connection::startCommunicationThread(); 101 102 return true; 102 103 } … … 112 113 return true; 113 114 this->established_ = false; 115 Connection::stopCommunicationThread(); 114 116 enet_peer_disconnect(this->server_, 0); 115 117 for( unsigned int i=0; i<NETWORK_CLIENT_CONNECTION_TIMEOUT/NETWORK_CLIENT_WAIT_TIME; i++) … … 138 140 139 141 140 bool ClientConnection::addPacket(ENetPacket *packet) {142 void ClientConnection::addPacket(ENetPacket *packet, uint8_t channelID) { 141 143 assert( this->server_ ); 142 144 assert( packet ); 143 return Connection::addPacket( packet, this->server_ );145 return Connection::addPacket( packet, this->server_, channelID ); 144 146 } 145 147 -
code/branches/network5/src/libraries/network/ClientConnection.h
r6417 r7772 51 51 virtual bool closeConnection(); 52 52 // add a packet to queue for the server 53 bool addPacket(ENetPacket *packet);53 void addPacket(ENetPacket *packet, uint8_t channelID); 54 54 inline bool isConnected(){ return this->established_; } 55 55 protected: -
code/branches/network5/src/libraries/network/Connection.cc
r7163 r7772 30 30 31 31 #include <cassert> 32 #include <deque> 32 33 #define WIN32_LEAN_AND_MEAN 33 34 #include <enet/enet.h> 35 #include <boost/thread.hpp> 36 #include <boost/thread/mutex.hpp> 37 #include <boost/date_time.hpp> 38 34 39 #include "packet/Packet.h" 35 40 36 41 namespace orxonox 37 42 { 38 // Connection *Connection::instance_=0;43 const boost::posix_time::millisec NETWORK_COMMUNICATION_THREAD_WAIT_TIME(20); 39 44 40 45 Connection::Connection(): 41 host_(0) 42 { 43 // assert(instance_==0); 44 // Connection::instance_=this; 46 host_(0), bCommunicationThreadRunning_(false) 47 { 45 48 enet_initialize(); 46 49 atexit(enet_deinitialize); 47 } 48 49 Connection::~Connection(){ 50 // Connection::instance_=0; 51 } 52 53 int Connection::service(ENetEvent* event) { 54 return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT ); 55 } 56 57 void Connection::disconnectPeer(ENetPeer *peer) { 58 enet_peer_disconnect(peer, 0); 59 } 60 61 bool Connection::addPacket(ENetPacket *packet, ENetPeer *peer) { 62 if(enet_peer_send(peer, NETWORK_DEFAULT_CHANNEL, packet)!=0) 63 return false; 64 else 65 return true; 66 } 67 68 bool Connection::sendPackets() { 69 if ( /*!Connection::instance_ || */this->host_==NULL ) 70 return false; 71 enet_host_flush(this->host_); 72 return true; 73 } 74 75 void Connection::processQueue() { 50 this->incomingEventsMutex_ = new boost::mutex; 51 this->outgoingEventsMutex_ = new boost::mutex; 52 } 53 54 Connection::~Connection() 55 { 56 delete this->incomingEventsMutex_; 57 delete this->outgoingEventsMutex_; 58 } 59 60 void Connection::startCommunicationThread() 61 { 62 this->bCommunicationThreadRunning_ = true; 63 this->communicationThread_ = new boost::thread(&Connection::communicationThread, this); 64 } 65 66 void Connection::stopCommunicationThread() 67 { 68 this->bCommunicationThreadRunning_ = false; 69 if( !this->communicationThread_->timed_join(NETWORK_COMMUNICATION_THREAD_WAIT_TIME) ) 70 { 71 // force thread to stop 72 this->communicationThread_->interrupt(); 73 } 74 delete this->communicationThread_; 75 } 76 77 78 // int Connection::service(ENetEvent* event) { 79 // return enet_host_service( this->host_, event, NETWORK_WAIT_TIMEOUT ); 80 // } 81 82 void Connection::disconnectPeer(ENetPeer *peer) 83 { 84 outgoingEvent outEvent = { peer, outgoingEventType::disconnectPeer, (ENetPacket*)10, 15 }; 85 86 this->outgoingEventsMutex_->lock(); 87 this->outgoingEvents_.push_back(outEvent); 88 this->outgoingEventsMutex_->unlock(); 89 } 90 91 void Connection::addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID) 92 { 93 outgoingEvent outEvent = { peer, outgoingEventType::sendPacket, packet, channelID }; 94 95 this->outgoingEventsMutex_->lock(); 96 this->outgoingEvents_.push_back(outEvent); 97 this->outgoingEventsMutex_->unlock(); 98 } 99 100 void Connection::broadcastPacket(ENetPacket* packet, uint8_t channelID) 101 { 102 outgoingEvent outEvent = { 0, outgoingEventType::broadcastPacket, packet, channelID }; 103 104 this->outgoingEventsMutex_->lock(); 105 this->outgoingEvents_.push_back(outEvent); 106 this->outgoingEventsMutex_->unlock(); 107 } 108 109 110 void Connection::communicationThread() 111 { 112 COUT(0) << "starting communication thread" << endl; 76 113 ENetEvent event; 77 78 assert(this->host_); 79 80 while( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 ) 114 115 while( bCommunicationThreadRunning_ ) 81 116 { 82 switch(event.type){ 117 // Receive all pending incoming Events (such as packets, connects and disconnects) 118 while( enet_host_check_events( this->host_, &event ) > 0 ) 119 { 120 // COUT(0) << "incoming event" << endl; 121 // received an event 122 this->incomingEventsMutex_->lock(); 123 this->incomingEvents_.push_back(event); 124 this->incomingEventsMutex_->unlock(); 125 } 126 127 // Send all waiting outgoing packets 128 this->outgoingEventsMutex_->lock(); 129 uint32_t outgoingEventsCount = this->outgoingEvents_.size(); 130 this->outgoingEventsMutex_->unlock(); 131 while( outgoingEventsCount > 0 ) 132 { 133 // COUT(0) << "outgoing event" << endl; 134 this->outgoingEventsMutex_->lock(); 135 outgoingEvent outEvent = this->outgoingEvents_.front(); 136 this->outgoingEvents_.pop_front(); 137 this->outgoingEventsMutex_->unlock(); 138 139 switch( outEvent.type ) 140 { 141 case outgoingEventType::sendPacket: 142 enet_peer_send( outEvent.peer, outEvent.channelID, outEvent.packet ); 143 break; 144 case outgoingEventType::disconnectPeer: 145 enet_peer_disconnect(outEvent.peer, 0); 146 break; 147 case outgoingEventType::broadcastPacket: 148 enet_host_broadcast( this->host_, outEvent.channelID, outEvent.packet ); 149 break; 150 default: 151 assert(0); 152 } 153 this->outgoingEventsMutex_->lock(); 154 outgoingEventsCount = this->outgoingEvents_.size(); 155 this->outgoingEventsMutex_->unlock(); 156 } 157 158 // Wait for incoming events (at most NETWORK_WAIT_TIMEOUT ms) 159 if( enet_host_service( this->host_, &event, NETWORK_WAIT_TIMEOUT ) > 0 ) 160 { 161 // COUT(0) << "incoming event after wait" << endl; 162 //received an event 163 this->incomingEventsMutex_->lock(); 164 this->incomingEvents_.push_back(event); 165 this->incomingEventsMutex_->unlock(); 166 } 167 } 168 } 169 170 void Connection::processQueue() 171 { 172 ENetEvent event; 173 174 this->incomingEventsMutex_->lock(); 175 uint32_t incomingEventsCount = this->incomingEvents_.size(); 176 this->incomingEventsMutex_->unlock(); 177 while( incomingEventsCount > 0 ) 178 { 179 this->incomingEventsMutex_->lock(); 180 event = this->incomingEvents_.front(); 181 this->incomingEvents_.pop_front(); 182 this->incomingEventsMutex_->unlock(); 183 184 switch(event.type) 185 { 83 186 // log handling ================ 84 187 case ENET_EVENT_TYPE_CONNECT: … … 89 192 break; 90 193 case ENET_EVENT_TYPE_RECEIVE: 194 // COUT(0) << "ENET_EVENT_TYPE_RECEIVE" << endl; 91 195 processPacket( &event ); 92 196 break; … … 94 198 break; 95 199 } 200 201 this->incomingEventsMutex_->lock(); 202 incomingEventsCount = this->incomingEvents_.size(); 203 this->incomingEventsMutex_->unlock(); 96 204 } 97 205 } 98 206 99 bool Connection::processPacket(ENetEvent* event) { 207 bool Connection::processPacket(ENetEvent* event) 208 { 100 209 packet::Packet *p = packet::Packet::createPacket(event->packet, event->peer); 101 210 return p->process(); -
code/branches/network5/src/libraries/network/Connection.h
r7163 r7772 43 43 #include "NetworkPrereqs.h" 44 44 45 #include <deque> 46 47 namespace boost 48 { 49 class thread; 50 class mutex; 51 } 52 45 53 namespace orxonox 46 54 { 47 const unsigned int NETWORK_PORT = 55556; 48 const unsigned int NETWORK_MAX_CONNECTIONS = 50; 49 const unsigned int NETWORK_WAIT_TIMEOUT = 0; 50 const unsigned int NETWORK_DEFAULT_CHANNEL = 0; 51 const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5; 52 53 class _NetworkExport Connection{ 55 const unsigned int NETWORK_PORT = 55556; 56 const unsigned int NETWORK_MAX_CONNECTIONS = 50; 57 const unsigned int NETWORK_WAIT_TIMEOUT = 1; 58 const unsigned int NETWORK_MAX_QUEUE_PROCESS_TIME = 5; 59 60 namespace outgoingEventType 61 { 62 enum Value 63 { 64 sendPacket = 1, 65 disconnectPeer = 2, 66 broadcastPacket = 3 67 }; 68 69 } 70 71 struct _NetworkExport outgoingEvent 72 { 73 ENetPeer* peer; 74 outgoingEventType::Value type; 75 ENetPacket* packet; 76 ENetChannelID channelID; 77 }; 78 79 class _NetworkExport Connection 80 { 54 81 public: 55 82 virtual ~Connection(); 56 83 57 static bool addPacket(ENetPacket *packet, ENetPeer *peer);58 bool sendPackets();84 void addPacket(ENetPacket *packet, ENetPeer *peer, uint8_t channelID); 85 void broadcastPacket(ENetPacket* packet, uint8_t channelID); 59 86 ENetHost* getHost(){ return this->host_; } 60 87 … … 63 90 // static Connection* getInstance(){ return Connection::instance_; } 64 91 65 int service(ENetEvent* event); 92 // int service(ENetEvent* event); 93 void startCommunicationThread(); 94 void stopCommunicationThread(); 95 void communicationThread(); 66 96 virtual void disconnectPeer(ENetPeer *peer); 67 97 … … 71 101 virtual bool processPacket(ENetEvent* event); 72 102 73 ENetHost *host_; 103 ENetHost* host_; 104 boost::mutex* incomingEventsMutex_; 105 boost::mutex* outgoingEventsMutex_; 74 106 private: 75 ENetAddress *bindAddress_; 107 boost::thread* communicationThread_; 108 bool bCommunicationThreadRunning_; 109 ENetAddress* bindAddress_; 110 std::deque<ENetEvent> incomingEvents_; 111 std::deque<outgoingEvent> outgoingEvents_; 76 112 77 113 // static Connection *instance_; -
code/branches/network5/src/libraries/network/Host.cc
r7284 r7772 76 76 * @return success? 77 77 */ 78 bool Host::addPacket(ENetPacket *packet, int clientID)78 void Host::addPacket(ENetPacket *packet, int clientID, uint8_t channelID) 79 79 { 80 bool result = true;81 80 for( std::vector<Host*>::iterator it = instances_s.begin(); it!=instances_s.end(); ++it ) 82 81 { 83 82 if( (*it)->isActive() ) 84 83 { 85 if( !(*it)->queuePacket(packet, clientID) ) 86 result = false; 84 (*it)->queuePacket(packet, clientID, channelID); 87 85 } 88 86 } 89 return result;90 87 } 91 88 -
code/branches/network5/src/libraries/network/Host.h
r7284 r7772 54 54 //virtual bool processChat(packet::Chat *message, unsigned int clientID)=0; 55 55 //virtual bool sendChat(packet::Chat *chat)=0; 56 virtual bool queuePacket(ENetPacket *packet, int clientID)=0;56 virtual void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID)=0; 57 57 virtual bool chat(const std::string& message)=0; 58 58 virtual bool broadcast(const std::string& message)=0; … … 71 71 // static Host* getInstance(){ return instance_; } 72 72 static bool running(){ return instances_s.size(); } 73 static bool addPacket(ENetPacket *packet, int clientID=0);73 static void addPacket(ENetPacket* packet, int clientID = NETWORK_PEER_ID_SERVER, uint8_t channelID = 0); 74 74 //static bool chat(std::string& message); 75 75 // static bool receiveChat(packet::Chat *message, unsigned int clientID); -
code/branches/network5/src/libraries/network/NetworkPrereqs.h
r7759 r7772 64 64 namespace orxonox 65 65 { 66 static const unsigned int GAMESTATEID_INITIAL = static_cast<unsigned int>(-1);67 static const unsigned int CLIENTID_UNKNOWN = static_cast<unsigned int>(-2);66 static const unsigned int GAMESTATEID_INITIAL = static_cast<unsigned int>(-1); 67 static const unsigned int CLIENTID_UNKNOWN = static_cast<unsigned int>(-2); 68 68 extern const char* LAN_DISCOVERY_MESSAGE; 69 69 extern const char* LAN_DISCOVERY_ACK; 70 static const unsigned int LAN_DISCOVERY_PORT = 55557; 71 static const unsigned int NETWORK_PEER_ID_SERVER = 0; 70 static const unsigned int LAN_DISCOVERY_PORT = 55557; 71 static const unsigned int NETWORK_PEER_ID_SERVER = 0; 72 static const unsigned int NETWORK_CHANNEL_DEFAULT = 0; 73 static const unsigned int NETWORK_CHANNEL_RELIABLE = 1; 72 74 } 73 75 … … 98 100 // from ENet 99 101 struct _ENetPeer; 100 typedef _ENetPeer ENetPeer;102 typedef _ENetPeer ENetPeer; 101 103 struct _ENetPacket; 102 typedef _ENetPacket ENetPacket;104 typedef _ENetPacket ENetPacket; 103 105 struct _ENetEvent; 104 typedef _ENetEvent ENetEvent;106 typedef _ENetEvent ENetEvent; 105 107 struct _ENetHost; 106 typedef _ENetHost ENetHost;108 typedef _ENetHost ENetHost; 107 109 struct _ENetAddress; 108 typedef _ENetAddress ENetAddress; 110 typedef _ENetAddress ENetAddress; 111 typedef uint8_t ENetChannelID; 109 112 110 113 namespace orxonox … … 162 165 } 163 166 167 namespace boost 168 { 169 class mutex; 170 class thread; 171 } 172 164 173 #endif /* _NetworkPrereqs_H__ */ -
code/branches/network5/src/libraries/network/Server.cc
r7759 r7772 229 229 updateGamestate(); 230 230 } 231 sendPackets(); // flush the enet queue232 } 233 } 234 235 bool Server::queuePacket(ENetPacket *packet, int clientID)236 { 237 return ServerConnection::addPacket(packet, clientID);231 // sendPackets(); // flush the enet queue 232 } 233 } 234 235 void Server::queuePacket(ENetPacket *packet, int clientID, uint8_t channelID) 236 { 237 ServerConnection::addPacket(packet, clientID, channelID); 238 238 } 239 239 -
code/branches/network5/src/libraries/network/Server.h
r7739 r7772 64 64 void close(); 65 65 bool processChat(const std::string& message, unsigned int playerID); 66 bool queuePacket(ENetPacket *packet, int clientID);66 void queuePacket(ENetPacket *packet, int clientID, uint8_t channelID); 67 67 void update(const Clock& time); 68 68 unsigned int getRTT(unsigned int clientID); -
code/branches/network5/src/libraries/network/ServerConnection.cc
r7459 r7772 49 49 } 50 50 51 ServerConnection::~ServerConnection(){ 51 ServerConnection::~ServerConnection() 52 { 52 53 if ( this->bListening_ ) 53 54 closeListener(); … … 55 56 } 56 57 57 void ServerConnection::setBindAddress( const std::string& bindAddress ) { 58 void ServerConnection::setBindAddress( const std::string& bindAddress ) 59 { 58 60 if (enet_address_set_host (this->bindAddress_, bindAddress.c_str()) < 0) 59 61 COUT(1) << "Error: Could not resolve \"" << bindAddress << "\"." << std::endl; … … 64 66 } 65 67 66 bool ServerConnection::openListener() { 68 bool ServerConnection::openListener() 69 { 67 70 this->host_ = enet_host_create(this->bindAddress_, NETWORK_MAX_CONNECTIONS, 0, 0, 0); 68 71 if ( this->host_ == NULL ) … … 78 81 else 79 82 COUT(3) << "Info: Using IPv4 and IPv6 Sockets." << std::endl; 83 84 Connection::startCommunicationThread(); 80 85 81 86 return true; 82 87 } 83 88 84 bool ServerConnection::closeListener() { 89 bool ServerConnection::closeListener() 90 { 85 91 this->bListening_=false; 86 92 disconnectClients(); … … 89 95 } 90 96 91 bool ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID) { 97 void ServerConnection::addPacket(ENetPacket *packet, unsigned int clientID, uint8_t channelID) 98 { 92 99 if ( clientID == CLIENTID_UNKNOWN ) 93 100 { 94 return addPacketAll(packet);101 broadcastPacket(packet, channelID); 95 102 } 96 103 else … … 99 106 if(!temp){ 100 107 COUT(3) << "C.Man: addPacket findClient failed" << std::endl; 101 return false;102 108 } 103 return Connection::addPacket(packet, temp->getPeer());109 Connection::addPacket(packet, temp->getPeer(), channelID); 104 110 } 105 }106 107 bool ServerConnection::addPacketAll(ENetPacket *packet) {108 // if ( !Connection::getInstance() )109 // return false;110 enet_host_broadcast( Connection::getHost(), 0, packet);111 return true;112 111 } 113 112 … … 117 116 } 118 117 119 void ServerConnection::disconnectClient(int clientID){ 118 void ServerConnection::disconnectClient(int clientID) 119 { 120 120 ClientInformation *client = ClientInformation::findClient(clientID); 121 121 if(client) … … 123 123 } 124 124 125 void ServerConnection::disconnectClients() {126 ENetEvent event;125 void ServerConnection::disconnectClients() 126 { 127 127 ClientInformation *temp = ClientInformation::getBegin(); 128 while(temp!=0){ 128 while(temp!=0) 129 { 129 130 ServerConnection::disconnectClient( temp ); 130 131 temp = temp->next(); 131 }132 temp = ClientInformation::getBegin();133 while( temp!=0 ){134 if( service( &event ) )135 {136 switch (event.type)137 {138 case ENET_EVENT_TYPE_NONE: break;139 case ENET_EVENT_TYPE_CONNECT: break;140 case ENET_EVENT_TYPE_RECEIVE:141 enet_packet_destroy(event.packet);142 break;143 case ENET_EVENT_TYPE_DISCONNECT:144 removePeer( &event );145 temp = ClientInformation::getBegin();146 break;147 }148 }149 132 } 150 133 return; … … 152 135 153 136 154 int ServerConnection::getClientID(ENetPeer* peer) { 137 int ServerConnection::getClientID(ENetPeer* peer) 138 { 155 139 return getClientID(&(peer->address)); 156 140 } 157 141 158 int ServerConnection::getClientID(ENetAddress* address) { 142 int ServerConnection::getClientID(ENetAddress* address) 143 { 159 144 return ClientInformation::findClient(address)->getID(); 160 145 } 161 146 162 ENetPeer *ServerConnection::getClientPeer(int clientID) { 147 ENetPeer *ServerConnection::getClientPeer(int clientID) 148 { 163 149 return ClientInformation::findClient(clientID)->getPeer(); 164 150 } -
code/branches/network5/src/libraries/network/ServerConnection.h
r7163 r7772 56 56 bool openListener(); 57 57 bool closeListener(); 58 bool addPacket(ENetPacket *packet, unsigned int ID); 59 bool addPacketAll(ENetPacket *packet); 58 void addPacket(ENetPacket *packet, unsigned int ID, uint8_t channelID); 60 59 virtual void disconnectClient(ClientInformation *client); 61 60 void disconnectClient(int clientID); -
code/branches/network5/src/libraries/network/packet/Packet.cc
r7759 r7772 35 35 #include <enet/enet.h> 36 36 #include <boost/static_assert.hpp> 37 #include <boost/thread/mutex.hpp> 37 38 38 39 #include "util/Debug.h" … … 61 62 62 63 std::map<size_t, Packet *> Packet::packetMap_; 64 boost::mutex Packet::packetMapMutex_; 63 65 64 66 Packet::Packet() … … 142 144 // Assures we don't create a packet and destroy it right after in another thread 143 145 // without having a reference in the packetMap_ 146 Packet::packetMapMutex_.lock(); 144 147 packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this; 148 Packet::packetMapMutex_.unlock(); 145 149 } 146 150 } … … 164 168 // ENetPacket *temp = enetPacket_; 165 169 // enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet 166 if(!Host::addPacket( enetPacket_, clientID_)) 167 enet_packet_destroy(this->enetPacket_); // if we could not add the packet to the enet queue delete it manually 170 if( this->flags_ & PacketFlag::Reliable ) 171 Host::addPacket( enetPacket_, clientID_, 0); 172 else 173 Host::addPacket( enetPacket_, clientID_, 0); 168 174 return true; 169 175 } … … 228 234 void Packet::deletePacket(ENetPacket *enetPacket){ 229 235 // Get our Packet from a global map with all Packets created in the send() method of Packet. 236 Packet::packetMapMutex_.lock(); 230 237 std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket)); 231 238 assert(it != packetMap_.end()); … … 234 241 delete it->second; 235 242 packetMap_.erase(it); 243 Packet::packetMapMutex_.unlock(); 236 244 // COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl; 237 245 } -
code/branches/network5/src/libraries/network/packet/Packet.h
r7490 r7772 96 96 private: 97 97 static std::map<size_t, Packet *> packetMap_; 98 static boost::mutex packetMapMutex_; 98 99 ENetPacket *enetPacket_; 99 100 };
Note: See TracChangeset
for help on using the changeset viewer.