1 /* |
1 /* |
2 * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package sun.nio.ch; |
26 package sun.nio.ch; |
27 |
27 |
28 import java.io.*; |
28 import java.io.IOException; |
29 import java.net.*; |
29 import java.net.DatagramPacket; |
30 import java.nio.*; |
30 import java.net.DatagramSocket; |
31 import java.nio.channels.*; |
31 import java.net.DatagramSocketImpl; |
|
32 import java.net.InetAddress; |
|
33 import java.net.InetSocketAddress; |
|
34 import java.net.NetworkInterface; |
|
35 import java.net.SocketAddress; |
|
36 import java.net.SocketException; |
|
37 import java.net.SocketOption; |
|
38 import java.net.SocketTimeoutException; |
|
39 import java.net.StandardSocketOptions; |
|
40 import java.nio.ByteBuffer; |
|
41 import java.nio.channels.ClosedChannelException; |
|
42 import java.nio.channels.DatagramChannel; |
|
43 import java.nio.channels.IllegalBlockingModeException; |
32 |
44 |
33 |
45 |
34 // Make a datagram-socket channel look like a datagram socket. |
46 // Make a datagram-socket channel look like a datagram socket. |
35 // |
47 // |
36 // The methods in this class are defined in exactly the same order as in |
48 // The methods in this class are defined in exactly the same order as in |
176 return dc.receive(bb); |
188 return dc.receive(bb); |
177 } |
189 } |
178 |
190 |
179 dc.configureBlocking(false); |
191 dc.configureBlocking(false); |
180 try { |
192 try { |
181 int n; |
|
182 SocketAddress sender; |
193 SocketAddress sender; |
183 if ((sender = dc.receive(bb)) != null) |
194 if ((sender = dc.receive(bb)) != null) |
184 return sender; |
195 return sender; |
185 long to = timeout; |
196 long to = timeout; |
186 for (;;) { |
197 for (;;) { |
187 if (!dc.isOpen()) |
198 if (!dc.isOpen()) |
188 throw new ClosedChannelException(); |
199 throw new ClosedChannelException(); |
189 long st = System.currentTimeMillis(); |
200 long st = System.currentTimeMillis(); |
190 int result = dc.poll(Net.POLLIN, to); |
201 int result = dc.poll(Net.POLLIN, to); |
191 if (result > 0 && |
202 if (result > 0 && ((result & Net.POLLIN) != 0)) { |
192 ((result & Net.POLLIN) != 0)) { |
|
193 if ((sender = dc.receive(bb)) != null) |
203 if ((sender = dc.receive(bb)) != null) |
194 return sender; |
204 return sender; |
195 } |
205 } |
196 to -= System.currentTimeMillis() - st; |
206 to -= System.currentTimeMillis() - st; |
197 if (to <= 0) |
207 if (to <= 0) |
198 throw new SocketTimeoutException(); |
208 throw new SocketTimeoutException(); |
199 |
|
200 } |
209 } |
201 } finally { |
210 } finally { |
202 if (dc.isOpen()) |
211 try { |
203 dc.configureBlocking(true); |
212 dc.configureBlocking(true); |
|
213 } catch (ClosedChannelException e) { } |
204 } |
214 } |
205 } |
215 } |
206 |
216 |
207 public void receive(DatagramPacket p) throws IOException { |
217 public void receive(DatagramPacket p) throws IOException { |
208 synchronized (dc.blockingLock()) { |
218 synchronized (dc.blockingLock()) { |