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
|
Line | |
---|
1 | // C++ PacketBuffer |
---|
2 | // |
---|
3 | // Author Oliver Scheuss |
---|
4 | #include <iostream> |
---|
5 | #include "network/PacketBuffer.h" |
---|
6 | |
---|
7 | namespace network{ |
---|
8 | |
---|
9 | PacketBuffer::PacketBuffer(){ |
---|
10 | closed=false; |
---|
11 | first=NULL; |
---|
12 | last=NULL; |
---|
13 | } |
---|
14 | |
---|
15 | bool PacketBuffer::push(ENetPacket *pck){ |
---|
16 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
17 | // if(closed) |
---|
18 | // return false; |
---|
19 | // also works if fifo is null (queue empty) |
---|
20 | // just to be sure last is really the last element |
---|
21 | /*if(last!=NULL) |
---|
22 | while(last->next!=NULL) |
---|
23 | last=last->next;*/ |
---|
24 | // first element? |
---|
25 | if(first==NULL){ |
---|
26 | first=new QueueItem; |
---|
27 | last=first; |
---|
28 | last->next=NULL; |
---|
29 | // change this!!!!!!! |
---|
30 | last->packet = pck; |
---|
31 | } else { |
---|
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 |
---|
38 | last->packet = pck; |
---|
39 | } |
---|
40 | return true; |
---|
41 | } |
---|
42 | |
---|
43 | ENetPacket *PacketBuffer::pop(){ |
---|
44 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
45 | if(first!=NULL /*&& !closed*/){ |
---|
46 | QueueItem *temp = first; |
---|
47 | // get packet |
---|
48 | ENetPacket *pck=first->packet; |
---|
49 | // remove first element |
---|
50 | first = first->next; |
---|
51 | delete temp; |
---|
52 | return pck; |
---|
53 | } else{ |
---|
54 | return NULL; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | bool PacketBuffer::isEmpty(){ |
---|
59 | return (first==NULL); |
---|
60 | } |
---|
61 | |
---|
62 | void 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 | |
---|
71 | bool PacketBuffer::isClosed(){ |
---|
72 | return closed; |
---|
73 | } |
---|
74 | |
---|
75 | void PacketBuffer::setClosed(bool value){ |
---|
76 | closed=value; |
---|
77 | return; |
---|
78 | } |
---|
79 | |
---|
80 | }// namespace network |
---|
Note: See
TracBrowser
for help on using the repository browser.