~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to external/ikvm/runtime/openjdk/sun.nio.ch.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2011 Jeroen Frijters
 
3
 
 
4
  This software is provided 'as-is', without any express or implied
 
5
  warranty.  In no event will the authors be held liable for any damages
 
6
  arising from the use of this software.
 
7
 
 
8
  Permission is granted to anyone to use this software for any purpose,
 
9
  including commercial applications, and to alter it and redistribute it
 
10
  freely, subject to the following restrictions:
 
11
 
 
12
  1. The origin of this software must not be misrepresented; you must not
 
13
     claim that you wrote the original software. If you use this software
 
14
     in a product, an acknowledgment in the product documentation would be
 
15
     appreciated but is not required.
 
16
  2. Altered source versions must be plainly marked as such, and must not be
 
17
     misrepresented as being the original software.
 
18
  3. This notice may not be removed or altered from any source distribution.
 
19
 
 
20
  Jeroen Frijters
 
21
  jeroen@frijters.net
 
22
  
 
23
*/
 
24
 
 
25
using System;
 
26
using System.Collections.Generic;
 
27
using FileDescriptor = java.io.FileDescriptor;
 
28
using InetAddress = java.net.InetAddress;
 
29
using ByteBuffer = java.nio.ByteBuffer;
 
30
 
 
31
static class Java_sun_nio_ch_DatagramChannelImpl
 
32
{
 
33
        public static void initIDs()
 
34
        {
 
35
        }
 
36
 
 
37
        public static void disconnect0(FileDescriptor fd)
 
38
        {
 
39
#if !FIRST_PASS
 
40
                try
 
41
                {
 
42
                        fd.getSocket().Connect(new System.Net.IPEndPoint(System.Net.IPAddress.Any, 0));
 
43
                        IKVM.NativeCode.sun.nio.ch.Net.setConnectionReset(fd.getSocket(), false);
 
44
                }
 
45
                catch (System.Net.Sockets.SocketException x)
 
46
                {
 
47
                        throw java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
48
                }
 
49
                catch (ObjectDisposedException)
 
50
                {
 
51
                        throw new java.net.SocketException("Socket is closed");
 
52
                }
 
53
#endif
 
54
        }
 
55
 
 
56
        public static int receive0(object obj, FileDescriptor fd, byte[] buf, int pos, int len, bool connected)
 
57
        {
 
58
#if FIRST_PASS
 
59
                return 0;
 
60
#else
 
61
                sun.nio.ch.DatagramChannelImpl impl = (sun.nio.ch.DatagramChannelImpl)obj;
 
62
                java.net.SocketAddress remoteAddress = impl.remoteAddress();
 
63
                System.Net.EndPoint remoteEP;
 
64
                if (fd.getSocket().AddressFamily == System.Net.Sockets.AddressFamily.InterNetworkV6)
 
65
                {
 
66
                        remoteEP = new System.Net.IPEndPoint(System.Net.IPAddress.IPv6Any, 0);
 
67
                }
 
68
                else
 
69
                {
 
70
                        remoteEP = new System.Net.IPEndPoint(0, 0);
 
71
                }
 
72
                java.net.InetSocketAddress addr;
 
73
                int length;
 
74
                do
 
75
                {
 
76
                        for (; ; )
 
77
                        {
 
78
                                try
 
79
                                {
 
80
                                        length = fd.getSocket().ReceiveFrom(buf, pos, len, System.Net.Sockets.SocketFlags.None, ref remoteEP);
 
81
                                        break;
 
82
                                }
 
83
                                catch (System.Net.Sockets.SocketException x)
 
84
                                {
 
85
                                        if (x.ErrorCode == java.net.SocketUtil.WSAECONNRESET)
 
86
                                        {
 
87
                                                // A previous send failed (i.e. the remote host responded with a ICMP that the port is closed) and
 
88
                                                // the winsock stack helpfully lets us know this, but we only care about this when we're connected,
 
89
                                                // otherwise we'll simply retry the receive (note that we use SIO_UDP_CONNRESET to prevent these
 
90
                                                // WSAECONNRESET exceptions, but when switching from connected to disconnected, some can slip through).
 
91
                                                if (connected)
 
92
                                                {
 
93
                                                        throw new java.net.PortUnreachableException();
 
94
                                                }
 
95
                                                continue;
 
96
                                        }
 
97
                                        if (x.ErrorCode == java.net.SocketUtil.WSAEMSGSIZE)
 
98
                                        {
 
99
                                                // The buffer size was too small for the packet, ReceiveFrom receives the part of the packet
 
100
                                                // that fits in the buffer and then throws an exception, so we have to ignore the exception in this case.
 
101
                                                length = len;
 
102
                                                break;
 
103
                                        }
 
104
                                        if (x.ErrorCode == java.net.SocketUtil.WSAEWOULDBLOCK)
 
105
                                        {
 
106
                                                return sun.nio.ch.IOStatus.UNAVAILABLE;
 
107
                                        }
 
108
                                        throw java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
109
                                }
 
110
                                catch (ObjectDisposedException)
 
111
                                {
 
112
                                        throw new java.net.SocketException("Socket is closed");
 
113
                                }
 
114
                        }
 
115
                        System.Net.IPEndPoint ep = (System.Net.IPEndPoint)remoteEP;
 
116
                        addr = new java.net.InetSocketAddress(java.net.SocketUtil.getInetAddressFromIPEndPoint(ep), ep.Port);
 
117
                } while (remoteAddress != null && !addr.equals(remoteAddress));
 
118
                impl.sender = addr;
 
119
                return length;
 
120
#endif
 
121
        }
 
122
 
 
123
        public static int send0(object obj, bool preferIPv6, FileDescriptor fd, byte[] buf, int pos, int len, object sa)
 
124
        {
 
125
#if FIRST_PASS
 
126
                return 0;
 
127
#else
 
128
                java.net.InetSocketAddress addr = (java.net.InetSocketAddress)sa;
 
129
                try
 
130
                {
 
131
                        return fd.getSocket().SendTo(buf, pos, len, System.Net.Sockets.SocketFlags.None, new System.Net.IPEndPoint(java.net.SocketUtil.getAddressFromInetAddress(addr.getAddress(), preferIPv6), addr.getPort()));
 
132
                }
 
133
                catch (System.Net.Sockets.SocketException x)
 
134
                {
 
135
                        if (x.ErrorCode == java.net.SocketUtil.WSAEWOULDBLOCK)
 
136
                        {
 
137
                                return sun.nio.ch.IOStatus.UNAVAILABLE;
 
138
                        }
 
139
                        throw java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
140
                }
 
141
                catch (ObjectDisposedException)
 
142
                {
 
143
                        throw new java.net.SocketException("Socket is closed");
 
144
                }
 
145
#endif
 
146
        }
 
147
}
 
