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