Changeset 174
- Timestamp:
- Nov 6, 2007, 11:32:34 PM (17 years ago)
- Location:
- code/branches/network/src/network
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/PacketBuffer.cc
r173 r174 8 8 9 9 PacketBuffer::PacketBuffer(){ 10 locked=false;10 closed=false; 11 11 first=NULL; 12 12 last=NULL; … … 14 14 15 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; 16 boost::mutex::scoped_lock lock(networkPacketBufferMutex); 17 // also works if fifo is null (queue empty) 18 // just to be sure last is really the last element 19 while(last!=NULL && last->next!=NULL) 20 last=last->next; 21 // first element? 22 if(first==NULL){ 23 first=new QueueItem; 24 last=first; 25 last->next=NULL; 26 // change this!!!!!!! 27 last->packet = new PacketEnvelope; 28 last->packet->data = pck.data; 29 last->packet->length = pck.length; 30 } else { 31 //insert a new element at the bottom 32 last->next = new QueueItem; 33 last=last->next; 34 // initialize last->next 35 last->next=NULL; 36 // save the packet to the new element 37 // change this!!!!!!!! 38 last->packet = new PacketEnvelope; 39 last->packet->data = pck.data; 40 last->packet->length = pck.length; 45 41 } 46 else 47 return false; 42 return true; 48 43 } 49 44 50 PacketEnvelope *PacketBuffer::pop(){51 if(!locked && first!=NULL){52 locked = true;45 PacketEnvelope PacketBuffer::pop(){ 46 boost::mutex::scoped_lock lock(networkPacketBufferMutex); 47 if(first!=NULL){ 53 48 QueueItem *temp = first; 54 49 // get packet … … 57 52 first = first->next; 58 53 delete temp; 59 locked=false; 54 return *p; 55 } else{ 56 PacketEnvelope p = {0,0}; 60 57 return p; 61 } else 62 return NULL; 58 } 63 59 } 64 60 65 61 bool PacketBuffer::isEmpty(){ 66 62 return (first==NULL); 67 }68 69 bool PacketBuffer::isLocked(){70 return locked;71 63 } 72 64 … … 80 72 } 81 73 74 bool PacketBuffer::isClosed(){ 75 return closed; 76 } 77 78 void PacketBuffer::setClosed(bool value){ 79 closed=value; 80 return; 81 } 82 82 83 }// namespace network -
code/branches/network/src/network/PacketBuffer.h
r173 r174 16 16 #include <queue> 17 17 #include <string> 18 #include <boost/bind.hpp> 19 #include <boost/thread/mutex.hpp> 20 #include <boost/thread/mutex.hpp> 21 22 //this is needed in order to make the packetbuffer threadsafe 23 boost::mutex networkPacketBufferMutex; 18 24 19 25 namespace network{ … … 32 38 public: 33 39 PacketBuffer(); 34 bool isLocked();35 40 bool isEmpty(); 41 bool isClosed(); 42 void setClosed(bool value); 43 void print(); 36 44 // pops a packet from the queue 37 PacketEnvelope *pop();45 PacketEnvelope pop(); 38 46 // pushs a packet to the queue 39 47 bool push(PacketEnvelope pck); 40 void print();41 48 private: 42 bool locked;43 49 QueueItem *first; 44 50 QueueItem *last; 51 bool closed; 52 53 //make it threadsafe 54 // boost::mutex mutex; 45 55 }; 46 56 -
code/branches/network/src/network/PacketBufferTest.cc
r173 r174 19 19 test.print(); 20 20 while(!test.isEmpty()){ 21 int i=test.pop() ->data;21 int i=test.pop().data; 22 22 std::cout << "We popped the value " << i << std::endl; 23 23 }
Note: See TracChangeset
for help on using the changeset viewer.