| [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); |
|---|
| [188] | 52 | // if(closed) |
|---|
| 53 | // return false; |
|---|
| [174] | 54 | // first element? |
|---|
| 55 | if(first==NULL){ |
|---|
| 56 | first=new QueueItem; |
|---|
| 57 | last=first; |
|---|
| 58 | last->next=NULL; |
|---|
| 59 | // change this!!!!!!! |
|---|
| [204] | 60 | last->packet = ev->packet; |
|---|
| 61 | last->address = ev->peer->address; |
|---|
| [188] | 62 | } else { |
|---|
| [174] | 63 | //insert a new element at the bottom |
|---|
| 64 | last->next = new QueueItem; |
|---|
| 65 | last=last->next; |
|---|
| 66 | // initialize last->next |
|---|
| 67 | last->next=NULL; |
|---|
| 68 | // save the packet to the new element |
|---|
| [204] | 69 | last->packet = ev->packet; |
|---|
| 70 | last->address = ev->peer->address; |
|---|
| [173] | 71 | } |
|---|
| [174] | 72 | return true; |
|---|
| [173] | 73 | } |
|---|
| 74 | |
|---|
| [188] | 75 | ENetPacket *PacketBuffer::pop(){ |
|---|
| [174] | 76 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
|---|
| [188] | 77 | if(first!=NULL /*&& !closed*/){ |
|---|
| [173] | 78 | QueueItem *temp = first; |
|---|
| 79 | // get packet |
|---|
| [188] | 80 | ENetPacket *pck=first->packet; |
|---|
| [173] | 81 | // remove first element |
|---|
| 82 | first = first->next; |
|---|
| 83 | delete temp; |
|---|
| [188] | 84 | return pck; |
|---|
| [174] | 85 | } else{ |
|---|
| [188] | 86 | return NULL; |
|---|
| [174] | 87 | } |
|---|
| [173] | 88 | } |
|---|
| 89 | |
|---|
| [204] | 90 | ENetPacket *PacketBuffer::pop(ENetAddress &address){ |
|---|
| 91 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
|---|
| 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 | return pck; |
|---|
| 101 | } else{ |
|---|
| 102 | return NULL; |
|---|
| 103 | } |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| [173] | 106 | bool PacketBuffer::isEmpty(){ |
|---|
| 107 | return (first==NULL); |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | void PacketBuffer::print(){ |
|---|
| 111 | QueueItem *temp=first; |
|---|
| 112 | while(temp!=NULL){ |
|---|
| [204] | 113 | // std::cout << temp->packet->data << std::endl; |
|---|
| [173] | 114 | temp=temp->next; |
|---|
| 115 | } |
|---|
| [285] | 116 | |
|---|
| [173] | 117 | } |
|---|
| 118 | |
|---|
| [174] | 119 | bool PacketBuffer::isClosed(){ |
|---|
| 120 | return closed; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | void PacketBuffer::setClosed(bool value){ |
|---|
| 124 | closed=value; |
|---|
| 125 | return; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| [173] | 128 | }// namespace network |
|---|
| [196] | 129 | |
|---|
| 130 | #endif |
|---|