File size: 34,517 Bytes
9375c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - bridge_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font>

<font color='#009900'>/*
    This is an example showing how to use the bridge object from from the 
    dlib C++ Library to send messages via TCP/IP.

    In particular, this example will walk you through four progressively
    more complex use cases of the bridge object.  Note that this example
    program assumes you are already familiar with the pipe object and at
    least the contents of the <a href="pipe_ex_2.cpp.html">pipe_ex_2.cpp</a> example program.
*/</font>


<font color='#009900'>// =========== Example program output ===========
</font><font color='#009900'>/*
     ---- Running example 1 ---- 
    dequeued value: 1
    dequeued value: 2
    dequeued value: 3

     ---- Running example 2 ---- 
    dequeued value: 1
    dequeued value: 2
    dequeued value: 3

     ---- Running example 3 ---- 
    dequeued int:    1
    dequeued int:    2
    dequeued struct: 3   some string

     ---- Running example 4 ---- 
    bridge 1 status: is_connected: true
    bridge 1 status: foreign_ip:   127.0.0.1
    bridge 1 status: foreign_port: 43156
    bridge 2 status: is_connected: true
    bridge 2 status: foreign_ip:   127.0.0.1
    bridge 2 status: foreign_port: 12345
    dequeued int:    1
    dequeued int:    2
    dequeued struct: 3   some string
    bridge 1 status: is_connected: false
    bridge 1 status: foreign_ip:   127.0.0.1
    bridge 1 status: foreign_port: 12345
*/</font>


<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>bridge.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>type_safe_union.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='run_example_1'></a>run_example_1</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='run_example_2'></a>run_example_2</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='run_example_3'></a>run_example_3</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'><u>void</u></font> <b><a name='run_example_4'></a>run_example_4</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
    <font color='#BB00BB'>run_example_1</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <font color='#BB00BB'>run_example_2</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <font color='#BB00BB'>run_example_3</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <font color='#BB00BB'>run_example_4</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='run_example_1'></a>run_example_1</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n ---- Running example 1 ---- </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#009900'>/*
        The idea of the bridge is basically to allow two different dlib::pipe objects
        to be connected together via a TCP connection.  This is best illustrated by
        the following short example.  In it we create two pipes, in and out.  When
        an object is enqueued into the out pipe it will be automatically sent 
        through a TCP connection and once received at the other end it will be 
        inserted into the in pipe.
    */</font>
    dlib::pipe<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font><font color='#5555FF'>&gt;</font> <font color='#BB00BB'>in</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>out</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>;


    <font color='#009900'>// This bridge will listen on port 12345 for an incoming TCP connection.  Then
