[514] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * |
---|
| 4 | * |
---|
| 5 | * License notice: |
---|
| 6 | * |
---|
| 7 | * This program is free software; you can redistribute it and/or |
---|
| 8 | * modify it under the terms of the GNU General Public License |
---|
| 9 | * as published by the Free Software Foundation; either version 2 |
---|
| 10 | * of the License, or (at your option) any later version. |
---|
| 11 | * |
---|
| 12 | * This program is distributed in the hope that it will be useful, |
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 15 | * GNU General Public License for more details. |
---|
| 16 | * |
---|
| 17 | * You should have received a copy of the GNU General Public License |
---|
| 18 | * along with this program; if not, write to the Free Software |
---|
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 20 | * |
---|
| 21 | * Author: |
---|
| 22 | * Oliver Scheuss, (C) 2007 |
---|
| 23 | * Co-authors: |
---|
| 24 | * ... |
---|
| 25 | * |
---|
| 26 | */ |
---|
| 27 | |
---|
[173] | 28 | // C++ PacketBuffer |
---|
[196] | 29 | // d |
---|
[173] | 30 | // Author Oliver Scheuss |
---|
[196] | 31 | |
---|
| 32 | #ifndef NETWORK_PACKETBUFFER_CC |
---|
| 33 | #define NETWORK_PACKETBUFFER_CC |
---|
| 34 | |
---|
[173] | 35 | #include <iostream> |
---|
[285] | 36 | #include "PacketBuffer.h" |
---|
[173] | 37 | |
---|
| 38 | namespace network{ |
---|
[285] | 39 | |
---|
[196] | 40 | boost::mutex networkPacketBufferMutex; |
---|
[285] | 41 | |
---|
[173] | 42 | PacketBuffer::PacketBuffer(){ |
---|
[174] | 43 | closed=false; |
---|
[173] | 44 | first=NULL; |
---|
| 45 | last=NULL; |
---|
| 46 | } |
---|
[196] | 47 | //this is needed in order to make the packetbuffer threadsafe |
---|
[173] | 48 | |
---|
[285] | 49 | |
---|
[204] | 50 | bool PacketBuffer::push(ENetEvent *ev){ |
---|
[174] | 51 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
[632] | 52 | //std::cout << "event size inside packetbuffer " << ev->packet->dataLength << std::endl; |
---|
[188] | 53 | // if(closed) |
---|
| 54 | // return false; |
---|
[174] | 55 | // first element? |
---|
| 56 | if(first==NULL){ |
---|
| 57 | first=new QueueItem; |
---|
| 58 | last=first; |
---|
| 59 | last->next=NULL; |
---|
| 60 | // change this!!!!!!! |
---|
[204] | 61 | last->packet = ev->packet; |
---|
| 62 | last->address = ev->peer->address; |
---|
[188] | 63 | } else { |
---|
[174] | 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 |
---|
[204] | 70 | last->packet = ev->packet; |
---|
| 71 | last->address = ev->peer->address; |
---|
[173] | 72 | } |
---|
[174] | 73 | return true; |
---|
[173] | 74 | } |
---|
| 75 | |
---|
[188] | 76 | ENetPacket *PacketBuffer::pop(){ |
---|
[174] | 77 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
[632] | 78 | //std::cout << "packetbuffer pop" << std::endl; |
---|
[188] | 79 | if(first!=NULL /*&& !closed*/){ |
---|
[173] | 80 | QueueItem *temp = first; |
---|
| 81 | // get packet |
---|
[188] | 82 | ENetPacket *pck=first->packet; |
---|
[173] | 83 | // remove first element |
---|
| 84 | first = first->next; |
---|
| 85 | delete temp; |
---|
[632] | 86 | //std::cout << "pop size of packet " << pck->dataLength << std::endl; |
---|
[188] | 87 | return pck; |
---|
[174] | 88 | } else{ |
---|
[632] | 89 | //std::cout << "nothing to return" << std::endl; |
---|
[188] | 90 | return NULL; |
---|
[174] | 91 | } |
---|
[173] | 92 | } |
---|
| 93 | |
---|
[204] | 94 | ENetPacket *PacketBuffer::pop(ENetAddress &address){ |
---|
| 95 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
[632] | 96 | //std::cout << "packetbuffer pop(address)" << std::endl; |
---|
[204] | 97 | if(first!=NULL /*&& !closed*/){ |
---|
| 98 | QueueItem *temp = first; |
---|
| 99 | // get packet |
---|
| 100 | ENetPacket *pck=first->packet; |
---|
| 101 | address = first->address; |
---|
| 102 | // remove first element |
---|
| 103 | first = first->next; |
---|
| 104 | delete temp; |
---|
[632] | 105 | //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl; |
---|
[204] | 106 | return pck; |
---|
| 107 | } else{ |
---|
| 108 | return NULL; |
---|
| 109 | } |
---|
| 110 | } |
---|
| 111 | |
---|
[173] | 112 | bool PacketBuffer::isEmpty(){ |
---|
| 113 | return (first==NULL); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void PacketBuffer::print(){ |
---|
| 117 | QueueItem *temp=first; |
---|
| 118 | while(temp!=NULL){ |
---|
[204] | 119 | // std::cout << temp->packet->data << std::endl; |
---|
[173] | 120 | temp=temp->next; |
---|
| 121 | } |
---|
[285] | 122 | |
---|
[173] | 123 | } |
---|
| 124 | |
---|
[174] | 125 | bool PacketBuffer::isClosed(){ |
---|
| 126 | return closed; |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void PacketBuffer::setClosed(bool value){ |
---|
| 130 | closed=value; |
---|
| 131 | return; |
---|
| 132 | } |
---|
| 133 | |
---|
[173] | 134 | }// namespace network |
---|
[196] | 135 | |
---|
| 136 | #endif |
---|