[196] | 1 | // |
---|
| 2 | // Dummy client to test ConnectionManager and PacketBuffer classes |
---|
| 3 | // |
---|
| 4 | // Author: Oliver Scheuss |
---|
| 5 | |
---|
| 6 | #include <iostream> |
---|
| 7 | #include "enet/enet.h" |
---|
| 8 | |
---|
| 9 | using namespace std; |
---|
| 10 | |
---|
| 11 | int main(){ |
---|
| 12 | ENetHost * client; |
---|
| 13 | ENetAddress address; |
---|
| 14 | ENetEvent event; |
---|
| 15 | ENetPeer *peer; |
---|
| 16 | |
---|
| 17 | enet_initialize(); |
---|
| 18 | atexit(enet_deinitialize); |
---|
| 19 | |
---|
| 20 | enet_address_set_host(&address, "127.0.0.1"); |
---|
| 21 | address.port = 5555; |
---|
| 22 | |
---|
| 23 | // create client object |
---|
| 24 | client = enet_host_create(NULL, 2, 0, 0); |
---|
| 25 | |
---|
| 26 | if(client==NULL){ |
---|
| 27 | fprintf(stderr, "An error occured"); |
---|
| 28 | exit(EXIT_FAILURE); |
---|
| 29 | } |
---|
| 30 | // connect peer |
---|
| 31 | peer = enet_host_connect(client, &address, 2); |
---|
| 32 | if(peer==NULL){ |
---|
| 33 | fprintf(stderr, "Peer establishing error"); |
---|
| 34 | exit(EXIT_FAILURE); |
---|
| 35 | } |
---|
| 36 | // wait 5 seconds for the connection attempt to succeed |
---|
| 37 | if(enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT){ |
---|
| 38 | puts("Connection to localhost:5555 succeeded."); |
---|
| 39 | }else{ |
---|
| 40 | enet_peer_reset(peer); |
---|
| 41 | puts("Connection to localhost:5555 failed."); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | for(int i=0; i<10; i++){ |
---|
| 45 | // weihnachtsmann bringt packete |
---|
| 46 | ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE); |
---|
| 47 | // extend the packet and append the string foo to it |
---|
| 48 | // send packet to peer on channel id 0 |
---|
| 49 | enet_peer_send(peer, 1, packet); |
---|
| 50 | // keep the timeout very small for low delay |
---|
| 51 | if(enet_host_service(client, &event, 1)==0){ |
---|
| 52 | cout << "successfully sent: " << event.type << endl; |
---|
| 53 | }else{ |
---|
| 54 | cout << "failed sending" << endl; |
---|
| 55 | } |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | // now disconnect |
---|
| 59 | enet_peer_disconnect (peer, 0); |
---|
| 60 | // 3 seconds timeout |
---|
| 61 | while(enet_host_service(client, &event, 3000) > 0){ |
---|
| 62 | switch (event.type) |
---|
| 63 | { |
---|
| 64 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 65 | enet_packet_destroy(event.packet); |
---|
| 66 | break; |
---|
| 67 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 68 | puts("Disconnection succeeded."); |
---|
| 69 | return 0; |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | // if disconnect failed |
---|
| 73 | enet_peer_reset(peer); |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | |
---|
| 77 | return 0; |
---|
| 78 | } |
---|