MCPcopy Create free account
hub / github.com/davisking/dlib / run_example_3

Function run_example_3

examples/bridge_ex.cpp:217–267  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

215// ----------------------------------------------------------------------------------------
216
217void run_example_3(
218)
219{
220 cout << "\n ---- Running example 3 ---- " << endl;
221
222 /*
223 In this example we will just send ints and my_example_object objects
224 over a TCP connection. Since we are sending more than one type of
225 object through a pipe we will need to use the type_safe_union.
226 */
227
228 typedef type_safe_union<int, my_example_object> tsu_type;
229
230 dlib::pipe<tsu_type> in(4), out(4);
231
232 // Note that we don't have to start the listening bridge first. If b2
233 // fails to make a connection it will just keep trying until successful.
234 bridge b2(connect_to("127.0.0.1:12345"), receive(in));
235 // We don't have to configure a bridge in it's constructor. If it's
236 // more convenient we can do so by calling reconfigure() instead.
237 bridge b1;
238 b1.reconfigure(listen_on_port(12345), transmit(out));
239
240 tsu_type msg;
241
242 msg = 1;
243 out.enqueue(msg);
244
245 msg = 2;
246 out.enqueue(msg);
247
248 msg.get<my_example_object>().value = 3;
249 msg.get<my_example_object>().str = "some string";
250 out.enqueue(msg);
251
252
253 // dequeue the three objects we sent and print them on the screen.
254 for (int i = 0; i < 3; ++i)
255 {
256 in.dequeue(msg);
257 if (msg.contains<int>())
258 {
259 cout << "dequeued int: "<< msg.get<int>() << endl;
260 }
261 else if (msg.contains<my_example_object>())
262 {
263 cout << "dequeued struct: "<< msg.get<my_example_object>().value << " "
264 << msg.get<my_example_object>().str << endl;
265 }
266 }
267}
268
269// ----------------------------------------------------------------------------------------
270// ----------------------------------------------------------------------------------------

Callers 1

mainFunction · 0.85

Calls 6

connect_toFunction · 0.85
receiveFunction · 0.85
transmitFunction · 0.85
reconfigureMethod · 0.80
listen_on_portClass · 0.50
enqueueMethod · 0.45

Tested by

no test coverage detected