Last change
on this file since 203 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
|
Line | |
---|
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 "network/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(ENetPacket *pck){ |
---|
24 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
25 | // if(closed) |
---|
26 | // return false; |
---|
27 | // also works if fifo is null (queue empty) |
---|
28 | // just to be sure last is really the last element |
---|
29 | /*if(last!=NULL) |
---|
30 | while(last->next!=NULL) |
---|
31 | last=last->next;*/ |
---|
32 | // first element? |
---|
33 | if(first==NULL){ |
---|
34 | first=new QueueItem; |
---|
35 | last=first; |
---|
36 | last->next=NULL; |
---|
37 | // change this!!!!!!! |
---|
38 | last->packet = pck; |
---|
39 | } else { |
---|
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 |
---|
46 | last->packet = pck; |
---|
47 | } |
---|
48 | return true; |
---|
49 | } |
---|
50 | |
---|
51 | ENetPacket *PacketBuffer::pop(){ |
---|
52 | boost::mutex::scoped_lock lock(networkPacketBufferMutex); |
---|
53 | if(first!=NULL /*&& !closed*/){ |
---|
54 | QueueItem *temp = first; |
---|
55 | // get packet |
---|
56 | ENetPacket *pck=first->packet; |
---|
57 | // remove first element |
---|
58 | first = first->next; |
---|
59 | delete temp; |
---|
60 | return pck; |
---|
61 | } else{ |
---|
62 | return NULL; |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | bool PacketBuffer::isEmpty(){ |
---|
67 | return (first==NULL); |
---|
68 | } |
---|
69 | |
---|
70 | void 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 | |
---|
79 | bool PacketBuffer::isClosed(){ |
---|
80 | return closed; |
---|
81 | } |
---|
82 | |
---|
83 | void PacketBuffer::setClosed(bool value){ |
---|
84 | closed=value; |
---|
85 | return; |
---|
86 | } |
---|
87 | |
---|
88 | }// namespace network |
---|
89 | |
---|
90 | #endif |
---|
Note: See
TracBrowser
for help on using the repository browser.