148
 
 
149
#if !FIRST_PASS
 
150
namespace IKVM.Internal.AsyncSocket
 
151
{
 
152
        using System.Net;
 
153
        using System.Net.Sockets;
 
154
 
 
155
        abstract class OperationBase<TInput>
 
156
        {
 
157
                private static readonly AsyncCallback callback = CallbackProc;
 
158
                private Socket socket;
 
159
                private sun.nio.ch.Iocp.ResultHandler handler;
 
160
                private int result;
 
161
                private Exception exception;
 
162
 
 
163
                internal int Do(Socket socket, TInput input, object handler)
 
164
                {
 
165
                        try
 
166
                        {
 
167
                                this.socket = socket;
 
168
                                this.handler = (sun.nio.ch.Iocp.ResultHandler)handler;
 
169
                                IAsyncResult ar = Begin(socket, input, callback, this);
 
170
                                if (ar.CompletedSynchronously)
 
171
                                {
 
172
                                        if (exception != null)
 
173
                                        {
 
174
                                                throw exception;
 
175
                                        }
 
176
                                        return result;
 
177
                                }
 
178
                                else
 
179
                                {
 
180
                                        return sun.nio.ch.IOStatus.UNAVAILABLE;
 
181
                                }
 
182
                        }
 
183
                        catch (SocketException x)
 
184
                        {
 
185
                                throw java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
186
                        }
 
187
                        catch (ObjectDisposedException)
 
188
                        {
 
189
                                throw new java.nio.channels.ClosedChannelException();
 
190
                        }
 
191
                }
 
192
 
 
193
                private static void CallbackProc(IAsyncResult ar)
 
194
                {
 
195
                        OperationBase<TInput> obj = (OperationBase<TInput>)ar.AsyncState;
 
196
                        try
 
197
                        {
 
198
                                int result = obj.End(obj.socket, ar);
 
199
                                if (ar.CompletedSynchronously)
 
200
                                {
 
201
                                        obj.result = result;
 
202
                                }
 
203
                                else
 
204
                                {
 
205
                                        obj.handler.completed(result, false);
 
206
                                }
 
207
                        }
 
208
                        catch (SocketException x)
 
209
                        {
 
210
                                if (ar.CompletedSynchronously)
 
211
                                {
 
212
                                        obj.exception = x;
 
213
                                }
 
214
                                else
 
215
                                {
 
216
                                        obj.handler.failed(x.ErrorCode, java.net.SocketUtil.convertSocketExceptionToIOException(x));
 
217
                                }
 
218
                        }
 
219
                        catch (ObjectDisposedException x)
 
220
                        {
 
221
                                if (ar.CompletedSynchronously)
 
222
                                {
 
223
                                        obj.exception = x;
 
224
                                }
 
225
                                else
 
226
                                {
 
227
                                        obj.handler.failed(0, new java.nio.channels.ClosedChannelException());
 
228
                                }
 
229
                        }
 
230
                }
 
231
 
 
232
                protected abstract IAsyncResult Begin(Socket socket, TInput input, AsyncCallback callback, object state);
 
233
                protected abstract int End(Socket socket, IAsyncResult ar);
 
234
        }
 
235
}
 
236
#endif
 
237
 
 
238
static class Java_sun_nio_ch_WindowsAsynchronousServerSocketChannelImpl
 
239
{
 
240
#if !FIRST_PASS
 
241
        sealed class Accept : IKVM.Internal.AsyncSocket.OperationBase<System.Net.Sockets.Socket>
 
242
        {
 
243
                protected override IAsyncResult Begin(System.Net.Sockets.Socket listenSocket, System.Net.Sockets.Socket acceptSocket, AsyncCallback callback, object state)
 
244
                {
 
245
                        return listenSocket.BeginAccept(acceptSocket, 0, callback, state);
 
246
                }
 
247
 
 
248
                protected override int End(System.Net.Sockets.Socket socket, IAsyncResult ar)
 
249
                {
 
250
                        socket.EndAccept(ar);
 
251
                        return 0;
 
252
                }
 
253
        }
 
254
#endif
 
255
 
 
256
        public static void initIDs()
 
257
        {
 
258
        }
 
259
 
 
260
        public static int accept0(FileDescriptor listenSocket, FileDescriptor acceptSocket, object handler)
 
261
        {
 
262
#if FIRST_PASS
 
263
                return 0;
 
264
#else
 
265
                return new Accept().Do(listenSocket.getSocket(), acceptSocket.getSocket(), handler);
 
266
#endif
 
267
        }
 
268
 
 
269
        public static void updateAcceptContext(FileDescriptor listenSocket, FileDescriptor acceptSocket)
 
270
        {
 
271
                // already handled by .NET Framework
 
272
        }
 
273
 
 
274
        public static void closesocket0(long socket)
 
275
        {
 
276
                // unused
 
277
        }
 
278
}
 
279
 
 
280
static class Java_sun_nio_ch_WindowsAsynchronousSocketChannelImpl
 
