Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/merger/src/network/PacketBuffer.cc @ 278

Last change on this file since 278 was 278, checked in by nicolasc, 16 years ago

merged network

File size: 2.0 KB
Line 
1// C++ PacketBuffer
2// d
3// Author Oliver Scheuss
4
5#ifndef NETWORK_PACKETBUFFER_CC
6#define NETWORK_PACKETBUFFER_CC
7
8#include <iostream>
9#include "network/PacketBuffer.h"
10
11namespace network{
12 
13   boost::mutex networkPacketBufferMutex;
14 
15PacketBuffer::PacketBuffer(){
16  closed=false;
17  first=NULL;
18  last=NULL;
19}
20    //this is needed in order to make the packetbuffer threadsafe
21 
22
23bool PacketBuffer::push(ENetEvent *ev){
24  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
25//   if(closed)
26//     return false;
27  // first element?
28  if(first==NULL){
29    first=new QueueItem;
30    last=first;
31    last->next=NULL;
32    // change this!!!!!!!
33    last->packet = ev->packet;
34    last->address = ev->peer->address;
35    } else {
36    //insert a new element at the bottom
37    last->next = new QueueItem;
38    last=last->next;
39    // initialize last->next
40    last->next=NULL;
41    // save the packet to the new element
42    last->packet = ev->packet;
43    last->address = ev->peer->address;
44  }
45  return true;
46}
47
48ENetPacket *PacketBuffer::pop(){
49  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
50  if(first!=NULL /*&& !closed*/){
51    QueueItem *temp = first;
52    // get packet
53    ENetPacket *pck=first->packet;
54    // remove first element
55    first = first->next;
56    delete temp;
57    return pck;
58  } else{
59    return NULL;
60  }
61}
62
63ENetPacket *PacketBuffer::pop(ENetAddress &address){
64  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
65  if(first!=NULL /*&& !closed*/){
66    QueueItem *temp = first;
67    // get packet
68    ENetPacket *pck=first->packet;
69    address = first->address;
70    // remove first element
71    first = first->next;
72    delete temp;
73    return pck;
74  } else{
75    return NULL;
76  }
77}
78
79bool PacketBuffer::isEmpty(){
80  return (first==NULL);
81}
82
83void PacketBuffer::print(){
84  QueueItem *temp=first;
85  while(temp!=NULL){
86//    std::cout << temp->packet->data << std::endl;
87    temp=temp->next;
88  }
89 
90}
91
92bool PacketBuffer::isClosed(){
93  return closed;
94}
95
96void PacketBuffer::setClosed(bool value){
97  closed=value;
98  return;
99}
100
101}// namespace network
102
103#endif
Note: See TracBrowser for help on using the repository browser.