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