281
{
 
282
#if !FIRST_PASS
 
283
        sealed class Connect : IKVM.Internal.AsyncSocket.OperationBase<System.Net.IPEndPoint>
 
284
        {
 
285
                protected override IAsyncResult Begin(System.Net.Sockets.Socket socket, System.Net.IPEndPoint remoteEP, AsyncCallback callback, object state)
 
286
                {
 
287
                        return socket.BeginConnect(remoteEP, callback, state);
 
288
                }
 
289
 
 
290
                protected override int End(System.Net.Sockets.Socket socket, IAsyncResult ar)
 
291
                {
 
292
                        socket.EndConnect(ar);
 
293
                        return 0;
 
294
                }
 
295
        }
 
296
 
 
297
        private static List<ArraySegment<byte>> ByteBuffersToList(ByteBuffer[] bufs)
 
298
        {
 
299
                List<ArraySegment<byte>> list = new List<ArraySegment<byte>>(bufs.Length);
 
300
                foreach (ByteBuffer bb in bufs)
 
301
                {
 
302
                        list.Add(new ArraySegment<byte>(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining()));
 
303
                }
 
304
                return list;
 
305
        }
 
306
 
 
307
        sealed class Receive : IKVM.Internal.AsyncSocket.OperationBase<ByteBuffer[]>
 
308
        {
 
309
                protected override IAsyncResult Begin(System.Net.Sockets.Socket socket, ByteBuffer[] bufs, AsyncCallback callback, object state)
 
310
                {
 
311
                        return socket.BeginReceive(ByteBuffersToList(bufs), System.Net.Sockets.SocketFlags.None, callback, state);
 
312
                }
 
313
 
 
314
                protected override int End(System.Net.Sockets.Socket socket, IAsyncResult ar)
 
315
                {
 
316
                        return socket.EndReceive(ar);
 
317
                }
 
318
        }
 
319
 
 
320
        sealed class Send : IKVM.Internal.AsyncSocket.OperationBase<ByteBuffer[]>
 
321
        {
 
322
                protected override IAsyncResult Begin(System.Net.Sockets.Socket socket, ByteBuffer[] bufs, AsyncCallback callback, object state)
 
323
                {
 
324
                        return socket.BeginSend(ByteBuffersToList(bufs), System.Net.Sockets.SocketFlags.None, callback, state);
 
325
                }
 
326
 
 
327
                protected override int End(System.Net.Sockets.Socket socket, IAsyncResult ar)
 
328
                {
 
329
                        return socket.EndSend(ar);
 
330
                }
 
331
        }
 
332
#endif
 
333
 
 
334
        public static void initIDs()
 
335
        {
 
336
        }
 
337
 
 
338
        public static int connect0(FileDescriptor fd, bool preferIPv6, InetAddress remote, int remotePort, object handler)
 
339
        {
 
340
#if FIRST_PASS
 
341
                return 0;
 
342
#else
 
343
                return new Connect().Do(fd.getSocket(), new System.Net.IPEndPoint(java.net.SocketUtil.getAddressFromInetAddress(remote, preferIPv6), remotePort), handler);
 
344
#endif
 
345
        }
 
346
 
 
347
        public static void updateConnectContext(FileDescriptor fd)
 
348
        {
 
349
                // already handled by .NET Framework
 
350
        }
 
351
 
 
352
        public static int read0(FileDescriptor fd, ByteBuffer[] bufs, object handler)
 
353
        {
 
354
#if FIRST_PASS
 
355
                return 0;
 
356
#else
 
357
                return new Receive().Do(fd.getSocket(), bufs, handler);
 
358
#endif
 
359
        }
 
360
 
 
361
        public static int write0(FileDescriptor fd, ByteBuffer[] bufs, object handler)
 
362
        {
 
363
#if FIRST_PASS
 
364
                return 0;
 
365
#else
 
366
                return new Send().Do(fd.getSocket(), bufs, handler);
 
367
#endif
 
368
        }
 
369
 
 
370
        public static void shutdown0(long socket, int how)
 
371
        {
 
372
                // unused
 
373
        }
 
374
 
 
375
        public static void closesocket0(long socket)
 
376
        {
 
377
                // unused
 
378
        }
 
379
}
 
380
 
 
381
namespace IKVM.NativeCode.sun.nio.ch
 
