[445] | 1 | //o |
---|
| 2 | // Dummy client to test ConnectionManager and PacketBuffer classes |
---|
| 3 | // |
---|
| 4 | // Author: Oliver Scheuss |
---|
| 5 | |
---|
| 6 | #include <iostream> |
---|
| 7 | #include <string> |
---|
| 8 | #include <enet/enet.h> |
---|
| 9 | #include "PacketManager.h" |
---|
| 10 | |
---|
[367] | 11 | #ifdef WIN32 |
---|
| 12 | #include <windows.h> |
---|
| 13 | #define usleep(x) Sleep((x)/1000) |
---|
| 14 | #else |
---|
| 15 | #include <unistd.h> |
---|
| 16 | #endif |
---|
[445] | 17 | |
---|
| 18 | |
---|
| 19 | using namespace std; |
---|
| 20 | |
---|
| 21 | int main(){ |
---|
| 22 | ENetHost * client; |
---|
| 23 | ENetAddress address; |
---|
| 24 | ENetEvent event; |
---|
| 25 | ENetPeer *peer; |
---|
| 26 | network::PacketGenerator pck; |
---|
| 27 | |
---|
| 28 | enet_initialize(); |
---|
| 29 | atexit(enet_deinitialize); |
---|
| 30 | |
---|
| 31 | cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << endl; |
---|
| 32 | string str; |
---|
| 33 | getline(cin, str); |
---|
| 34 | cout << "You entered: " << str << endl; |
---|
| 35 | if(str.compare("")==0) |
---|
| 36 | str="127.0.0.1"; |
---|
| 37 | |
---|
| 38 | enet_address_set_host(&address, str.c_str()); |
---|
| 39 | address.port = 55556; |
---|
| 40 | |
---|
| 41 | // create client object |
---|
| 42 | client = enet_host_create(NULL, 2, 0, 0); |
---|
| 43 | |
---|
| 44 | if(client==NULL){ |
---|
| 45 | fprintf(stderr, "An error occured"); |
---|
| 46 | exit(EXIT_FAILURE); |
---|
| 47 | } |
---|
| 48 | // connect peer |
---|
| 49 | peer = enet_host_connect(client, &address, 2); |
---|
| 50 | if(peer==NULL){ |
---|
| 51 | fprintf(stderr, "Peer establishing error"); |
---|
| 52 | exit(EXIT_FAILURE); |
---|
| 53 | } |
---|
| 54 | // wait 5 seconds for the connection attempt to succeed |
---|
| 55 | if(enet_host_service(client, &event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT){ |
---|
| 56 | cout << "Connection to " << str << " succeeded." << endl; |
---|
| 57 | //puts("Connection to localhost:5555 succeeded."); |
---|
| 58 | }else{ |
---|
| 59 | enet_peer_reset(peer); |
---|
| 60 | cout << "Connection to " << str << " failed." << endl; |
---|
| 61 | //puts("Connection to localhost:5555 failed."); |
---|
| 62 | exit(EXIT_FAILURE); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | for(int i=0; i<10; i++){ |
---|
| 66 | // weihnachtsmann bringt packete |
---|
| 67 | //ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE); |
---|
| 68 | // extend the packet and append the string foo to it |
---|
| 69 | // send packet to peer on channel id 0 |
---|
| 70 | enet_peer_send(peer, 1, pck.chatMessage("test2")); |
---|
| 71 | // keep the timeout very small for low delay |
---|
| 72 | if(enet_host_service(client, &event, 1)==0){ |
---|
| 73 | cout << "successfully sent: " << event.type << endl; |
---|
| 74 | }else{ |
---|
| 75 | cout << "failed sending" << endl; |
---|
| 76 | } |
---|
| 77 | usleep(1000000); |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | // now disconnect |
---|
| 81 | // enet_peer_disconnect (peer); |
---|
| 82 | enet_peer_disconnect (peer, 0); |
---|
| 83 | // 3 seconds timeout |
---|
| 84 | while(enet_host_service(client, &event, 3000) > 0){ |
---|
| 85 | switch (event.type) |
---|
| 86 | { |
---|
| 87 | case ENET_EVENT_TYPE_RECEIVE: |
---|
| 88 | enet_packet_destroy(event.packet); |
---|
| 89 | break; |
---|
| 90 | case ENET_EVENT_TYPE_DISCONNECT: |
---|
| 91 | puts("Disconnection succeeded."); |
---|
| 92 | return 0; |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | // if disconnect failed |
---|
| 96 | enet_peer_reset(peer); |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | |
---|
| 100 | return 0; |
---|
| 101 | } |
---|