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