| 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) 2007 | 
|---|
| 24 |  *   Co-authors: | 
|---|
| 25 |  *      ... | 
|---|
| 26 |  * | 
|---|
| 27 |  */ | 
|---|
| 28 |  | 
|---|
| 29 | // C++ PacketBuffer | 
|---|
| 30 | // d | 
|---|
| 31 | // Author Oliver Scheuss | 
|---|
| 32 |  | 
|---|
| 33 | #include "PacketBuffer.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include <iostream> | 
|---|
| 36 | #include <queue> | 
|---|
| 37 | #include <string> | 
|---|
| 38 | #include <boost/bind.hpp> | 
|---|
| 39 | #include <boost/thread/mutex.hpp> | 
|---|
| 40 |  | 
|---|
| 41 | namespace network | 
|---|
| 42 | { | 
|---|
| 43 |    boost::recursive_mutex PacketBuffer::mutex_; | 
|---|
| 44 |  | 
|---|
| 45 |   PacketBuffer::PacketBuffer() { | 
|---|
| 46 |     closed=false; | 
|---|
| 47 |     first=NULL; | 
|---|
| 48 |     last=NULL; | 
|---|
| 49 |   } | 
|---|
| 50 |   //this is needed in order to make the packetbuffer threadsafe | 
|---|
| 51 |  | 
|---|
| 52 |  | 
|---|
| 53 |   bool PacketBuffer::push(ENetEvent *ev) { | 
|---|
| 54 |     boost::recursive_mutex::scoped_lock lock(mutex_); | 
|---|
| 55 |     //std::cout << "event size inside packetbuffer " << ev->packet->dataLength << std::endl; | 
|---|
| 56 |     //   if(closed) | 
|---|
| 57 |     //     return false; | 
|---|
| 58 |     // first element? | 
|---|
| 59 |     if(first==NULL){ | 
|---|
| 60 |       first=new QueueItem; | 
|---|
| 61 |       last=first; | 
|---|
| 62 |       last->next=NULL; | 
|---|
| 63 |       // change this!!!!!!!  ---- we are not doing stl so we won't change this | 
|---|
| 64 |       last->packet = ev->packet; | 
|---|
| 65 |       last->address = ev->peer->address; | 
|---|
| 66 |       //last->address = ev->peer->address; | 
|---|
| 67 |     } else { | 
|---|
| 68 |       //insert a new element at the bottom | 
|---|
| 69 |       last->next = new QueueItem; | 
|---|
| 70 |       last=last->next; | 
|---|
| 71 |       // initialize last->next | 
|---|
| 72 |       last->next=NULL; | 
|---|
| 73 |       // save the packet to the new element | 
|---|
| 74 |       last->packet = ev->packet; | 
|---|
| 75 |       last->address = ev->peer->address; | 
|---|
| 76 |       //last->address = ev->peer->address; | 
|---|
| 77 |     } | 
|---|
| 78 |     lock.unlock(); | 
|---|
| 79 |     return true; | 
|---|
| 80 |   } | 
|---|
| 81 |  | 
|---|
| 82 |   //returns the first element in the list without deleting it but | 
|---|
| 83 |   //moving first pointer to next element | 
|---|
| 84 |   ENetPacket *PacketBuffer::pop() { | 
|---|
| 85 |     ENetAddress address; | 
|---|
| 86 |     return pop(address); | 
|---|
| 87 |   } | 
|---|
| 88 |  | 
|---|
| 89 |   ENetPacket *PacketBuffer::pop(ENetAddress &address) { | 
|---|
| 90 |     boost::recursive_mutex::scoped_lock lock(mutex_); | 
|---|
| 91 |     //std::cout << "packetbuffer pop(address)" << std::endl; | 
|---|
| 92 |     if(first!=NULL /*&& !closed*/){ | 
|---|
| 93 |       QueueItem *temp = first; | 
|---|
| 94 |       // get packet | 
|---|
| 95 |       ENetPacket *pck=first->packet; | 
|---|
| 96 |       address = first->address; | 
|---|
| 97 |       // remove first element | 
|---|
| 98 |       first = first->next; | 
|---|
| 99 |       delete temp; | 
|---|
| 100 |       lock.unlock(); | 
|---|
| 101 |       //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl; | 
|---|
| 102 |       return pck; | 
|---|
| 103 |     } else{ | 
|---|
| 104 |       lock.unlock(); | 
|---|
| 105 |       return NULL; | 
|---|
| 106 |     } | 
|---|
| 107 |   } | 
|---|
| 108 |  | 
|---|
| 109 |   bool PacketBuffer::isEmpty() { | 
|---|
| 110 |     return (first==NULL); | 
|---|
| 111 |   } | 
|---|
| 112 |  | 
|---|
| 113 |   void PacketBuffer::print() { | 
|---|
| 114 |     QueueItem *temp=first; | 
|---|
| 115 |     while(temp!=NULL){ | 
|---|
| 116 |       //    std::cout << temp->packet->data << std::endl; | 
|---|
| 117 |       temp=temp->next; | 
|---|
| 118 |     } | 
|---|
| 119 |  | 
|---|
| 120 |   } | 
|---|
| 121 |  | 
|---|
| 122 |   bool PacketBuffer::isClosed() { | 
|---|
| 123 |     return closed; | 
|---|
| 124 |   } | 
|---|
| 125 |  | 
|---|
| 126 |   void PacketBuffer::setClosed(bool value){ | 
|---|
| 127 |     closed=value; | 
|---|
| 128 |     return; | 
|---|
| 129 |   } | 
|---|
| 130 |  | 
|---|
| 131 | } // namespace network | 
|---|