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