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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
// file : ace/RMCast/Socket.cpp
// author : Boris Kolpackov <boris@kolpackov.net>
// cvs-id : $Id$
#include "ace/OS_Memory.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_stdlib.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_sys_time.h" // gettimeofday
#include "ace/Unbounded_Queue.h"
#include "ace/Pipe.h"
#include "Stack.h"
#include "Protocol.h"
#include "Bits.h"
#include "Fragment.h"
#include "Reassemble.h"
#include "Acknowledge.h"
#include "Retransmit.h"
#include "Flow.h"
#include "Link.h"
#include "Socket.h"
/*
#include <iostream>
using std::cerr;
using std::endl;
*/
namespace ACE_RMCast
{
class Socket_Impl : protected Element
{
public:
~Socket_Impl ();
Socket_Impl (Address const& a, bool loop, Parameters const& params);
public:
void
send_ (void const* buf, size_t s);
ssize_t
recv_ (void* buf,
size_t s,
ACE_Time_Value const* timeout,
ACE_INET_Addr* from);
ssize_t
size_ (ACE_Time_Value const* timeout);
ACE_HANDLE
get_handle_ ();
private:
virtual void
recv (Message_ptr m);
private:
bool loop_;
Parameters const params_;
Mutex mutex_;
Condition cond_;
ACE_Unbounded_Queue<Message_ptr> queue_;
ACE_Pipe signal_pipe_;
ACE_Auto_Ptr<Fragment> fragment_;
ACE_Auto_Ptr<Reassemble> reassemble_;
ACE_Auto_Ptr<Acknowledge> acknowledge_;
ACE_Auto_Ptr<Retransmit> retransmit_;
ACE_Auto_Ptr<Flow> flow_;
ACE_Auto_Ptr<Link> link_;
};
Socket_Impl::
Socket_Impl (Address const& a, bool loop, Parameters const& params)
: loop_ (loop),
params_ (params),
cond_ (mutex_)
{
fragment_.reset (new Fragment (params_));
reassemble_.reset (new Reassemble (params_));
acknowledge_.reset (new Acknowledge (params_));
retransmit_.reset (new Retransmit (params_));
flow_.reset (new Flow (params_));
link_.reset (new Link (a, params_));
// Start IN stack from top to bottom.
//
in_start (0);
fragment_->in_start (this);
reassemble_->in_start (fragment_.get ());
acknowledge_->in_start (reassemble_.get ());
retransmit_->in_start (acknowledge_.get ());
flow_->in_start (retransmit_.get ());
link_->in_start (flow_.get ());
// Start OUT stack from bottom up.
//
link_->out_start (0);
flow_->out_start (link_.get ());
retransmit_->out_start (flow_.get ());
acknowledge_->out_start (retransmit_.get ());
reassemble_->out_start (acknowledge_.get ());
fragment_->out_start (reassemble_.get ());
out_start (fragment_.get ());
}
Socket_Impl::
~Socket_Impl ()
{
// Stop OUT stack from top to bottom.
//
out_stop ();
fragment_->out_stop ();
reassemble_->out_stop ();
acknowledge_->out_stop ();
retransmit_->out_stop ();
flow_->out_stop ();
link_->out_stop ();
// Stop IN stack from bottom up.
//
link_->in_stop ();
flow_->in_stop ();
retransmit_->in_stop ();
acknowledge_->in_stop ();
reassemble_->in_stop ();
fragment_->in_stop ();
in_stop ();
// Close signal pipe.
//
if (signal_pipe_.read_handle () != ACE_INVALID_HANDLE)
signal_pipe_.close ();
}
void Socket_Impl::
send_ (void const* buf, size_t s)
{
Message_ptr m (new Message);
m->add (Profile_ptr (new Data (buf, s)));
// Qualification is for VC6 and VxWorks.
//
Element::send (m);
}
ssize_t Socket_Impl::
recv_ (void* buf,
size_t s,
ACE_Time_Value const* timeout,
ACE_INET_Addr* from)
{
ACE_Time_Value abs_time;
if (timeout)
abs_time = ACE_OS::gettimeofday () + *timeout;
Lock l (mutex_);
while (queue_.is_empty ())
{
if (timeout)
{
if (cond_.wait (&abs_time) != -1)
break;
}
else
{
if (cond_.wait () != -1)
break;
}
return -1; // errno is already set
}
Message_ptr m;
if (queue_.dequeue_head (m) == -1)
ACE_OS::abort ();
if (queue_.is_empty ())
{
// Remove data from the pipe.
//
if (signal_pipe_.read_handle () != ACE_INVALID_HANDLE)
{
char c;
if (signal_pipe_.recv (&c, 1) != 1)
{
ACE_OS::perror ("read: ");
ACE_OS::abort ();
}
}
}
if (from)
*from = static_cast<From const*> (m->find (From::id))->address ();
if (m->find (NoData::id) != 0)
{
errno = ENOENT;
return -1;
}
Data const* d = static_cast<Data const*>(m->find (Data::id));
ssize_t r (static_cast<ssize_t> (d->size () < s ? d->size () : s));
ACE_OS::memcpy (buf, d->buf (), r);
return r;
}
ssize_t Socket_Impl::
size_ (ACE_Time_Value const* timeout)
{
ACE_Time_Value abs_time;
if (timeout)
abs_time = ACE_OS::gettimeofday () + *timeout;
Lock l (mutex_);
while (queue_.is_empty ())
{
if (timeout)
{
if (cond_.wait (&abs_time) != -1)
break;
}
else
{
if (cond_.wait () != -1)
break;
}
return -1; // errno is already set
}
// I can't get the head of the queue without actually dequeuing
// the element.
//
Message_ptr m;
if (queue_.dequeue_head (m) == -1)
ACE_OS::abort ();
if (queue_.enqueue_head (m) == -1)
ACE_OS::abort ();
if (m->find (NoData::id) != 0)
{
errno = ENOENT;
return -1;
}
Data const* d = static_cast<Data const*>(m->find (Data::id));
return static_cast<ssize_t> (d->size ());
}
ACE_HANDLE Socket_Impl::
get_handle_ ()
{
if (signal_pipe_.read_handle () == ACE_INVALID_HANDLE)
{
signal_pipe_.open ();
}
return signal_pipe_.read_handle ();
}
void Socket_Impl::
recv (Message_ptr m)
{
if (m->find (Data::id) != 0 || m->find (NoData::id) != 0)
{
if (!loop_)
{
Address to (static_cast<To const*> (m->find (To::id))->address ());
Address from (
static_cast<From const*> (m->find (From::id))->address ());
if (to == from)
return;
}
Lock l (mutex_);
//if (queue_.size () != 0)
// cerr << "recv socket queue size: " << queue_.size () << endl;
bool signal (queue_.is_empty ());
queue_.enqueue_tail (m);
if (signal)
{
// Also write to the pipe.
if (signal_pipe_.write_handle () != ACE_INVALID_HANDLE)
{
char c;
if (signal_pipe_.send (&c, 1) != 1)
{
// perror ("write: ");
ACE_OS::abort ();
}
}
cond_.signal ();
}
}
}
// Socket
//
//
Socket::
~Socket ()
{
}
Socket::
Socket (Address const& a, bool loop, Parameters const& params)
: impl_ (new Socket_Impl (a, loop, params))
{
}
void Socket::
send (void const* buf, size_t s)
{
impl_->send_ (buf, s);
}
ssize_t Socket::
recv (void* buf, size_t s)
{
return impl_->recv_ (buf, s, 0, 0);
}
ssize_t Socket::
recv (void* buf, size_t s, ACE_INET_Addr& from)
{
return impl_->recv_ (buf, s, 0, &from);
}
ssize_t Socket::
recv (void* buf, size_t s, ACE_Time_Value const& timeout)
{
return impl_->recv_ (buf, s, &timeout, 0);
}
ssize_t Socket::
recv (void* buf,
size_t s,
ACE_Time_Value const& timeout,
ACE_INET_Addr& from)
{
return impl_->recv_ (buf, s, &timeout, &from);
}
ssize_t Socket::
size ()
{
return impl_->size_ (0);
}
ssize_t Socket::
size (ACE_Time_Value const& timeout)
{
return impl_->size_ (&timeout);
}
ACE_HANDLE Socket::
get_handle ()
{
return impl_->get_handle_ ();
}
}
|