Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Sep 5, 2008, 11:32:42 PM (16 years ago)
Author:
scheusso
Message:

we use enetcallback for destroying packets now (unfortunately there are still some problems)

Location:
code/branches/network/src/network/packet
Files:
1 deleted
12 edited

Legend:

Unmodified
Added
Removed
  • code/branches/network/src/network/packet/Acknowledgement.cc

    r1705 r1711  
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Oliver Scheuss, (C) 2008
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
     28
     29
    130#include "Acknowledgement.h"
    2 #include "Packet.h"
    331#include "network/Host.h"
    432#include "network/GamestateHandler.h"
     
    1240 
    1341Acknowledgement::Acknowledgement( unsigned int id, int clientID )
    14  : PacketContent()
     42 : Packet()
    1543{
    1644  flags_ = flags_ | PACKET_FLAGS_ACK;
     
    2250
    2351Acknowledgement::Acknowledgement( unsigned char *data, int clientID )
    24   : PacketContent(data, clientID)
     52  : Packet(data, clientID)
    2553{
    26   clientID_=clientID;
    2754}
    2855
    2956Acknowledgement::~Acknowledgement()
    3057{
    31 }
    32 
    33 unsigned char *Acknowledgement::getData(){
    34   return data_;
    3558}
    3659
     
    4063
    4164bool Acknowledgement::process(){
    42   return GamestateHandler::ackGamestate(data_[_ACKID], clientID_);
     65  bool b = GamestateHandler::ackGamestate(data_[_ACKID], clientID_);
     66  delete this;
     67  return b;
    4368}
    4469
  • code/branches/network/src/network/packet/Acknowledgement.h

    r1705 r1711  
    2929#define NETWORKACKNOLEDGEMENT_H
    3030
    31 #include "PacketContent.h"
     31#include "Packet.h"
    3232
    3333
     
    3737        @author
    3838*/
    39 class Acknowledgement : public PacketContent
     39class Acknowledgement : public Packet
    4040{
    4141public:
     
    4444  ~Acknowledgement();
    4545 
    46   unsigned char *getData();
    4746  inline unsigned int getSize() const;
    4847  bool process();
  • code/branches/network/src/network/packet/Chat.cc

    r1705 r1711  
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Oliver Scheuss <scheusso [at] ee.ethz.ch>, (C) 2008
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
     28
    129#include "Chat.h"
    2 #include "Packet.h"
     30#include <assert.h>
    331
    432namespace network {
     
    1139
    1240Chat::Chat( std::string& message, int clientID )
    13  : PacketContent()
     41 : Packet()
    1442{
    1543  flags_ = flags_ | PACKET_FLAGS_CHAT;
     
    2351
    2452Chat::Chat( unsigned char *data, int clientID )
    25   : PacketContent(data, clientID)
     53  : Packet(data, clientID)
    2654{
    2755  messageLength_ = *(unsigned int *)&data[ _MESSAGELENGTH ];
     
    3260}
    3361
    34 unsigned char *Chat::getData(){
    35   return data_;
    36 }
    37 
    3862unsigned int Chat::getSize() const{
    3963  return _MESSAGE + messageLength_;
     
    4266bool Chat::process(){
    4367  //TODO: change this !!!
     68  assert(0);
     69  delete this;
    4470  return true;
    4571}
  • code/branches/network/src/network/packet/Chat.h

    r1701 r1711  
    1 /*
    2  *   ORXONOX - the hottest 3D action shooter ever to exist
    3  *                    > www.orxonox.net <
    4  *
    5  *
    6  *   License notice:
    7  *
    8  *   This program is free software; you can redistribute it and/or
    9  *   modify it under the terms of the GNU General Public License
    10  *   as published by the Free Software Foundation; either version 2
    11  *   of the License, or (at your option) any later version.
    12  *
    13  *   This program is distributed in the hope that it will be useful,
    14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  *   GNU General Public License for more details.
    17  *
    18  *   You should have received a copy of the GNU General Public License
    19  *   along with this program; if not, write to the Free Software
    20  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
    21  *
    22  *   Author:
    23  *      Oliver Scheuss <scheusso [at] ee.ethz.ch>, (C) 2008
    24  *   Co-authors:
    25  *      ...
    26  *
    27  */
     1
    282#ifndef NETWORKCHAT_H
    293#define NETWORKCHAT_H
     
    315#include <string>
    326
    33 #include "PacketContent.h"
     7#include "Packet.h"
    348
    359namespace network {
     
    3812        @author
    3913*/
    40 class Chat : public PacketContent
     14class Chat : public Packet
    4115{
    4216public:
     
    4519  ~Chat();
    4620 
    47   unsigned char *getData();
    4821  inline unsigned int getSize() const;
    4922  bool process();
  • code/branches/network/src/network/packet/ClassID.cc

    r1708 r1711  
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Oliver Scheuss, (C) 2008
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
     28
     29
     30
    131#include "ClassID.h"
    2 #include "Packet.h"
    332#include "core/CoreIncludes.h"
    433#include <string>
     
    1544 
    1645  ClassID::ClassID( unsigned int classID, std::string className )
    17  : PacketContent()
     46 : Packet()
    1847{
    1948  flags_ = flags_ | PACKET_FLAGS_CLASSID;
     
    2958
    3059ClassID::ClassID( unsigned char *data, int clientID )
    31   : PacketContent(data, clientID)
     60  : Packet(data, clientID)
    3261{
    3362  memcpy( (void *)&classNameLength_, &data[ _CLASSNAMELENGTH ], sizeof(classNameLength_) );
     
    3665ClassID::~ClassID()
    3766{
    38 }
    39 
    40 unsigned char *ClassID::getData(){
    41   return data_;
    4267}
    4368
     
    5176    return false;
    5277  id->setNetworkID( getClassID() );
     78  delete this;
    5379  return true;
    5480}
     
    5884}
    5985
    60 // unsigned int ClassID::getClassNameLength(){
    61 //   return *(unsigned int *)&data[ _CLASSNAMELENGTH ];
    62 // }
    63 
    6486} //namespace packet
    6587}//namespace network
  • code/branches/network/src/network/packet/ClassID.h

    r1705 r1711  
    3131#include <string>
    3232
    33 #include "PacketContent.h"
     33#include "Packet.h"
    3434
    3535namespace network {
     
    3939        @author
    4040*/
    41 class ClassID : public PacketContent
     41class ClassID : public Packet
    4242{
    4343public:
     
    4646  ~ClassID();
    4747 
    48   unsigned char *getData();
    4948  inline unsigned int getSize() const;
    5049  bool process();
  • code/branches/network/src/network/packet/Gamestate.cc

    r1710 r1711  
     1
     2
     3
     4
     5
     6
     7
     8
     9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19/*
     20 *   ORXONOX - the hottest 3D action shooter ever to exist
     21 *                    > www.orxonox.net <
     22 *
     23 *
     24 *   License notice:
     25 *
     26 *   This program is free software; you can redistribute it and/or
     27 *   modify it under the terms of the GNU General Public License
     28 *   as published by the Free Software Foundation; either version 2
     29 *   of the License, or (at your option) any later version.
     30 *
     31 *   This program is distributed in the hope that it will be useful,
     32 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     33 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     34 *   GNU General Public License for more details.
     35 *
     36 *   You should have received a copy of the GNU General Public License
     37 *   along with this program; if not, write to the Free Software
     38 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     39 *
     40 *   Author:
     41 *      Oliver Scheuss, (C) 2008
     42 *   Co-authors:
     43 *      ...
     44 *
     45 */
     46
    147#include "Gamestate.h"
    248#include "network/ClientInformation.h"
     
    2268
    2369Gamestate::Gamestate(unsigned char *data, bool compressed, int clientID):
    24     PacketContent(data, clientID)
     70    Packet(data, clientID)
    2571{
    2672  compressed_ = compressed;
    27   //GAMESTATE_HEADER = (GamestateHeader *)data;
    28   //if(!compressed)
    29     //bs_ = new Bytestream(data+sizeof(GamestateHeader), GAMESTATE_HEADER->compsize);
    3073}
    3174
     
    138181}
    139182
    140 unsigned char *Gamestate::getData()
    141 {
    142   assert(data_!=0);
    143   return data_;
    144 }
    145 
    146183unsigned int Gamestate::getSize() const
    147184{
     185  assert(data_);
    148186  if(compressed_)
    149187    return HEADER->compsize+sizeof(GamestateHeader);
  • code/branches/network/src/network/packet/Gamestate.h

    r1710 r1711  
    2727 */
    2828
    29 #include "PacketContent.h"
    3029#include "Packet.h"
    3130#include "network/Synchronisable.h"
     
    5352        @author
    5453*/
    55 class Gamestate: public PacketContent{
     54class Gamestate: public Packet{
    5655  public:
    5756    Gamestate();
     
    6867    bool decompressData();
    6968   
    70     // PacketContent functions
    71     virtual unsigned char *getData();
     69    // Packet functions
    7270    virtual unsigned int getSize() const;
    7371    virtual bool process();
  • code/branches/network/src/network/packet/Packet.cc

    r1710 r1711  
     1/*
     2 *   ORXONOX - the hottest 3D action shooter ever to exist
     3 *                    > www.orxonox.net <
     4 *
     5 *
     6 *   License notice:
     7 *
     8 *   This program is free software; you can redistribute it and/or
     9 *   modify it under the terms of the GNU General Public License
     10 *   as published by the Free Software Foundation; either version 2
     11 *   of the License, or (at your option) any later version.
     12 *
     13 *   This program is distributed in the hope that it will be useful,
     14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16 *   GNU General Public License for more details.
     17 *
     18 *   You should have received a copy of the GNU General Public License
     19 *   along with this program; if not, write to the Free Software
     20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     21 *
     22 *   Author:
     23 *      Oliver Scheuss, (C) 2008
     24 *   Co-authors:
     25 *      ...
     26 *
     27 */
     28
    129
    230#include <enet/enet.h>
     31#include <boost/bind.hpp>
    332
    433#include "Packet.h"
     
    1039#include "ClassID.h"
    1140#include "Gamestate.h"
     41#include "Welcome.h"
    1242#include "network/Host.h"
    1343#include "core/CoreIncludes.h"
     
    1747namespace packet{
    1848 
     49#define PACKET_FLAG_DEFAULT ENET_PACKET_FLAG_NO_ALLOCATE
    1950#define _PACKETID           0
    2051 
    21 
     52std::map<ENetPacket *, Packet *> Packet::packetMap_;
    2253 
    2354Packet::Packet(){
     55  flags_ = PACKET_FLAG_DEFAULT;
    2456  packetDirection_ = ENUM::Outgoing;
    2557  clientID_=0;
    26   packetContent_=0;
     58  data_=0;
    2759  enetPacket_=0;
    2860}
    2961
    30 Packet::Packet(PacketContent *content){
     62Packet::Packet(unsigned char *data, int clientID){
     63  flags_ = PACKET_FLAG_DEFAULT;
    3164  packetDirection_ = ENUM::Outgoing;
    32   clientID_=0;
    33   packetContent_=content;
     65  clientID_=clientID;
     66  data_=data;
    3467  enetPacket_=0;
    3568}
     
    3972  enetPacket_ = packet;
    4073  clientID_ = ClientInformation::findClient(&peer->address)->getID();
    41   packetContent_ = createPacketContent(packet);
     74  data_ = packet->data;
    4275  enet_packet_destroy(packet);
    4376}
    4477
    45 Packet::~Packet(){
    46   if(enetPacket_){
    47     enet_packet_destroy(enetPacket_);
    48   }
     78Packet::Packet(const Packet &p){
     79  flags_=p.flags_;
     80  if(p.data_){
     81    data_ = new unsigned char[p.getSize()];
     82    memcpy(data_, p.data_, p.getSize());
     83  }else
     84    data_=0;
    4985}
    5086
    51 void Packet::setPacketContent(PacketContent *content){
    52   packetContent_ = content;
    53   if(enetPacket_){
     87Packet::~Packet(){
     88  if(enetPacket_)
    5489    enet_packet_destroy(enetPacket_);
    55     enetPacket_=0;
    56   }
     90  if(data_)
     91    delete[] data_;
    5792}
    5893
     
    6196    return false;
    6297  if(!enetPacket_){
    63     if(!packetContent_)
     98    if(!data_)
    6499      return false;
    65     enetPacket_ = enet_packet_create(packetContent_->getData(), packetContent_->getSize(), packetContent_->getFlags());
     100    enetPacket_ = enet_packet_create(getData(), getSize(), getFlags());
     101    enetPacket_->freeCallback = &Packet::deletePacket;
     102    packetMap_[enetPacket_] = this;
    66103  }
    67104  network::Host::addPacket( enetPacket_, clientID_);
     
    70107}
    71108
    72 PacketContent *Packet::createPacketContent(ENetPacket *packet){
     109Packet *Packet::createPacket(ENetPacket *packet, ENetPeer *peer){
    73110  unsigned char *data = packet->data;
     111  unsigned int clientID = ClientInformation::findClient(&peer->address)->getID();
     112  Packet *p;
    74113  switch( *(ENUM::Type *)&data[_PACKETID] )
    75114  {
    76115    case ENUM::Acknowledgement:
    77       packetContent_ = new Acknowledgement( data, clientID_ );
     116      p = new Acknowledgement( data, clientID );
    78117      break;
    79118    case ENUM::Chat:
    80       packetContent_ = new Chat( data, clientID_ );
     119      p = new Chat( data, clientID );
    81120      break;
    82121    case ENUM::ClassID:
    83       packetContent_ = new ClassID( data, clientID_ );
     122      p = new ClassID( data, clientID );
    84123      break;
    85124    case ENUM::Gamestate:
    86125      // TODO: remove brackets
    87       packetContent_ = new Gamestate( data, true, clientID_ );
     126      p = new Gamestate( data, true, clientID );
    88127      break;
     128    case ENUM::Welcome:
     129      p = new Welcome( data, clientID );
    89130    default:
    90131      assert(0); //TODO: repair this
    91132      break;
    92133  }
    93   return packetContent_;
     134  return p;
     135}
     136
     137void Packet::deletePacket(ENetPacket *packet){
     138  assert(packetMap_[packet]);
     139  delete packetMap_[packet];
    94140}
    95141
  • code/branches/network/src/network/packet/Packet.h

    r1709 r1711  
    2929#define NETWORKPACKET_H
    3030
    31 #include "PacketContent.h"
    32 
     31#include <map>
    3332#include <enet/enet.h>
    3433
     
    5756class Packet{
    5857  public:
    59     Packet();
    60     Packet(PacketContent *content);
    61     Packet(ENetPacket *packet, ENetPeer *peer);
     58    Packet(const Packet &p);
    6259    virtual ~Packet();
     60    static Packet *createPacket(ENetPacket *packet, ENetPeer *peer);
     61    static void deletePacket(ENetPacket *packet);
    6362   
    64     int getClientID(){ return clientID_; }
    65     PacketContent *getPacketContent(){ return packetContent_; }
    66    
    67     void setClientID( int id ){ clientID_ = id; }
    68     void setPacketContent(PacketContent *content);
     63    virtual unsigned char *getData(){ return data_; };
     64    virtual unsigned int getSize() const =0;
     65    virtual bool process()=0;
     66    enet_uint32 getFlags()
     67      { return flags_; }
     68    int getClientID()
     69      { return clientID_; }
     70    void setClientID( int id )
     71      { clientID_ = id; }
    6972   
    7073    bool send();
    7174  protected:
     75    Packet();
     76    Packet(unsigned char *data, int clientID);
     77    Packet(ENetPacket *packet, ENetPeer *peer);
     78    enet_uint32 flags_;
     79    int clientID_;
     80    unsigned char *data_;
    7281  private:
    73     PacketContent *createPacketContent(ENetPacket *packet);
    74    
    75     int clientID_;
    76     PacketContent *packetContent_;
     82    static std::map<ENetPacket *, Packet *> packetMap_;
    7783    ENetPacket *enetPacket_;
    7884    ENUM::Direction packetDirection_;
  • code/branches/network/src/network/packet/Welcome.cc

    r1709 r1711  
     1
     2
     3/*
     4 *   ORXONOX - the hottest 3D action shooter ever to exist
     5 *                    > www.orxonox.net <
     6 *
     7 *
     8 *   License notice:
     9 *
     10 *   This program is free software; you can redistribute it and/or
     11 *   modify it under the terms of the GNU General Public License
     12 *   as published by the Free Software Foundation; either version 2
     13 *   of the License, or (at your option) any later version.
     14 *
     15 *   This program is distributed in the hope that it will be useful,
     16 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
     17 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     18 *   GNU General Public License for more details.
     19 *
     20 *   You should have received a copy of the GNU General Public License
     21 *   along with this program; if not, write to the Free Software
     22 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
     23 *
     24 *   Author:
     25 *      Oliver Scheuss, (C) 2008
     26 *   Co-authors:
     27 *      ...
     28 *
     29 */
     30
     31
    132#include "Welcome.h"
    2 #include "Packet.h"
     33#include "network/Host.h"
    334#include <assert.h>
    435
     
    1243 
    1344  Welcome::Welcome( unsigned int clientID, unsigned int shipID )
    14  : PacketContent()
     45 : Packet()
    1546{
    1647  flags_ = flags_ | PACKET_FLAGS_CLASSID;
     
    2455
    2556Welcome::Welcome( unsigned char *data, int clientID )
    26   : PacketContent(data, clientID)
     57  : Packet(data, clientID)
    2758{
    2859}
     
    4475  clientID = *(unsigned int *)&data_[ _CLIENTID ];
    4576  shipID = *(unsigned int *)&data_[ _SHIPID ];
     77  Host::setClientID(clientID);
     78  Host::setShipID(shipID);
     79  delete this;
    4680  return true;
    4781}
  • code/branches/network/src/network/packet/Welcome.h

    r1709 r1711  
    2929#define NETWORKWELCOME_H
    3030
    31 #include "PacketContent.h"
     31#include "Packet.h"
    3232
    3333namespace network {
     
    3737        @author
    3838*/
    39 class Welcome : public PacketContent
     39class Welcome : public Packet
    4040{
    4141public:
Note: See TracChangeset for help on using the changeset viewer.