Debian Codegraph @

Tests/NetSessionsTest/netsessions_test.cpp

raw
1 #include <ClanLib/core.h>
2 #include <ClanLib/display.h>
3 #include <ClanLib/network.h>
4 #include <ClanLib/application.h>
5
6 #include <iostream>
7
8 class NetChannelDemo : public CL_ClanApplication
9 {
10 public:
11
12 NetChannelDemo() {}
13 ~NetChannelDemo() {}
14
15 virtual int main(int argc, char **argv)
16 {
17 CL_ConsoleWindow console("NetChannelDemo", 80, 200);
18 console.redirect_stdio();
19
20 try
21 {
22 CL_SetupCore::init();
23 CL_SetupNetwork::init();
24
25 connect_test();
26 packet_test();
27 stream_test();
28 stress_test();
29 }
30 catch (CL_Error error)
31 {
32 std::cout << "Error: " << error.message.c_str() << std::endl;
33 }
34
35 console.display_close_message();
36 return 0;
37 }
38
39 void connect_test()
40 {
41 std::cout << "Running connect/disconnect test.." << std::endl;
42
43 std::cout << "Creating host.." << std::endl;
44 host = new CL_NetSession("Hello");
45 CL_Slot slot_connect = host->sig_computer_connected().connect(this, &NetChannelDemo::on_connect);
46 CL_Slot slot_disconnect = host->sig_computer_disconnected().connect(this, &NetChannelDemo::on_disconnect);
47
48 std::cout << "Starting listen on port 9999" << std::endl;
49 host->start_listen("9999");
50 std::cout << std::endl;
51
52 // Try check if connect/disconnect works in the simplest case:
53 std::cout << "Creating client.." << std::endl;
54 client = new CL_NetSession("Hello");
55
56 std::cout << "Connecting client.." << std::endl;
57 client->connect(CL_IPAddress("localhost", "9999"));
58
59 std::cout << "Entering 1 second loop.." << std::endl;
60 CL_System::keep_alive(1000); // Wait a little to see if host notices the connect.
61
62 std::cout << "Deleting client.." << std::endl;
63 delete client;
64
65 std::cout << "Entering 1 second loop.." << std::endl;
66 CL_System::keep_alive(1000); // Wait a little to see if host notices the disconnect.
67
68 std::cout << std::endl;
69
70 // Try disconnect server from client test:
71 std::cout << "Creating client.." << std::endl;
72 client = new CL_NetSession("Hello");
73
74 std::cout << "Connecting client async.." << std::endl;
75 client->connect_async("localhost", "9999");
76
77 std::cout << "Entering 1 second loop.." << std::endl;
78 CL_System::keep_alive(1000); // Wait for connect.
79
80 std::cout << "Disconnecting client.." << std::endl;
81 client->get_all().front().disconnect();
82
83 std::cout << "Entering 1 second loop.." << std::endl;
84 CL_System::keep_alive(1000); // Wait for host to notice disconnect.
85
86 std::cout << "Deleting client.." << std::endl;
87 delete client;
88
89 std::cout << "Entering 1 second loop (No disconnect should happen here)" << std::endl;
90 CL_System::keep_alive(1000); // No disconnect should happen here.
91
92 std::cout << std::endl;
93
94 // Try disconnect client from server test:
95
96 std::cout << "Creating client.." << std::endl;
97 client = new CL_NetSession("Hello");
98 CL_Slot slot_connect_client = client->sig_computer_connected().connect(this, &NetChannelDemo::on_connect_client);
99 CL_Slot slot_disconnect_client = client->sig_computer_disconnected().connect(this, &NetChannelDemo::on_disconnect_client);
100
101 std::cout << "Connecting client.." << std::endl;
102 client->connect(CL_IPAddress("localhost", "9999"));
103
104 std::cout << "Entering 1 second loop.." << std::endl;
105 CL_System::keep_alive(1000); // Wait for connect.
106
107 std::cout << "Disconnecting client from host.." << std::endl;
108 host->get_all().front().disconnect();
109
110 std::cout << "Entering 1 second loop.." << std::endl;
111 CL_System::keep_alive(1000); // Wait for host to notice disconnect.
112
113 std::cout << "Deleting client.." << std::endl;
114 delete client;
115
116 std::cout << "Entering 1 second loop (No disconnect should happen here)" << std::endl;
117 CL_System::keep_alive(1000); // No disconnect should happen here.
118
119 std::cout << std::endl;
120
121 // Try connect client after we stopped listening:
122
123 std::cout << "Stopping listen.." << std::endl;
124 host->stop_listen();
125 CL_System::keep_alive(1000);
126
127 std::cout << "Creating client.." << std::endl;
128 client = new CL_NetSession("Hello");
129
130 std::cout << "Connecting client.." << std::endl;
131 try
132 {
133 client->connect(CL_IPAddress("localhost", "9999"));
134 }
135 catch (CL_Error error)
136 {
137 std::cout << "Connect failed with error: " << error.message.c_str() << std::endl;
138 }
139
140 std::cout << "Deleting client.." << std::endl;
141 delete client;
142
143 std::cout << "Starting listen.." << std::endl;
144 host->start_listen("9999");
145 CL_System::keep_alive(1000);
146
147 std::cout << std::endl;
148
149 // Try delete server while a client is connected:
150
151 std::cout << "Creating client.." << std::endl;
152 client = new CL_NetSession("Hello");
153 slot_connect_client = client->sig_computer_connected().connect(this, &NetChannelDemo::on_connect_client);
154 slot_disconnect_client = client->sig_computer_disconnected().connect(this, &NetChannelDemo::on_disconnect_client);
155
156 std::cout << "Connecting client.." << std::endl;
157 client->connect(CL_IPAddress("localhost", "9999"));
158
159 std::cout << "Entering 1 second loop.." << std::endl;
160 CL_System::keep_alive(1000); // Wait for connect.
161
162 std::cout << host->get_all().size() << " computers connected to host." << std::endl;
163
164 std::cout << "Deleting host.." << std::endl;
165 delete host;
166
167 std::cout << "Entering 1 second loop" << std::endl;
168 CL_System::keep_alive(1000);
169
170 std::cout << "Deleting client.." << std::endl;
171 delete client;
172
173 std::cout << "End of connect/disconnect test." << std::endl;
174 }
175
176 void packet_test()
177 {
178 }
179
180 void stream_test()
181 {
182 }
183
184 void stress_test()
185 {
186 }
187
188 void on_connect(CL_NetComputer &computer)
189 {
190 std::cout << "SERVER: Connect from " << computer.get_address().get_address() << std::endl;
191 }
192
193 void on_disconnect(CL_NetComputer &computer)
194 {
195 std::cout << "SERVER: Disconnect from " << computer.get_address().get_address() << std::endl;
196 }
197
198 void on_connect_client(CL_NetComputer &computer)
199 {
200 std::cout << "CLIENT: Connect from " << computer.get_address().get_address() << std::endl;
201 }
202
203 void on_disconnect_client(CL_NetComputer &computer)
204 {
205 std::cout << "CLIENT: Disconnect from " << computer.get_address().get_address() << std::endl;
206 }
207
208 private:
209 CL_NetSession *client;
210 CL_NetSession *host;
211 } app;