Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/network/src/network/PacketBuffer.cc @ 188

Last change on this file since 188 was 188, checked in by scheusso, 16 years ago

adapted PacketBuffer to work with ENetPacket *

File size: 1.6 KB
RevLine 
[173]1// C++ PacketBuffer
2//
3// Author Oliver Scheuss
4#include <iostream>
5#include "network/PacketBuffer.h"
6
7namespace network{
8
9PacketBuffer::PacketBuffer(){
[174]10  closed=false;
[173]11  first=NULL;
12  last=NULL;
13}
14
[188]15bool PacketBuffer::push(ENetPacket *pck){
[174]16  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
[188]17//   if(closed)
18//     return false;
[174]19  // also works if fifo is null (queue empty)
20  // just to be sure last is really the last element
[188]21  /*if(last!=NULL)
22    while(last->next!=NULL)
23      last=last->next;*/
[174]24  // first element?
25  if(first==NULL){
26    first=new QueueItem;
27    last=first;
28    last->next=NULL;
29    // change this!!!!!!!
[188]30    last->packet = pck;
31    } else {
[174]32    //insert a new element at the bottom
33    last->next = new QueueItem;
34    last=last->next;
35    // initialize last->next
36    last->next=NULL;
37    // save the packet to the new element
[188]38    last->packet = pck;
[173]39  }
[174]40  return true;
[173]41}
42
[188]43ENetPacket *PacketBuffer::pop(){
[174]44  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
[188]45  if(first!=NULL /*&& !closed*/){
[173]46    QueueItem *temp = first;
47    // get packet
[188]48    ENetPacket *pck=first->packet;
[173]49    // remove first element
50    first = first->next;
51    delete temp;
[188]52    return pck;
[174]53  } else{
[188]54    return NULL;
[174]55  }
[173]56}
57
58bool PacketBuffer::isEmpty(){
59  return (first==NULL);
60}
61
62void PacketBuffer::print(){
63  QueueItem *temp=first;
64  while(temp!=NULL){
65    std::cout << temp->packet->data << std::endl;
66    temp=temp->next;
67  }
68 
69}
70
[174]71bool PacketBuffer::isClosed(){
72  return closed;
73}
74
75void PacketBuffer::setClosed(bool value){
76  closed=value;
77  return;
78}
79
[173]80}// namespace network
Note: See TracBrowser for help on using the repository browser.