Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network3/src/network/PacketBuffer.cc @ 1254

Last change on this file since 1254 was 1168, checked in by scheusso, 17 years ago

added new network files

File size: 3.8 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    } else {
67      //insert a new element at the bottom
68      last->next = new QueueItem;
69      last=last->next;
70      // initialize last->next
71      last->next=NULL;
72      // save the packet to the new element
73      last->packet = ev->packet;
74      //last->address = ev->peer->address;
75    }
76    // pseudo bugfix: added a return false statement for error handling
77    if ( last->packet == ev->packet ) return true;
78    return false;
79  }
80
81  //returns the first element in the list without deleting it but
82  //moving first pointer to next element
83  ENetPacket *PacketBuffer::pop() {
84    boost::mutex::scoped_lock lock(networkPacketBufferMutex);
85    //std::cout << "packetbuffer pop" << std::endl;
86    if(first!=NULL /*&& !closed*/){
87      QueueItem *temp = first;
88      // get packet
89      ENetPacket *pck=first->packet;
90      // remove first element
91      first = first->next;
92      delete temp;
93      //std::cout << "pop size of packet " << pck->dataLength << std::endl;
94      return pck;
95    } else{
96      //std::cout << "nothing to return" << std::endl;
97      return NULL;
98    }
99  }
100
101  ENetPacket *PacketBuffer::pop(ENetAddress &address) {
102    boost::mutex::scoped_lock lock(networkPacketBufferMutex);
103    //std::cout << "packetbuffer pop(address)" << std::endl;
104    if(first!=NULL /*&& !closed*/){
105      QueueItem *temp = first;
106      // get packet
107      ENetPacket *pck=first->packet;
108      address = first->address;
109      // remove first element
110      first = first->next;
111      delete temp;
112      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
113      return pck;
114    } else{
115      return NULL;
116    }
117  }
118
119  bool PacketBuffer::isEmpty() {
120    return (first==NULL);
121  }
122
123  void PacketBuffer::print() {
124    QueueItem *temp=first;
125    while(temp!=NULL){
126      //    std::cout << temp->packet->data << std::endl;
127      temp=temp->next;
128    }
129
130  }
131
132  bool PacketBuffer::isClosed() {
133    return closed;
134  }
135
136  void PacketBuffer::setClosed(bool value){
137    closed=value;
138    return;
139  }
140
141} // namespace network
Note: See TracBrowser for help on using the repository browser.