Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/network/PacketBuffer.cc @ 1129

Last change on this file since 1129 was 1062, checked in by rgrieder, 17 years ago
  • changed header file inclusion order
File size: 3.6 KB
RevLine 
[1056]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 */
[514]28
[173]29// C++ PacketBuffer
[196]30// d
[173]31// Author Oliver Scheuss
[196]32
[1062]33#include "PacketBuffer.h"
34
[777]35#include <iostream>
36#include <queue>
37#include <string>
38#include <boost/bind.hpp>
39#include <boost/thread/mutex.hpp>
[196]40
[777]41namespace network
42{
43  boost::mutex networkPacketBufferMutex;
[285]44
[777]45  PacketBuffer::PacketBuffer() {
46    closed=false;
47    first=NULL;
48    last=NULL;
49  }
50  //this is needed in order to make the packetbuffer threadsafe
[285]51
[173]52
[777]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;
[188]66    } else {
[777]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    return true;
[173]77  }
78
[777]79  ENetPacket *PacketBuffer::pop() {
80    boost::mutex::scoped_lock lock(networkPacketBufferMutex);
81    //std::cout << "packetbuffer pop" << std::endl;
82    if(first!=NULL /*&& !closed*/){
83      QueueItem *temp = first;
84      // get packet
85      ENetPacket *pck=first->packet;
86      // remove first element
87      first = first->next;
88      delete temp;
89      //std::cout << "pop size of packet " << pck->dataLength << std::endl;
90      return pck;
91    } else{
92      //std::cout << "nothing to return" << std::endl;
93      return NULL;
94    }
[174]95  }
[173]96
[777]97  ENetPacket *PacketBuffer::pop(ENetAddress &address) {
98    boost::mutex::scoped_lock lock(networkPacketBufferMutex);
99    //std::cout << "packetbuffer pop(address)" << std::endl;
100    if(first!=NULL /*&& !closed*/){
101      QueueItem *temp = first;
102      // get packet
103      ENetPacket *pck=first->packet;
104      address = first->address;
105      // remove first element
106      first = first->next;
107      delete temp;
108      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
109      return pck;
110    } else{
111      return NULL;
112    }
[204]113  }
114
[777]115  bool PacketBuffer::isEmpty() {
116    return (first==NULL);
[173]117  }
[285]118
[777]119  void PacketBuffer::print() {
120    QueueItem *temp=first;
121    while(temp!=NULL){
122      //    std::cout << temp->packet->data << std::endl;
123      temp=temp->next;
124    }
[173]125
[777]126  }
[174]127
[777]128  bool PacketBuffer::isClosed() {
129    return closed;
130  }
[174]131
[777]132  void PacketBuffer::setClosed(bool value){
133    closed=value;
134    return;
135  }
[196]136
[777]137} // namespace network
Note: See TracBrowser for help on using the repository browser.