1 | // |
---|
2 | // Dummy client to test ConnectionManager, PacketBuffer, ClientConnection and other classes |
---|
3 | // |
---|
4 | // Author: Oliver Scheuss |
---|
5 | |
---|
6 | #include <iostream> |
---|
7 | #include <string> |
---|
8 | #include <enet/enet.h> |
---|
9 | #include "PacketManager.h" |
---|
10 | #include "ClientConnection.h" |
---|
11 | |
---|
12 | using namespace network; |
---|
13 | |
---|
14 | int main(){ |
---|
15 | network::PacketGenerator pck; |
---|
16 | |
---|
17 | std::cout << "Enter address of the server xxx.xxx.xxx.xxx (enter for localhost)" << std::endl; |
---|
18 | std::string str; |
---|
19 | std::getline(std::cin, str); |
---|
20 | std::cout << "You entered: " << str << std::endl; |
---|
21 | if(str.compare("")==0) |
---|
22 | str="127.0.0.1"; |
---|
23 | |
---|
24 | ClientConnection client = ClientConnection(55556, str); |
---|
25 | |
---|
26 | client.createConnection(); |
---|
27 | |
---|
28 | if(client.waitEstablished(10000)) |
---|
29 | std::cout << "Connection established" << std::endl; |
---|
30 | else |
---|
31 | std::cout << "Connection failed" << std::endl; |
---|
32 | |
---|
33 | ENetPacket *packet; |
---|
34 | ENetEvent event; |
---|
35 | |
---|
36 | |
---|
37 | |
---|
38 | for(int i=0; i<10; i++){ |
---|
39 | // weihnachtsmann bringt packete |
---|
40 | // extend the packet and append the string foo to it |
---|
41 | // send packet to peer on channel id 0 |
---|
42 | client.addPacket(pck.chatMessage("test")); |
---|
43 | // keep the timeout very small for low delay |
---|
44 | if(client.sendPackets(&event)){ |
---|
45 | std::cout << "successfully sent: " << event.type << std::endl; |
---|
46 | }else{ |
---|
47 | std::cout << "failed sending" << std::endl; |
---|
48 | } |
---|
49 | // usleep(1000000); |
---|
50 | } |
---|
51 | usleep(1000000); |
---|
52 | // now disconnect |
---|
53 | if(client.closeConnection()) |
---|
54 | std::cout << "Connection successfully closed" << std::endl; |
---|
55 | else |
---|
56 | std::cout << "Connection closing failed" << std::endl; |
---|
57 | |
---|
58 | // 3 seconds timeout |
---|
59 | return 0; |
---|
60 | } |
---|
61 | |
---|