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
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_LINKER_KERNEL_1_CPp_
#define DLIB_LINKER_KERNEL_1_CPp_
#include "linker_kernel_1.h"
namespace dlib
{
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
// member function definitions
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
linker::
linker (
) :
running(false),
running_signaler(running_mutex),
A(0),
B(0),
service_connection_running_signaler(service_connection_running_mutex)
{
}
// ----------------------------------------------------------------------------------------
linker::
linker (
connection& a,
connection& b
) :
running(false),
running_signaler(running_mutex),
A(0),
B(0),
service_connection_running_signaler(service_connection_running_mutex)
{
link(a,b);
}
// ----------------------------------------------------------------------------------------
linker::
~linker (
)
{
clear();
}
// ----------------------------------------------------------------------------------------
void linker::
clear (
)
{
// shutdown the connections
cons_mutex.lock();
if (A != 0 )
{
A->shutdown();
A = 0;
}
if (B != 0)
{
B->shutdown();
B = 0;
}
cons_mutex.unlock();
// wait for the other threads to signal that they have ended
running_mutex.lock();
while (running == true)
{
running_signaler.wait();
}
running_mutex.unlock();
}
// ----------------------------------------------------------------------------------------
bool linker::
is_running (
) const
{
running_mutex.lock();
bool temp = running;
running_mutex.unlock();
return temp;
}
// ----------------------------------------------------------------------------------------
void linker::
link (
connection& a,
connection& b
)
{
// make sure requires clause is not broken
DLIB_CASSERT(
this->is_running() == false ,
"\tvoid linker::link"
<< "\n\tis_running() == " << this->is_running()
<< "\n\tthis: " << this
);
running_mutex.lock();
running = true;
running_mutex.unlock();
cons_mutex.lock();
A = &a;
B = &b;
cons_mutex.unlock();
service_connection_running_mutex.lock();
service_connection_running = true;
service_connection_running_mutex.unlock();
service_connection_error_mutex.lock();
service_connection_error = false;
service_connection_error_mutex.unlock();
// if we fail to make the thread
if (!create_new_thread(service_connection,this))
{
a.shutdown();
b.shutdown();
service_connection_running_mutex.lock();
service_connection_running = false;
service_connection_running_mutex.unlock();
cons_mutex.lock();
A = 0;
B = 0;
cons_mutex.unlock();
running_mutex.lock();
running = false;
running_mutex.unlock();
throw dlib::thread_error (
ECREATE_THREAD,
"failed to make new thread in linker::link()"
);
}
// forward data from a to b
char buf[200];
int status;
bool error = false; // becomes true if one of the connections returns an error
while (true)
{
status = a.read(buf,sizeof(buf));
// if there was an error reading from the socket
if (status == OTHER_ERROR)
{
error = true;
break;
}
else if (status == SHUTDOWN)
{
b.shutdown();
}
if (status <= 0)
{
// if a has closed normally
if (status == 0)
b.shutdown_outgoing();
break;
}
status = b.write(buf,status);
// if there was an error writing to the socket then break
if (status == OTHER_ERROR)
{
error = true;
break;
}
if (status <= 0)
break;
}
// if there was an error then shutdown both connections
if (error)
{
a.shutdown();
b.shutdown();
}
// wait for the other thread to end
service_connection_running_mutex.lock();
while(service_connection_running)
{
service_connection_running_signaler.wait();
}
service_connection_running_mutex.unlock();
// make sure connections are shutdown
a.shutdown();
b.shutdown();
// both threads have ended so the connections are no longer needed
cons_mutex.lock();
A = 0;
B = 0;
cons_mutex.unlock();
// if service_connection terminated due to an error then set error to true
service_connection_error_mutex.lock();
if (service_connection_error)
error = true;
service_connection_error_mutex.unlock();
// if we are ending because of an error
if (error)
{
// signal that the link() function is ending
running_mutex.lock();
running = false;
running_signaler.broadcast();
running_mutex.unlock();
// throw the exception for this error
throw dlib::socket_error (
ECONNECTION,
"a connection returned an error in linker::link()"
);
}
// signal that the link() function is ending
running_mutex.lock();
running = false;
running_signaler.broadcast();
running_mutex.unlock();
}
// ----------------------------------------------------------------------------------------
void linker::
service_connection (
void* param
)
{
linker& p = *static_cast<linker*>(param);
p.cons_mutex.lock();
// if the connections are gone for whatever reason then return
if (p.A == 0 || p.B == 0)
{
// signal that this function is ending
p.service_connection_running_mutex.lock();
p.service_connection_running = false;
p.service_connection_running_signaler.broadcast();
p.service_connection_running_mutex.unlock();
return;
}
connection& a = *p.A;
connection& b = *p.B;
p.cons_mutex.unlock();
// forward data from b to a
char buf[200];
int status;
bool error = false;
while (true)
{
status = b.read(buf,sizeof(buf));
// if there was an error reading from the socket
if (status == OTHER_ERROR)
{
error = true;
break;
}
else if (status == SHUTDOWN)
{
a.shutdown();
}
if (status <= 0)
{
// if b has closed normally
if (status == 0)
a.shutdown_outgoing();
break;
}
status = a.write(buf,status);
// if there was an error writing to the socket then break
if (status == OTHER_ERROR)
{
error = true;
break;
}
if (status <= 0)
break;
}
// if there was an error then shutdown both connections
if (error)
{
a.shutdown();
b.shutdown();
}
// if there was an error then signal that
if (error)
{
p.service_connection_error_mutex.lock();
p.service_connection_error = true;
p.service_connection_error_mutex.unlock();
}
// signal that this function is ending
p.service_connection_running_mutex.lock();
p.service_connection_running = false;
p.service_connection_running_signaler.broadcast();
p.service_connection_running_mutex.unlock();
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_LINKER_KERNEL_1_CPp_