Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/PacketBuffer.cc @ 1088

Last change on this file since 1088 was 1088, checked in by dumenim, 16 years ago

catched some return values

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