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