| 271 | // ---------------------------------------------------------------------------------------- |
| 272 | |
| 273 | void run_example_4( |
| 274 | ) |
| 275 | { |
| 276 | cout << "\n ---- Running example 4 ---- " << endl; |
| 277 | |
| 278 | /* |
| 279 | This final example is the same as example 3 except we will also now be getting |
| 280 | status messages from the bridges. These bridge_status messages tell us the |
| 281 | state of the TCP connection associated with a bridge. Is it connected or not? |
| 282 | Who it is connected to? |
| 283 | |
| 284 | The way you get these status messages is by ensuring that your receive pipe is |
| 285 | capable of storing bridge_status objects. If it is then the bridge will |
| 286 | automatically insert bridge_status messages into your receive pipe whenever |
| 287 | there is a status change. |
| 288 | |
| 289 | There are only two kinds of status changes. The establishment of a connection |
| 290 | or the closing of a connection. Also, a connection which closes due to you |
| 291 | calling clear(), reconfigure(), or destructing a bridge does not generate a |
| 292 | status message since, in this case, you already know about it and just want |
| 293 | the bridge to destroy itself as quickly as possible. |
| 294 | */ |
| 295 | |
| 296 | |
| 297 | typedef type_safe_union<int, my_example_object, bridge_status> tsu_type; |
| 298 | |
| 299 | dlib::pipe<tsu_type> in(4), out(4); |
| 300 | dlib::pipe<bridge_status> b1_status(4); |
| 301 | |
| 302 | // setup both bridges to have receive pipes capable of holding bridge_status messages. |
| 303 | bridge b1(listen_on_port(12345), transmit(out), receive(b1_status)); |
| 304 | // Note that we can also use a hostname with connect_to() instead of supplying an IP address. |
| 305 | bridge b2(connect_to("localhost:12345"), receive(in)); |
| 306 | |
| 307 | tsu_type msg; |
| 308 | bridge_status bs; |
| 309 | |
| 310 | // Once a connection is established it will generate a status message from each bridge. |
| 311 | // Let's get those and print them. |
| 312 | b1_status.dequeue(bs); |
| 313 | cout << "bridge 1 status: is_connected: " << boolalpha << bs.is_connected << endl; |
| 314 | cout << "bridge 1 status: foreign_ip: " << bs.foreign_ip << endl; |
| 315 | cout << "bridge 1 status: foreign_port: " << bs.foreign_port << endl; |
| 316 | |
| 317 | in.dequeue(msg); |
| 318 | bs = msg.get<bridge_status>(); |
| 319 | cout << "bridge 2 status: is_connected: " << bs.is_connected << endl; |
| 320 | cout << "bridge 2 status: foreign_ip: " << bs.foreign_ip << endl; |
| 321 | cout << "bridge 2 status: foreign_port: " << bs.foreign_port << endl; |
| 322 | |
| 323 | |
| 324 | |
| 325 | msg = 1; |
| 326 | out.enqueue(msg); |
| 327 | |
| 328 | msg = 2; |
| 329 | out.enqueue(msg); |
| 330 |
no test coverage detected