Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merge/src/network/PacketBuffer.cc @ 1341

Last change on this file since 1341 was 1318, checked in by scheusso, 17 years ago

made clientinformation threadsafe and improved packetbuffer; wrote a static function SpaceShip *SpaceShip::getLocalShip. it returns the pointer to the ship that should be steered

File size: 3.5 KB
RevLine 
[1168]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 */
[1299]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{
[1318]43   boost::recursive_mutex PacketBuffer::mutex_;
[1299]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) {
[1318]54    boost::recursive_mutex::scoped_lock lock(mutex_);
[1299]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;
[1318]63      // change this!!!!!!!  ---- we are not doing stl so we won't change this
[1299]64      last->packet = ev->packet;
65      last->address = ev->peer->address;
66      //last->address = ev->peer->address;
67    } else {
68      //insert a new element at the bottom
69      last->next = new QueueItem;
70      last=last->next;
71      // initialize last->next
72      last->next=NULL;
73      // save the packet to the new element
74      last->packet = ev->packet;
75      last->address = ev->peer->address;
76      //last->address = ev->peer->address;
77    }
[1318]78    lock.unlock();
79    return true;
[1299]80  }
81
82  //returns the first element in the list without deleting it but
83  //moving first pointer to next element
84  ENetPacket *PacketBuffer::pop() {
[1318]85    ENetAddress address;
86    return pop(address);
[1299]87  }
88
89  ENetPacket *PacketBuffer::pop(ENetAddress &address) {
[1318]90    boost::recursive_mutex::scoped_lock lock(mutex_);
[1299]91    //std::cout << "packetbuffer pop(address)" << std::endl;
92    if(first!=NULL /*&& !closed*/){
93      QueueItem *temp = first;
94      // get packet
95      ENetPacket *pck=first->packet;
96      address = first->address;
97      // remove first element
98      first = first->next;
99      delete temp;
[1318]100      lock.unlock();
[1299]101      //std::cout << "pop(address) size of packet " << pck->dataLength << std::endl;
102      return pck;
103    } else{
[1318]104      lock.unlock();
[1299]105      return NULL;
106    }
107  }
108
109  bool PacketBuffer::isEmpty() {
110    return (first==NULL);
111  }
112
113  void PacketBuffer::print() {
114    QueueItem *temp=first;
115    while(temp!=NULL){
116      //    std::cout << temp->packet->data << std::endl;
117      temp=temp->next;
118    }
119
120  }
121
122  bool PacketBuffer::isClosed() {
123    return closed;
124  }
125
126  void PacketBuffer::setClosed(bool value){
127    closed=value;
128    return;
129  }
130
131} // namespace network
Note: See TracBrowser for help on using the repository browser.