Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

cleaned up network, builds with CML

File size: 2.0 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>
[285]9#include "PacketBuffer.h"
[173]10
11namespace network{
[285]12
[196]13   boost::mutex networkPacketBufferMutex;
[285]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
[173]21
[285]22
[204]23bool PacketBuffer::push(ENetEvent *ev){
[174]24  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
[188]25//   if(closed)
26//     return false;
[174]27  // first element?
28  if(first==NULL){
29    first=new QueueItem;
30    last=first;
31    last->next=NULL;
32    // change this!!!!!!!
[204]33    last->packet = ev->packet;
34    last->address = ev->peer->address;
[188]35    } else {
[174]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
[204]42    last->packet = ev->packet;
43    last->address = ev->peer->address;
[173]44  }
[174]45  return true;
[173]46}
47
[188]48ENetPacket *PacketBuffer::pop(){
[174]49  boost::mutex::scoped_lock lock(networkPacketBufferMutex);
[188]50  if(first!=NULL /*&& !closed*/){
[173]51    QueueItem *temp = first;
52    // get packet
[188]53    ENetPacket *pck=first->packet;
[173]54    // remove first element
55    first = first->next;
56    delete temp;
[188]57    return pck;
[174]58  } else{
[188]59    return NULL;
[174]60  }
[173]61}
62
[204]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
[173]79bool PacketBuffer::isEmpty(){
80  return (first==NULL);
81}
82
83void PacketBuffer::print(){
84  QueueItem *temp=first;
85  while(temp!=NULL){
[204]86//    std::cout << temp->packet->data << std::endl;
[173]87    temp=temp->next;
88  }
[285]89
[173]90}
91
[174]92bool PacketBuffer::isClosed(){
93  return closed;
94}
95
96void PacketBuffer::setClosed(bool value){
97  closed=value;
98  return;
99}
100
[173]101}// namespace network
[196]102
103#endif
Note: See TracBrowser for help on using the repository browser.