382
{
 
383
        static class SocketDispatcher
 
384
        {
 
385
                public static long read(object nd, FileDescriptor fd, ByteBuffer[] bufs, int offset, int length)
 
386
                {
 
387
#if FIRST_PASS
 
388
                        return 0;
 
389
#else
 
390
                        ByteBuffer[] altBufs = null;
 
391
                        List<ArraySegment<byte>> list = new List<ArraySegment<byte>>(length);
 
392
                        for (int i = 0; i < length; i++)
 
393
                        {
 
394
                                ByteBuffer bb = bufs[i + offset];
 
395
                                if (!bb.hasArray())
 
396
                                {
 
397
                                        if (altBufs == null)
 
398
                                        {
 
399
                                                altBufs = new ByteBuffer[bufs.Length];
 
400
                                        }
 
401
                                        bb = altBufs[i + offset] = ByteBuffer.allocate(bb.remaining());
 
402
                                }
 
403
                                list.Add(new ArraySegment<byte>(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining()));
 
404
                        }
 
405
                        int count;
 
406
                        try
 
407
                        {
 
408
                                count = fd.getSocket().Receive(list);
 
409
                        }
 
410
                        catch (System.Net.Sockets.SocketException x)
 
411
                        {
 
412
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
413
                        }
 
414
                        catch (ObjectDisposedException)
 
415
                        {
 
416
                                throw new global::java.net.SocketException("Socket is closed");
 
417
                        }
 
418
                        int total = count;
 
419
                        for (int i = 0; total > 0 && i < length; i++)
 
420
                        {
 
421
                                ByteBuffer bb = bufs[i + offset];
 
422
                                ByteBuffer abb;
 
423
                                int consumed = Math.Min(total, bb.remaining());
 
424
                                if (altBufs != null && (abb = altBufs[i + offset]) != null)
 
425
                                {
 
426
                                        abb.position(consumed);
 
427
                                        abb.flip();
 
428
                                        bb.put(abb);
 
429
                                }
 
430
                                else
 
431
                                {
 
432
                                        bb.position(bb.position() + consumed);
 
433
                                }
 
434
                                total -= consumed;
 
435
                        }
 
436
                        return count;
 
437
#endif
 
438
                }
 
439
 
 
440
                public static long write(object nd, FileDescriptor fd, ByteBuffer[] bufs, int offset, int length)
 
441
                {
 
442
#if FIRST_PASS
 
443
                        return 0;
 
444
#else
 
445
                        ByteBuffer[] altBufs = null;
 
446
                        List<ArraySegment<byte>> list = new List<ArraySegment<byte>>(length);
 
447
                        for (int i = 0; i < length; i++)
 
448
                        {
 
449
                                ByteBuffer bb = bufs[i + offset];
 
450
                                if (!bb.hasArray())
 
451
                                {
 
452
                                        if (altBufs == null)
 
453
                                        {
 
454
                                                altBufs = new ByteBuffer[bufs.Length];
 
455
                                        }
 
456
                                        ByteBuffer abb = ByteBuffer.allocate(bb.remaining());
 
457
                                        int pos = bb.position();
 
458
                                        abb.put(bb);
 
459
                                        bb.position(pos);
 
460
                                        abb.flip();
 
461
                                        bb = altBufs[i + offset] = abb;
 
462
                                }
 
463
                                list.Add(new ArraySegment<byte>(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining()));
 
464
                        }
 
465
                        int count;
 
466
                        try
 
467
                        {
 
468
                                count = fd.getSocket().Send(list);
 
469
                        }
 
470
                        catch (System.Net.Sockets.SocketException x)
 
471
                        {
 
472
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
473
                        }
 
474
                        catch (ObjectDisposedException)
 
475
                        {
 
476
                                throw new global::java.net.SocketException("Socket is closed");
 
477
                        }
 
478
                        int total = count;
 
479
                        for (int i = 0; total > 0 && i < length; i++)
 
480
                        {
 
481
                                ByteBuffer bb = bufs[i + offset];
 
482
                                int consumed = Math.Min(total, bb.remaining());
 
483
                                bb.position(bb.position() + consumed);
 
484
                                total -= consumed;
 
485
                        }
 
486
                        return count;
 
487
#endif
 
488
                }
 
489
        }
 
490
 
 
491
        static class Net
 
492
        {
 
493
                public static bool isIPv6Available0()
 
494
                {
 
495
                        string env = IKVM.Internal.JVM.SafeGetEnvironmentVariable("IKVM_IPV6");
 
496
                        int val;
 
497
                        if (env != null && Int32.TryParse(env, out val))
 
498
                        {
 
499
                                return (val & 2) != 0;
 
500
                        }
 
501
                        // we only support IPv6 on Vista and up (because there is no TwoStacks nio implementation)
 
502
                        // (non-Windows OSses are currently not supported)
 
503
                        // Mono on Windows doesn't appear to support IPv6 either (Mono on Linux does).
 
504
                        return Type.GetType("Mono.Runtime") == null
 
505
                                && System.Net.Sockets.Socket.OSSupportsIPv6
 
506
                                && Environment.OSVersion.Platform == PlatformID.Win32NT
 
507
                                && Environment.OSVersion.Version.Major >= 6;
 
508
                }
 
509
 
 
510
                public static bool canIPv6SocketJoinIPv4Group0()
 
511
                {
 
512
                        return false;
 
513
                }
 
514
 
 
515
                public static bool canJoin6WithIPv4Group0()
 
516
                {
 
517
                        return false;
 
518
                }
 
519
 
 
520
                public static void shutdown(FileDescriptor fd, int how)
 
521
                {
 
522
#if !FIRST_PASS
 
523
                        try
 
524
                        {
 
525
                                fd.getSocket().Shutdown(how == global::sun.nio.ch.Net.SHUT_RD
 
526
                                        ? System.Net.Sockets.SocketShutdown.Receive
 
527
                                        : System.Net.Sockets.SocketShutdown.Send);
 
528
                        }
 
529
                        catch (System.Net.Sockets.SocketException x)
 
530
                        {
 
531
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
532
                        }
 
533
                        catch (ObjectDisposedException)
 
534
                        {
 
535
                                throw new global::java.net.SocketException("Socket is closed");
 
536
                        }
 
537
#endif
 
538
                }
 
539
 
 
540
                public static int localPort(FileDescriptor fd)
 
541
                {
 
542
#if FIRST_PASS
 
543
                        return 0;
 
544
#else
 
545
                        try
 
546
                        {
 
547
                                System.Net.IPEndPoint ep = (System.Net.IPEndPoint)fd.getSocket().LocalEndPoint;
 
548
                                return ep.Port;
 
549
                        }
 
550
                        catch (System.Net.Sockets.SocketException x)
 
551
                        {
 
552
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
553
                        }
 
554
                        catch (System.ObjectDisposedException)
 
555
                        {
 
556
                                throw new global::java.net.SocketException("Socket is closed");
 
557
                        }
 
558
#endif
 
559
                }
 
560
 
 
561
                public static InetAddress localInetAddress(FileDescriptor fd)
 
562
                {
 
563
#if FIRST_PASS
 
564
                        return null;
 
565
#else
 
566
                        try
 
567
                        {
 
568
                                System.Net.IPEndPoint ep = (System.Net.IPEndPoint)fd.getSocket().LocalEndPoint;
 
569
                                return global::java.net.SocketUtil.getInetAddressFromIPEndPoint(ep);
 
570
                        }
 
571
                        catch (System.Net.Sockets.SocketException x)
 
572
                        {
 
573
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
574
                        }
 
575
                        catch (System.ObjectDisposedException)
 
576
                        {
 
577
                                throw new global::java.net.SocketException("Socket is closed");
 
578
                        }
 
579
#endif
 
580
                }
 
581
 
 
582
                public static int remotePort(FileDescriptor fd)
 
583
                {
 
584
#if FIRST_PASS
 
585
                        return 0;
 
586
#else
 
587
                        try
 
588
                        {
 
589
                                System.Net.IPEndPoint ep = (System.Net.IPEndPoint)fd.getSocket().RemoteEndPoint;
 
590
                                return ep.Port;
 
591
                        }
 
592
                        catch (System.Net.Sockets.SocketException x)
 
593
                        {
 
594
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
595
                        }
 
596
                        catch (System.ObjectDisposedException)
 
597
                        {
 
598
                                throw new global::java.net.SocketException("Socket is closed");
 
599
                        }
 
600
#endif
 
601
                }
 
602
 
 
603
                public static InetAddress remoteInetAddress(FileDescriptor fd)
 
604
                {
 
605
#if FIRST_PASS
 
606
                        return null;
 
607
#else
 
608
                        try
 
609
                        {
 
610
                                System.Net.IPEndPoint ep = (System.Net.IPEndPoint)fd.getSocket().RemoteEndPoint;
 
611
                                return global::java.net.SocketUtil.getInetAddressFromIPEndPoint(ep);
 
612
                        }
 
613
                        catch (System.Net.Sockets.SocketException x)
 
614
                        {
 
615
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
616
                        }
 
617
                        catch (System.ObjectDisposedException)
 
618
                        {
 
619
                                throw new global::java.net.SocketException("Socket is closed");
 
620
                        }
 
621
#endif
 
622
                }
 
623
 
 
624
                public static int getIntOption0(FileDescriptor fd, bool mayNeedConversion, int level, int opt)
 
625
                {
 
626
#if FIRST_PASS
 
627
                        return 0;
 
628
#else
 
629
                        System.Net.Sockets.SocketOptionLevel sol = (System.Net.Sockets.SocketOptionLevel)level;
 
630
                        System.Net.Sockets.SocketOptionName son = (System.Net.Sockets.SocketOptionName)opt;
 
631
                        try
 
632
                        {
 
633
                                object obj = fd.getSocket().GetSocketOption(sol, son);
 
634
                                System.Net.Sockets.LingerOption linger = obj as System.Net.Sockets.LingerOption;
 
635
                                if (linger != null)
 
636
                                {
 
637
                                        return linger.Enabled ? linger.LingerTime : -1;
 
638
                                }
 
639
                                return (int)obj;
 
640
                        }
 
641
                        catch (System.Net.Sockets.SocketException x)
 
642
                        {
 
643
                                if (mayNeedConversion)
 
644
                                {
 
645
                                        if (x.ErrorCode == global::java.net.SocketUtil.WSAENOPROTOOPT
 
646
                                                && sol == System.Net.Sockets.SocketOptionLevel.IP
 
647
                                                && son == System.Net.Sockets.SocketOptionName.TypeOfService)
 
648
                                        {
 
649
                                                return 0;
 
650
                                        }
 
651
                                }
 
652
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
653
                        }
 
654
                        catch (System.ObjectDisposedException)
 
655
                        {
 
656
                                throw new global::java.net.SocketException("Socket is closed");
 
657
                        }
 
658
#endif
 
659
                }
 
660
 
 
661
                public static void setIntOption0(FileDescriptor fd, bool mayNeedConversion, int level, int opt, int arg)
 
662
                {
 
663
#if !FIRST_PASS
 
664
                        System.Net.Sockets.SocketOptionLevel sol = (System.Net.Sockets.SocketOptionLevel)level;
 
665
                        System.Net.Sockets.SocketOptionName son = (System.Net.Sockets.SocketOptionName)opt;
 
666
                        if (mayNeedConversion)
 
667
                        {
 
668
                                const int IPTOS_TOS_MASK = 0x1e;
 
669
                                const int IPTOS_PREC_MASK = 0xe0;
 
670
                                if (sol == System.Net.Sockets.SocketOptionLevel.IP
 
671
                                        && son == System.Net.Sockets.SocketOptionName.TypeOfService)
 
672
                                {
 
673
                                        arg &= (IPTOS_TOS_MASK | IPTOS_PREC_MASK);
 
674
                                }
 
675
                        }
 
676
                        try
 
677
                        {
 
678
                                fd.getSocket().SetSocketOption(sol, son, arg);
 
679
                        }
 
680
                        catch (System.Net.Sockets.SocketException x)
 
681
                        {
 
682
                                if (mayNeedConversion)
 
683
                                {
 
684
                                        if (x.ErrorCode == global::java.net.SocketUtil.WSAENOPROTOOPT
 
685
                                                && sol == System.Net.Sockets.SocketOptionLevel.IP
 
686
                                                && (son == System.Net.Sockets.SocketOptionName.TypeOfService || son == System.Net.Sockets.SocketOptionName.MulticastLoopback))
 
687
                                        {
 
688
                                                return;
 
689
                                        }
 
690
                                        if (x.ErrorCode == global::java.net.SocketUtil.WSAEINVAL
 
691
                                                && sol == System.Net.Sockets.SocketOptionLevel.IP
 
692
                                                && son == System.Net.Sockets.SocketOptionName.TypeOfService)
 
693
                                        {
 
694
                                                return;
 
695
                                        }
 
696
                                }
 
697
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
698
                        }
 
699
                        catch (System.ObjectDisposedException)
 
700
                        {
 
701
                                throw new global::java.net.SocketException("Socket is closed");
 
702
                        }
 
703
#endif
 
704
                }
 
705
 
 
706
                private static void PutInt(byte[] buf, int pos, int value)
 
707
                {
 
708
                        buf[pos + 0] = (byte)(value >> 24);
 
709
                        buf[pos + 1] = (byte)(value >> 16);
 
710
                        buf[pos + 2] = (byte)(value >> 8);
 
711
                        buf[pos + 3] = (byte)(value >> 0);
 
712
                }
 
713
 
 
714
                public static int joinOrDrop4(bool join, FileDescriptor fd, int group, int interf, int source)
 
715
                {
 
716
#if FIRST_PASS
 
717
                        return 0;
 
718
#else
 
719
                        try
 
720
                        {
 
721
                                if (source == 0)
 
722
                                {
 
723
                                        fd.getSocket().SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP,
 
724
                                                join ? System.Net.Sockets.SocketOptionName.AddMembership : System.Net.Sockets.SocketOptionName.DropMembership,
 
725
                                                new System.Net.Sockets.MulticastOption(new System.Net.IPAddress(System.Net.IPAddress.HostToNetworkOrder(group) & 0xFFFFFFFFL), new System.Net.IPAddress(System.Net.IPAddress.HostToNetworkOrder(interf) & 0xFFFFFFFFL)));
 
726
                                }
 
727
                                else
 
728
                                {
 
729
                                        // ip_mreq_source
 
730
                                        byte[] optionValue = new byte[12];
 
731
                                        PutInt(optionValue, 0, group);
 
732
                                        PutInt(optionValue, 4, source);
 
733
                                        PutInt(optionValue, 8, interf);
 
734
                                        fd.getSocket().SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP,
 
735
                                                join ? System.Net.Sockets.SocketOptionName.AddSourceMembership : System.Net.Sockets.SocketOptionName.DropSourceMembership,
 
736
                                                optionValue);
 
737
                                }
 
738
                                return 0;
 
739
                        }
 
740
                        catch (System.Net.Sockets.SocketException x)
 
741
                        {
 
742
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
743
                        }
 
744
                        catch (System.ObjectDisposedException)
 
745
                        {
 
746
                                throw new global::java.net.SocketException("Socket is closed");
 
747
                        }
 
748
#endif
 
749
                }
 
