[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 | |
---|
[367] | 11 | #ifdef WIN32 |
---|
| 12 | #include <windows.h> |
---|
| 13 | #define usleep(x) Sleep((x)/1000) |
---|
| 14 | #else |
---|
| 15 | #include <unistd.h> |
---|
| 16 | #endif |
---|
[346] | 17 | |
---|
[367] | 18 | |
---|
[196] | 19 | using namespace std; |
---|
| 20 | |
---|
| 21 | int main(){ |
---|
| 22 | ENetHost * client; |
---|
| 23 | ENetAddress address; |
---|
| 24 | ENetEvent event; |
---|
| 25 | ENetPeer *peer; |
---|
[204] | 26 | network::PacketGenerator pck; |
---|
[196] | 27 | |
---|
| 28 | enet_initialize(); |
---|
| 29 | atexit(enet_deinitialize); |
---|
[285] | 30 | |
---|
[208] | 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"; |
---|
[285] | 37 | |
---|
[208] | 38 | enet_address_set_host(&address, str.c_str()); |
---|
[213] | 39 | address.port = 55556; |
---|
[196] | 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){ |
---|
[208] | 56 | cout << "Connection to " << str << " succeeded." << endl; |
---|
| 57 | //puts("Connection to localhost:5555 succeeded."); |
---|
[196] | 58 | }else{ |
---|
| 59 | enet_peer_reset(peer); |
---|
[208] | 60 | cout << "Connection to " << str << " failed." << endl; |
---|
| 61 | //puts("Connection to localhost:5555 failed."); |
---|
[217] | 62 | exit(EXIT_FAILURE); |
---|
[196] | 63 | } |
---|
| 64 | |
---|
| 65 | for(int i=0; i<10; i++){ |
---|
| 66 | // weihnachtsmann bringt packete |
---|
[204] | 67 | //ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE); |
---|
[196] | 68 | // extend the packet and append the string foo to it |
---|
| 69 | // send packet to peer on channel id 0 |
---|
[217] | 70 | enet_peer_send(peer, 1, pck.chatMessage("test2")); |
---|
[196] | 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 | } |
---|
[217] | 77 | usleep(1000000); |
---|
[196] | 78 | } |
---|
| 79 | |
---|
| 80 | // now disconnect |
---|
[298] | 81 | // enet_peer_disconnect (peer); |
---|
| 82 | enet_peer_disconnect (peer, 0); |
---|
[196] | 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 | |
---|
[285] | 98 | |
---|
| 99 | |
---|
[196] | 100 | return 0; |
---|
| 101 | } |
---|