</font>    <font color='#009900'>// it will read data from that connection and put it into the in pipe.
</font>    bridge <font color='#BB00BB'>b2</font><font face='Lucida Console'>(</font><font color='#BB00BB'>listen_on_port</font><font face='Lucida Console'>(</font><font color='#979000'>12345</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>receive</font><font face='Lucida Console'>(</font>in<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// This bridge will initiate a TCP connection and then start dequeuing 
</font>    <font color='#009900'>// objects from out and transmitting them over the connection.
</font>    bridge <font color='#BB00BB'>b1</font><font face='Lucida Console'>(</font><font color='#BB00BB'>connect_to_ip_and_port</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>127.0.0.1</font>", <font color='#979000'>12345</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>transmit</font><font face='Lucida Console'>(</font>out<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// As an aside, in a real program, each of these bridges and pipes would be in a 
</font>    <font color='#009900'>// separate application.  But to make this example self contained they are both 
</font>    <font color='#009900'>// right here.
</font>


    <font color='#009900'>// Now let's put some things into the out pipe
</font>    <font color='#0000FF'><u>int</u></font> value <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;

    value <font color='#5555FF'>=</font> <font color='#979000'>2</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;

    value <font color='#5555FF'>=</font> <font color='#979000'>3</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;


    <font color='#009900'>// Now those 3 ints can be dequeued from the in pipe.  They will show up
</font>    <font color='#009900'>// in the same order they were inserted into the out pipe.
</font>    in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued value: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> value <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued value: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> value <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued value: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> value <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
<b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='run_example_2'></a>run_example_2</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n ---- Running example 2 ---- </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#009900'>/*
        This example makes a simple echo server on port 12345.  When an object
        is inserted into the out pipe it will be sent over a TCP connection, get 
        put into the echo pipe and then immediately read out of the echo pipe and
        sent back over the TCP connection where it will finally be placed into the in
        pipe.
    */</font>

    dlib::pipe<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font><font color='#5555FF'>&gt;</font> <font color='#BB00BB'>in</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>out</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>echo</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// Just like TCP connections, a bridge can send data both directions.  The directionality
</font>    <font color='#009900'>// of a pipe is indicated by the receive() and transmit() type decorations.  Also, the order
</font>    <font color='#009900'>// they are listed doesn't matter.
</font>    bridge <font color='#BB00BB'>echo_bridge</font><font face='Lucida Console'>(</font><font color='#BB00BB'>listen_on_port</font><font face='Lucida Console'>(</font><font color='#979000'>12345</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>receive</font><font face='Lucida Console'>(</font>echo<font face='Lucida Console'>)</font>, <font color='#BB00BB'>transmit</font><font face='Lucida Console'>(</font>echo<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// Note that you can also specify the ip and port as a string by using connect_to().
</font>    bridge <font color='#BB00BB'>b1</font><font face='Lucida Console'>(</font><font color='#BB00BB'>connect_to</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>127.0.0.1:12345</font>"<font face='Lucida Console'>)</font>, <font color='#BB00BB'>transmit</font><font face='Lucida Console'>(</font>out<font face='Lucida Console'>)</font>, <font color='#BB00BB'>receive</font><font face='Lucida Console'>(</font>in<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;


    <font color='#0000FF'><u>int</u></font> value <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;

    value <font color='#5555FF'>=</font> <font color='#979000'>2</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;

    value <font color='#5555FF'>=</font> <font color='#979000'>3</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;


    in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued value: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> value <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued value: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> value <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>value<font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued value: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> value <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
<b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>struct</font> <b><a name='my_example_object'></a>my_example_object</b>
<b>{</b>
    <font color='#009900'>/*
        All objects passing through a dlib::bridge must be serializable.  This
        means there must exist global functions called serialize() and deserialize()
        which can convert an object into a bit stream and then reverse the process.

        This example object illustrates how this is done.
    */</font>

    <font color='#0000FF'><u>int</u></font> value;
    std::string str;
<b>}</b>;

<font color='#0000FF'><u>void</u></font> <b><a name='serialize'></a>serialize</b> <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> my_example_object<font color='#5555FF'>&amp;</font> item, std::ostream<font color='#5555FF'>&amp;</font> out<font face='Lucida Console'>)</font>
<b>{</b>
    <font color='#009900'>/*
        serialize() just needs to write the state of item to the output stream.
        You can do this however you like.  Below, I'm using the serialize functions
        for int and std::string which come with dlib.  But again, you can do whatever
        you want here.
    */</font>
    dlib::<font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>item.value, out<font face='Lucida Console'>)</font>;
    dlib::<font color='#BB00BB'>serialize</font><font face='Lucida Console'>(</font>item.str, out<font face='Lucida Console'>)</font>;
<b>}</b>

<font color='#0000FF'><u>void</u></font> <b><a name='deserialize'></a>deserialize</b> <font face='Lucida Console'>(</font>my_example_object<font color='#5555FF'>&amp;</font> item, std::istream<font color='#5555FF'>&amp;</font> in<font face='Lucida Console'>)</font>
<b>{</b>
    <font color='#009900'>/*
        deserialize() is just the inverse of serialize().  Again, you can do
        whatever you want here so long as it correctly reconstructs item.  This
        also means that deserialize() must always consume as many bytes as serialize()
        generates.
    */</font>
    dlib::<font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>item.value, in<font face='Lucida Console'>)</font>;
    dlib::<font color='#BB00BB'>deserialize</font><font face='Lucida Console'>(</font>item.str, in<font face='Lucida Console'>)</font>;
<b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='run_example_3'></a>run_example_3</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n ---- Running example 3 ---- </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#009900'>/*
        In this example we will just send ints and my_example_object objects
        over a TCP connection.  Since we are sending more than one type of
        object through a pipe we will need to use the type_safe_union.
    */</font>

    <font color='#0000FF'>typedef</font> type_safe_union<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font>, my_example_object<font color='#5555FF'>&gt;</font> tsu_type;

    dlib::pipe<font color='#5555FF'>&lt;</font>tsu_type<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>in</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>out</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// Note that we don't have to start the listening bridge first.  If b2
</font>    <font color='#009900'>// fails to make a connection it will just keep trying until successful.
</font>    bridge <font color='#BB00BB'>b2</font><font face='Lucida Console'>(</font><font color='#BB00BB'>connect_to</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>127.0.0.1:12345</font>"<font face='Lucida Console'>)</font>, <font color='#BB00BB'>receive</font><font face='Lucida Console'>(</font>in<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <font color='#009900'>// We don't have to configure a bridge in it's constructor.  If it's 
</font>    <font color='#009900'>// more convenient we can do so by calling reconfigure() instead.
</font>    bridge b1;
    b1.<font color='#BB00BB'>reconfigure</font><font face='Lucida Console'>(</font><font color='#BB00BB'>listen_on_port</font><font face='Lucida Console'>(</font><font color='#979000'>12345</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>transmit</font><font face='Lucida Console'>(</font>out<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

    tsu_type msg;

    msg <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;

    msg <font color='#5555FF'>=</font> <font color='#979000'>2</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;

    msg.get<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.value <font color='#5555FF'>=</font> <font color='#979000'>3</font>;
    msg.get<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.str <font color='#5555FF'>=</font> "<font color='#CC0000'>some string</font>";
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;


    <font color='#009900'>// dequeue the three objects we sent and print them on the screen.
</font>    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> <font color='#979000'>3</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
    <b>{</b>
        in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>msg.contains<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
        <b>{</b>
            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued int:    </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> msg.get<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
        <b>}</b>
        <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>msg.contains<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
        <b>{</b>
            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued struct: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> msg.get<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.value <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>   </font>" 
                                       <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> msg.get<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.str <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
        <b>}</b>
    <b>}</b>
<b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'><u>void</u></font> <b><a name='run_example_4'></a>run_example_4</b><font face='Lucida Console'>(</font>
<font face='Lucida Console'>)</font>
<b>{</b>
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n ---- Running example 4 ---- </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    <font color='#009900'>/*
        This final example is the same as example 3 except we will also now be getting 
        status messages from the bridges.  These bridge_status messages tell us the 
        state of the TCP connection associated with a bridge.  Is it connected or not?  
        Who it is connected to?

        The way you get these status messages is by ensuring that your receive pipe is 
        capable of storing bridge_status objects.  If it is then the bridge will 
        automatically insert bridge_status messages into your receive pipe whenever 
        there is a status change. 

        There are only two kinds of status changes.  The establishment of a connection 
        or the closing of a connection.   Also, a connection which closes due to you 
        calling clear(), reconfigure(), or destructing a bridge does not generate a 
        status message since, in this case, you already know about it and just want 
        the bridge to destroy itself as quickly as possible.
    */</font>


    <font color='#0000FF'>typedef</font> type_safe_union<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font>, my_example_object, bridge_status<font color='#5555FF'>&gt;</font> tsu_type;

    dlib::pipe<font color='#5555FF'>&lt;</font>tsu_type<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>in</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>out</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>;
    dlib::pipe<font color='#5555FF'>&lt;</font>bridge_status<font color='#5555FF'>&gt;</font> <font color='#BB00BB'>b1_status</font><font face='Lucida Console'>(</font><font color='#979000'>4</font><font face='Lucida Console'>)</font>;

    <font color='#009900'>// setup both bridges to have receive pipes capable of holding bridge_status messages.
</font>    bridge <font color='#BB00BB'>b1</font><font face='Lucida Console'>(</font><font color='#BB00BB'>listen_on_port</font><font face='Lucida Console'>(</font><font color='#979000'>12345</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>transmit</font><font face='Lucida Console'>(</font>out<font face='Lucida Console'>)</font>, <font color='#BB00BB'>receive</font><font face='Lucida Console'>(</font>b1_status<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <font color='#009900'>// Note that we can also use a hostname with connect_to() instead of supplying an IP address.
</font>    bridge <font color='#BB00BB'>b2</font><font face='Lucida Console'>(</font><font color='#BB00BB'>connect_to</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>localhost:12345</font>"<font face='Lucida Console'>)</font>, <font color='#BB00BB'>receive</font><font face='Lucida Console'>(</font>in<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

    tsu_type msg;
    bridge_status bs;

    <font color='#009900'>// Once a connection is established it will generate a status message from each bridge. 
</font>    <font color='#009900'>// Let's get those and print them.  
</font>    b1_status.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>bs<font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 1 status: is_connected: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> boolalpha <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.is_connected <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 1 status: foreign_ip:   </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.foreign_ip <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 1 status: foreign_port: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.foreign_port <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

    in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;
    bs <font color='#5555FF'>=</font> msg.get<font color='#5555FF'>&lt;</font>bridge_status<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 2 status: is_connected: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.is_connected <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 2 status: foreign_ip:   </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.foreign_ip <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 2 status: foreign_port: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.foreign_port <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;



    msg <font color='#5555FF'>=</font> <font color='#979000'>1</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;

    msg <font color='#5555FF'>=</font> <font color='#979000'>2</font>;
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;

    msg.get<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.value <font color='#5555FF'>=</font> <font color='#979000'>3</font>;
    msg.get<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.str <font color='#5555FF'>=</font> "<font color='#CC0000'>some string</font>";
    out.<font color='#BB00BB'>enqueue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;


    <font color='#009900'>// Read the 3 things we sent over the connection.
</font>    <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>int</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> <font color='#979000'>3</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
    <b>{</b>
        in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>msg.contains<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
        <b>{</b>
            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued int:    </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> msg.get<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>int</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
        <b>}</b>
        <font color='#0000FF'>else</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>msg.contains<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
        <b>{</b>
            cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>dequeued struct: </font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> msg.get<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.value <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>   </font>" 
                                       <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> msg.get<font color='#5555FF'>&lt;</font>my_example_object<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.str <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
        <b>}</b>
    <b>}</b>

    <font color='#009900'>// cause bridge 1 to shutdown completely.  This will close the connection and
</font>    <font color='#009900'>// therefore bridge 2 will generate a status message indicating the connection
</font>    <font color='#009900'>// just closed.
</font>    b1.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    in.<font color='#BB00BB'>dequeue</font><font face='Lucida Console'>(</font>msg<font face='Lucida Console'>)</font>;
    bs <font color='#5555FF'>=</font> msg.get<font color='#5555FF'>&lt;</font>bridge_status<font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 1 status: is_connected: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.is_connected <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 1 status: foreign_ip:   </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.foreign_ip <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>bridge 1 status: foreign_port: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> bs.foreign_port <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
<b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>

</pre></body></html>