- Timestamp:
- Nov 7, 2007, 8:38:47 PM (17 years ago)
- Location:
- code/branches/network/src/network
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/network/src/network/PacketBuffer.cc
r174 r188 13 13 } 14 14 15 bool PacketBuffer::push( PacketEnvelopepck){15 bool PacketBuffer::push(ENetPacket *pck){ 16 16 boost::mutex::scoped_lock lock(networkPacketBufferMutex); 17 // if(closed) 18 // return false; 17 19 // also works if fifo is null (queue empty) 18 20 // just to be sure last is really the last element 19 while(last!=NULL && last->next!=NULL) 20 last=last->next; 21 /*if(last!=NULL) 22 while(last->next!=NULL) 23 last=last->next;*/ 21 24 // first element? 22 25 if(first==NULL){ … … 25 28 last->next=NULL; 26 29 // change this!!!!!!! 27 last->packet = new PacketEnvelope; 28 last->packet->data = pck.data; 29 last->packet->length = pck.length; 30 } else { 30 last->packet = pck; 31 } else { 31 32 //insert a new element at the bottom 32 33 last->next = new QueueItem; … … 35 36 last->next=NULL; 36 37 // 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; 38 last->packet = pck; 41 39 } 42 40 return true; 43 41 } 44 42 45 PacketEnvelopePacketBuffer::pop(){43 ENetPacket *PacketBuffer::pop(){ 46 44 boost::mutex::scoped_lock lock(networkPacketBufferMutex); 47 if(first!=NULL ){45 if(first!=NULL /*&& !closed*/){ 48 46 QueueItem *temp = first; 49 47 // get packet 50 PacketEnvelope *p =first->packet;48 ENetPacket *pck=first->packet; 51 49 // remove first element 52 50 first = first->next; 53 51 delete temp; 54 return *p;52 return pck; 55 53 } else{ 56 PacketEnvelope p = {0,0}; 57 return p; 54 return NULL; 58 55 } 59 56 } -
code/branches/network/src/network/PacketBuffer.h
r174 r188 16 16 #include <queue> 17 17 #include <string> 18 #include <enet/enet.h> 18 19 #include <boost/bind.hpp> 19 20 #include <boost/thread/mutex.hpp> … … 31 32 32 33 struct QueueItem{ 33 PacketEnvelope*packet;34 ENetPacket *packet; 34 35 QueueItem *next; 35 36 }; … … 43 44 void print(); 44 45 // pops a packet from the queue 45 PacketEnvelopepop();46 ENetPacket *pop(); 46 47 // pushs a packet to the queue 47 bool push( PacketEnvelopepck);48 bool push(ENetPacket *pck); 48 49 private: 49 50 QueueItem *first; -
code/branches/network/src/network/PacketBufferTestExt.cc
r187 r188 1 #include <string> 2 #include <iostream> 3 #include <enet/enet.h> 4 #include <boost/thread/thread.hpp> 1 5 #include "network/PacketBuffer.h" 2 6 #include "network/PacketBuffer.cc" 3 #include <boost/thread/thread.hpp>4 #include <iostream>5 #include <string>6 7 7 8 using namespace network; … … 9 10 10 11 void write(PacketBuffer *test){ 11 PacketEnvelope p;12 ENetPacket *packet; 12 13 if(test->isEmpty()) 13 14 std::cout << "empty buffer" << std::endl; 14 15 for(int i=0; i<10; i++){ 15 p.data=i*i; 16 std::cout << i << ": pushing " << p.data << std::endl; 17 test->push(p); 16 std::string temp = "packet "; 17 packet = enet_packet_create("packet", strlen("packet ")+1, 18 ENET_PACKET_FLAG_RELIABLE); 19 std::cout << i << ": pushing " << packet->data << std::endl; 20 test->push(packet); 18 21 if(i==5) 19 22 usleep(200000); … … 29 32 // only pop if the queue isn't empty 30 33 while(!test->isEmpty()){ 31 int i=test->pop().data; 32 std::cout << "We popped the value " << i << std::endl; 34 std::cout << "We popped the value " << test->pop()->data << std::endl; 33 35 } 34 36 }
Note: See TracChangeset
for help on using the changeset viewer.