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