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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
|
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
package IceInternal;
final class UdpTransceiver implements Transceiver
{
public java.nio.channels.SelectableChannel
fd()
{
assert(_fd != null);
return _fd;
}
public synchronized void
close()
{
//
// NOTE: closeSocket() may have already been invoked by shutdownReadWrite().
//
closeSocket();
if(_readSelector != null)
{
try
{
_readSelector.close();
}
catch(java.io.IOException ex)
{
// Ignore.
}
_readSelector = null;
}
}
public void
shutdownWrite()
{
//
// NOTE: DatagramSocket does not support shutdownOutput.
//
}
public synchronized void
shutdownReadWrite()
{
//
// NOTE: DatagramSocket does not support shutdownInput, and we
// cannot use the C++ technique of sending a "wakeup" packet to
// this socket because the Java implementation deadlocks when we
// call disconnect() while receive() is in progress. Therefore
// we close the socket here and wake up the selector.
//
closeSocket();
if(_readSelector != null)
{
_readSelector.wakeup();
}
}
public void
write(BasicStream stream, int timeout) // NOTE: timeout is not used
throws LocalExceptionWrapper
{
java.nio.ByteBuffer buf = stream.prepareWrite();
assert(buf.position() == 0);
final int packetSize = java.lang.Math.min(_maxPacketSize, _sndSize - _udpOverhead);
if(packetSize < buf.limit())
{
//
// We don't log a warning here because the client gets an exception anyway.
//
throw new Ice.DatagramLimitException();
}
while(buf.hasRemaining())
{
try
{
assert(_fd != null);
int ret = _fd.write(buf);
if(_traceLevels.network >= 3)
{
String s = "sent " + ret + " bytes via udp\n" + toString();
_logger.trace(_traceLevels.networkCat, s);
}
if(_stats != null)
{
_stats.bytesSent(type(), ret);
}
assert(ret == buf.limit());
break;
}
catch(java.nio.channels.AsynchronousCloseException ex)
{
Ice.ConnectionLostException se = new Ice.ConnectionLostException();
se.initCause(ex);
throw se;
}
catch(java.net.PortUnreachableException ex)
{
Ice.ConnectionLostException se = new Ice.ConnectionLostException();
se.initCause(ex);
throw se;
}
catch(java.io.InterruptedIOException ex)
{
continue;
}
catch(java.io.IOException ex)
{
Ice.SocketException se = new Ice.SocketException();
se.initCause(ex);
throw se;
}
}
}
public boolean
read(BasicStream stream, int timeout) // NOTE: timeout is not used
{
assert(stream.pos() == 0);
final int packetSize = java.lang.Math.min(_maxPacketSize, _rcvSize - _udpOverhead);
if(packetSize < stream.size())
{
//
// We log a warning here because this is the server side -- without the
// the warning, there would only be silence.
//
if(_warn)
{
_logger.warning("DatagramLimitException: maximum size of " + packetSize + " exceeded");
}
throw new Ice.DatagramLimitException();
}
stream.resize(packetSize, true);
java.nio.ByteBuffer buf = stream.prepareRead();
buf.position(0);
synchronized(this)
{
assert(_fd != null);
if(_readSelector == null)
{
try
{
_readSelector = java.nio.channels.Selector.open();
_fd.register(_readSelector, java.nio.channels.SelectionKey.OP_READ, null);
}
catch(java.io.IOException ex)
{
if(Network.connectionLost(ex))
{
Ice.ConnectionLostException se = new Ice.ConnectionLostException();
se.initCause(ex);
throw se;
}
Ice.SocketException se = new Ice.SocketException();
se.initCause(ex);
throw se;
}
}
}
int ret = 0;
while(true)
{
//
// Check for shutdown.
//
java.nio.channels.DatagramChannel fd = null;
synchronized(this)
{
if(_fd == null)
{
throw new Ice.ConnectionLostException();
}
fd = _fd;
}
try
{
java.net.InetSocketAddress sender = (java.net.InetSocketAddress)fd.receive(buf);
if(sender == null || buf.position() == 0)
{
//
// Wait until packet arrives or socket is closed.
//
_readSelector.select();
continue;
}
ret = buf.position();
if(_connect)
{
//
// If we must connect, then we connect to the first peer that
// sends us a packet.
//
Network.doConnect(fd, sender, -1);
_connect = false; // We're connected now
if(_traceLevels.network >= 1)
{
String s = "connected udp socket\n" + toString();
_logger.trace(_traceLevels.networkCat, s);
}
}
break;
}
catch(java.nio.channels.AsynchronousCloseException ex)
{
Ice.ConnectionLostException se = new Ice.ConnectionLostException();
se.initCause(ex);
throw se;
}
catch(java.net.PortUnreachableException ex)
{
Ice.ConnectionLostException se = new Ice.ConnectionLostException();
se.initCause(ex);
throw se;
}
catch(java.io.InterruptedIOException ex)
{
continue;
}
catch(java.io.IOException ex)
{
if(Network.connectionLost(ex))
{
Ice.ConnectionLostException se = new Ice.ConnectionLostException();
se.initCause(ex);
throw se;
}
Ice.SocketException se = new Ice.SocketException();
se.initCause(ex);
throw se;
}
}
if(_traceLevels.network >= 3)
{
String s = "received " + ret + " bytes via udp\n" + toString();
_logger.trace(_traceLevels.networkCat, s);
}
if(_stats != null)
{
_stats.bytesReceived(type(), ret);
}
stream.resize(ret, true);
stream.pos(ret);
return false;
}
public String
type()
{
return "udp";
}
public String
toString()
{
return Network.fdToString(_fd);
}
public void
checkSendSize(BasicStream stream, int messageSizeMax)
{
if(stream.size() > messageSizeMax)
{
throw new Ice.MemoryLimitException();
}
final int packetSize = java.lang.Math.min(_maxPacketSize, _sndSize - _udpOverhead);
if(packetSize < stream.size())
{
throw new Ice.DatagramLimitException();
}
}
public final boolean
equivalent(String host, int port)
{
java.net.InetSocketAddress addr = Network.getAddress(host, port);
return addr.equals(_addr);
}
public final int
effectivePort()
{
return _addr.getPort();
}
//
// Only for use by UdpEndpoint
//
UdpTransceiver(Instance instance, String host, int port)
{
_traceLevels = instance.traceLevels();
_logger = instance.initializationData().logger;
_stats = instance.initializationData().stats;
_incoming = false;
_connect = true;
_warn = instance.initializationData().properties.getPropertyAsInt("Ice.Warn.Datagrams") > 0;
try
{
_fd = Network.createUdpSocket();
setBufSize(instance);
Network.setBlock(_fd, false);
_addr = Network.getAddress(host, port);
Network.doConnect(_fd, _addr, -1);
_connect = false; // We're connected now
if(_traceLevels.network >= 1)
{
String s = "starting to send udp packets\n" + toString();
_logger.trace(_traceLevels.networkCat, s);
}
}
catch(Ice.LocalException ex)
{
_fd = null;
throw ex;
}
}
//
// Only for use by UdpEndpoint
//
UdpTransceiver(Instance instance, String host, int port, boolean connect)
{
_traceLevels = instance.traceLevels();
_logger = instance.initializationData().logger;
_stats = instance.initializationData().stats;
_incoming = true;
_connect = connect;
_warn = instance.initializationData().properties.getPropertyAsInt("Ice.Warn.Datagrams") > 0;
try
{
_fd = Network.createUdpSocket();
setBufSize(instance);
Network.setBlock(_fd, false);
_addr = new java.net.InetSocketAddress(host, port);
if(_traceLevels.network >= 2)
{
String s = "attempting to bind to udp socket " + Network.addrToString(_addr);
_logger.trace(_traceLevels.networkCat, s);
}
_addr = Network.doBind(_fd, _addr);
if(_traceLevels.network >= 1)
{
String s = "starting to receive udp packets\n" + toString();
_logger.trace(_traceLevels.networkCat, s);
}
}
catch(Ice.LocalException ex)
{
_fd = null;
throw ex;
}
}
private synchronized void
setBufSize(Instance instance)
{
assert(_fd != null);
for(int i = 0; i < 2; ++i)
{
String direction;
String prop;
int dfltSize;
if(i == 0)
{
direction = "receive";
prop = "Ice.UDP.RcvSize";
dfltSize = Network.getRecvBufferSize(_fd);
_rcvSize = dfltSize;
}
else
{
direction = "send";
prop = "Ice.UDP.SndSize";
dfltSize = Network.getSendBufferSize(_fd);
_sndSize = dfltSize;
}
//
// Get property for buffer size and check for sanity.
//
int sizeRequested = instance.initializationData().properties.getPropertyAsIntWithDefault(prop, dfltSize);
if(sizeRequested < _udpOverhead)
{
_logger.warning("Invalid " + prop + " value of " + sizeRequested + " adjusted to " + dfltSize);
sizeRequested = dfltSize;
}
if(sizeRequested != dfltSize)
{
//
// Try to set the buffer size. The kernel will silently adjust
// the size to an acceptable value. Then read the size back to
// get the size that was actually set.
//
int sizeSet;
if(i == 0)
{
Network.setRecvBufferSize(_fd, sizeRequested);
_rcvSize = Network.getRecvBufferSize(_fd);
sizeSet = _rcvSize;
}
else
{
Network.setSendBufferSize(_fd, sizeRequested);
_sndSize = Network.getSendBufferSize(_fd);
sizeSet = _sndSize;
}
//
// Warn if the size that was set is less than the requested size.
//
if(sizeSet < sizeRequested)
{
_logger.warning("UDP " + direction + " buffer size: requested size of "
+ sizeRequested + " adjusted to " + sizeSet);
}
}
}
}
private void
closeSocket()
{
if(_fd != null)
{
if(_traceLevels.network >= 1)
{
String s = "closing udp connection\n" + toString();
_logger.trace(_traceLevels.networkCat, s);
}
try
{
_fd.close();
}
catch(java.io.IOException ex)
{
}
_fd = null;
}
}
protected synchronized void
finalize()
throws Throwable
{
IceUtil.Assert.FinalizerAssert(_fd == null);
super.finalize();
}
private TraceLevels _traceLevels;
private Ice.Logger _logger;
private Ice.Stats _stats;
private boolean _incoming;
private boolean _connect;
private final boolean _warn;
private int _rcvSize;
private int _sndSize;
private java.nio.channels.DatagramChannel _fd;
private java.net.InetSocketAddress _addr;
private java.nio.channels.Selector _readSelector;
//
// The maximum IP datagram size is 65535. Subtract 20 bytes for the IP header and 8 bytes for the UDP header
// to get the maximum payload.
//
private final static int _udpOverhead = 20 + 8;
private final static int _maxPacketSize = 65535 - _udpOverhead;
}
|