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 | |
---|
28 | // C++ PacketBuffer |
---|
29 | // d |
---|
30 | // Author Oliver Scheuss |
---|
31 | |
---|
32 | #ifndef NETWORK_PACKETBUFFER_CC |
---|
33 | #define NETWORK_PACKETBUFFER_CC |
---|
34 | |
---|
35 | #include <iostream> |
---|
36 | #include "PacketBuffer.h" |
---|
37 | |
---|
38 | namespace network{ |
---|
39 | |
---|
40 | boost::mutex networkPacketBufferMutex; |
---|
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){ |
---|
51 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
52 | // if(closed) |
---|
53 | // return false; |
---|
54 | // first element? |
---|
55 | if(first==NULL){ |
---|
56 | first=new QueueItem; |
---|
57 | last=first; |
---|
58 | last->next=NULL; |
---|
59 | // change this!!!!!!! |
---|
60 | last->packet = ev->packet; |
---|
61 | last->address = ev->peer->address; |
---|
62 | } else { |
---|
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 |
---|
69 | last->packet = ev->packet; |
---|
70 | last->address = ev->peer->address; |
---|
71 | } |
---|
72 | return true; |
---|
73 | } |
---|
74 | |
---|
75 | ENetPacket *PacketBuffer::pop(){ |
---|
76 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
77 | if(first!=NULL /*&& !closed*/){ |
---|
78 | QueueItem *temp = first; |
---|
79 | // get packet |
---|
80 | ENetPacket *pck=first->packet; |
---|
81 | // remove first element |
---|
82 | first = first->next; |
---|
83 | delete temp; |
---|
84 | return pck; |
---|
85 | } else{ |
---|
86 | return NULL; |
---|
87 | } |
---|
88 | } |
---|
89 | |
---|
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 | |
---|
106 | bool PacketBuffer::isEmpty(){ |
---|
107 | return (first==NULL); |
---|
108 | } |
---|
109 | |
---|
110 | void PacketBuffer::print(){ |
---|
111 | QueueItem *temp=first; |
---|
112 | while(temp!=NULL){ |
---|
113 | // std::cout << temp->packet->data << std::endl; |
---|
114 | temp=temp->next; |
---|
115 | } |
---|
116 | |
---|
117 | } |
---|
118 | |
---|
119 | bool PacketBuffer::isClosed(){ |
---|
120 | return closed; |
---|
121 | } |
---|
122 | |
---|
123 | void PacketBuffer::setClosed(bool value){ |
---|
124 | closed=value; |
---|
125 | return; |
---|
126 | } |
---|
127 | |
---|
128 | }// namespace network |
---|
129 | |
---|
130 | #endif |
---|