1 | #include "ClassID.h" |
---|
2 | #include "Packet.h" |
---|
3 | #include "core/CoreIncludes.h" |
---|
4 | #include <string> |
---|
5 | #include <assert.h> |
---|
6 | |
---|
7 | namespace network { |
---|
8 | namespace packet { |
---|
9 | |
---|
10 | #define PACKET_FLAGS_CLASSID ENET_PACKET_FLAG_RELIABLE |
---|
11 | #define _PACKETID 0 |
---|
12 | #define _CLASSID _PACKETID + sizeof(ENUM::Type) |
---|
13 | #define _CLASSNAMELENGTH _CLASSID + sizeof(unsigned int) |
---|
14 | #define _CLASSNAME _CLASSNAMELENGTH + sizeof(classNameLength_) |
---|
15 | |
---|
16 | ClassID::ClassID( unsigned int classID, std::string className ) |
---|
17 | : PacketContent() |
---|
18 | { |
---|
19 | flags_ = flags_ | PACKET_FLAGS_CLASSID; |
---|
20 | classNameLength_=className.length()+1; |
---|
21 | assert(getSize()); |
---|
22 | data_=new unsigned char[ getSize() ]; |
---|
23 | if(!data_) |
---|
24 | return; |
---|
25 | *(packet::ENUM::Type *)&data_[ _PACKETID ] = packet::ENUM::ClassID; |
---|
26 | *(unsigned int *)&data_[ _CLASSID ] = classID; |
---|
27 | *(unsigned int *)&data_[ _CLASSNAMELENGTH ] = classNameLength_; |
---|
28 | memcpy( &data_[ _CLASSNAME ], (void *)className.c_str(), classNameLength_ ); |
---|
29 | } |
---|
30 | |
---|
31 | ClassID::ClassID( unsigned char *data, int clientID ) |
---|
32 | : PacketContent(data, clientID) |
---|
33 | { |
---|
34 | memcpy( (void *)&classNameLength_, &data[ sizeof(ENUM::Type) + sizeof(int) ], sizeof(classNameLength_) ); |
---|
35 | } |
---|
36 | |
---|
37 | ClassID::~ClassID() |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | unsigned char *ClassID::getData(){ |
---|
42 | return data_; |
---|
43 | } |
---|
44 | |
---|
45 | unsigned int ClassID::getSize() const{ |
---|
46 | return sizeof(network::packet::ENUM::Type) + sizeof(int) + classNameLength_; |
---|
47 | } |
---|
48 | |
---|
49 | bool ClassID::process(){ |
---|
50 | orxonox::Identifier *id=ID( std::string((const char*)&data_[ sizeof(network::packet::ENUM::Type) ]) ); |
---|
51 | if(id==NULL) |
---|
52 | return false; |
---|
53 | id->setNetworkID( getClassID() ); |
---|
54 | return true; |
---|
55 | } |
---|
56 | |
---|
57 | unsigned int ClassID::getClassID(){ |
---|
58 | return *(unsigned int *)&data_[ _CLASSID ]; |
---|
59 | } |
---|
60 | |
---|
61 | // unsigned int ClassID::getClassNameLength(){ |
---|
62 | // return *(unsigned int *)&data[ _CLASSNAMELENGTH ]; |
---|
63 | // } |
---|
64 | |
---|
65 | } //namespace packet |
---|
66 | }//namespace network |
---|