|
Last change
on this file since 351 was
351,
checked in by scheusso, 18 years ago
|
|
adjustet PacketBufferTestExt (because of changes in PacketBuffer) and Synchronisable.cc (registerAllVariables must be called by the Constructor of Synchronisable)
|
|
File size:
1.4 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 | |
|---|
| [346] | 10 | // workaround for usleep(int) under windows |
|---|
| 11 | #ifdef WIN32 |
|---|
| 12 | #include "winbase.h" |
|---|
| 13 | #endif |
|---|
| [175] | 14 | |
|---|
| [346] | 15 | |
|---|
| [175] | 16 | void write(PacketBuffer *test){ |
|---|
| [351] | 17 | ENetEvent event; |
|---|
| [188] | 18 | ENetPacket *packet; |
|---|
| [175] | 19 | if(test->isEmpty()) |
|---|
| 20 | std::cout << "empty buffer" << std::endl; |
|---|
| 21 | for(int i=0; i<10; i++){ |
|---|
| [188] | 22 | std::string temp = "packet "; |
|---|
| [285] | 23 | packet = enet_packet_create("packet", strlen("packet ")+1, |
|---|
| [188] | 24 | ENET_PACKET_FLAG_RELIABLE); |
|---|
| 25 | std::cout << i << ": pushing " << packet->data << std::endl; |
|---|
| [351] | 26 | event.packet=packet; |
|---|
| 27 | test->push(&event); |
|---|
| [175] | 28 | if(i==5) |
|---|
| [346] | 29 | // under windows, use Sleep(milliseconds) instead of usleep(microseconds) |
|---|
| 30 | #ifdef WIN32 |
|---|
| 31 | Sleep(200); |
|---|
| 32 | #else |
|---|
| [175] | 33 | usleep(200000); |
|---|
| [346] | 34 | #endif |
|---|
| [175] | 35 | } |
|---|
| 36 | test->setClosed(true); |
|---|
| 37 | return; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | void read(PacketBuffer *test){ |
|---|
| [187] | 41 | //test->print(); |
|---|
| [175] | 42 | // exit if the queue is closed and empty |
|---|
| 43 | while(!test->isClosed() || !test->isEmpty()){ |
|---|
| 44 | // only pop if the queue isn't empty |
|---|
| 45 | while(!test->isEmpty()){ |
|---|
| [188] | 46 | std::cout << "We popped the value " << test->pop()->data << std::endl; |
|---|
| [175] | 47 | } |
|---|
| 48 | } |
|---|
| 49 | return; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | int main(int argc, char **argv[]){ |
|---|
| 54 | PacketBuffer test = PacketBuffer(); |
|---|
| 55 | boost::thread thrd1(boost::bind(&write, &test)); |
|---|
| 56 | boost::thread thrd2(boost::bind(&read, &test)); |
|---|
| [285] | 57 | |
|---|
| [175] | 58 | thrd1.join(); |
|---|
| 59 | thrd2.join(); |
|---|
| [285] | 60 | |
|---|
| [175] | 61 | return 0; |
|---|
| 62 | } |
|---|
| 63 | |
|---|
Note: See
TracBrowser
for help on using the repository browser.