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 | //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!!!!!!! |
---|
61 | last->packet = ev->packet; |
---|
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->packet = ev->packet; |
---|
71 | last->address = ev->peer->address; |
---|
72 | } |
---|
73 | return true; |
---|
74 | } |
---|
75 | |
---|
76 | ENetPacket *PacketBuffer::pop(){ |
---|
77 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
78 | //std::cout << "packetbuffer pop" << std::endl; |
---|
79 | if(first!=NULL /*&& !closed*/){ |
---|
80 | QueueItem *temp = first; |
---|
81 | // get packet |
---|
82 | ENetPacket *pck=first->packet; |
---|
83 | // remove first element |
---|
84 | first = first->next; |
---|
85 | delete temp; |
---|
86 | //std::cout << "pop size of packet " << pck->dataLength << std::endl; |
---|
87 | return pck; |
---|
88 | } else{ |
---|
89 | //std::cout << "nothing to return" << std::endl; |
---|
90 | return NULL; |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | ENetPacket *PacketBuffer::pop(ENetAddress &address){ |
---|
95 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
96 | //std::cout << "packetbuffer pop(address)" << std::endl; |
---|
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; |
---|
105 | //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl; |
---|
106 | return pck; |
---|
107 | } else{ |
---|
108 | return NULL; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | bool PacketBuffer::isEmpty(){ |
---|
113 | return (first==NULL); |
---|
114 | } |
---|
115 | |
---|
116 | void PacketBuffer::print(){ |
---|
117 | QueueItem *temp=first; |
---|
118 | while(temp!=NULL){ |
---|
119 | // std::cout << temp->packet->data << std::endl; |
---|
120 | temp=temp->next; |
---|
121 | } |
---|
122 | |
---|
123 | } |
---|
124 | |
---|
125 | bool PacketBuffer::isClosed(){ |
---|
126 | return closed; |
---|
127 | } |
---|
128 | |
---|
129 | void PacketBuffer::setClosed(bool value){ |
---|
130 | closed=value; |
---|
131 | return; |
---|
132 | } |
---|
133 | |
---|
134 | }// namespace network |
---|
135 | |
---|
136 | #endif |
---|