author | michaelm |
Thu, 16 Sep 2010 08:08:06 -0700 | |
changeset 6875 | 81d6ec3397e5 |
parent 5506 | 202f599c92aa |
child 7027 | d3404b1406d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.OutputStream; |
|
31 |
import java.io.InterruptedIOException; |
|
32 |
import java.io.FileDescriptor; |
|
33 |
import java.io.ByteArrayOutputStream; |
|
34 |
||
35 |
import sun.net.ConnectionResetException; |
|
2446 | 36 |
import sun.net.NetHooks; |
2 | 37 |
|
38 |
/** |
|
39 |
* Default Socket Implementation. This implementation does |
|
40 |
* not implement any security checks. |
|
41 |
* Note this class should <b>NOT</b> be public. |
|
42 |
* |
|
43 |
* @author Steven B. Byrne |
|
44 |
*/ |
|
45 |
abstract class AbstractPlainSocketImpl extends SocketImpl |
|
46 |
{ |
|
47 |
/* instance variable for SO_TIMEOUT */ |
|
48 |
int timeout; // timeout in millisec |
|
49 |
// traffic class |
|
50 |
private int trafficClass; |
|
51 |
||
52 |
private boolean shut_rd = false; |
|
53 |
private boolean shut_wr = false; |
|
54 |
||
55 |
private SocketInputStream socketInputStream = null; |
|
56 |
||
57 |
/* number of threads using the FileDescriptor */ |
|
58 |
protected int fdUseCount = 0; |
|
59 |
||
60 |
/* lock when increment/decrementing fdUseCount */ |
|
61 |
protected Object fdLock = new Object(); |
|
62 |
||
63 |
/* indicates a close is pending on the file descriptor */ |
|
64 |
protected boolean closePending = false; |
|
65 |
||
66 |
/* indicates connection reset state */ |
|
67 |
private int CONNECTION_NOT_RESET = 0; |
|
68 |
private int CONNECTION_RESET_PENDING = 1; |
|
69 |
private int CONNECTION_RESET = 2; |
|
70 |
private int resetState; |
|
71 |
private Object resetLock = new Object(); |
|
72 |
||
73 |
/** |
|
74 |
* Load net library into runtime. |
|
75 |
*/ |
|
76 |
static { |
|
77 |
java.security.AccessController.doPrivileged( |
|
78 |
new sun.security.action.LoadLibraryAction("net")); |
|
79 |
} |
|
80 |
||
81 |
/** |
|
82 |
* Creates a socket with a boolean that specifies whether this |
|
83 |
* is a stream socket (true) or an unconnected UDP socket (false). |
|
84 |
*/ |
|
85 |
protected synchronized void create(boolean stream) throws IOException { |
|
86 |
fd = new FileDescriptor(); |
|
87 |
socketCreate(stream); |
|
88 |
if (socket != null) |
|
89 |
socket.setCreated(); |
|
90 |
if (serverSocket != null) |
|
91 |
serverSocket.setCreated(); |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Creates a socket and connects it to the specified port on |
|
96 |
* the specified host. |
|
97 |
* @param host the specified host |
|
98 |
* @param port the specified port |
|
99 |
*/ |
|
100 |
protected void connect(String host, int port) |
|
101 |
throws UnknownHostException, IOException |
|
102 |
{ |
|
103 |
IOException pending = null; |
|
104 |
try { |
|
105 |
InetAddress address = InetAddress.getByName(host); |
|
106 |
this.port = port; |
|
107 |
this.address = address; |
|
108 |
||
109 |
try { |
|
110 |
connectToAddress(address, port, timeout); |
|
111 |
return; |
|
112 |
} catch (IOException e) { |
|
113 |
pending = e; |
|
114 |
} |
|
115 |
} catch (UnknownHostException e) { |
|
116 |
pending = e; |
|
117 |
} |
|
118 |
||
119 |
// everything failed |
|
120 |
close(); |
|
121 |
throw pending; |
|
122 |
} |
|
123 |
||
124 |
/** |
|
125 |
* Creates a socket and connects it to the specified address on |
|
126 |
* the specified port. |
|
127 |
* @param address the address |
|
128 |
* @param port the specified port |
|
129 |
*/ |
|
130 |
protected void connect(InetAddress address, int port) throws IOException { |
|
131 |
this.port = port; |
|
132 |
this.address = address; |
|
133 |
||
134 |
try { |
|
135 |
connectToAddress(address, port, timeout); |
|
136 |
return; |
|
137 |
} catch (IOException e) { |
|
138 |
// everything failed |
|
139 |
close(); |
|
140 |
throw e; |
|
141 |
} |
|
142 |
} |
|
143 |
||
144 |
/** |
|
145 |
* Creates a socket and connects it to the specified address on |
|
146 |
* the specified port. |
|
147 |
* @param address the address |
|
148 |
* @param timeout the timeout value in milliseconds, or zero for no timeout. |
|
149 |
* @throws IOException if connection fails |
|
150 |
* @throws IllegalArgumentException if address is null or is a |
|
151 |
* SocketAddress subclass not supported by this socket |
|
152 |
* @since 1.4 |
|
153 |
*/ |
|
154 |
protected void connect(SocketAddress address, int timeout) throws IOException { |
|
155 |
if (address == null || !(address instanceof InetSocketAddress)) |
|
156 |
throw new IllegalArgumentException("unsupported address type"); |
|
157 |
InetSocketAddress addr = (InetSocketAddress) address; |
|
158 |
if (addr.isUnresolved()) |
|
159 |
throw new UnknownHostException(addr.getHostName()); |
|
160 |
this.port = addr.getPort(); |
|
161 |
this.address = addr.getAddress(); |
|
162 |
||
163 |
try { |
|
164 |
connectToAddress(this.address, port, timeout); |
|
165 |
return; |
|
166 |
} catch (IOException e) { |
|
167 |
// everything failed |
|
168 |
close(); |
|
169 |
throw e; |
|
170 |
} |
|
171 |
} |
|
172 |
||
173 |
private void connectToAddress(InetAddress address, int port, int timeout) throws IOException { |
|
174 |
if (address.isAnyLocalAddress()) { |
|
175 |
doConnect(InetAddress.getLocalHost(), port, timeout); |
|
176 |
} else { |
|
177 |
doConnect(address, port, timeout); |
|
178 |
} |
|
179 |
} |
|
180 |
||
181 |
public void setOption(int opt, Object val) throws SocketException { |
|
182 |
if (isClosedOrPending()) { |
|
183 |
throw new SocketException("Socket Closed"); |
|
184 |
} |
|
185 |
boolean on = true; |
|
186 |
switch (opt) { |
|
187 |
/* check type safety b4 going native. These should never |
|
188 |
* fail, since only java.Socket* has access to |
|
189 |
* PlainSocketImpl.setOption(). |
|
190 |
*/ |
|
191 |
case SO_LINGER: |
|
192 |
if (val == null || (!(val instanceof Integer) && !(val instanceof Boolean))) |
|
193 |
throw new SocketException("Bad parameter for option"); |
|
194 |
if (val instanceof Boolean) { |
|
195 |
/* true only if disabling - enabling should be Integer */ |
|
196 |
on = false; |
|
197 |
} |
|
198 |
break; |
|
199 |
case SO_TIMEOUT: |
|
200 |
if (val == null || (!(val instanceof Integer))) |
|
201 |
throw new SocketException("Bad parameter for SO_TIMEOUT"); |
|
202 |
int tmp = ((Integer) val).intValue(); |
|
203 |
if (tmp < 0) |
|
204 |
throw new IllegalArgumentException("timeout < 0"); |
|
205 |
timeout = tmp; |
|
206 |
break; |
|
207 |
case IP_TOS: |
|
208 |
if (val == null || !(val instanceof Integer)) { |
|
209 |
throw new SocketException("bad argument for IP_TOS"); |
|
210 |
} |
|
211 |
trafficClass = ((Integer)val).intValue(); |
|
212 |
break; |
|
213 |
case SO_BINDADDR: |
|
214 |
throw new SocketException("Cannot re-bind socket"); |
|
215 |
case TCP_NODELAY: |
|
216 |
if (val == null || !(val instanceof Boolean)) |
|
217 |
throw new SocketException("bad parameter for TCP_NODELAY"); |
|
218 |
on = ((Boolean)val).booleanValue(); |
|
219 |
break; |
|
220 |
case SO_SNDBUF: |
|
221 |
case SO_RCVBUF: |
|
222 |
if (val == null || !(val instanceof Integer) || |
|
223 |
!(((Integer)val).intValue() > 0)) { |
|
224 |
throw new SocketException("bad parameter for SO_SNDBUF " + |
|
225 |
"or SO_RCVBUF"); |
|
226 |
} |
|
227 |
break; |
|
228 |
case SO_KEEPALIVE: |
|
229 |
if (val == null || !(val instanceof Boolean)) |
|
230 |
throw new SocketException("bad parameter for SO_KEEPALIVE"); |
|
231 |
on = ((Boolean)val).booleanValue(); |
|
232 |
break; |
|
233 |
case SO_OOBINLINE: |
|
234 |
if (val == null || !(val instanceof Boolean)) |
|
235 |
throw new SocketException("bad parameter for SO_OOBINLINE"); |
|
236 |
on = ((Boolean)val).booleanValue(); |
|
237 |
break; |
|
238 |
case SO_REUSEADDR: |
|
239 |
if (val == null || !(val instanceof Boolean)) |
|
240 |
throw new SocketException("bad parameter for SO_REUSEADDR"); |
|
241 |
on = ((Boolean)val).booleanValue(); |
|
242 |
break; |
|
243 |
default: |
|
244 |
throw new SocketException("unrecognized TCP option: " + opt); |
|
245 |
} |
|
246 |
socketSetOption(opt, on, val); |
|
247 |
} |
|
248 |
public Object getOption(int opt) throws SocketException { |
|
249 |
if (isClosedOrPending()) { |
|
250 |
throw new SocketException("Socket Closed"); |
|
251 |
} |
|
252 |
if (opt == SO_TIMEOUT) { |
|
253 |
return new Integer(timeout); |
|
254 |
} |
|
255 |
int ret = 0; |
|
256 |
/* |
|
257 |
* The native socketGetOption() knows about 3 options. |
|
258 |
* The 32 bit value it returns will be interpreted according |
|
259 |
* to what we're asking. A return of -1 means it understands |
|
260 |
* the option but its turned off. It will raise a SocketException |
|
261 |
* if "opt" isn't one it understands. |
|
262 |
*/ |
|
263 |
||
264 |
switch (opt) { |
|
265 |
case TCP_NODELAY: |
|
266 |
ret = socketGetOption(opt, null); |
|
267 |
return Boolean.valueOf(ret != -1); |
|
268 |
case SO_OOBINLINE: |
|
269 |
ret = socketGetOption(opt, null); |
|
270 |
return Boolean.valueOf(ret != -1); |
|
271 |
case SO_LINGER: |
|
272 |
ret = socketGetOption(opt, null); |
|
273 |
return (ret == -1) ? Boolean.FALSE: (Object)(new Integer(ret)); |
|
274 |
case SO_REUSEADDR: |
|
275 |
ret = socketGetOption(opt, null); |
|
276 |
return Boolean.valueOf(ret != -1); |
|
277 |
case SO_BINDADDR: |
|
278 |
InetAddressContainer in = new InetAddressContainer(); |
|
279 |
ret = socketGetOption(opt, in); |
|
280 |
return in.addr; |
|
281 |
case SO_SNDBUF: |
|
282 |
case SO_RCVBUF: |
|
283 |
ret = socketGetOption(opt, null); |
|
284 |
return new Integer(ret); |
|
285 |
case IP_TOS: |
|
286 |
ret = socketGetOption(opt, null); |
|
287 |
if (ret == -1) { // ipv6 tos |
|
288 |
return new Integer(trafficClass); |
|
289 |
} else { |
|
290 |
return new Integer(ret); |
|
291 |
} |
|
292 |
case SO_KEEPALIVE: |
|
293 |
ret = socketGetOption(opt, null); |
|
294 |
return Boolean.valueOf(ret != -1); |
|
295 |
// should never get here |
|
296 |
default: |
|
297 |
return null; |
|
298 |
} |
|
299 |
} |
|
300 |
||
301 |
/** |
|
302 |
* The workhorse of the connection operation. Tries several times to |
|
303 |
* establish a connection to the given <host, port>. If unsuccessful, |
|
304 |
* throws an IOException indicating what went wrong. |
|
305 |
*/ |
|
306 |
||
307 |
synchronized void doConnect(InetAddress address, int port, int timeout) throws IOException { |
|
2446 | 308 |
synchronized (fdLock) { |
309 |
if (!closePending && (socket == null || !socket.isBound())) { |
|
310 |
NetHooks.beforeTcpConnect(fd, address, port); |
|
311 |
} |
|
312 |
} |
|
2 | 313 |
try { |
314 |
FileDescriptor fd = acquireFD(); |
|
315 |
try { |
|
316 |
socketConnect(address, port, timeout); |
|
1335
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
317 |
/* socket may have been closed during poll/select */ |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
318 |
synchronized (fdLock) { |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
319 |
if (closePending) { |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
320 |
throw new SocketException ("Socket closed"); |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
321 |
} |
79ed7fd4bb49
6739920: java 6u4~ use larger C heap if there are many threads
michaelm
parents:
715
diff
changeset
|
322 |
} |
2 | 323 |
// If we have a ref. to the Socket, then sets the flags |
324 |
// created, bound & connected to true. |
|
325 |
// This is normally done in Socket.connect() but some |
|
326 |
// subclasses of Socket may call impl.connect() directly! |
|
327 |
if (socket != null) { |
|
328 |
socket.setBound(); |
|
329 |
socket.setConnected(); |
|
330 |
} |
|
331 |
} finally { |
|
332 |
releaseFD(); |
|
333 |
} |
|
334 |
} catch (IOException e) { |
|
335 |
close(); |
|
336 |
throw e; |
|
337 |
} |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Binds the socket to the specified address of the specified local port. |
|
342 |
* @param address the address |
|
343 |
* @param port the port |
|
344 |
*/ |
|
345 |
protected synchronized void bind(InetAddress address, int lport) |
|
346 |
throws IOException |
|
347 |
{ |
|
2446 | 348 |
synchronized (fdLock) { |
349 |
if (!closePending && (socket == null || !socket.isBound())) { |
|
350 |
NetHooks.beforeTcpBind(fd, address, lport); |
|
351 |
} |
|
352 |
} |
|
2 | 353 |
socketBind(address, lport); |
354 |
if (socket != null) |
|
355 |
socket.setBound(); |
|
356 |
if (serverSocket != null) |
|
357 |
serverSocket.setBound(); |
|
358 |
} |
|
359 |
||
360 |
/** |
|
361 |
* Listens, for a specified amount of time, for connections. |
|
362 |
* @param count the amount of time to listen for connections |
|
363 |
*/ |
|
364 |
protected synchronized void listen(int count) throws IOException { |
|
365 |
socketListen(count); |
|
366 |
} |
|
367 |
||
368 |
/** |
|
369 |
* Accepts connections. |
|
370 |
* @param s the connection |
|
371 |
*/ |
|
372 |
protected void accept(SocketImpl s) throws IOException { |
|
373 |
FileDescriptor fd = acquireFD(); |
|
374 |
try { |
|
375 |
socketAccept(s); |
|
376 |
} finally { |
|
377 |
releaseFD(); |
|
378 |
} |
|
379 |
} |
|
380 |
||
381 |
/** |
|
382 |
* Gets an InputStream for this socket. |
|
383 |
*/ |
|
384 |
protected synchronized InputStream getInputStream() throws IOException { |
|
385 |
if (isClosedOrPending()) { |
|
386 |
throw new IOException("Socket Closed"); |
|
387 |
} |
|
388 |
if (shut_rd) { |
|
389 |
throw new IOException("Socket input is shutdown"); |
|
390 |
} |
|
391 |
if (socketInputStream == null) { |
|
392 |
socketInputStream = new SocketInputStream(this); |
|
393 |
} |
|
394 |
return socketInputStream; |
|
395 |
} |
|
396 |
||
397 |
void setInputStream(SocketInputStream in) { |
|
398 |
socketInputStream = in; |
|
399 |
} |
|
400 |
||
401 |
/** |
|
402 |
* Gets an OutputStream for this socket. |
|
403 |
*/ |
|
404 |
protected synchronized OutputStream getOutputStream() throws IOException { |
|
405 |
if (isClosedOrPending()) { |
|
406 |
throw new IOException("Socket Closed"); |
|
407 |
} |
|
408 |
if (shut_wr) { |
|
409 |
throw new IOException("Socket output is shutdown"); |
|
410 |
} |
|
411 |
return new SocketOutputStream(this); |
|
412 |
} |
|
413 |
||
414 |
void setFileDescriptor(FileDescriptor fd) { |
|
415 |
this.fd = fd; |
|
416 |
} |
|
417 |
||
418 |
void setAddress(InetAddress address) { |
|
419 |
this.address = address; |
|
420 |
} |
|
421 |
||
422 |
void setPort(int port) { |
|
423 |
this.port = port; |
|
424 |
} |
|
425 |
||
426 |
void setLocalPort(int localport) { |
|
427 |
this.localport = localport; |
|
428 |
} |
|
429 |
||
430 |
/** |
|
431 |
* Returns the number of bytes that can be read without blocking. |
|
432 |
*/ |
|
433 |
protected synchronized int available() throws IOException { |
|
434 |
if (isClosedOrPending()) { |
|
435 |
throw new IOException("Stream closed."); |
|
436 |
} |
|
437 |
||
438 |
/* |
|
439 |
* If connection has been reset then return 0 to indicate |
|
440 |
* there are no buffered bytes. |
|
441 |
*/ |
|
442 |
if (isConnectionReset()) { |
|
443 |
return 0; |
|
444 |
} |
|
445 |
||
446 |
/* |
|
447 |
* If no bytes available and we were previously notified |
|
448 |
* of a connection reset then we move to the reset state. |
|
449 |
* |
|
450 |
* If are notified of a connection reset then check |
|
451 |
* again if there are bytes buffered on the socket. |
|
452 |
*/ |
|
453 |
int n = 0; |
|
454 |
try { |
|
455 |
n = socketAvailable(); |
|
456 |
if (n == 0 && isConnectionResetPending()) { |
|
457 |
setConnectionReset(); |
|
458 |
} |
|
459 |
} catch (ConnectionResetException exc1) { |
|
460 |
setConnectionResetPending(); |
|
461 |
try { |
|
462 |
n = socketAvailable(); |
|
463 |
if (n == 0) { |
|
464 |
setConnectionReset(); |
|
465 |
} |
|
466 |
} catch (ConnectionResetException exc2) { |
|
467 |
} |
|
468 |
} |
|
469 |
return n; |
|
470 |
} |
|
471 |
||
472 |
/** |
|
473 |
* Closes the socket. |
|
474 |
*/ |
|
475 |
protected void close() throws IOException { |
|
476 |
synchronized(fdLock) { |
|
477 |
if (fd != null) { |
|
478 |
if (fdUseCount == 0) { |
|
479 |
if (closePending) { |
|
480 |
return; |
|
481 |
} |
|
482 |
closePending = true; |
|
483 |
/* |
|
484 |
* We close the FileDescriptor in two-steps - first the |
|
485 |
* "pre-close" which closes the socket but doesn't |
|
486 |
* release the underlying file descriptor. This operation |
|
487 |
* may be lengthy due to untransmitted data and a long |
|
488 |
* linger interval. Once the pre-close is done we do the |
|
489 |
* actual socket to release the fd. |
|
490 |
*/ |
|
491 |
try { |
|
492 |
socketPreClose(); |
|
493 |
} finally { |
|
494 |
socketClose(); |
|
495 |
} |
|
496 |
fd = null; |
|
497 |
return; |
|
498 |
} else { |
|
499 |
/* |
|
500 |
* If a thread has acquired the fd and a close |
|
501 |
* isn't pending then use a deferred close. |
|
502 |
* Also decrement fdUseCount to signal the last |
|
503 |
* thread that releases the fd to close it. |
|
504 |
*/ |
|
505 |
if (!closePending) { |
|
506 |
closePending = true; |
|
507 |
fdUseCount--; |
|
508 |
socketPreClose(); |
|
509 |
} |
|
510 |
} |
|
511 |
} |
|
512 |
} |
|
513 |
} |
|
514 |
||
515 |
void reset() throws IOException { |
|
516 |
if (fd != null) { |
|
517 |
socketClose(); |
|
518 |
} |
|
519 |
fd = null; |
|
520 |
super.reset(); |
|
521 |
} |
|
522 |
||
523 |
||
524 |
/** |
|
525 |
* Shutdown read-half of the socket connection; |
|
526 |
*/ |
|
527 |
protected void shutdownInput() throws IOException { |
|
528 |
if (fd != null) { |
|
529 |
socketShutdown(SHUT_RD); |
|
530 |
if (socketInputStream != null) { |
|
531 |
socketInputStream.setEOF(true); |
|
532 |
} |
|
533 |
shut_rd = true; |
|
534 |
} |
|
535 |
} |
|
536 |
||
537 |
/** |
|
538 |
* Shutdown write-half of the socket connection; |
|
539 |
*/ |
|
540 |
protected void shutdownOutput() throws IOException { |
|
541 |
if (fd != null) { |
|
542 |
socketShutdown(SHUT_WR); |
|
543 |
shut_wr = true; |
|
544 |
} |
|
545 |
} |
|
546 |
||
547 |
protected boolean supportsUrgentData () { |
|
548 |
return true; |
|
549 |
} |
|
550 |
||
551 |
protected void sendUrgentData (int data) throws IOException { |
|
552 |
if (fd == null) { |
|
553 |
throw new IOException("Socket Closed"); |
|
554 |
} |
|
555 |
socketSendUrgentData (data); |
|
556 |
} |
|
557 |
||
558 |
/** |
|
559 |
* Cleans up if the user forgets to close it. |
|
560 |
*/ |
|
561 |
protected void finalize() throws IOException { |
|
562 |
close(); |
|
563 |
} |
|
564 |
||
565 |
||
566 |
/* |
|
567 |
* "Acquires" and returns the FileDescriptor for this impl |
|
568 |
* |
|
569 |
* A corresponding releaseFD is required to "release" the |
|
570 |
* FileDescriptor. |
|
571 |
*/ |
|
572 |
FileDescriptor acquireFD() { |
|
573 |
synchronized (fdLock) { |
|
574 |
fdUseCount++; |
|
575 |
return fd; |
|
576 |
} |
|
577 |
} |
|
578 |
||
579 |
/* |
|
580 |
* "Release" the FileDescriptor for this impl. |
|
581 |
* |
|
582 |
* If the use count goes to -1 then the socket is closed. |
|
583 |
*/ |
|
584 |
void releaseFD() { |
|
585 |
synchronized (fdLock) { |
|
586 |
fdUseCount--; |
|
587 |
if (fdUseCount == -1) { |
|
588 |
if (fd != null) { |
|
589 |
try { |
|
590 |
socketClose(); |
|
591 |
} catch (IOException e) { |
|
592 |
} finally { |
|
593 |
fd = null; |
|
594 |
} |
|
595 |
} |
|
596 |
} |
|
597 |
} |
|
598 |
} |
|
599 |
||
600 |
public boolean isConnectionReset() { |
|
601 |
synchronized (resetLock) { |
|
602 |
return (resetState == CONNECTION_RESET); |
|
603 |
} |
|
604 |
} |
|
605 |
||
606 |
public boolean isConnectionResetPending() { |
|
607 |
synchronized (resetLock) { |
|
608 |
return (resetState == CONNECTION_RESET_PENDING); |
|
609 |
} |
|
610 |
} |
|
611 |
||
612 |
public void setConnectionReset() { |
|
613 |
synchronized (resetLock) { |
|
614 |
resetState = CONNECTION_RESET; |
|
615 |
} |
|
616 |
} |
|
617 |
||
618 |
public void setConnectionResetPending() { |
|
619 |
synchronized (resetLock) { |
|
620 |
if (resetState == CONNECTION_NOT_RESET) { |
|
621 |
resetState = CONNECTION_RESET_PENDING; |
|
622 |
} |
|
623 |
} |
|
624 |
||
625 |
} |
|
626 |
||
627 |
/* |
|
628 |
* Return true if already closed or close is pending |
|
629 |
*/ |
|
630 |
public boolean isClosedOrPending() { |
|
631 |
/* |
|
632 |
* Lock on fdLock to ensure that we wait if a |
|
633 |
* close is in progress. |
|
634 |
*/ |
|
635 |
synchronized (fdLock) { |
|
636 |
if (closePending || (fd == null)) { |
|
637 |
return true; |
|
638 |
} else { |
|
639 |
return false; |
|
640 |
} |
|
641 |
} |
|
642 |
} |
|
643 |
||
644 |
/* |
|
645 |
* Return the current value of SO_TIMEOUT |
|
646 |
*/ |
|
647 |
public int getTimeout() { |
|
648 |
return timeout; |
|
649 |
} |
|
650 |
||
651 |
/* |
|
652 |
* "Pre-close" a socket by dup'ing the file descriptor - this enables |
|
653 |
* the socket to be closed without releasing the file descriptor. |
|
654 |
*/ |
|
655 |
private void socketPreClose() throws IOException { |
|
656 |
socketClose0(true); |
|
657 |
} |
|
658 |
||
659 |
/* |
|
660 |
* Close the socket (and release the file descriptor). |
|
661 |
*/ |
|
662 |
protected void socketClose() throws IOException { |
|
663 |
socketClose0(false); |
|
664 |
} |
|
665 |
||
666 |
abstract void socketCreate(boolean isServer) throws IOException; |
|
667 |
abstract void socketConnect(InetAddress address, int port, int timeout) |
|
668 |
throws IOException; |
|
669 |
abstract void socketBind(InetAddress address, int port) |
|
670 |
throws IOException; |
|
671 |
abstract void socketListen(int count) |
|
672 |
throws IOException; |
|
673 |
abstract void socketAccept(SocketImpl s) |
|
674 |
throws IOException; |
|
675 |
abstract int socketAvailable() |
|
676 |
throws IOException; |
|
677 |
abstract void socketClose0(boolean useDeferredClose) |
|
678 |
throws IOException; |
|
679 |
abstract void socketShutdown(int howto) |
|
680 |
throws IOException; |
|
681 |
abstract void socketSetOption(int cmd, boolean on, Object value) |
|
682 |
throws SocketException; |
|
683 |
abstract int socketGetOption(int opt, Object iaContainerObj) throws SocketException; |
|
684 |
abstract void socketSendUrgentData(int data) |
|
685 |
throws IOException; |
|
686 |
||
687 |
public final static int SHUT_RD = 0; |
|
688 |
public final static int SHUT_WR = 1; |
|
689 |
} |
|
690 |
||
691 |
class InetAddressContainer { |
|
692 |
InetAddress addr; |
|
693 |
} |