750
 
 
751
                public static int blockOrUnblock4(bool block, FileDescriptor fd, int group, int interf, int source)
 
752
                {
 
753
#if FIRST_PASS
 
754
                        return 0;
 
755
#else
 
756
                        try
 
757
                        {
 
758
                                // ip_mreq_source
 
759
                                byte[] optionValue = new byte[12];
 
760
                                PutInt(optionValue, 0, group);
 
761
                                PutInt(optionValue, 4, source);
 
762
                                PutInt(optionValue, 8, interf);
 
763
                                fd.getSocket().SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP,
 
764
                                        block ? System.Net.Sockets.SocketOptionName.BlockSource : System.Net.Sockets.SocketOptionName.UnblockSource,
 
765
                                        optionValue);
 
766
                                return 0;
 
767
                        }
 
768
                        catch (System.Net.Sockets.SocketException x)
 
769
                        {
 
770
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
771
                        }
 
772
                        catch (System.ObjectDisposedException)
 
773
                        {
 
774
                                throw new global::java.net.SocketException("Socket is closed");
 
775
                        }
 
776
#endif
 
777
                }
 
778
 
 
779
                // write a sockaddr_in6 into optionValue at offset pos
 
780
                private static void PutSockAddrIn6(byte[] optionValue, int pos, byte[] addr)
 
