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 | #ifndef NETWORKPACKET_H |
---|
29 | #define NETWORKPACKET_H |
---|
30 | |
---|
31 | #include "network/NetworkPrereqs.h" |
---|
32 | |
---|
33 | #include <map> |
---|
34 | #include <enet/enet.h> |
---|
35 | #include <boost/thread/recursive_mutex.hpp> |
---|
36 | |
---|
37 | #include "util/Integers.h" |
---|
38 | |
---|
39 | namespace orxonox { |
---|
40 | |
---|
41 | namespace packet{ |
---|
42 | |
---|
43 | namespace ENUM{ |
---|
44 | enum Direction{ |
---|
45 | Incoming, |
---|
46 | Outgoing, |
---|
47 | Bidirectional |
---|
48 | }; |
---|
49 | enum Type{ |
---|
50 | Acknowledgement, |
---|
51 | Gamestate, |
---|
52 | ClassID, |
---|
53 | Chat, |
---|
54 | Welcome, |
---|
55 | DeleteObjects |
---|
56 | }; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | @author Oliver Scheuss <scheusso [at] ee.ethz.ch> |
---|
61 | */ |
---|
62 | class _NetworkExport Packet{ |
---|
63 | public: |
---|
64 | Packet(const Packet &p); |
---|
65 | virtual ~Packet(); |
---|
66 | static Packet *createPacket(ENetPacket *packet, ENetPeer *peer); |
---|
67 | static void deletePacket(ENetPacket *packet); |
---|
68 | |
---|
69 | virtual unsigned char *getData(){ return data_; }; |
---|
70 | virtual unsigned int getSize() const =0; |
---|
71 | virtual bool process()=0; |
---|
72 | enet_uint32 getFlags() |
---|
73 | { return flags_; } |
---|
74 | int getClientID() |
---|
75 | { return clientID_; } |
---|
76 | void setClientID( int id ) |
---|
77 | { clientID_ = id; } |
---|
78 | |
---|
79 | bool send(); |
---|
80 | protected: |
---|
81 | Packet(); |
---|
82 | Packet(uint8_t *data, unsigned int clientID); |
---|
83 | // Packet(ENetPacket *packet, ENetPeer *peer); |
---|
84 | enet_uint32 flags_; |
---|
85 | unsigned int clientID_; |
---|
86 | ENUM::Direction packetDirection_; |
---|
87 | /** Pointer to the data. Be careful when deleting it because it might |
---|
88 | point to a location that was allocated by ENet. |
---|
89 | See bDataENetAllocated_ */ |
---|
90 | uint8_t *data_; |
---|
91 | /** Tells whether data_ was allocated by ENet or ourselves. |
---|
92 | data_ might no correlate with enetPacket_->data. */ |
---|
93 | bool bDataENetAllocated_; |
---|
94 | private: |
---|
95 | static std::map<size_t, Packet *> packetMap_; |
---|
96 | //! Static mutex for any packetMap_ access |
---|
97 | static boost::recursive_mutex packetMap_mutex; |
---|
98 | ENetPacket *enetPacket_; |
---|
99 | }; |
---|
100 | |
---|
101 | } //namespace packet |
---|
102 | |
---|
103 | } //namespace orxonox |
---|
104 | |
---|
105 | #endif |
---|