Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.base/windows/classes/sun/nio/ch/WindowsAsynchronousServerSocketChannelImpl.java
41139 views
1
/*
2
* Copyright (c) 2008, 2021, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
package sun.nio.ch;
27
28
import java.nio.channels.*;
29
import java.net.InetSocketAddress;
30
import java.util.concurrent.Future;
31
import java.util.concurrent.atomic.AtomicBoolean;
32
import java.io.IOException;
33
import java.security.AccessControlContext;
34
import java.security.AccessController;
35
import java.security.PrivilegedAction;
36
import jdk.internal.misc.Unsafe;
37
38
/**
39
* Windows implementation of AsynchronousServerSocketChannel using overlapped I/O.
40
*/
41
42
class WindowsAsynchronousServerSocketChannelImpl
43
extends AsynchronousServerSocketChannelImpl implements Iocp.OverlappedChannel
44
{
45
private static final Unsafe unsafe = Unsafe.getUnsafe();
46
47
// 2 * (sizeof(SOCKET_ADDRESS) + 16)
48
private static final int DATA_BUFFER_SIZE = 88;
49
50
private final long handle;
51
private final int completionKey;
52
private final Iocp iocp;
53
54
// typically there will be zero, or one I/O operations pending. In rare
55
// cases there may be more. These rare cases arise when a sequence of accept
56
// operations complete immediately and handled by the initiating thread.
57
// The corresponding OVERLAPPED cannot be reused/released until the completion
58
// event has been posted.
59
private final PendingIoCache ioCache;
60
61
// the data buffer to receive the local/remote socket address
62
private final long dataBuffer;
63
64
// flag to indicate that an accept operation is outstanding
65
private AtomicBoolean accepting = new AtomicBoolean();
66
67
68
WindowsAsynchronousServerSocketChannelImpl(Iocp iocp) throws IOException {
69
super(iocp);
70
71
// associate socket with given completion port
72
long h = IOUtil.fdVal(fd);
73
int key;
74
try {
75
key = iocp.associate(this, h);
76
} catch (IOException x) {
77
closesocket0(h); // prevent leak
78
throw x;
79
}
80
81
this.handle = h;
82
this.completionKey = key;
83
this.iocp = iocp;
84
this.ioCache = new PendingIoCache();
85
this.dataBuffer = unsafe.allocateMemory(DATA_BUFFER_SIZE);
86
}
87
88
@Override
89
public <V,A> PendingFuture<V,A> getByOverlapped(long overlapped) {
90
return ioCache.remove(overlapped);
91
}
92
93
@Override
94
void implClose() throws IOException {
95
// close socket (which may cause outstanding accept to be aborted).
96
closesocket0(handle);
97
98
// waits until the accept operations have completed
99
ioCache.close();
100
101
// finally disassociate from the completion port
102
iocp.disassociate(completionKey);
103
104
// release other resources
105
unsafe.freeMemory(dataBuffer);
106
}
107
108
@Override
109
public AsynchronousChannelGroupImpl group() {
110
return iocp;
111
}
112
113
/**
114
* Task to initiate accept operation and to handle result.
115
*/
116
private class AcceptTask implements Runnable, Iocp.ResultHandler {
117
private final WindowsAsynchronousSocketChannelImpl channel;
118
@SuppressWarnings("removal")
119
private final AccessControlContext acc;
120
private final PendingFuture<AsynchronousSocketChannel,Object> result;
121
122
AcceptTask(WindowsAsynchronousSocketChannelImpl channel,
123
@SuppressWarnings("removal") AccessControlContext acc,
124
PendingFuture<AsynchronousSocketChannel,Object> result)
125
{
126
this.channel = channel;
127
this.acc = acc;
128
this.result = result;
129
}
130
131
void enableAccept() {
132
accepting.set(false);
133
}
134
135
void closeChildChannel() {
136
try {
137
channel.close();
138
} catch (IOException ignore) { }
139
}
140
141
// caller must have acquired read lock for the listener and child channel.
142
@SuppressWarnings("removal")
143
void finishAccept() throws IOException {
144
/**
145
* Set local/remote addresses. This is currently very inefficient
146
* in that it requires 2 calls to getsockname and 2 calls to getpeername.
147
* (should change this to use GetAcceptExSockaddrs)
148
*/
149
updateAcceptContext(handle, channel.handle());
150
151
InetSocketAddress local = Net.localAddress(channel.fd);
152
final InetSocketAddress remote = Net.remoteAddress(channel.fd);
153
channel.setConnected(local, remote);
154
155
// permission check (in context of initiating thread)
156
if (acc != null) {
157
AccessController.doPrivileged(new PrivilegedAction<Void>() {
158
public Void run() {
159
SecurityManager sm = System.getSecurityManager();
160
sm.checkAccept(remote.getAddress().getHostAddress(),
161
remote.getPort());
162
return null;
163
}
164
}, acc);
165
}
166
}
167
168
/**
169
* Initiates the accept operation.
170
*/
171
@Override
172
public void run() {
173
long overlapped = 0L;
174
175
try {
176
// begin usage of listener socket
177
begin();
178
try {
179
// begin usage of child socket (as it is registered with
180
// completion port and so may be closed in the event that
181
// the group is forcefully closed).
182
channel.begin();
183
184
synchronized (result) {
185
overlapped = ioCache.add(result);
186
187
int n = accept0(handle, channel.handle(), overlapped, dataBuffer);
188
if (n == IOStatus.UNAVAILABLE) {
189
return;
190
}
191
192
// connection accepted immediately
193
finishAccept();
194
195
// allow another accept before the result is set
196
enableAccept();
197
result.setResult(channel);
198
}
199
} finally {
200
// end usage on child socket
201
channel.end();
202
}
203
} catch (Throwable x) {
204
// failed to initiate accept so release resources
205
if (overlapped != 0L)
206
ioCache.remove(overlapped);
207
closeChildChannel();
208
if (x instanceof ClosedChannelException)
209
x = new AsynchronousCloseException();
210
if (!(x instanceof IOException) && !(x instanceof SecurityException))
211
x = new IOException(x);
212
enableAccept();
213
result.setFailure(x);
214
} finally {
215
// end of usage of listener socket
216
end();
217
}
218
219
// accept completed immediately but may not have executed on
220
// initiating thread in which case the operation may have been
221
// cancelled.
222
if (result.isCancelled()) {
223
closeChildChannel();
224
}
225
226
// invoke completion handler
227
Invoker.invokeIndirectly(result);
228
}
229
230
/**
231
* Executed when the I/O has completed
232
*/
233
@Override
234
public void completed(int bytesTransferred, boolean canInvokeDirect) {
235
try {
236
// connection accept after group has shutdown
237
if (iocp.isShutdown()) {
238
throw new IOException(new ShutdownChannelGroupException());
239
}
240
241
// finish the accept
242
try {
243
begin();
244
try {
245
channel.begin();
246
finishAccept();
247
} finally {
248
channel.end();
249
}
250
} finally {
251
end();
252
}
253
254
// allow another accept before the result is set
255
enableAccept();
256
result.setResult(channel);
257
} catch (Throwable x) {
258
enableAccept();
259
closeChildChannel();
260
if (x instanceof ClosedChannelException)
261
x = new AsynchronousCloseException();
262
if (!(x instanceof IOException) && !(x instanceof SecurityException))
263
x = new IOException(x);
264
result.setFailure(x);
265
}
266
267
// if an async cancel has already cancelled the operation then
268
// close the new channel so as to free resources
269
if (result.isCancelled()) {
270
closeChildChannel();
271
}
272
273
// invoke handler (but not directly)
274
Invoker.invokeIndirectly(result);
275
}
276
277
@Override
278
public void failed(int error, IOException x) {
279
enableAccept();
280
closeChildChannel();
281
282
// release waiters
283
if (isOpen()) {
284
result.setFailure(x);
285
} else {
286
result.setFailure(new AsynchronousCloseException());
287
}
288
Invoker.invokeIndirectly(result);
289
}
290
}
291
292
@Override
293
Future<AsynchronousSocketChannel> implAccept(Object attachment,
294
final CompletionHandler<AsynchronousSocketChannel,Object> handler)
295
{
296
if (!isOpen()) {
297
Throwable exc = new ClosedChannelException();
298
if (handler == null)
299
return CompletedFuture.withFailure(exc);
300
Invoker.invokeIndirectly(this, handler, attachment, null, exc);
301
return null;
302
}
303
if (isAcceptKilled())
304
throw new RuntimeException("Accept not allowed due to cancellation");
305
306
// ensure channel is bound to local address
307
if (localAddress == null)
308
throw new NotYetBoundException();
309
310
// create the socket that will be accepted. The creation of the socket
311
// is enclosed by a begin/end for the listener socket to ensure that
312
// we check that the listener is open and also to prevent the I/O
313
// port from being closed as the new socket is registered.
314
WindowsAsynchronousSocketChannelImpl ch = null;
315
IOException ioe = null;
316
try {
317
begin();
318
ch = new WindowsAsynchronousSocketChannelImpl(iocp, false);
319
} catch (IOException x) {
320
ioe = x;
321
} finally {
322
end();
323
}
324
if (ioe != null) {
325
if (handler == null)
326
return CompletedFuture.withFailure(ioe);
327
Invoker.invokeIndirectly(this, handler, attachment, null, ioe);
328
return null;
329
}
330
331
// need calling context when there is security manager as
332
// permission check may be done in a different thread without
333
// any application call frames on the stack
334
@SuppressWarnings("removal")
335
AccessControlContext acc = (System.getSecurityManager() == null) ?
336
null : AccessController.getContext();
337
338
PendingFuture<AsynchronousSocketChannel,Object> result =
339
new PendingFuture<AsynchronousSocketChannel,Object>(this, handler, attachment);
340
AcceptTask task = new AcceptTask(ch, acc, result);
341
result.setContext(task);
342
343
// check and set flag to prevent concurrent accepting
344
if (!accepting.compareAndSet(false, true))
345
throw new AcceptPendingException();
346
347
// initiate I/O
348
task.run();
349
return result;
350
}
351
352
// -- Native methods --
353
354
private static native void initIDs();
355
356
private static native int accept0(long listenSocket, long acceptSocket,
357
long overlapped, long dataBuffer) throws IOException;
358
359
private static native void updateAcceptContext(long listenSocket,
360
long acceptSocket) throws IOException;
361
362
private static native void closesocket0(long socket) throws IOException;
363
364
static {
365
IOUtil.load();
366
initIDs();
367
}
368
}
369
370