781
                {
 
782
                        // sin6_family
 
783
                        optionValue[pos] = 23; // AF_INET6
 
784
 
 
785
                        // sin6_addr
 
786
                        Buffer.BlockCopy(addr, 0, optionValue, pos + 8, addr.Length);
 
787
                }
 
788
 
 
789
                public static int joinOrDrop6(bool join, FileDescriptor fd, byte[] group, int index, byte[] source)
 
790
                {
 
791
#if FIRST_PASS
 
792
                        return 0;
 
793
#else
 
794
                        try
 
795
                        {
 
796
                                if (source == null)
 
797
                                {
 
798
                                        fd.getSocket().SetSocketOption(System.Net.Sockets.SocketOptionLevel.IPv6,
 
799
                                                join ? System.Net.Sockets.SocketOptionName.AddMembership : System.Net.Sockets.SocketOptionName.DropMembership,
 
800
                                                new System.Net.Sockets.IPv6MulticastOption(new System.Net.IPAddress(group), index));
 
801
                                }
 
802
                                else
 
803
                                {
 
804
                                        const System.Net.Sockets.SocketOptionName MCAST_JOIN_SOURCE_GROUP = (System.Net.Sockets.SocketOptionName)45;
 
805
                                        const System.Net.Sockets.SocketOptionName MCAST_LEAVE_SOURCE_GROUP = (System.Net.Sockets.SocketOptionName)46;
 
806
                                        // group_source_req
 
807
                                        byte[] optionValue = new byte[264];
 
808
                                        optionValue[0] = (byte)index;
 
809
                                        optionValue[1] = (byte)(index >> 8);
 
810
                                        optionValue[2] = (byte)(index >> 16);
 
811
                                        optionValue[3] = (byte)(index >> 24);
 
812
                                        PutSockAddrIn6(optionValue, 8, group);
 
813
                                        PutSockAddrIn6(optionValue, 136, source);
 
814
                                        fd.getSocket().SetSocketOption(System.Net.Sockets.SocketOptionLevel.IPv6, join ? MCAST_JOIN_SOURCE_GROUP : MCAST_LEAVE_SOURCE_GROUP, optionValue);
 
815
                                }
 
816
                                return 0;
 
817
                        }
 
818
                        catch (System.Net.Sockets.SocketException x)
 
819
                        {
 
820
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
821
                        }
 
822
                        catch (System.ObjectDisposedException)
 
823
                        {
 
824
                                throw new global::java.net.SocketException("Socket is closed");
 
825
                        }
 
826
#endif
 
827
                }
 
828
 
 
829
                public static int blockOrUnblock6(bool block, FileDescriptor fd, byte[] group, int index, byte[] source)
 
830
                {
 
831
#if FIRST_PASS
 
832
                        return 0;
 
833
#else
 
834
                        try
 
835
                        {
 
836
                                const System.Net.Sockets.SocketOptionName MCAST_BLOCK_SOURCE = (System.Net.Sockets.SocketOptionName)43;
 
837
                                const System.Net.Sockets.SocketOptionName MCAST_UNBLOCK_SOURCE = (System.Net.Sockets.SocketOptionName)44;
 
838
                                // group_source_req
 
839
                                byte[] optionValue = new byte[264];
 
840
                                optionValue[0] = (byte)index;
 
841
                                optionValue[1] = (byte)(index >> 8);
 
842
                                optionValue[2] = (byte)(index >> 16);
 
843
                                optionValue[3] = (byte)(index >> 24);
 
844
                                PutSockAddrIn6(optionValue, 8, group);
 
845
                                PutSockAddrIn6(optionValue, 136, source);
 
846
                                fd.getSocket().SetSocketOption(System.Net.Sockets.SocketOptionLevel.IPv6, block ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE, optionValue);
 
847
                                return 0;
 
848
                        }
 
849
                        catch (System.Net.Sockets.SocketException x)
 
850
                        {
 
851
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
852
                        }
 
853
                        catch (System.ObjectDisposedException)
 
854
                        {
 
855
                                throw new global::java.net.SocketException("Socket is closed");
 
856
                        }
 
857
#endif
 
858
                }
 
859
 
 
860
                public static void setInterface4(FileDescriptor fd, int interf)
 
861
                {
 
862
#if !FIRST_PASS
 
863
                        try
 
864
                        {
 
865
                                fd.getSocket().SetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.MulticastInterface, System.Net.IPAddress.HostToNetworkOrder(interf));
 
866
                        }
 
867
                        catch (System.Net.Sockets.SocketException x)
 
868
                        {
 
869
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
870
                        }
 
