| [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 | |
|---|
| [2171] | 31 | #include "network/NetworkPrereqs.h" |
|---|
| [2087] | 32 | |
|---|
| [1711] | 33 | #include <map> |
|---|
| [1701] | 34 | #include <enet/enet.h> |
|---|
| [2171] | 35 | #include <boost/thread/recursive_mutex.hpp> |
|---|
| [1701] | 36 | |
|---|
| [2171] | 37 | namespace orxonox { |
|---|
| [1701] | 38 | |
|---|
| 39 | namespace packet{ |
|---|
| [2087] | 40 | |
|---|
| [1701] | 41 | namespace ENUM{ |
|---|
| 42 | enum Direction{ |
|---|
| 43 | Incoming, |
|---|
| 44 | Outgoing, |
|---|
| 45 | Bidirectional |
|---|
| 46 | }; |
|---|
| 47 | enum Type{ |
|---|
| 48 | Acknowledgement, |
|---|
| 49 | Gamestate, |
|---|
| 50 | ClassID, |
|---|
| [1705] | 51 | Chat, |
|---|
| [1907] | 52 | Welcome, |
|---|
| 53 | DeleteObjects |
|---|
| [1701] | 54 | }; |
|---|
| 55 | } |
|---|
| [2087] | 56 | |
|---|
| [1701] | 57 | /** |
|---|
| 58 | @author Oliver Scheuss <scheusso [at] ee.ethz.ch> |
|---|
| 59 | */ |
|---|
| [2087] | 60 | class _NetworkExport Packet{ |
|---|
| [1701] | 61 | public: |
|---|
| [1711] | 62 | Packet(const Packet &p); |
|---|
| [1709] | 63 | virtual ~Packet(); |
|---|
| [1711] | 64 | static Packet *createPacket(ENetPacket *packet, ENetPeer *peer); |
|---|
| 65 | static void deletePacket(ENetPacket *packet); |
|---|
| [2087] | 66 | |
|---|
| [1711] | 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; } |
|---|
| [2087] | 76 | |
|---|
| [1701] | 77 | bool send(); |
|---|
| 78 | protected: |
|---|
| [1711] | 79 | Packet(); |
|---|
| [1907] | 80 | Packet(uint8_t *data, unsigned int clientID); |
|---|
| [1713] | 81 | // Packet(ENetPacket *packet, ENetPeer *peer); |
|---|
| [1711] | 82 | enet_uint32 flags_; |
|---|
| [1907] | 83 | unsigned int clientID_; |
|---|
| [1751] | 84 | ENUM::Direction packetDirection_; |
|---|
| [2087] | 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_ */ |
|---|
| [1907] | 88 | uint8_t *data_; |
|---|
| [2087] | 89 | /** Tells whether data_ was allocated by ENet or ourselves. |
|---|
| 90 | data_ might no correlate with enetPacket_->data. */ |
|---|
| 91 | bool bDataENetAllocated_; |
|---|
| [1701] | 92 | private: |
|---|
| [2171] | 93 | static std::map<size_t, Packet *> packetMap_; |
|---|
| 94 | //! Static mutex for any packetMap_ access |
|---|
| 95 | static boost::recursive_mutex packetMap_mutex; |
|---|
| [1701] | 96 | ENetPacket *enetPacket_; |
|---|
| 97 | }; |
|---|
| 98 | |
|---|
| 99 | } //namespace packet |
|---|
| 100 | |
|---|
| [2171] | 101 | } //namespace orxonox |
|---|
| [1701] | 102 | |
|---|
| 103 | #endif |
|---|