Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/packet/ClassID.cc @ 1711

Last change on this file since 1711 was 1711, checked in by scheusso, 16 years ago

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

File size: 2.5 KB
Line 
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
31#include "ClassID.h"
32#include "core/CoreIncludes.h"
33#include <string>
34#include <assert.h>
35
36namespace network {
37namespace packet {
38
39#define PACKET_FLAGS_CLASSID  ENET_PACKET_FLAG_RELIABLE
40#define _PACKETID             0
41#define _CLASSID              _PACKETID + sizeof(ENUM::Type)
42#define _CLASSNAMELENGTH      _CLASSID + sizeof(unsigned int)
43#define _CLASSNAME            _CLASSNAMELENGTH + sizeof(classNameLength_)
44 
45  ClassID::ClassID( unsigned int classID, std::string className )
46 : Packet()
47{
48  flags_ = flags_ | PACKET_FLAGS_CLASSID;
49  classNameLength_=className.length()+1;
50  assert(getSize());
51  data_=new unsigned char[ getSize() ];
52  assert(data_);
53  *(packet::ENUM::Type *)&data_[ _PACKETID ] = packet::ENUM::ClassID;
54  *(unsigned int *)&data_[ _CLASSID ] = classID;
55  *(unsigned int *)&data_[ _CLASSNAMELENGTH ] = classNameLength_;
56  memcpy( &data_[ _CLASSNAME ], (void *)className.c_str(), classNameLength_ );
57}
58
59ClassID::ClassID( unsigned char *data, int clientID )
60  : Packet(data, clientID)
61{
62  memcpy( (void *)&classNameLength_, &data[ _CLASSNAMELENGTH ], sizeof(classNameLength_) );
63}
64
65ClassID::~ClassID()
66{
67}
68
69unsigned int ClassID::getSize() const{
70  return sizeof(network::packet::ENUM::Type) + 2*sizeof(unsigned int) + classNameLength_;
71}
72
73bool ClassID::process(){
74  orxonox::Identifier *id=ID( std::string((const char*)&data_[ sizeof(network::packet::ENUM::Type) ]) );
75  if(id==NULL)
76    return false;
77  id->setNetworkID( getClassID() );
78  delete this;
79  return true;
80}
81
82unsigned int ClassID::getClassID(){
83  return *(unsigned int *)&data_[ _CLASSID ];
84}
85
86} //namespace packet
87}//namespace network
Note: See TracBrowser for help on using the repository browser.