| 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 "PacketBuffer.h" |
|---|
| 10 | |
|---|
| 11 | namespace network{ |
|---|
| 12 | |
|---|
| 13 | boost::mutex networkPacketBufferMutex; |
|---|
| 14 | |
|---|
| 15 | PacketBuffer::PacketBuffer(){ |
|---|
| 16 | closed=false; |
|---|
| 17 | first=NULL; |
|---|
| 18 | last=NULL; |
|---|
| 19 | } |
|---|
| 20 | //this is needed in order to make the packetbuffer threadsafe |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | bool 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 | |
|---|
| 48 | ENetPacket *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 | |
|---|
| 63 | ENetPacket *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 | |
|---|
| 79 | bool PacketBuffer::isEmpty(){ |
|---|
| 80 | return (first==NULL); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void 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 | |
|---|
| 92 | bool PacketBuffer::isClosed(){ |
|---|
| 93 | return closed; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void PacketBuffer::setClosed(bool value){ |
|---|
| 97 | closed=value; |
|---|
| 98 | return; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | }// namespace network |
|---|
| 102 | |
|---|
| 103 | #endif |
|---|