| 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 orxonox | 
|---|
| 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->event = ev; | 
|---|
| 65 |       //last->address = ev->peer->address; | 
|---|
| 66 |     } else { | 
|---|
| 67 |       //insert a new element at the bottom | 
|---|
| 68 |       last->next = new QueueItem; | 
|---|
| 69 |       last=last->next; | 
|---|
| 70 |       // initialize last->next | 
|---|
| 71 |       last->next=NULL; | 
|---|
| 72 |       // save the packet to the new element | 
|---|
| 73 |       last->event = ev; | 
|---|
| 74 |       //last->address = ev->peer->address; | 
|---|
| 75 |     } | 
|---|
| 76 |     lock.unlock(); | 
|---|
| 77 |     return true; | 
|---|
| 78 |   } | 
|---|
| 79 |  | 
|---|
| 80 |   //returns the first element in the list without deleting it but | 
|---|
| 81 |   //moving first pointer to next element | 
|---|
| 82 |   /*ENetPacket *PacketBuffer::pop() { | 
|---|
| 83 |     ENetAddress address; | 
|---|
| 84 |     return pop(address); | 
|---|
| 85 |   }*/ | 
|---|
| 86 |    | 
|---|
| 87 |   ENetEvent *PacketBuffer::pop(){ | 
|---|
| 88 |     boost::recursive_mutex::scoped_lock lock(mutex_); | 
|---|
| 89 |     //std::cout << "packetbuffer pop(address)" << std::endl; | 
|---|
| 90 |     if(first!=NULL /*&& !closed*/){ | 
|---|
| 91 |       QueueItem *temp = first; | 
|---|
| 92 |       // get packet | 
|---|
| 93 |       ENetEvent *ev=first->event; | 
|---|
| 94 |       //address = first->address; | 
|---|
| 95 |       // remove first element | 
|---|
| 96 |       first = first->next; | 
|---|
| 97 |       delete temp; | 
|---|
| 98 |       lock.unlock(); | 
|---|
| 99 |       //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl; | 
|---|
| 100 |       return ev; | 
|---|
| 101 |     } else{ | 
|---|
| 102 |       lock.unlock(); | 
|---|
| 103 |       return NULL; | 
|---|
| 104 |     } | 
|---|
| 105 |   } | 
|---|
| 106 |  | 
|---|
| 107 |   /*ENetPacket *PacketBuffer::pop(ENetAddress &address) { | 
|---|
| 108 |     boost::recursive_mutex::scoped_lock lock(mutex_); | 
|---|
| 109 |     //std::cout << "packetbuffer pop(address)" << std::endl; | 
|---|
| 110 |     if(first!=NULL ){ | 
|---|
| 111 |       QueueItem *temp = first; | 
|---|
| 112 |       // get packet | 
|---|
| 113 |       ENetPacket *pck=first->event->packet; | 
|---|
| 114 |       address = first->event->peer->address; | 
|---|
| 115 |       // remove first element | 
|---|
| 116 |       first = first->next; | 
|---|
| 117 |       delete temp; | 
|---|
| 118 |       lock.unlock(); | 
|---|
| 119 |       //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl; | 
|---|
| 120 |       return pck; | 
|---|
| 121 |     } else{ | 
|---|
| 122 |       lock.unlock(); | 
|---|
| 123 |       return NULL; | 
|---|
| 124 |     } | 
|---|
| 125 |   }*/ | 
|---|
| 126 |  | 
|---|
| 127 |   bool PacketBuffer::isEmpty() { | 
|---|
| 128 |     return (first==NULL); | 
|---|
| 129 |   } | 
|---|
| 130 |  | 
|---|
| 131 |   void PacketBuffer::print() { | 
|---|
| 132 |     QueueItem *temp=first; | 
|---|
| 133 |     while(temp!=NULL){ | 
|---|
| 134 |       //    std::cout << temp->packet->data << std::endl; | 
|---|
| 135 |       temp=temp->next; | 
|---|
| 136 |     } | 
|---|
| 137 |  | 
|---|
| 138 |   } | 
|---|
| 139 |  | 
|---|
| 140 |   bool PacketBuffer::isClosed() { | 
|---|
| 141 |     return closed; | 
|---|
| 142 |   } | 
|---|
| 143 |  | 
|---|
| 144 |   void PacketBuffer::setClosed(bool value){ | 
|---|
| 145 |     closed=value; | 
|---|
| 146 |     return; | 
|---|
| 147 |   } | 
|---|
| 148 |  | 
|---|
| 149 | } // namespace orxonox | 
|---|