871
                        catch (System.ObjectDisposedException)
 
872
                        {
 
873
                                throw new global::java.net.SocketException("Socket is closed");
 
874
                        }
 
875
#endif
 
876
                }
 
877
 
 
878
                public static int getInterface4(FileDescriptor fd)
 
879
                {
 
880
#if FIRST_PASS
 
881
                        return 0;
 
882
#else
 
883
                        try
 
884
                        {
 
885
                                return System.Net.IPAddress.NetworkToHostOrder((int)fd.getSocket().GetSocketOption(System.Net.Sockets.SocketOptionLevel.IP, System.Net.Sockets.SocketOptionName.MulticastInterface));
 
886
                        }
 
887
                        catch (System.Net.Sockets.SocketException x)
 
888
                        {
 
889
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
890
                        }
 
891
                        catch (System.ObjectDisposedException)
 
892
                        {
 
893
                                throw new global::java.net.SocketException("Socket is closed");
 
894
                        }
 
895
#endif
 
896
                }
 
897
 
 
898
                public static void setInterface6(FileDescriptor fd, int index)
 
899
                {
 
900
#if !FIRST_PASS
 
901
                        try
 
902
                        {
 
903
                                fd.getSocket().SetSocketOption(System.Net.Sockets.SocketOptionLevel.IPv6, System.Net.Sockets.SocketOptionName.MulticastInterface, index);
 
904
                        }
 
905
                        catch (System.Net.Sockets.SocketException x)
 
906
                        {
 
907
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
908
                        }
 
909
                        catch (System.ObjectDisposedException)
 
910
                        {
 
911
                                throw new global::java.net.SocketException("Socket is closed");
 
912
                        }
 
913
#endif
 
914
                }
 
915
 
 
916
                public static int getInterface6(FileDescriptor fd)
 
917
                {
 
918
#if FIRST_PASS
 
919
                        return 0;
 
920
#else
 
921
                        try
 
922
                        {
 
923
                                return (int)fd.getSocket().GetSocketOption(System.Net.Sockets.SocketOptionLevel.IPv6, System.Net.Sockets.SocketOptionName.MulticastInterface);
 
924
                        }
 
925
                        catch (System.Net.Sockets.SocketException x)
 
926
                        {
 
927
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
928
                        }
 
929
                        catch (System.ObjectDisposedException)
 
930
                        {
 
931
                                throw new global::java.net.SocketException("Socket is closed");
 
932
                        }
 
933
#endif
 
934
                }
 
935
 
 
936
                public static FileDescriptor socket0(bool preferIPv6, bool stream, bool reuse)
 
937
                {
 
938
#if FIRST_PASS
 
939
                        return null;
 
940
#else
 
941
                        try
 
942
                        {
 
943
                                System.Net.Sockets.AddressFamily addressFamily = preferIPv6
 
944
                                        ? System.Net.Sockets.AddressFamily.InterNetworkV6
 
945
                                        : System.Net.Sockets.AddressFamily.InterNetwork;
 
946
                                System.Net.Sockets.SocketType socketType = stream
 
947
                                        ? System.Net.Sockets.SocketType.Stream
 
948
                                        : System.Net.Sockets.SocketType.Dgram;
 
949
                                System.Net.Sockets.ProtocolType protocolType = stream
 
950
                                        ? System.Net.Sockets.ProtocolType.Tcp
 
951
                                        : System.Net.Sockets.ProtocolType.Udp;
 
952
                                System.Net.Sockets.Socket socket = new System.Net.Sockets.Socket(addressFamily, socketType, protocolType);
 
953
                                if (preferIPv6)
 
954
                                {
 
955
                                        // enable IPv4 over IPv6 sockets (note that we don't have to check for >= Vista here, because nio sockets only support IPv6 on >= Vista)
 
956
                                        const System.Net.Sockets.SocketOptionName IPV6_V6ONLY = (System.Net.Sockets.SocketOptionName)27;
 
957
                                        socket.SetSocketOption(System.Net.Sockets.SocketOptionLevel.IPv6, IPV6_V6ONLY, 0);
 
958
                                }
 
959
                                if (!stream)
 
960
                                {
 
961
                                        setConnectionReset(socket, false);
 
962
                                }
 
963
                                FileDescriptor fd = new FileDescriptor();
 
964
                                fd.setSocket(socket);
 
965
                                return fd;
 
966
                        }
 
967
                        catch (System.Net.Sockets.SocketException x)
 
968
                        {
 
969
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
970
                        }
 
971
#endif
 
972
                }
 
973
 
 
974
                public static void bind0(bool preferIPv6, FileDescriptor fd, InetAddress addr, int port)
 
975
                {
 
976
#if !FIRST_PASS
 
977
                        try
 
978
                        {
 
979
                                fd.getSocket().Bind(new System.Net.IPEndPoint(global::java.net.SocketUtil.getAddressFromInetAddress(addr, preferIPv6), port));
 
980
                        }
 
981
                        catch (System.Net.Sockets.SocketException x)
 
982
                        {
 
983
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
984
                        }
 
985
                        catch (System.ObjectDisposedException)
 
986
                        {
 
987
                                throw new global::java.net.SocketException("Socket is closed");
 
988
                        }
 
989
#endif
 
990
                }
 
991
 
 
992
                public static void listen(FileDescriptor fd, int backlog)
 
993
                {
 
994
#if !FIRST_PASS
 
995
                        try
 
996
                        {
 
997
                                fd.getSocket().Listen(backlog);
 
998
                        }
 
999
                        catch (System.Net.Sockets.SocketException x)
 
1000
                        {
 
1001
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
1002
                        }
 
1003
                        catch (System.ObjectDisposedException)
 
1004
                        {
 
1005
                                throw new global::java.net.SocketException("Socket is closed");
 
1006
                        }
 
1007
#endif
 
1008
                }
 
1009
 
 
1010
                internal static void setConnectionReset(System.Net.Sockets.Socket socket, bool enable)
 
