author | alanb |
Sun, 21 Apr 2019 07:05:04 +0100 | |
branch | niosocketimpl-branch |
changeset 57336 | 766140c67efa |
parent 57281 | c08d024d6bf9 |
child 57339 | 40fdbdd92617 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
53419
eac105e3ec13
8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents:
50722
diff
changeset
|
2 |
* Copyright (c) 2000, 2019, 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 sun.nio.ch; |
|
27 |
||
28 |
import java.io.FileDescriptor; |
|
29 |
import java.io.IOException; |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
30 |
import java.net.InetAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
31 |
import java.net.InetSocketAddress; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
32 |
import java.net.ProtocolFamily; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
33 |
import java.net.Socket; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
34 |
import java.net.SocketAddress; |
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
35 |
import java.net.SocketException; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
36 |
import java.net.SocketOption; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
37 |
import java.net.StandardProtocolFamily; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
38 |
import java.net.StandardSocketOptions; |
2 | 39 |
import java.nio.ByteBuffer; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
40 |
import java.nio.channels.AlreadyBoundException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
41 |
import java.nio.channels.AlreadyConnectedException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
42 |
import java.nio.channels.AsynchronousCloseException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
43 |
import java.nio.channels.ClosedChannelException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
44 |
import java.nio.channels.ConnectionPendingException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
45 |
import java.nio.channels.NoConnectionPendingException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
46 |
import java.nio.channels.NotYetConnectedException; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
47 |
import java.nio.channels.SelectionKey; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
48 |
import java.nio.channels.SocketChannel; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
49 |
import java.nio.channels.spi.SelectorProvider; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
50 |
import java.util.Collections; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
51 |
import java.util.HashSet; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
52 |
import java.util.Objects; |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
53 |
import java.util.Set; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
54 |
import java.util.concurrent.locks.ReentrantLock; |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
55 |
|
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
56 |
import sun.net.ConnectionResetException; |
2446 | 57 |
import sun.net.NetHooks; |
37676
24ef455da1b0
8044773: Refactor jdk.net API so that it can be moved out of the base module
chegar
parents:
36115
diff
changeset
|
58 |
import sun.net.ext.ExtendedSocketOptions; |
50722
bc104aaf24e9
8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents:
50303
diff
changeset
|
59 |
import sun.net.util.SocketExceptions; |
2 | 60 |
|
61 |
/** |
|
62 |
* An implementation of SocketChannels |
|
63 |
*/ |
|
64 |
||
65 |
class SocketChannelImpl |
|
66 |
extends SocketChannel |
|
67 |
implements SelChImpl |
|
68 |
{ |
|
69 |
// Used to make native read and write calls |
|
53445
c96f9aa1f3d8
8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents:
53419
diff
changeset
|
70 |
private static final NativeDispatcher nd = new SocketDispatcher(); |
2 | 71 |
|
72 |
// Our file descriptor object |
|
73 |
private final FileDescriptor fd; |
|
74 |
private final int fdVal; |
|
75 |
||
76 |
// Lock held by current reading or connecting thread |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
77 |
private final ReentrantLock readLock = new ReentrantLock(); |
2 | 78 |
|
79 |
// Lock held by current writing or connecting thread |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
80 |
private final ReentrantLock writeLock = new ReentrantLock(); |
2 | 81 |
|
82 |
// Lock held by any thread that modifies the state fields declared below |
|
83 |
// DO NOT invoke a blocking I/O operation while holding this lock! |
|
84 |
private final Object stateLock = new Object(); |
|
85 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
86 |
// Input/Output closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
87 |
private volatile boolean isInputClosed; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
88 |
private volatile boolean isOutputClosed; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
89 |
|
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
90 |
// Connection reset protected by readLock |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
91 |
private boolean connectionReset; |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
92 |
|
2 | 93 |
// -- The following fields are protected by stateLock |
94 |
||
18192 | 95 |
// set true when exclusive binding is on and SO_REUSEADDR is emulated |
96 |
private boolean isReuseAddress; |
|
97 |
||
2 | 98 |
// State, increases monotonically |
99 |
private static final int ST_UNCONNECTED = 0; |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
100 |
private static final int ST_CONNECTIONPENDING = 1; |
2 | 101 |
private static final int ST_CONNECTED = 2; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
102 |
private static final int ST_CLOSING = 3; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
103 |
private static final int ST_KILLPENDING = 4; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
104 |
private static final int ST_KILLED = 5; |
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
105 |
private volatile int state; // need stateLock to change |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
106 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
107 |
// IDs of native threads doing reads and writes, for signalling |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
108 |
private long readerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
109 |
private long writerThread; |
2 | 110 |
|
111 |
// Binding |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
112 |
private InetSocketAddress localAddress; |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
113 |
private InetSocketAddress remoteAddress; |
2 | 114 |
|
115 |
// Socket adaptor, created on demand |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
116 |
private Socket socket; |
2 | 117 |
|
118 |
// -- End of fields protected by stateLock |
|
119 |
||
120 |
||
121 |
// Constructor for normal connecting sockets |
|
122 |
// |
|
123 |
SocketChannelImpl(SelectorProvider sp) throws IOException { |
|
124 |
super(sp); |
|
125 |
this.fd = Net.socket(true); |
|
126 |
this.fdVal = IOUtil.fdVal(fd); |
|
127 |
} |
|
128 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
129 |
SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, boolean bound) |
6525 | 130 |
throws IOException |
131 |
{ |
|
132 |
super(sp); |
|
133 |
this.fd = fd; |
|
134 |
this.fdVal = IOUtil.fdVal(fd); |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
135 |
if (bound) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
136 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
137 |
this.localAddress = Net.localAddress(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
138 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
139 |
} |
6525 | 140 |
} |
141 |
||
2 | 142 |
// Constructor for sockets obtained from server sockets |
143 |
// |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
144 |
SocketChannelImpl(SelectorProvider sp, FileDescriptor fd, InetSocketAddress isa) |
2 | 145 |
throws IOException |
146 |
{ |
|
147 |
super(sp); |
|
148 |
this.fd = fd; |
|
149 |
this.fdVal = IOUtil.fdVal(fd); |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
150 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
151 |
this.localAddress = Net.localAddress(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
152 |
this.remoteAddress = isa; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
153 |
this.state = ST_CONNECTED; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
154 |
} |
2 | 155 |
} |
156 |
||
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
157 |
/** |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
158 |
* Checks that the channel is open. |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
159 |
* |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
160 |
* @throws ClosedChannelException if channel is closed (or closing) |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
161 |
*/ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
162 |
private void ensureOpen() throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
163 |
if (!isOpen()) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
164 |
throw new ClosedChannelException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
165 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
166 |
|
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
167 |
/** |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
168 |
* Checks that the channel is open and connected. |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
169 |
* |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
170 |
* @apiNote This method uses the "state" field to check if the channel is |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
171 |
* open. It should never be used in conjuncion with isOpen or ensureOpen |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
172 |
* as these methods check AbstractInterruptibleChannel's closed field - that |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
173 |
* field is set before implCloseSelectableChannel is called and so before |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
174 |
* the state is changed. |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
175 |
* |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
176 |
* @throws ClosedChannelException if channel is closed (or closing) |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
177 |
* @throws NotYetConnectedException if open and not connected |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
178 |
*/ |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
179 |
private void ensureOpenAndConnected() throws ClosedChannelException { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
180 |
int state = this.state; |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
181 |
if (state < ST_CONNECTED) { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
182 |
throw new NotYetConnectedException(); |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
183 |
} else if (state > ST_CONNECTED) { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
184 |
throw new ClosedChannelException(); |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
185 |
} |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
186 |
} |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
187 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
188 |
@Override |
2 | 189 |
public Socket socket() { |
190 |
synchronized (stateLock) { |
|
191 |
if (socket == null) |
|
192 |
socket = SocketAdaptor.create(this); |
|
193 |
return socket; |
|
194 |
} |
|
195 |
} |
|
196 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
197 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
198 |
public SocketAddress getLocalAddress() throws IOException { |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
199 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
200 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
201 |
return Net.getRevealedLocalAddress(localAddress); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
202 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
203 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
204 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
205 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
206 |
public SocketAddress getRemoteAddress() throws IOException { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
207 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
208 |
ensureOpen(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
209 |
return remoteAddress; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
210 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
211 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
212 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
213 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
214 |
public <T> SocketChannel setOption(SocketOption<T> name, T value) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
215 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
216 |
{ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
217 |
Objects.requireNonNull(name); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
218 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
219 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
220 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
221 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
222 |
ensureOpen(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
223 |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
224 |
if (name == StandardSocketOptions.IP_TOS) { |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
225 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
226 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
227 |
Net.setSocketOption(fd, family, name, value); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
228 |
return this; |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
229 |
} |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
230 |
|
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
231 |
if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { |
18192 | 232 |
// SO_REUSEADDR emulated when using exclusive bind |
233 |
isReuseAddress = (Boolean)value; |
|
234 |
return this; |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
235 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
236 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
237 |
// no options that require special handling |
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
238 |
Net.setSocketOption(fd, name, value); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
239 |
return this; |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
240 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
241 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
242 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
243 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
244 |
@SuppressWarnings("unchecked") |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
245 |
public <T> T getOption(SocketOption<T> name) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
246 |
throws IOException |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
247 |
{ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
248 |
Objects.requireNonNull(name); |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
249 |
if (!supportedOptions().contains(name)) |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
250 |
throw new UnsupportedOperationException("'" + name + "' not supported"); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
251 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
252 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
253 |
ensureOpen(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
254 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
255 |
if (name == StandardSocketOptions.SO_REUSEADDR && Net.useExclusiveBind()) { |
18192 | 256 |
// SO_REUSEADDR emulated when using exclusive bind |
257 |
return (T)Boolean.valueOf(isReuseAddress); |
|
258 |
} |
|
259 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
260 |
// special handling for IP_TOS: always return 0 when IPv6 |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
261 |
if (name == StandardSocketOptions.IP_TOS) { |
25170
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
262 |
ProtocolFamily family = Net.isIPv6Available() ? |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
263 |
StandardProtocolFamily.INET6 : StandardProtocolFamily.INET; |
f58832169add
8029607: Type of Service (TOS) cannot be set in IPv6 header
michaelm
parents:
24969
diff
changeset
|
264 |
return (T) Net.getSocketOption(fd, family, name); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
265 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
266 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
267 |
// no options that require special handling |
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
268 |
return (T) Net.getSocketOption(fd, name); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
269 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
270 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
271 |
|
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
272 |
private static class DefaultOptionsHolder { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
273 |
static final Set<SocketOption<?>> defaultOptions = defaultOptions(); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
274 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
275 |
private static Set<SocketOption<?>> defaultOptions() { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
276 |
HashSet<SocketOption<?>> set = new HashSet<>(); |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
277 |
set.add(StandardSocketOptions.SO_SNDBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
278 |
set.add(StandardSocketOptions.SO_RCVBUF); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
279 |
set.add(StandardSocketOptions.SO_KEEPALIVE); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
280 |
set.add(StandardSocketOptions.SO_REUSEADDR); |
36115 | 281 |
if (Net.isReusePortAvailable()) { |
282 |
set.add(StandardSocketOptions.SO_REUSEPORT); |
|
283 |
} |
|
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
284 |
set.add(StandardSocketOptions.SO_LINGER); |
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
285 |
set.add(StandardSocketOptions.TCP_NODELAY); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
286 |
// additional options required by socket adaptor |
9679
d98ae8bc45fc
7042979: Rename StandardSocketOption and StandardWatchEventKind
alanb
parents:
7668
diff
changeset
|
287 |
set.add(StandardSocketOptions.IP_TOS); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
288 |
set.add(ExtendedSocketOption.SO_OOBINLINE); |
53419
eac105e3ec13
8217451: ExtendedSocketOptions should encapsulate support for SO_FLOW_SLA
alanb
parents:
50722
diff
changeset
|
289 |
set.addAll(ExtendedSocketOptions.clientSocketOptions()); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
290 |
return Collections.unmodifiableSet(set); |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
291 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
292 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
293 |
|
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
294 |
@Override |
2057
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
295 |
public final Set<SocketOption<?>> supportedOptions() { |
3acf8e5e2ca0
6781363: New I/O: Update socket-channel API to jsr203/nio2-b99
alanb
parents:
1247
diff
changeset
|
296 |
return DefaultOptionsHolder.defaultOptions; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
297 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
298 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
299 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
300 |
* Marks the beginning of a read operation that might block. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
301 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
302 |
* @throws ClosedChannelException if the channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
303 |
* @throws NotYetConnectedException if the channel is not yet connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
304 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
305 |
private void beginRead(boolean blocking) throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
306 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
307 |
// set hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
308 |
begin(); |
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
309 |
|
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
310 |
synchronized (stateLock) { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
311 |
ensureOpenAndConnected(); |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
312 |
// record thread so it can be signalled if needed |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
313 |
readerThread = NativeThread.current(); |
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
314 |
} |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
315 |
} else { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
316 |
ensureOpenAndConnected(); |
2 | 317 |
} |
318 |
} |
|
319 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
320 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
321 |
* Marks the end of a read operation that may have blocked. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
322 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
323 |
* @throws AsynchronousCloseException if the channel was closed due to this |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
324 |
* thread being interrupted on a blocking read operation. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
325 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
326 |
private void endRead(boolean blocking, boolean completed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
327 |
throws AsynchronousCloseException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
328 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
329 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
330 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
331 |
readerThread = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
332 |
// notify any thread waiting in implCloseSelectableChannel |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
333 |
if (state == ST_CLOSING) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
334 |
stateLock.notifyAll(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
335 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
336 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
337 |
// remove hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
338 |
end(completed); |
2 | 339 |
} |
340 |
} |
|
341 |
||
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
342 |
private void throwConnectionReset() throws SocketException { |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
343 |
throw new SocketException("Connection reset"); |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
344 |
} |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
345 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
346 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
347 |
public int read(ByteBuffer buf) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
348 |
Objects.requireNonNull(buf); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
349 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
350 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
351 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
352 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
353 |
int n = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
354 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
355 |
beginRead(blocking); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
356 |
|
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
357 |
// check if connection has been reset |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
358 |
if (connectionReset) |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
359 |
throwConnectionReset(); |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
360 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
361 |
// check if input is shutdown |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
362 |
if (isInputClosed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
363 |
return IOStatus.EOF; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
364 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
365 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
366 |
do { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
367 |
n = IOUtil.read(fd, buf, -1, nd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
368 |
} while (n == IOStatus.INTERRUPTED && isOpen()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
369 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
370 |
n = IOUtil.read(fd, buf, -1, nd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
371 |
} |
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
372 |
} catch (ConnectionResetException e) { |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
373 |
connectionReset = true; |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
374 |
throwConnectionReset(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
375 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
376 |
endRead(blocking, n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
377 |
if (n <= 0 && isInputClosed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
378 |
return IOStatus.EOF; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
379 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
380 |
return IOStatus.normalize(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
381 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
382 |
readLock.unlock(); |
2 | 383 |
} |
384 |
} |
|
385 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
386 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
387 |
public long read(ByteBuffer[] dsts, int offset, int length) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
388 |
throws IOException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
389 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
390 |
Objects.checkFromIndexSize(offset, length, dsts.length); |
2 | 391 |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
392 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
393 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
394 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
395 |
long n = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
396 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
397 |
beginRead(blocking); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
398 |
|
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
399 |
// check if connection has been reset |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
400 |
if (connectionReset) |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
401 |
throwConnectionReset(); |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
402 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
403 |
// check if input is shutdown |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
404 |
if (isInputClosed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
405 |
return IOStatus.EOF; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
406 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
407 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
408 |
do { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
409 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
410 |
} while (n == IOStatus.INTERRUPTED && isOpen()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
411 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
412 |
n = IOUtil.read(fd, dsts, offset, length, nd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
413 |
} |
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
414 |
} catch (ConnectionResetException e) { |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
415 |
connectionReset = true; |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
416 |
throwConnectionReset(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
417 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
418 |
endRead(blocking, n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
419 |
if (n <= 0 && isInputClosed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
420 |
return IOStatus.EOF; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
421 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
422 |
return IOStatus.normalize(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
423 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
424 |
readLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
425 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
426 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
427 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
428 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
429 |
* Marks the beginning of a write operation that might block. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
430 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
431 |
* @throws ClosedChannelException if the channel is closed or output shutdown |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
432 |
* @throws NotYetConnectedException if the channel is not yet connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
433 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
434 |
private void beginWrite(boolean blocking) throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
435 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
436 |
// set hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
437 |
begin(); |
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
438 |
|
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
439 |
synchronized (stateLock) { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
440 |
ensureOpenAndConnected(); |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
441 |
if (isOutputClosed) |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
442 |
throw new ClosedChannelException(); |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
443 |
// record thread so it can be signalled if needed |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
444 |
writerThread = NativeThread.current(); |
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
445 |
} |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
446 |
} else { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
447 |
ensureOpenAndConnected(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
448 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
449 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
450 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
451 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
452 |
* Marks the end of a write operation that may have blocked. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
453 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
454 |
* @throws AsynchronousCloseException if the channel was closed due to this |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
455 |
* thread being interrupted on a blocking write operation. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
456 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
457 |
private void endWrite(boolean blocking, boolean completed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
458 |
throws AsynchronousCloseException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
459 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
460 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
461 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
462 |
writerThread = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
463 |
// notify any thread waiting in implCloseSelectableChannel |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
464 |
if (state == ST_CLOSING) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
465 |
stateLock.notifyAll(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
466 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
467 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
468 |
// remove hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
469 |
end(completed); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
470 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
471 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
472 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
473 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
474 |
public int write(ByteBuffer buf) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
475 |
Objects.requireNonNull(buf); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
476 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
477 |
writeLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
478 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
479 |
boolean blocking = isBlocking(); |
2 | 480 |
int n = 0; |
481 |
try { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
482 |
beginWrite(blocking); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
483 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
484 |
do { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
485 |
n = IOUtil.write(fd, buf, -1, nd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
486 |
} while (n == IOStatus.INTERRUPTED && isOpen()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
487 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
488 |
n = IOUtil.write(fd, buf, -1, nd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
489 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
490 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
491 |
endWrite(blocking, n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
492 |
if (n <= 0 && isOutputClosed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
493 |
throw new AsynchronousCloseException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
494 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
495 |
return IOStatus.normalize(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
496 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
497 |
writeLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
498 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
499 |
} |
2 | 500 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
501 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
502 |
public long write(ByteBuffer[] srcs, int offset, int length) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
503 |
throws IOException |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
504 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
505 |
Objects.checkFromIndexSize(offset, length, srcs.length); |
2 | 506 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
507 |
writeLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
508 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
509 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
510 |
long n = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
511 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
512 |
beginWrite(blocking); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
513 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
514 |
do { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
515 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
516 |
} while (n == IOStatus.INTERRUPTED && isOpen()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
517 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
518 |
n = IOUtil.write(fd, srcs, offset, length, nd); |
2 | 519 |
} |
520 |
} finally { |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
521 |
endWrite(blocking, n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
522 |
if (n <= 0 && isOutputClosed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
523 |
throw new AsynchronousCloseException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
524 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
525 |
return IOStatus.normalize(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
526 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
527 |
writeLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
528 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
529 |
} |
2 | 530 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
531 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
532 |
* Writes a byte of out of band data. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
533 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
534 |
int sendOutOfBandData(byte b) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
535 |
writeLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
536 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
537 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
538 |
int n = 0; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
539 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
540 |
beginWrite(blocking); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
541 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
542 |
do { |
53445
c96f9aa1f3d8
8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents:
53419
diff
changeset
|
543 |
n = Net.sendOOB(fd, b); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
544 |
} while (n == IOStatus.INTERRUPTED && isOpen()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
545 |
} else { |
53445
c96f9aa1f3d8
8217500: (sc) Move SocketChannelImpl's remaining native methods to Net
alanb
parents:
53419
diff
changeset
|
546 |
n = Net.sendOOB(fd, b); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
547 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
548 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
549 |
endWrite(blocking, n > 0); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
550 |
if (n <= 0 && isOutputClosed) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
551 |
throw new AsynchronousCloseException(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
552 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
553 |
return IOStatus.normalize(n); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
554 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
555 |
writeLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
556 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
557 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
558 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
559 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
560 |
protected void implConfigureBlocking(boolean block) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
561 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
562 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
563 |
writeLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
564 |
try { |
2 | 565 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
566 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
567 |
IOUtil.configureBlocking(fd, block); |
2 | 568 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
569 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
570 |
writeLock.unlock(); |
2 | 571 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
572 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
573 |
readLock.unlock(); |
2 | 574 |
} |
575 |
} |
|
576 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
577 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
578 |
* Returns the local address, or null if not bound |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
579 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
580 |
InetSocketAddress localAddress() { |
2 | 581 |
synchronized (stateLock) { |
582 |
return localAddress; |
|
583 |
} |
|
584 |
} |
|
585 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
586 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
587 |
* Returns the remote address, or null if not connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
588 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
589 |
InetSocketAddress remoteAddress() { |
2 | 590 |
synchronized (stateLock) { |
591 |
return remoteAddress; |
|
592 |
} |
|
593 |
} |
|
594 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
595 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
596 |
public SocketChannel bind(SocketAddress local) throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
597 |
readLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
598 |
try { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
599 |
writeLock.lock(); |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
600 |
try { |
2 | 601 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
602 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
603 |
if (state == ST_CONNECTIONPENDING) |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
604 |
throw new ConnectionPendingException(); |
2 | 605 |
if (localAddress != null) |
606 |
throw new AlreadyBoundException(); |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
607 |
InetSocketAddress isa = (local == null) ? |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
608 |
new InetSocketAddress(0) : Net.checkAddress(local); |
22339 | 609 |
SecurityManager sm = System.getSecurityManager(); |
610 |
if (sm != null) { |
|
611 |
sm.checkListen(isa.getPort()); |
|
612 |
} |
|
2446 | 613 |
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort()); |
2 | 614 |
Net.bind(fd, isa.getAddress(), isa.getPort()); |
615 |
localAddress = Net.localAddress(fd); |
|
616 |
} |
|
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
617 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
618 |
writeLock.unlock(); |
2 | 619 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
620 |
} finally { |
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
621 |
readLock.unlock(); |
2 | 622 |
} |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
623 |
return this; |
2 | 624 |
} |
625 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
626 |
@Override |
2 | 627 |
public boolean isConnected() { |
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
628 |
return (state == ST_CONNECTED); |
2 | 629 |
} |
630 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
631 |
@Override |
2 | 632 |
public boolean isConnectionPending() { |
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
633 |
return (state == ST_CONNECTIONPENDING); |
2 | 634 |
} |
635 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
636 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
637 |
* Marks the beginning of a connect operation that might block. |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
638 |
* @param blocking true if configured blocking |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
639 |
* @param isa the remote address |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
640 |
* @throws ClosedChannelException if the channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
641 |
* @throws AlreadyConnectedException if already connected |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
642 |
* @throws ConnectionPendingException is a connection is pending |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
643 |
* @throws IOException if the pre-connect hook fails |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
644 |
*/ |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
645 |
private void beginConnect(boolean blocking, InetSocketAddress isa) |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
646 |
throws IOException |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
647 |
{ |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
648 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
649 |
// set hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
650 |
begin(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
651 |
} |
2 | 652 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
653 |
ensureOpen(); |
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
654 |
int state = this.state; |
2 | 655 |
if (state == ST_CONNECTED) |
656 |
throw new AlreadyConnectedException(); |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
657 |
if (state == ST_CONNECTIONPENDING) |
2 | 658 |
throw new ConnectionPendingException(); |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
659 |
assert state == ST_UNCONNECTED; |
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
660 |
this.state = ST_CONNECTIONPENDING; |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
661 |
|
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
662 |
if (localAddress == null) |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
663 |
NetHooks.beforeTcpConnect(fd, isa.getAddress(), isa.getPort()); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
664 |
remoteAddress = isa; |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
665 |
|
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
666 |
if (blocking) { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
667 |
// record thread so it can be signalled if needed |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
668 |
readerThread = NativeThread.current(); |
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
669 |
} |
2 | 670 |
} |
671 |
} |
|
672 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
673 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
674 |
* Marks the end of a connect operation that may have blocked. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
675 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
676 |
* @throws AsynchronousCloseException if the channel was closed due to this |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
677 |
* thread being interrupted on a blocking connect operation. |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
678 |
* @throws IOException if completed and unable to obtain the local address |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
679 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
680 |
private void endConnect(boolean blocking, boolean completed) |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
681 |
throws IOException |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
682 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
683 |
endRead(blocking, completed); |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
684 |
|
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
685 |
if (completed) { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
686 |
synchronized (stateLock) { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
687 |
if (state == ST_CONNECTIONPENDING) { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
688 |
localAddress = Net.localAddress(fd); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
689 |
state = ST_CONNECTED; |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
690 |
} |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
691 |
} |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
692 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
693 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
694 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
695 |
@Override |
2 | 696 |
public boolean connect(SocketAddress sa) throws IOException { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
697 |
InetSocketAddress isa = Net.checkAddress(sa); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
698 |
SecurityManager sm = System.getSecurityManager(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
699 |
if (sm != null) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
700 |
sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort()); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
701 |
|
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
702 |
InetAddress ia = isa.getAddress(); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
703 |
if (ia.isAnyLocalAddress()) |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
704 |
ia = InetAddress.getLocalHost(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
705 |
|
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
706 |
try { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
707 |
readLock.lock(); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
708 |
try { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
709 |
writeLock.lock(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
710 |
try { |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
711 |
int n = 0; |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
712 |
boolean blocking = isBlocking(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
713 |
try { |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
714 |
beginConnect(blocking, isa); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
715 |
do { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
716 |
n = Net.connect(fd, ia, isa.getPort()); |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
717 |
} while (n == IOStatus.INTERRUPTED && isOpen()); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
718 |
} finally { |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
719 |
endConnect(blocking, (n > 0)); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
720 |
} |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
721 |
assert IOStatus.check(n); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
722 |
return n > 0; |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
723 |
} finally { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
724 |
writeLock.unlock(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
725 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
726 |
} finally { |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
727 |
readLock.unlock(); |
2 | 728 |
} |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
729 |
} catch (IOException ioe) { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
730 |
// connect failed, close the channel |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
731 |
close(); |
50722
bc104aaf24e9
8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents:
50303
diff
changeset
|
732 |
throw SocketExceptions.of(ioe, isa); |
2 | 733 |
} |
734 |
} |
|
735 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
736 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
737 |
* Marks the beginning of a finishConnect operation that might block. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
738 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
739 |
* @throws ClosedChannelException if the channel is closed |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
740 |
* @throws NoConnectionPendingException if no connection is pending |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
741 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
742 |
private void beginFinishConnect(boolean blocking) throws ClosedChannelException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
743 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
744 |
// set hook for Thread.interrupt |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
745 |
begin(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
746 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
747 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
748 |
ensureOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
749 |
if (state != ST_CONNECTIONPENDING) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
750 |
throw new NoConnectionPendingException(); |
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
751 |
if (blocking) { |
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
752 |
// record thread so it can be signalled if needed |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
753 |
readerThread = NativeThread.current(); |
49143
2d5cc05d877e
8199120: (so) SocketChannelImpl read/write don't need stateLock when channel is configured non-blocking
alanb
parents:
49142
diff
changeset
|
754 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
755 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
756 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
757 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
758 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
759 |
* Marks the end of a finishConnect operation that may have blocked. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
760 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
761 |
* @throws AsynchronousCloseException if the channel was closed due to this |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
762 |
* thread being interrupted on a blocking connect operation. |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
763 |
* @throws IOException if completed and unable to obtain the local address |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
764 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
765 |
private void endFinishConnect(boolean blocking, boolean completed) |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
766 |
throws IOException |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
767 |
{ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
768 |
endRead(blocking, completed); |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
769 |
|
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
770 |
if (completed) { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
771 |
synchronized (stateLock) { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
772 |
if (state == ST_CONNECTIONPENDING) { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
773 |
localAddress = Net.localAddress(fd); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
774 |
state = ST_CONNECTED; |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
775 |
} |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
776 |
} |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
777 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
778 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
779 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
780 |
@Override |
2 | 781 |
public boolean finishConnect() throws IOException { |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
782 |
try { |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
783 |
readLock.lock(); |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
784 |
try { |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
785 |
writeLock.lock(); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
786 |
try { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
787 |
// no-op if already connected |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
788 |
if (isConnected()) |
2 | 789 |
return true; |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
790 |
|
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
791 |
boolean blocking = isBlocking(); |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
792 |
boolean connected = false; |
2 | 793 |
try { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
794 |
beginFinishConnect(blocking); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
795 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
796 |
do { |
57336 | 797 |
connected = Net.pollConnect(fd, -1); |
798 |
} while (!connected && isOpen()); |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
799 |
} else { |
57336 | 800 |
connected = Net.pollConnect(fd, 0); |
2 | 801 |
} |
802 |
} finally { |
|
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
803 |
endFinishConnect(blocking, connected); |
2 | 804 |
} |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
805 |
assert (blocking && connected) ^ !blocking; |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
806 |
return connected; |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
807 |
} finally { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
808 |
writeLock.unlock(); |
2 | 809 |
} |
48750
ffbb784a8873
8196787: (ch) Moving network channels to use j.u.c locks
alanb
parents:
47216
diff
changeset
|
810 |
} finally { |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
811 |
readLock.unlock(); |
2 | 812 |
} |
49141
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
813 |
} catch (IOException ioe) { |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
814 |
// connect failed, close the channel |
ac95c7a76132
8198928: (so) SocketChannel connect may deadlock if closed at around same time that connect fails
alanb
parents:
49001
diff
changeset
|
815 |
close(); |
50722
bc104aaf24e9
8204233: Add configurable option for enhanced socket IOException messages
michaelm
parents:
50303
diff
changeset
|
816 |
throw SocketExceptions.of(ioe, remoteAddress); |
2 | 817 |
} |
818 |
} |
|
819 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
820 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
821 |
* Invoked by implCloseChannel to close the channel. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
822 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
823 |
* This method waits for outstanding I/O operations to complete. When in |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
824 |
* blocking mode, the socket is pre-closed and the threads in blocking I/O |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
825 |
* operations are signalled to ensure that the outstanding I/O operations |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
826 |
* complete quickly. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
827 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
828 |
* If the socket is connected then it is shutdown by this method. The |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
829 |
* shutdown ensures that the peer reads EOF for the case that the socket is |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
830 |
* not pre-closed or closed by this method. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
831 |
* |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
832 |
* The socket is closed by this method when it is not registered with a |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
833 |
* Selector. Note that a channel configured blocking may be registered with |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
834 |
* a Selector. This arises when a key is canceled and the channel configured |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
835 |
* to blocking mode before the key is flushed from the Selector. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
836 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
837 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
838 |
protected void implCloseSelectableChannel() throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
839 |
assert !isOpen(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
840 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
841 |
boolean blocking; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
842 |
boolean connected; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
843 |
boolean interrupted = false; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
844 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
845 |
// set state to ST_CLOSING |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
846 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
847 |
assert state < ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
848 |
blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
849 |
connected = (state == ST_CONNECTED); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
850 |
state = ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
851 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
852 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
853 |
// wait for any outstanding I/O operations to complete |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
854 |
if (blocking) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
855 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
856 |
assert state == ST_CLOSING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
857 |
long reader = readerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
858 |
long writer = writerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
859 |
if (reader != 0 || writer != 0) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
860 |
nd.preClose(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
861 |
connected = false; // fd is no longer connected socket |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
862 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
863 |
if (reader != 0) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
864 |
NativeThread.signal(reader); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
865 |
if (writer != 0) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
866 |
NativeThread.signal(writer); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
867 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
868 |
// wait for blocking I/O operations to end |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
869 |
while (readerThread != 0 || writerThread != 0) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
870 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
871 |
stateLock.wait(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
872 |
} catch (InterruptedException e) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
873 |
interrupted = true; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
874 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
875 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
876 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
877 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
878 |
} else { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
879 |
// non-blocking mode: wait for read/write to complete |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
880 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
881 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
882 |
writeLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
883 |
writeLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
884 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
885 |
readLock.unlock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
886 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
887 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
888 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
889 |
// set state to ST_KILLPENDING |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
890 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
891 |
assert state == ST_CLOSING; |
50109
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
892 |
// if connected and the channel is registered with a Selector then |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
893 |
// shutdown the output if possible so that the peer reads EOF. If |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
894 |
// SO_LINGER is enabled and set to a non-zero value then it needs to |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
895 |
// be disabled so that the Selector does not wait when it closes |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
896 |
// the socket. |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
897 |
if (connected && isRegistered()) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
898 |
try { |
50109
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
899 |
SocketOption<Integer> opt = StandardSocketOptions.SO_LINGER; |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
900 |
int interval = (int) Net.getSocketOption(fd, Net.UNSPEC, opt); |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
901 |
if (interval != 0) { |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
902 |
if (interval > 0) { |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
903 |
// disable SO_LINGER |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
904 |
Net.setSocketOption(fd, Net.UNSPEC, opt, -1); |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
905 |
} |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
906 |
Net.shutdown(fd, Net.SHUT_WR); |
ea54197f4fe4
8203059: (so) Closing a socket channel registered with Selector and with SO_LINGER set to 0 does not reset connection
alanb
parents:
49702
diff
changeset
|
907 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
908 |
} catch (IOException ignore) { } |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
909 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
910 |
state = ST_KILLPENDING; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
911 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
912 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
913 |
// close socket if not registered with Selector |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
914 |
if (!isRegistered()) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
915 |
kill(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
916 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
917 |
// restore interrupt status |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
918 |
if (interrupted) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
919 |
Thread.currentThread().interrupt(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
920 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
921 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
922 |
@Override |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
923 |
public void kill() throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
924 |
synchronized (stateLock) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
925 |
if (state == ST_KILLPENDING) { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
926 |
state = ST_KILLED; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
927 |
nd.close(fd); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
928 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
929 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
930 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
931 |
|
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
932 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
933 |
public SocketChannel shutdownInput() throws IOException { |
2 | 934 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
935 |
ensureOpen(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
936 |
if (!isConnected()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
937 |
throw new NotYetConnectedException(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
938 |
if (!isInputClosed) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
939 |
Net.shutdown(fd, Net.SHUT_RD); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
940 |
long thread = readerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
941 |
if (thread != 0) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
942 |
NativeThread.signal(thread); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
943 |
isInputClosed = true; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
944 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
945 |
return this; |
2 | 946 |
} |
947 |
} |
|
948 |
||
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
949 |
@Override |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
950 |
public SocketChannel shutdownOutput() throws IOException { |
2 | 951 |
synchronized (stateLock) { |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
952 |
ensureOpen(); |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
953 |
if (!isConnected()) |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
954 |
throw new NotYetConnectedException(); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
955 |
if (!isOutputClosed) { |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
956 |
Net.shutdown(fd, Net.SHUT_WR); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
957 |
long thread = writerThread; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
958 |
if (thread != 0) |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
959 |
NativeThread.signal(thread); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
960 |
isOutputClosed = true; |
1152
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
961 |
} |
29d6145d1097
4640544: New I/O: Complete socket-channel functionality
alanb
parents:
2
diff
changeset
|
962 |
return this; |
2 | 963 |
} |
964 |
} |
|
965 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
966 |
boolean isInputOpen() { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
967 |
return !isInputClosed; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
968 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
969 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
970 |
boolean isOutputOpen() { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
971 |
return !isOutputClosed; |
2 | 972 |
} |
973 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
974 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
975 |
* Poll this channel's socket for reading up to the given timeout. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
976 |
* @return {@code true} if the socket is polled |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
977 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
978 |
boolean pollRead(long timeout) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
979 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
980 |
assert Thread.holdsLock(blockingLock()) && blocking; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
981 |
|
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
982 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
983 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
984 |
boolean polled = false; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
985 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
986 |
beginRead(blocking); |
49702
09c01737ad27
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents:
49493
diff
changeset
|
987 |
int events = Net.poll(fd, Net.POLLIN, timeout); |
09c01737ad27
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents:
49493
diff
changeset
|
988 |
polled = (events != 0); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
989 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
990 |
endRead(blocking, polled); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
991 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
992 |
return polled; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
993 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
994 |
readLock.unlock(); |
2 | 995 |
} |
996 |
} |
|
997 |
||
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
998 |
/** |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
999 |
* Poll this channel's socket for a connection, up to the given timeout. |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1000 |
* @return {@code true} if the socket is polled |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1001 |
*/ |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1002 |
boolean pollConnected(long timeout) throws IOException { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1003 |
boolean blocking = isBlocking(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1004 |
assert Thread.holdsLock(blockingLock()) && blocking; |
2 | 1005 |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1006 |
readLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1007 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1008 |
writeLock.lock(); |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1009 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1010 |
boolean polled = false; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1011 |
try { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1012 |
beginFinishConnect(blocking); |
49702
09c01737ad27
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents:
49493
diff
changeset
|
1013 |
int events = Net.poll(fd, Net.POLLCONN, timeout); |
09c01737ad27
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents:
49493
diff
changeset
|
1014 |
polled = (events != 0); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1015 |
} finally { |
49702
09c01737ad27
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents:
49493
diff
changeset
|
1016 |
// invoke endFinishConnect with completed = false so that |
09c01737ad27
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents:
49493
diff
changeset
|
1017 |
// the state is not changed to ST_CONNECTED. The socket |
09c01737ad27
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents:
49493
diff
changeset
|
1018 |
// adaptor will use finishConnect to finish. |
09c01737ad27
8201474: (so) Socket adaptor connect(InetAddress, timeout) succeeds when connection fails
alanb
parents:
49493
diff
changeset
|
1019 |
endFinishConnect(blocking, /*completed*/false); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1020 |
} |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1021 |
return polled; |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1022 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1023 |
writeLock.unlock(); |
2 | 1024 |
} |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1025 |
} finally { |
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1026 |
readLock.unlock(); |
2 | 1027 |
} |
1028 |
} |
|
1029 |
||
1030 |
/** |
|
54246
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1031 |
* Return the number of bytes in the socket input buffer. |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1032 |
*/ |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1033 |
int available() throws IOException { |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1034 |
synchronized (stateLock) { |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1035 |
ensureOpenAndConnected(); |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1036 |
if (isInputClosed) { |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1037 |
return 0; |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1038 |
} else { |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1039 |
return Net.available(fd); |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1040 |
} |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1041 |
} |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1042 |
} |
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1043 |
|
f04e3492fd88
8221252: (sc) SocketChannel and its socket adaptor need to handle connection reset
alanb
parents:
53445
diff
changeset
|
1044 |
/** |
2 | 1045 |
* Translates native poll revent ops into a ready operation ops |
1046 |
*/ |
|
49493 | 1047 |
public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl ski) { |
1048 |
int intOps = ski.nioInterestOps(); |
|
1049 |
int oldOps = ski.nioReadyOps(); |
|
2 | 1050 |
int newOps = initialOps; |
1051 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1052 |
if ((ops & Net.POLLNVAL) != 0) { |
2 | 1053 |
// This should only happen if this channel is pre-closed while a |
1054 |
// selection operation is in progress |
|
1055 |
// ## Throw an error if this channel has not been pre-closed |
|
1056 |
return false; |
|
1057 |
} |
|
1058 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1059 |
if ((ops & (Net.POLLERR | Net.POLLHUP)) != 0) { |
2 | 1060 |
newOps = intOps; |
49493 | 1061 |
ski.nioReadyOps(newOps); |
2 | 1062 |
return (newOps & ~oldOps) != 0; |
1063 |
} |
|
1064 |
||
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
1065 |
boolean connected = isConnected(); |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1066 |
if (((ops & Net.POLLIN) != 0) && |
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
1067 |
((intOps & SelectionKey.OP_READ) != 0) && connected) |
2 | 1068 |
newOps |= SelectionKey.OP_READ; |
1069 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1070 |
if (((ops & Net.POLLCONN) != 0) && |
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
1071 |
((intOps & SelectionKey.OP_CONNECT) != 0) && isConnectionPending()) |
2 | 1072 |
newOps |= SelectionKey.OP_CONNECT; |
1073 |
||
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1074 |
if (((ops & Net.POLLOUT) != 0) && |
49142
4affaea00c05
8199015: (se) SocketChannelImpl.translateXXXOps access channel state without synchronization
alanb
parents:
49141
diff
changeset
|
1075 |
((intOps & SelectionKey.OP_WRITE) != 0) && connected) |
2 | 1076 |
newOps |= SelectionKey.OP_WRITE; |
1077 |
||
49493 | 1078 |
ski.nioReadyOps(newOps); |
2 | 1079 |
return (newOps & ~oldOps) != 0; |
1080 |
} |
|
1081 |
||
49493 | 1082 |
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) { |
1083 |
return translateReadyOps(ops, ski.nioReadyOps(), ski); |
|
2 | 1084 |
} |
1085 |
||
49493 | 1086 |
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl ski) { |
1087 |
return translateReadyOps(ops, 0, ski); |
|
2 | 1088 |
} |
1089 |
||
1090 |
/** |
|
1091 |
* Translates an interest operation set into a native poll event set |
|
1092 |
*/ |
|
49493 | 1093 |
public int translateInterestOps(int ops) { |
2 | 1094 |
int newOps = 0; |
1095 |
if ((ops & SelectionKey.OP_READ) != 0) |
|
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1096 |
newOps |= Net.POLLIN; |
2 | 1097 |
if ((ops & SelectionKey.OP_WRITE) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1098 |
newOps |= Net.POLLOUT; |
2 | 1099 |
if ((ops & SelectionKey.OP_CONNECT) != 0) |
22604
9b394795e216
8031997: PPC64: Make the various POLL constants system dependant
simonis
parents:
19607
diff
changeset
|
1100 |
newOps |= Net.POLLCONN; |
49493 | 1101 |
return newOps; |
2 | 1102 |
} |
1103 |
||
1104 |
public FileDescriptor getFD() { |
|
1105 |
return fd; |
|
1106 |
} |
|
1107 |
||
1108 |
public int getFDVal() { |
|
1109 |
return fdVal; |
|
1110 |
} |
|
1111 |
||
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1112 |
@Override |
2 | 1113 |
public String toString() { |
24969
afa6934dd8e8
8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents:
23879
diff
changeset
|
1114 |
StringBuilder sb = new StringBuilder(); |
2 | 1115 |
sb.append(this.getClass().getSuperclass().getName()); |
1116 |
sb.append('['); |
|
1117 |
if (!isOpen()) |
|
1118 |
sb.append("closed"); |
|
1119 |
else { |
|
1120 |
synchronized (stateLock) { |
|
1121 |
switch (state) { |
|
1122 |
case ST_UNCONNECTED: |
|
1123 |
sb.append("unconnected"); |
|
1124 |
break; |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1125 |
case ST_CONNECTIONPENDING: |
2 | 1126 |
sb.append("connection-pending"); |
1127 |
break; |
|
1128 |
case ST_CONNECTED: |
|
1129 |
sb.append("connected"); |
|
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1130 |
if (isInputClosed) |
2 | 1131 |
sb.append(" ishut"); |
49001
ce06058197a4
8198562: (ch) Separate blocking and non-blocking code paths (part 1)
alanb
parents:
48761
diff
changeset
|
1132 |
if (isOutputClosed) |
2 | 1133 |
sb.append(" oshut"); |
1134 |
break; |
|
1135 |
} |
|
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1136 |
InetSocketAddress addr = localAddress(); |
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1137 |
if (addr != null) { |
2 | 1138 |
sb.append(" local="); |
18212
22f8c33b0690
8001318: Socket.getLocalAddress not consistent with InetAddress.getLocalHost
khazra
parents:
18192
diff
changeset
|
1139 |
sb.append(Net.getRevealedLocalAddressAsString(addr)); |
2 | 1140 |
} |
1141 |
if (remoteAddress() != null) { |
|
1142 |
sb.append(" remote="); |
|
1143 |
sb.append(remoteAddress().toString()); |
|
1144 |
} |
|
1145 |
} |
|
1146 |
} |
|
1147 |
sb.append(']'); |
|
1148 |
return sb.toString(); |
|
1149 |
} |
|
1150 |
} |