Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/network/PacketBuffer.cc @ 1299

Last change on this file since 1299 was 1299, checked in by scheusso, 16 years ago

a huge fix in packetbuffer

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