Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

created a dummyserver and dummyclient in order to test ConnectionManager and PacketBuffer with enet and boost_thread\n Makefile is used to build server and client

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