Changeset 7801 for code/trunk/src/libraries/network/packet/Packet.cc
- Timestamp:
- Dec 22, 2010, 7:24:24 PM (13 years ago)
- Location:
- code/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk
- Property svn:mergeinfo changed
-
code/trunk/src/libraries/network/packet/Packet.cc
r7163 r7801 35 35 #include <enet/enet.h> 36 36 #include <boost/static_assert.hpp> 37 #include <boost/thread/mutex.hpp> 37 38 38 39 #include "util/Debug.h" … … 53 54 54 55 // Make sure we assume the right values 55 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable) == static_cast<int>(ENET_PACKET_FLAG_RELIABLE));56 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequence ) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));57 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate) == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE));56 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable) == static_cast<int>(ENET_PACKET_FLAG_RELIABLE)); 57 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequenced) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED)); 58 BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate) == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE)); 58 59 59 60 #define PACKET_FLAG_DEFAULT PacketFlag::NoAllocate … … 61 62 62 63 std::map<size_t, Packet *> Packet::packetMap_; 64 boost::mutex Packet::packetMapMutex_; 63 65 64 66 Packet::Packet() … … 66 68 flags_ = PACKET_FLAG_DEFAULT; 67 69 packetDirection_ = Direction::Outgoing; 68 clientID_=0;70 peerID_=0; 69 71 data_=0; 70 72 enetPacket_=0; … … 72 74 } 73 75 74 Packet::Packet(uint8_t *data, unsigned int clientID)76 Packet::Packet(uint8_t *data, unsigned int peerID) 75 77 { 76 78 flags_ = PACKET_FLAG_DEFAULT; 77 79 packetDirection_ = Direction::Incoming; 78 clientID_=clientID;80 peerID_=peerID; 79 81 data_=data; 80 82 enetPacket_=0; … … 87 89 flags_=p.flags_; 88 90 packetDirection_ = p.packetDirection_; 89 clientID_ = p.clientID_;91 peerID_ = p.peerID_; 90 92 if(p.data_){ 91 93 data_ = new uint8_t[p.getSize()]; … … 123 125 } 124 126 125 bool Packet::send( ){127 bool Packet::send(orxonox::Host* host){ 126 128 if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional ){ 127 129 assert(0); … … 142 144 // Assures we don't create a packet and destroy it right after in another thread 143 145 // without having a reference in the packetMap_ 146 Packet::packetMapMutex_.lock(); 144 147 packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this; 148 Packet::packetMapMutex_.unlock(); 145 149 } 146 150 } … … 164 168 // ENetPacket *temp = enetPacket_; 165 169 // enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet 166 if(!Host::addPacket( enetPacket_, clientID_)) 167 enet_packet_destroy(this->enetPacket_); // if we could not add the packet to the enet queue delete it manually 170 if( this->flags_ & PacketFlag::Reliable ) 171 host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT); 172 else 173 host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE); 168 174 return true; 169 175 } … … 172 178 uint8_t *data = packet->data; 173 179 assert(ClientInformation::findClient(&peer->address)->getID() != static_cast<unsigned int>(-2) || !Host::isServer()); 174 unsigned int clientID = ClientInformation::findClient(&peer->address)->getID(); 180 unsigned int peerID = ClientInformation::findClient(&peer->address)->getID(); 181 // HACK 182 if( peerID==static_cast<unsigned int>(-2)) 183 peerID = NETWORK_PEER_ID_SERVER; 175 184 Packet *p = 0; 176 COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl;185 // COUT(6) << "packet type: " << *(Type::Value *)&data[_PACKETID] << std::endl; 177 186 switch( *(Type::Value *)(data + _PACKETID) ) 178 187 { 179 188 case Type::Acknowledgement: 180 COUT(5) << "ack" << std::endl;181 p = new Acknowledgement( data, clientID );189 // COUT(5) << "ack" << std::endl; 190 p = new Acknowledgement( data, peerID ); 182 191 break; 183 192 case Type::Chat: 184 COUT(5) << "chat" << std::endl;185 p = new Chat( data, clientID );193 // COUT(5) << "chat" << std::endl; 194 p = new Chat( data, peerID ); 186 195 break; 187 196 case Type::ClassID: 188 COUT(5) << "classid" << std::endl;189 p = new ClassID( data, clientID );197 // COUT(5) << "classid" << std::endl; 198 p = new ClassID( data, peerID ); 190 199 break; 191 200 case Type::Gamestate: 192 COUT(5) << "gamestate" << std::endl; 193 // TODO: remove brackets 194 p = new Gamestate( data, clientID ); 201 // COUT(5) << "gamestate" << std::endl; 202 p = new Gamestate( data, peerID ); 195 203 break; 196 204 case Type::Welcome: 197 COUT(5) << "welcome" << std::endl;198 p = new Welcome( data, clientID );205 // COUT(5) << "welcome" << std::endl; 206 p = new Welcome( data, peerID ); 199 207 break; 200 208 case Type::DeleteObjects: 201 COUT(5) << "deleteobjects" << std::endl;202 p = new DeleteObjects( data, clientID );209 // COUT(5) << "deleteobjects" << std::endl; 210 p = new DeleteObjects( data, peerID ); 203 211 break; 204 212 case Type::FunctionCalls: 205 COUT(5) << "functionCalls" << std::endl;206 p = new FunctionCalls( data, clientID );213 // COUT(5) << "functionCalls" << std::endl; 214 p = new FunctionCalls( data, peerID ); 207 215 break; 208 216 case Type::FunctionIDs: 209 COUT(5) << "functionIDs" << std::endl;210 p = new FunctionIDs( data, clientID );217 // COUT(5) << "functionIDs" << std::endl; 218 p = new FunctionIDs( data, peerID ); 211 219 break; 212 220 default: 213 assert(0); //TODO: repair this221 assert(0); 214 222 break; 215 223 } … … 229 237 void Packet::deletePacket(ENetPacket *enetPacket){ 230 238 // Get our Packet from a global map with all Packets created in the send() method of Packet. 239 Packet::packetMapMutex_.lock(); 231 240 std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket)); 232 241 assert(it != packetMap_.end()); … … 235 244 delete it->second; 236 245 packetMap_.erase(it); 237 COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl; 246 Packet::packetMapMutex_.unlock(); 247 // COUT(6) << "PacketMap size: " << packetMap_.size() << std::endl; 238 248 } 239 249
Note: See TracChangeset
for help on using the changeset viewer.