Changeset 1711 for code/branches/network/src/network/packet/Packet.cc
- Timestamp:
- Sep 5, 2008, 11:32:42 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/packet/Packet.cc
r1710 r1711 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, (C) 2008 24 * Co-authors: 25 * ... 26 * 27 */ 28 1 29 2 30 #include <enet/enet.h> 31 #include <boost/bind.hpp> 3 32 4 33 #include "Packet.h" … … 10 39 #include "ClassID.h" 11 40 #include "Gamestate.h" 41 #include "Welcome.h" 12 42 #include "network/Host.h" 13 43 #include "core/CoreIncludes.h" … … 17 47 namespace packet{ 18 48 49 #define PACKET_FLAG_DEFAULT ENET_PACKET_FLAG_NO_ALLOCATE 19 50 #define _PACKETID 0 20 51 21 52 std::map<ENetPacket *, Packet *> Packet::packetMap_; 22 53 23 54 Packet::Packet(){ 55 flags_ = PACKET_FLAG_DEFAULT; 24 56 packetDirection_ = ENUM::Outgoing; 25 57 clientID_=0; 26 packetContent_=0;58 data_=0; 27 59 enetPacket_=0; 28 60 } 29 61 30 Packet::Packet(PacketContent *content){ 62 Packet::Packet(unsigned char *data, int clientID){ 63 flags_ = PACKET_FLAG_DEFAULT; 31 64 packetDirection_ = ENUM::Outgoing; 32 clientID_= 0;33 packetContent_=content;65 clientID_=clientID; 66 data_=data; 34 67 enetPacket_=0; 35 68 } … … 39 72 enetPacket_ = packet; 40 73 clientID_ = ClientInformation::findClient(&peer->address)->getID(); 41 packetContent_ = createPacketContent(packet);74 data_ = packet->data; 42 75 enet_packet_destroy(packet); 43 76 } 44 77 45 Packet::~Packet(){ 46 if(enetPacket_){ 47 enet_packet_destroy(enetPacket_); 48 } 78 Packet::Packet(const Packet &p){ 79 flags_=p.flags_; 80 if(p.data_){ 81 data_ = new unsigned char[p.getSize()]; 82 memcpy(data_, p.data_, p.getSize()); 83 }else 84 data_=0; 49 85 } 50 86 51 void Packet::setPacketContent(PacketContent *content){ 52 packetContent_ = content; 53 if(enetPacket_){ 87 Packet::~Packet(){ 88 if(enetPacket_) 54 89 enet_packet_destroy(enetPacket_); 55 enetPacket_=0;56 }90 if(data_) 91 delete[] data_; 57 92 } 58 93 … … 61 96 return false; 62 97 if(!enetPacket_){ 63 if(! packetContent_)98 if(!data_) 64 99 return false; 65 enetPacket_ = enet_packet_create(packetContent_->getData(), packetContent_->getSize(), packetContent_->getFlags()); 100 enetPacket_ = enet_packet_create(getData(), getSize(), getFlags()); 101 enetPacket_->freeCallback = &Packet::deletePacket; 102 packetMap_[enetPacket_] = this; 66 103 } 67 104 network::Host::addPacket( enetPacket_, clientID_); … … 70 107 } 71 108 72 Packet Content *Packet::createPacketContent(ENetPacket *packet){109 Packet *Packet::createPacket(ENetPacket *packet, ENetPeer *peer){ 73 110 unsigned char *data = packet->data; 111 unsigned int clientID = ClientInformation::findClient(&peer->address)->getID(); 112 Packet *p; 74 113 switch( *(ENUM::Type *)&data[_PACKETID] ) 75 114 { 76 115 case ENUM::Acknowledgement: 77 p acketContent_ = new Acknowledgement( data, clientID_);116 p = new Acknowledgement( data, clientID ); 78 117 break; 79 118 case ENUM::Chat: 80 p acketContent_ = new Chat( data, clientID_);119 p = new Chat( data, clientID ); 81 120 break; 82 121 case ENUM::ClassID: 83 p acketContent_ = new ClassID( data, clientID_);122 p = new ClassID( data, clientID ); 84 123 break; 85 124 case ENUM::Gamestate: 86 125 // TODO: remove brackets 87 p acketContent_ = new Gamestate( data, true, clientID_);126 p = new Gamestate( data, true, clientID ); 88 127 break; 128 case ENUM::Welcome: 129 p = new Welcome( data, clientID ); 89 130 default: 90 131 assert(0); //TODO: repair this 91 132 break; 92 133 } 93 return packetContent_; 134 return p; 135 } 136 137 void Packet::deletePacket(ENetPacket *packet){ 138 assert(packetMap_[packet]); 139 delete packetMap_[packet]; 94 140 } 95 141
Note: See TracChangeset
for help on using the changeset viewer.