1 | |
---|
2 | #include <enet/enet.h> |
---|
3 | |
---|
4 | #include "Packet.h" |
---|
5 | #include "network/ConnectionManager.h" |
---|
6 | #include "network/ClientInformation.h" |
---|
7 | |
---|
8 | #include "Acknowledgement.h" |
---|
9 | #include "Chat.h" |
---|
10 | #include "ClassID.h" |
---|
11 | #include "Gamestate.h" |
---|
12 | #include "network/Host.h" |
---|
13 | #include "core/CoreIncludes.h" |
---|
14 | |
---|
15 | namespace network{ |
---|
16 | |
---|
17 | namespace packet{ |
---|
18 | |
---|
19 | #define _PACKETID 0 |
---|
20 | |
---|
21 | |
---|
22 | |
---|
23 | Packet::Packet(){ |
---|
24 | packetDirection_ = ENUM::Outgoing; |
---|
25 | clientID_=0; |
---|
26 | packetContent_=0; |
---|
27 | enetPacket_=0; |
---|
28 | } |
---|
29 | |
---|
30 | Packet::Packet(PacketContent *content){ |
---|
31 | packetDirection_ = ENUM::Outgoing; |
---|
32 | clientID_=0; |
---|
33 | packetContent_=content; |
---|
34 | enetPacket_=0; |
---|
35 | } |
---|
36 | |
---|
37 | Packet::Packet(ENetPacket *packet, ENetPeer *peer){ |
---|
38 | packetDirection_ = ENUM::Incoming; |
---|
39 | enetPacket_ = packet; |
---|
40 | clientID_ = ClientInformation::findClient(&peer->address)->getID(); |
---|
41 | packetContent_ = createPacketContent(packet); |
---|
42 | enet_packet_destroy(packet); |
---|
43 | delete peer; |
---|
44 | } |
---|
45 | |
---|
46 | Packet::~Packet(){ |
---|
47 | if(packetContent_) |
---|
48 | delete packetContent_; |
---|
49 | if(enetPacket_){ |
---|
50 | enet_packet_destroy(enetPacket_); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | void Packet::setPacketContent(PacketContent *content){ |
---|
55 | packetContent_ = content; |
---|
56 | if(enetPacket_){ |
---|
57 | enet_packet_destroy(enetPacket_); |
---|
58 | enetPacket_=0; |
---|
59 | } |
---|
60 | } |
---|
61 | |
---|
62 | bool Packet::send(){ |
---|
63 | if(packetDirection_ != ENUM::Outgoing && packetDirection_ != ENUM::Bidirectional ) |
---|
64 | return false; |
---|
65 | if(!enetPacket_){ |
---|
66 | if(!packetContent_) |
---|
67 | return false; |
---|
68 | COUT(3) << "creating packet with data: " << packetContent_->getData() << " size: " << packetContent_->getSize() << " and flags: " << packetContent_->getFlags() << std::endl; |
---|
69 | enetPacket_ = enet_packet_create(packetContent_->getData(), packetContent_->getSize(), packetContent_->getFlags()); |
---|
70 | } |
---|
71 | network::Host::addPacket( enetPacket_, clientID_); |
---|
72 | enetPacket_ = 0; // otherwise we have a double free because enet already handles the deallocation of the packet |
---|
73 | return true; |
---|
74 | } |
---|
75 | |
---|
76 | PacketContent *Packet::createPacketContent(ENetPacket *packet){ |
---|
77 | unsigned char *data = packet->data; |
---|
78 | switch( *(ENUM::Type *)&data[_PACKETID] ) |
---|
79 | { |
---|
80 | case ENUM::Acknowledgement: |
---|
81 | packetContent_ = new Acknowledgement( data, clientID_ ); |
---|
82 | break; |
---|
83 | case ENUM::Chat: |
---|
84 | packetContent_ = new Chat( data, clientID_ ); |
---|
85 | break; |
---|
86 | case ENUM::ClassID: |
---|
87 | packetContent_ = new ClassID( data, clientID_ ); |
---|
88 | break; |
---|
89 | case ENUM::Gamestate: |
---|
90 | // TODO: remove brackets |
---|
91 | packetContent_ = new Gamestate( data, true, clientID_ ); |
---|
92 | break; |
---|
93 | default: |
---|
94 | assert(0); |
---|
95 | } |
---|
96 | return packetContent_; |
---|
97 | } |
---|
98 | |
---|
99 | } // namespace packet |
---|
100 | |
---|
101 | } // namespace network |
---|
102 | |
---|