1011
                {
 
1012
                        // Windows 2000 introduced a "feature" that causes it to return WSAECONNRESET from receive,
 
1013
                        // if a previous send resulted in an ICMP port unreachable. For unconnected datagram sockets,
 
1014
                        // we disable this feature by using this ioctl.
 
1015
                        const int IOC_IN = unchecked((int)0x80000000);
 
1016
                        const int IOC_VENDOR = 0x18000000;
 
1017
                        const int SIO_UDP_CONNRESET = IOC_IN | IOC_VENDOR | 12;
 
1018
 
 
1019
                        socket.IOControl(SIO_UDP_CONNRESET, new byte[] { enable ? (byte)1 : (byte)0 }, null);
 
1020
                }
 
1021
 
 
1022
                public static int connect0(bool preferIPv6, FileDescriptor fd, InetAddress remote, int remotePort)
 
1023
                {
 
1024
#if FIRST_PASS
 
1025
                        return 0;
 
1026
#else
 
1027
                        try
 
1028
                        {
 
1029
                                System.Net.IPEndPoint ep = new System.Net.IPEndPoint(global::java.net.SocketUtil.getAddressFromInetAddress(remote, preferIPv6), remotePort);
 
1030
                                bool datagram = fd.getSocket().SocketType == System.Net.Sockets.SocketType.Dgram;
 
1031
                                if (datagram || fd.isSocketBlocking())
 
1032
                                {
 
1033
                                        fd.getSocket().Connect(ep);
 
1034
                                        if (datagram)
 
1035
                                        {
 
1036
                                                setConnectionReset(fd.getSocket(), true);
 
1037
                                        }
 
1038
                                        return 1;
 
1039
                                }
 
1040
                                else
 
1041
                                {
 
1042
                                        fd.setAsyncResult(fd.getSocket().BeginConnect(ep, null, null));
 
1043
                                        return global::sun.nio.ch.IOStatus.UNAVAILABLE;
 
1044
                                }
 
1045
                        }
 
1046
                        catch (System.Net.Sockets.SocketException x)
 
1047
                        {
 
1048
                                throw new global::java.net.ConnectException(x.Message);
 
1049
                        }
 
1050
                        catch (System.ObjectDisposedException)
 
1051
                        {
 
1052
                                throw new global::java.net.SocketException("Socket is closed");
 
1053
                        }
 
1054
#endif
 
1055
                }
 
1056
        }
 
1057
 
 
1058
        static class ServerSocketChannelImpl
 
1059
        {
 
1060
                public static int accept0(object _this, FileDescriptor ssfd, FileDescriptor newfd, object isaa)
 
1061
                {
 
1062
#if FIRST_PASS
 
1063
                        return 0;
 
1064
#else
 
1065
                        try
 
1066
                        {
 
1067
                                System.Net.Sockets.Socket netSocket = ssfd.getSocket();
 
1068
                                if (netSocket.Blocking || netSocket.Poll(0, System.Net.Sockets.SelectMode.SelectRead))
 
1069
                                {
 
1070
                                        System.Net.Sockets.Socket accsock = netSocket.Accept();
 
1071
                                        newfd.setSocket(accsock);
 
1072
                                        System.Net.IPEndPoint ep = (System.Net.IPEndPoint)accsock.RemoteEndPoint;
 
1073
                                        ((global::java.net.InetSocketAddress[])isaa)[0] = new global::java.net.InetSocketAddress(global::java.net.SocketUtil.getInetAddressFromIPEndPoint(ep), ep.Port);
 
1074
                                        return 1;
 
1075
                                }
 
1076
                                else
 
1077
                                {
 
1078
                                        return global::sun.nio.ch.IOStatus.UNAVAILABLE;
 
1079
                                }
 
1080
                        }
 
1081
                        catch (System.Net.Sockets.SocketException x)
 
1082
                        {
 
1083
                                throw global::java.net.SocketUtil.convertSocketExceptionToIOException(x);
 
1084
                        }
 
1085
                        catch (System.ObjectDisposedException)
 
1086
                        {
 
1087
                                throw new global::java.net.SocketException("Socket is closed");
 
1088
                        }
 
1089
#endif
 
1090
                }
 
1091
 
 
1092
                public static void initIDs()
 
1093
                {
 
1094
                }
 
1095
        }
 
1096
 
 
1097
        static class SocketChannelImpl
 
1098
        {
 
1099
                public static int checkConnect(FileDescriptor fd, bool block, bool ready)
 
1100
                {
 
1101
#if FIRST_PASS
 
1102
                        return 0;
 
1103
#else
 
1104
                        try
 
1105
                        {
 
1106
                                IAsyncResult asyncConnect = fd.getAsyncResult();
 
1107
                                if (block || ready || asyncConnect.IsCompleted)
 
1108
                                {
 
1109
                                        fd.setAsyncResult(null);
 
1110
                                        fd.getSocket().EndConnect(asyncConnect);
 
1111
                                        // work around for blocking issue
 
1112
                                        fd.getSocket().Blocking = fd.isSocketBlocking();
 
1113
                                        return 1;
 
1114
                                }
 
1115
                                else
 
1116
                                {
 
1117
                                        return global::sun.nio.ch.IOStatus.UNAVAILABLE;
 
1118
                                }
 
1119
                        }
 
1120
                        catch (System.Net.Sockets.SocketException x)
 
1121
                        {
 
1122
                                throw new global::java.net.ConnectException(x.Message);
 
1123
                        }
 
1124
                        catch (System.ObjectDisposedException)
 
1125
                        {
 
1126
                                throw new global::java.net.SocketException("Socket is closed");
 
1127
                        }
 
1128
#endif
 
1129
                }
 
1130
 
 
1131
                public static int sendOutOfBandData(FileDescriptor fd, byte data)
 
1132
                {
 
1133
#if FIRST_PASS
 
1134
                        return 0;
 
1135
#else
 
1136
                        try
 
1137
                        {
 
1138
                                fd.getSocket().Send(new byte[] { data }, 1, System.Net.Sockets.SocketFlags.OutOfBand);
 
1139
                                return 1;
 
1140
                        }
 
1141
                        catch (System.Net.Sockets.SocketException x)
 
1142
                        {
 
1143
                                throw new global::java.net.ConnectException(x.Message);
 
1144
                        }
 
1145
                        catch (System.ObjectDisposedException)
 
1146
                        {
 
1147
                                throw new global::java.net.SocketException("Socket is closed");
 
1148
                        }
 
1149
#endif
 
1150
                }
 
1151
        }
 
1152
}