Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weapon/src/network/PacketBuffer.cc @ 1265

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

merged FICN back into trunk
awaiting release.

File size: 3.5 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    return true;
76  }
77
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    }
94  }
95
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    }
112  }
113
114  bool PacketBuffer::isEmpty() {
115    return (first==NULL);
116  }
117
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    }
124
125  }
126
127  bool PacketBuffer::isClosed() {
128    return closed;
129  }
130
131  void PacketBuffer::setClosed(bool value){
132    closed=value;
133    return;
134  }
135
136} // namespace network
Note: See TracBrowser for help on using the repository browser.