Last change
on this file since 356 was
355,
checked in by landauf, 17 years ago
|
changed retos usleep hack to a more generic solution:
#ifdef WIN32
#include <windows.h>
#define usleep(x) Sleep((x)/1000)
|
File size:
1.2 KB
|
Rev | Line | |
---|
[188] | 1 | #include <string> |
---|
| 2 | #include <iostream> |
---|
| 3 | #include <enet/enet.h> |
---|
| 4 | #include <boost/thread/thread.hpp> |
---|
[285] | 5 | #include "PacketBuffer.h" |
---|
| 6 | #include "PacketBuffer.cc" |
---|
[175] | 7 | |
---|
| 8 | using namespace network; |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | void write(PacketBuffer *test){ |
---|
[351] | 12 | ENetEvent event; |
---|
[188] | 13 | ENetPacket *packet; |
---|
[175] | 14 | if(test->isEmpty()) |
---|
| 15 | std::cout << "empty buffer" << std::endl; |
---|
| 16 | for(int i=0; i<10; i++){ |
---|
[188] | 17 | std::string temp = "packet "; |
---|
[285] | 18 | packet = enet_packet_create("packet", strlen("packet ")+1, |
---|
[188] | 19 | ENET_PACKET_FLAG_RELIABLE); |
---|
| 20 | std::cout << i << ": pushing " << packet->data << std::endl; |
---|
[351] | 21 | event.packet=packet; |
---|
| 22 | test->push(&event); |
---|
[175] | 23 | if(i==5) |
---|
| 24 | usleep(200000); |
---|
| 25 | } |
---|
| 26 | test->setClosed(true); |
---|
| 27 | return; |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | void read(PacketBuffer *test){ |
---|
[187] | 31 | //test->print(); |
---|
[175] | 32 | // exit if the queue is closed and empty |
---|
| 33 | while(!test->isClosed() || !test->isEmpty()){ |
---|
| 34 | // only pop if the queue isn't empty |
---|
| 35 | while(!test->isEmpty()){ |
---|
[188] | 36 | std::cout << "We popped the value " << test->pop()->data << std::endl; |
---|
[175] | 37 | } |
---|
| 38 | } |
---|
| 39 | return; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | int main(int argc, char **argv[]){ |
---|
| 44 | PacketBuffer test = PacketBuffer(); |
---|
| 45 | boost::thread thrd1(boost::bind(&write, &test)); |
---|
| 46 | boost::thread thrd2(boost::bind(&read, &test)); |
---|
[285] | 47 | |
---|
[175] | 48 | thrd1.join(); |
---|
| 49 | thrd2.join(); |
---|
[285] | 50 | |
---|
[175] | 51 | return 0; |
---|
| 52 | } |
---|
| 53 | |
---|
Note: See
TracBrowser
for help on using the repository browser.