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