/* * ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Oliver Scheuss, (C) 2008 * Co-authors: * ... * */ #include "Packet.h" #include #include #define WIN32_LEAN_AND_MEAN #include #include #include "util/Output.h" #include "Acknowledgement.h" #include "Chat.h" #include "ClassID.h" #include "DeleteObjects.h" #include "FunctionCalls.h" #include "FunctionIDs.h" #include "Gamestate.h" #include "Welcome.h" #include "network/Host.h" // #include "network/ClientInformation.h" namespace orxonox{ namespace packet{ // Make sure we assume the right values static_assert(static_cast(PacketFlag::Reliable) == static_cast(ENET_PACKET_FLAG_RELIABLE), "check enum"); static_assert(static_cast(PacketFlag::Unsequenced) == static_cast(ENET_PACKET_FLAG_UNSEQUENCED), "check enum"); static_assert(static_cast(PacketFlag::NoAllocate) == static_cast(ENET_PACKET_FLAG_NO_ALLOCATE), "check enum"); #define PACKET_FLAG_DEFAULT PacketFlag::NoAllocate #define _PACKETID 0 std::map Packet::packetMap_; boost::mutex Packet::packetMapMutex_; Packet::Packet() { flags_ = PACKET_FLAG_DEFAULT; packetDirection_ = Direction::Outgoing; peerID_=0; data_=nullptr; enetPacket_=nullptr; bDataENetAllocated_ = false; } Packet::Packet(uint8_t *data, unsigned int peerID) { flags_ = PACKET_FLAG_DEFAULT; packetDirection_ = Direction::Incoming; peerID_=peerID; data_=data; enetPacket_=nullptr; bDataENetAllocated_ = false; } Packet::Packet(const Packet &p) { enetPacket_=p.enetPacket_; flags_=p.flags_; packetDirection_ = p.packetDirection_; peerID_ = p.peerID_; if(p.data_){ data_ = new uint8_t[p.getSize()]; memcpy(data_, p.data_, p.getSize()); }else data_=nullptr; bDataENetAllocated_ = p.bDataENetAllocated_; } /** @brief Destroys a packet completely. That also means destroying the ENetPacket if one exists. There */ Packet::~Packet() { // Deallocate data_ memory if necessary. if (this->bDataENetAllocated_) { // In this case ENet allocated data_ and will destroy it. } else if (this->data_) { // This destructor was probably called as a consequence of ENet executing our callback. // It simply serves us to be able to deallocate the packet content (data_) ourselves since // we have created it in the first place. delete[] this->data_; } // Destroy the ENetPacket if necessary. // Note: For the case ENet used the callback to destroy the packet, we have already set // enetPacket_ to nullptr to avoid destroying it again. if (this->enetPacket_) { // enetPacket_->data gets destroyed too by ENet if it was allocated by it. enet_packet_destroy(enetPacket_); } } /** * Send the Packet. * @param host The host which sends the packet */ bool Packet::send(orxonox::Host* host) { // Deny sending incoming packets if(packetDirection_ != Direction::Outgoing && packetDirection_ != Direction::Bidirectional ) { assert(0); return false; } if(!enetPacket_) { // Deny sending empty packets if(!data_) { assert(0); return false; } // We deliver ENet the data address so that it doesn't memcpy everything again. // --> We have to delete data_ ourselves! enetPacket_ = enet_packet_create(getData(), getSize(), getFlags()); enetPacket_->freeCallback = &Packet::deletePacket; // Add the packet to a global list so we can access it again once enet calls our // deletePacket method. We can of course only give a one argument function to the ENet C library. { // Assures we don't create a packet and destroy it right after in another thread // without having a reference in the packetMap_ Packet::packetMapMutex_.lock(); Packet::packetMap_[reinterpret_cast(enetPacket_)] = this; Packet::packetMapMutex_.unlock(); } } #ifndef NDEBUG switch( *(Type *)(data_ + _PACKETID) ) { case Type::Acknowledgement: case Type::Chat: case Type::ClassID: case Type::Gamestate: case Type::Welcome: case Type::DeleteObjects: case Type::FunctionIDs: case Type::FunctionCalls: break; default: assert(0); //there was some error, if this is the case break; } #endif // Send via reliable or standard channel respectively if( this->flags_ & PacketFlag::Reliable ) host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_DEFAULT); else host->addPacket( enetPacket_, peerID_, NETWORK_CHANNEL_UNRELIABLE); return true; } /** * Given an ENetPacket, create an Orxonox packet * @param packet The ENetPacket * @param peerID The sender */ Packet *Packet::createPacket(ENetPacket* packet, uint32_t peerID) { uint8_t *data = packet->data; Packet *p = nullptr; switch( *(Type *)(data + _PACKETID) ) { case Type::Acknowledgement: p = new Acknowledgement( data, peerID ); break; case Type::Chat: p = new Chat( data, peerID ); break; case Type::ClassID: p = new ClassID( data, peerID ); break; case Type::Gamestate: p = new Gamestate( data, peerID ); break; case Type::Welcome: p = new Welcome( data, peerID ); break; case Type::DeleteObjects: p = new DeleteObjects( data, peerID ); break; case Type::FunctionCalls: p = new FunctionCalls( data, peerID ); break; case Type::FunctionIDs: p = new FunctionIDs( data, peerID ); break; default: assert(0); break; } // Data was created by ENet p->bDataENetAllocated_ = true; p->enetPacket_ = packet; return p; } /** @brief ENet calls this method whenever it wants to destroy a packet that contains data we allocated ourselves. */ void Packet::deletePacket(ENetPacket *enetPacket) { // Get our Packet from a global map with all Packets created in the send() method of Packet. Packet::packetMapMutex_.lock(); std::map::iterator it = Packet::packetMap_.find(reinterpret_cast(enetPacket)); assert(it != packetMap_.end()); // Make sure we don't delete it again in the destructor it->second->enetPacket_ = nullptr; delete it->second; packetMap_.erase(it); Packet::packetMapMutex_.unlock(); } } // namespace packet } // namespace orxonox