Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Dec 22, 2010, 7:24:24 PM (13 years ago)
Author:
dafrick
Message:

Merging presentation2 branch back to trunk.

Location:
code/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/trunk

  • code/trunk/src/libraries/network/packet/Packet.cc

    r7163 r7801  
    3535#include <enet/enet.h>
    3636#include <boost/static_assert.hpp>
     37#include <boost/thread/mutex.hpp>
    3738
    3839#include "util/Debug.h"
     
    5354
    5455// 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));
     56BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Reliable)    == static_cast<int>(ENET_PACKET_FLAG_RELIABLE));
     57BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::Unsequenced) == static_cast<int>(ENET_PACKET_FLAG_UNSEQUENCED));
     58BOOST_STATIC_ASSERT(static_cast<int>(PacketFlag::NoAllocate)  == static_cast<int>(ENET_PACKET_FLAG_NO_ALLOCATE));
    5859
    5960#define PACKET_FLAG_DEFAULT PacketFlag::NoAllocate
     
    6162
    6263std::map<size_t, Packet *> Packet::packetMap_;
     64boost::mutex Packet::packetMapMutex_;
    6365
    6466Packet::Packet()
     
    6668  flags_ = PACKET_FLAG_DEFAULT;
    6769  packetDirection_ = Direction::Outgoing;
    68   clientID_=0;
     70  peerID_=0;
    6971  data_=0;
    7072  enetPacket_=0;
     
    7274}
    7375
    74 Packet::Packet(uint8_t *data, unsigned int clientID)
     76Packet::Packet(uint8_t *data, unsigned int peerID)
    7577{
    7678  flags_ = PACKET_FLAG_DEFAULT;
    7779  packetDirection_ = Direction::Incoming;
    78   clientID_=clientID;
     80  peerID_=peerID;
    7981  data_=data;
    8082  enetPacket_=0;
     
    8789  flags_=p.flags_;
    8890  packetDirection_ = p.packetDirection_;
    89   clientID_ = p.clientID_;
     91  peerID_ = p.peerID_;
    9092  if(p.data_){
    9193    data_ = new uint8_t[p.getSize()];
     
    123125}
    124126
    125 bool Packet::send(){
     127bool Packet::send(orxonox::Host* host){
    126128  if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional ){
    127129    assert(0);
     
    142144      // Assures we don't create a packet and destroy it right after in another thread
    143145      // without having a reference in the packetMap_
     146      Packet::packetMapMutex_.lock();
    144147      packetMap_[reinterpret_cast<size_t>(enetPacket_)] = this;
     148      Packet::packetMapMutex_.unlock();
    145149    }
    146150  }
     
    164168//  ENetPacket *temp = enetPacket_;
    165169//  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);
    168174  return true;
    169175}
     
    172178  uint8_t *data = packet->data;
    173179  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;
    175184  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;
    177186  switch( *(Type::Value *)(data + _PACKETID) )
    178187  {
    179188    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 );
    182191      break;
    183192    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 );
    186195      break;
    187196    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 );
    190199      break;
    191200    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 );
    195203      break;
    196204    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 );
    199207      break;
    200208    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 );
    203211      break;
    204212    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 );
    207215      break;
    208216    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 );
    211219      break;
    212220    default:
    213       assert(0); //TODO: repair this
     221      assert(0);
    214222      break;
    215223  }
     
    229237void Packet::deletePacket(ENetPacket *enetPacket){
    230238  // Get our Packet from a global map with all Packets created in the send() method of Packet.
     239  Packet::packetMapMutex_.lock();
    231240  std::map<size_t, Packet*>::iterator it = packetMap_.find(reinterpret_cast<size_t>(enetPacket));
    232241  assert(it != packetMap_.end());
     
    235244  delete it->second;
    236245  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;
    238248}
    239249
Note: See TracChangeset for help on using the changeset viewer.