1 | // |
---|
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 | |
---|
11 | // workaround for usleep(int) under windows |
---|
12 | #ifdef WIN32 |
---|
13 | #include "winbase.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | |
---|
17 | using namespace std; |
---|
18 | |
---|
19 | int main(){ |
---|
20 | ENetHost * client; |
---|
21 | ENetAddress address; |
---|
22 | ENetEvent event; |
---|
23 | ENetPeer *peer; |
---|
24 | network::PacketGenerator pck; |
---|
25 | |
---|
26 | enet_initialize(); |
---|
27 | atexit(enet_deinitialize); |
---|
28 | |
---|
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"; |
---|
35 | |
---|
36 | enet_address_set_host(&address, str.c_str()); |
---|
37 | address.port = 55556; |
---|
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){ |
---|
54 | cout << "Connection to " << str << " succeeded." << endl; |
---|
55 | //puts("Connection to localhost:5555 succeeded."); |
---|
56 | }else{ |
---|
57 | enet_peer_reset(peer); |
---|
58 | cout << "Connection to " << str << " failed." << endl; |
---|
59 | //puts("Connection to localhost:5555 failed."); |
---|
60 | exit(EXIT_FAILURE); |
---|
61 | } |
---|
62 | |
---|
63 | for(int i=0; i<10; i++){ |
---|
64 | // weihnachtsmann bringt packete |
---|
65 | //ENetPacket *packet = enet_packet_create ("packet1234", strlen("packet1234") + 1, ENET_PACKET_FLAG_RELIABLE); |
---|
66 | // extend the packet and append the string foo to it |
---|
67 | // send packet to peer on channel id 0 |
---|
68 | enet_peer_send(peer, 1, pck.chatMessage("test2")); |
---|
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 | } |
---|
75 | // under windows, use Sleep(milliseconds) instead of usleep(microseconds) |
---|
76 | #ifdef WIN32 |
---|
77 | Sleep(1000); |
---|
78 | #else |
---|
79 | usleep(1000000); |
---|
80 | #endif |
---|
81 | } |
---|
82 | |
---|
83 | // now disconnect |
---|
84 | // enet_peer_disconnect (peer); |
---|
85 | enet_peer_disconnect (peer, 0); |
---|
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 | |
---|
101 | |
---|
102 | |
---|
103 | return 0; |
---|
104 | } |
---|