Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 938 was 790, checked in by nicolasc, 17 years ago

merged FICN back into trunk
awaiting release.

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