-
Notifications
You must be signed in to change notification settings - Fork 668
/
SocketAdaptor.java
619 lines (488 loc) · 24.8 KB
/
SocketAdaptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/*
* Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package sun.nio.ch;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketException;
import java.net.SocketImpl;
import java.net.SocketOption;
import java.net.SocketTimeoutException;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.IllegalBlockingModeException;
import java.nio.channels.SocketChannel;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
/**
* Make a socket channel look like a socket.
* The methods in this class are defined in exactly the same order as in
* java.net.Socket so as to simplify tracking future changes to that class.
*/
// Socket适配器,用来将SocketChannelImpl当做Socket使唤
class SocketAdaptor extends Socket {
// 将要被适配的Socket通道;适配之后,该通道可以被当成普通的Socket使用
private final SocketChannelImpl socketChannel;
/** Timeout "option" value for reads */
// 等待读取数据时的超时设置
private volatile int timeout;
// 指向被适配通道的输入流,单例
private InputStream socketInputStream = null;
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
/*
* 构造Socket适配器,用来将指定的Socket通道"伪装"为Socket。
*
* 在这里,不再需要传统的"Scoket委托",而是需要参数中的Socket通道来做委托。
* 由于这里的构造器中需要显式调用父类的构造器,但又不能使用传统的"Scoket委托",
* 因此给父构造器赋了一个null值来欺骗父构造器。
*/
private SocketAdaptor(SocketChannelImpl socketChannel) throws SocketException {
super((SocketImpl) null);
this.socketChannel = socketChannel;
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 工厂方法 ████████████████████████████████████████████████████████████████████████████████┓ */
// 返回由指定的Socket通道适配而成的Socket
public static Socket create(SocketChannelImpl socketChannel) {
try {
return new SocketAdaptor(socketChannel);
} catch(SocketException e) {
throw new InternalError("Should not reach here");
}
}
/*▲ 工厂方法 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ Socket操作 ████████████████████████████████████████████████████████████████████████████████┓ */
/*
* 对[客户端Socket]执行【bind】操作。
*
* local: 待绑定的地址(ip+port)
*/
public void bind(SocketAddress local) throws IOException {
try {
socketChannel.bind(local);
} catch(Exception x) {
Net.translateException(x);
}
}
/** Override this method just to protect against changes in the superclass */
/*
* 对本地Socket执行【connect】操作,以便连接到远端Socket;如果远端还未就绪,则立即返回。
*
* remote: 等待连接的远端地址
*/
public void connect(SocketAddress remote) throws IOException {
connect(remote, 0);
}
/*
* 对本地Socket执行【connect】操作,以便连接到远端Socket;允许指定超时,以便等待远端就绪。
*
* remote : 等待连接的远端地址
* timeout: 超时时间,即允许连接等待的时间
*/
public void connect(SocketAddress remote, int timeout) throws IOException {
if(remote == null) {
throw new IllegalArgumentException("connect: The address can't be null");
}
if(timeout<0) {
throw new IllegalArgumentException("connect: timeout can't be negative");
}
synchronized(socketChannel.blockingLock()) {
// 如果是非阻塞Socket,则抛出异常
if(!socketChannel.isBlocking()) {
throw new IllegalBlockingModeException();
}
try {
// time=0表示成功连上之前,或者抛出IO异常之前,不会返回
if(timeout == 0) {
socketChannel.connect(remote);
return;
}
// 如果time不为0,表示需要阻塞一段时间,这里临时将socket更改为非阻塞Socket
socketChannel.configureBlocking(false);
try {
// 非阻塞式Socket的连接,如果连接成功,直接返回,连接失败的话进行后续操作
if(socketChannel.connect(remote)) {
return;
}
} finally {
try {
// 恢复为阻塞式Socket
socketChannel.configureBlocking(true);
} catch(ClosedChannelException e) {
}
}
// 将毫秒时间转换为纳秒
long timeoutNanos = NANOSECONDS.convert(timeout, MILLISECONDS);
long to = timeout;
// 进入死循环,直到连接成功,或者抛出了IO异常才退出循环
for(; ; ) {
long startTime = System.nanoTime();
// 注册监听连接事件(Net.POLLCONN),连接成功后,当前Socket会收到通知
if(socketChannel.pollConnected(to)) {
// 检测是否连接成功,由于这里已恢复为阻塞式Socket,所以内部会轮询检查连接,当有了返回结果时,就说明连接成功了
boolean connected = socketChannel.finishConnect();
assert connected;
break;
}
// 判断连接是否超时
timeoutNanos -= System.nanoTime() - startTime;
if(timeoutNanos<=0) {
try {
socketChannel.close();
} catch(IOException x) {
}
throw new SocketTimeoutException();
}
to = MILLISECONDS.convert(timeoutNanos, NANOSECONDS);
}
} catch(Exception x) {
Net.translateException(x, true);
}
}
}
/*▲ Socket操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 数据传输 ████████████████████████████████████████████████████████████████████████████████┓ */
// 获取Socket输入流,从中读取数据
public InputStream getInputStream() throws IOException {
if(!socketChannel.isOpen()) {
throw new SocketException("Socket is closed");
}
if(!socketChannel.isConnected()) {
throw new SocketException("Socket is not connected");
}
if(!socketChannel.isInputOpen()) {
throw new SocketException("Socket input is shutdown");
}
// 如果已经实例化过,则直接返回
if(socketInputStream != null) {
return socketInputStream;
}
try {
socketInputStream = AccessController.doPrivileged(new PrivilegedExceptionAction<InputStream>() {
public InputStream run() throws IOException {
return new SocketInputStream();
}
});
} catch(PrivilegedActionException e) {
throw (IOException) e.getException();
}
return socketInputStream;
}
// 获取Socket输出流,向其写入数据
public OutputStream getOutputStream() throws IOException {
if(!socketChannel.isOpen()) {
throw new SocketException("Socket is closed");
}
if(!socketChannel.isConnected()) {
throw new SocketException("Socket is not connected");
}
if(!socketChannel.isOutputOpen()) {
throw new SocketException("Socket output is shutdown");
}
OutputStream os = null;
try {
os = AccessController.doPrivileged(new PrivilegedExceptionAction<OutputStream>() {
public OutputStream run() throws IOException {
return Channels.newOutputStream(socketChannel);
}
});
} catch(java.security.PrivilegedActionException e) {
throw (IOException) e.getException();
}
return os;
}
/*▲ 数据传输 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 关闭 ████████████████████████████████████████████████████████████████████████████████┓ */
// 关闭socket连接
public void close() throws IOException {
socketChannel.close();
}
// 关闭读取功能
public void shutdownInput() throws IOException {
try {
socketChannel.shutdownInput();
} catch(Exception x) {
Net.translateException(x);
}
}
// 关闭写入功能
public void shutdownOutput() throws IOException {
try {
socketChannel.shutdownOutput();
} catch(Exception x) {
Net.translateException(x);
}
}
/*▲ 关闭 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 属性 ████████████████████████████████████████████████████████████████████████████████┓ */
// 获取被当前Socket适配器适配的Socket通道
public SocketChannel getChannel() {
return socketChannel;
}
// 返回本地IP,如果没有绑定地址,则返回通配地址
public InetAddress getLocalAddress() {
if(socketChannel.isOpen()) {
InetSocketAddress local = socketChannel.localAddress();
if(local != null) {
// 对指定的地址进行安全校验
return Net.getRevealedLocalAddress(local).getAddress();
}
}
return new InetSocketAddress(0).getAddress();
}
// 返回本地端口
public int getLocalPort() {
InetSocketAddress local = socketChannel.localAddress();
if(local == null) {
return -1;
}
return local.getPort();
}
// 获取远程IP,如果没有连接就返回null
public InetAddress getInetAddress() {
InetSocketAddress remote = socketChannel.remoteAddress();
if(remote == null) {
return null;
}
return remote.getAddress();
}
// 返回远程端口
public int getPort() {
InetSocketAddress remote = socketChannel.remoteAddress();
if(remote == null) {
return 0;
}
return remote.getPort();
}
/*▲ 属性 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 状态 ████████████████████████████████████████████████████████████████████████████████┓ */
// 判断当前Socket是否已建立连接
public boolean isConnected() {
return socketChannel.isConnected();
}
// 判断Socket是否已绑定本地地址
public boolean isBound() {
return socketChannel.localAddress() != null;
}
// 判断Socket是否已关闭
public boolean isClosed() {
return !socketChannel.isOpen();
}
// 判断Socket输入流是否已关闭(关闭了从当前Socket读取的功能)
public boolean isInputShutdown() {
return !socketChannel.isInputOpen();
}
// 判断Socket输出流是否已关闭(关闭了向当前Socket写入的功能)
public boolean isOutputShutdown() {
return !socketChannel.isOutputOpen();
}
/*▲ 状态 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ Socket配置参数 ████████████████████████████████████████████████████████████████████████████████┓ */
// 设置指定名称的Socket配置参数(Boolean类型的参数)
private void setBooleanOption(SocketOption<Boolean> name, boolean value) throws SocketException {
try {
socketChannel.setOption(name, value);
} catch(IOException x) {
Net.translateToSocketException(x);
}
}
// 设置指定名称的Socket配置参数(Integer类型的参数)
private void setIntOption(SocketOption<Integer> name, int value) throws SocketException {
try {
socketChannel.setOption(name, value);
} catch(IOException x) {
Net.translateToSocketException(x);
}
}
// 获取指定名称的Socket配置参数(Boolean类型的参数)
private boolean getBooleanOption(SocketOption<Boolean> name) throws SocketException {
try {
return socketChannel.getOption(name);
} catch(IOException x) {
Net.translateToSocketException(x);
return false; // keep compiler happy
}
}
// 获取指定名称的Socket配置参数(Integer类型的参数)
private int getIntOption(SocketOption<Integer> name) throws SocketException {
try {
return socketChannel.getOption(name);
} catch(IOException x) {
Net.translateToSocketException(x);
return -1; // keep compiler happy
}
}
// 获取超时约束的时间
public int getSoTimeout() throws SocketException {
return timeout;
}
// 设置超时约束的时间
public void setSoTimeout(int timeout) throws SocketException {
if(timeout<0) {
throw new IllegalArgumentException("timeout can't be negative");
}
this.timeout = timeout;
}
// 获取输出流缓冲区大小
public int getSendBufferSize() throws SocketException {
return getIntOption(StandardSocketOptions.SO_SNDBUF);
}
// 设置输出流缓冲区大小
public void setSendBufferSize(int size) throws SocketException {
// size 0 valid for SocketChannel, invalid for Socket
if(size<=0) {
throw new IllegalArgumentException("Invalid send size");
}
setIntOption(StandardSocketOptions.SO_SNDBUF, size);
}
// 获取设置输入流缓冲区大小
public int getReceiveBufferSize() throws SocketException {
return getIntOption(StandardSocketOptions.SO_RCVBUF);
}
// 设置设置输入流缓冲区大小
public void setReceiveBufferSize(int size) throws SocketException {
// size 0 valid for SocketChannel, invalid for Socket
if(size<=0) {
throw new IllegalArgumentException("Invalid receive size");
}
setIntOption(StandardSocketOptions.SO_RCVBUF, size);
}
// 获取是否禁用Nagle算法
public boolean getTcpNoDelay() throws SocketException {
return getBooleanOption(StandardSocketOptions.TCP_NODELAY);
}
// 设置是否禁用Nagle算法
public void setTcpNoDelay(boolean on) throws SocketException {
setBooleanOption(StandardSocketOptions.TCP_NODELAY, on);
}
// 获取是否启用延时关闭
public int getSoLinger() throws SocketException {
return getIntOption(StandardSocketOptions.SO_LINGER);
}
// 设置是否启用延时关闭
public void setSoLinger(boolean on, int linger) throws SocketException {
if(!on) {
linger = -1;
}
setIntOption(StandardSocketOptions.SO_LINGER, linger);
}
// 获取是否允许发送"紧急数据"
public boolean getOOBInline() throws SocketException {
return getBooleanOption(ExtendedSocketOption.SO_OOBINLINE);
}
// 设置是否允许发送"紧急数据"
public void setOOBInline(boolean on) throws SocketException {
setBooleanOption(ExtendedSocketOption.SO_OOBINLINE, on);
}
// 获取是否开启设置心跳机制
public boolean getKeepAlive() throws SocketException {
return getBooleanOption(StandardSocketOptions.SO_KEEPALIVE);
}
// 设置是否开启设置心跳机制
public void setKeepAlive(boolean on) throws SocketException {
setBooleanOption(StandardSocketOptions.SO_KEEPALIVE, on);
}
// 获取是否允许立刻重用已关闭的socket端口
public boolean getReuseAddress() throws SocketException {
return getBooleanOption(StandardSocketOptions.SO_REUSEADDR);
}
// 设置是否允许立刻重用已关闭的socket端口
public void setReuseAddress(boolean on) throws SocketException {
setBooleanOption(StandardSocketOptions.SO_REUSEADDR, on);
}
// 获取IP头部的Type-of-Service字段的值
public int getTrafficClass() throws SocketException {
return getIntOption(StandardSocketOptions.IP_TOS);
}
// 设置IP参数,即设置IP头部的Type-of-Service字段,用于描述IP包的优先级和QoS选项
public void setTrafficClass(int tc) throws SocketException {
setIntOption(StandardSocketOptions.IP_TOS, tc);
}
/*▲ Socket配置参数 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 杂项 ████████████████████████████████████████████████████████████████████████████████┓ */
// 发送一个字节的"紧急数据",参见SocketOptions#SO_OOBINLINE参数
public void sendUrgentData(int data) throws IOException {
int n = socketChannel.sendOutOfBandData((byte) data);
if(n == 0) {
throw new IOException("Socket buffer full");
}
}
/*▲ 杂项 ████████████████████████████████████████████████████████████████████████████████┛ */
public String toString() {
if(socketChannel.isConnected()) {
return "Socket[addr=" + getInetAddress() + ",port=" + getPort() + ",localport=" + getLocalPort() + "]";
}
return "Socket[unconnected]";
}
// Socket输入流(要求适配内的通道是阻塞式的)
private class SocketInputStream extends ChannelInputStream {
private SocketInputStream() {
super(socketChannel);
}
// 从内部通道中读取,读到的内容写入dst
protected int read(ByteBuffer dst) throws IOException {
synchronized(socketChannel.blockingLock()) {
// 如果将要被适配的Socket通道是非阻塞的,则抛出异常
if(!socketChannel.isBlocking()) {
throw new IllegalBlockingModeException();
}
long to = SocketAdaptor.this.timeout;
// 如果允许一直阻塞
if(to == 0) {
return socketChannel.read(dst);
}
// 获取超时时间
long timeoutNanos = NANOSECONDS.convert(to, MILLISECONDS);
// 轮询读取,直到成功读取到数据
for(; ; ) {
// 获取当前时间
long startTime = System.nanoTime();
// 注册监听可读事件(Net.POLLIN),当通道内有数据可读时,当前Socket会收到通知
if(socketChannel.pollRead(to)) {
// 如果已经有数据达到,则立即读取
return socketChannel.read(dst);
}
// 计算剩余允许阻塞的时间
timeoutNanos -= System.nanoTime() - startTime;
// 如果已经超时了,则直接抛异常
if(timeoutNanos<=0) {
throw new SocketTimeoutException();
}
// 纳秒转换为毫秒
to = MILLISECONDS.convert(timeoutNanos, NANOSECONDS);
}
}
}
}
}