Last change
on this file since 173 was
173,
checked in by scheusso, 16 years ago
|
created classes PacketBuffer and ConnectionManager
PacketBuffer: (hopefully) threadsafe implementation of a packet buffer
for communication between threads
ConnectionManager: low-level managment of connection; implementation of
multiple threads
|
File size:
1.7 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 | locked=false; |
---|
11 | first=NULL; |
---|
12 | last=NULL; |
---|
13 | } |
---|
14 | |
---|
15 | bool PacketBuffer::push(PacketEnvelope pck){ |
---|
16 | if(!locked){ |
---|
17 | locked=true; |
---|
18 | // also works if fifo is null (queue empty) |
---|
19 | // just to be sure last is really the last element |
---|
20 | while(last!=NULL && last->next!=NULL) |
---|
21 | last=last->next; |
---|
22 | // first element? |
---|
23 | if(first==NULL){ |
---|
24 | first=new QueueItem; |
---|
25 | last=first; |
---|
26 | last->next=NULL; |
---|
27 | // change this!!!!!!! |
---|
28 | last->packet = new PacketEnvelope; |
---|
29 | last->packet->data = pck.data; |
---|
30 | last->packet->length = pck.length; |
---|
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 | // change this!!!!!!!! |
---|
39 | last->packet = new PacketEnvelope; |
---|
40 | last->packet->data = pck.data; |
---|
41 | last->packet->length = pck.length; |
---|
42 | } |
---|
43 | locked=false; |
---|
44 | return true; |
---|
45 | } |
---|
46 | else |
---|
47 | return false; |
---|
48 | } |
---|
49 | |
---|
50 | PacketEnvelope *PacketBuffer::pop(){ |
---|
51 | if(!locked && first!=NULL){ |
---|
52 | locked = true; |
---|
53 | QueueItem *temp = first; |
---|
54 | // get packet |
---|
55 | PacketEnvelope *p = first->packet; |
---|
56 | // remove first element |
---|
57 | first = first->next; |
---|
58 | delete temp; |
---|
59 | locked=false; |
---|
60 | return p; |
---|
61 | } else |
---|
62 | return NULL; |
---|
63 | } |
---|
64 | |
---|
65 | bool PacketBuffer::isEmpty(){ |
---|
66 | return (first==NULL); |
---|
67 | } |
---|
68 | |
---|
69 | bool PacketBuffer::isLocked(){ |
---|
70 | return locked; |
---|
71 | } |
---|
72 | |
---|
73 | void PacketBuffer::print(){ |
---|
74 | QueueItem *temp=first; |
---|
75 | while(temp!=NULL){ |
---|
76 | std::cout << temp->packet->data << std::endl; |
---|
77 | temp=temp->next; |
---|
78 | } |
---|
79 | |
---|
80 | } |
---|
81 | |
---|
82 | }// namespace network |
---|
Note: See
TracBrowser
for help on using the repository browser.