-
Notifications
You must be signed in to change notification settings - Fork 668
/
SocketChannelImpl.java
1630 lines (1347 loc) · 64.6 KB
/
SocketChannelImpl.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.FileDescriptor;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ProtocolFamily;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.SocketOption;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.AlreadyBoundException;
import java.nio.channels.AlreadyConnectedException;
import java.nio.channels.AsynchronousCloseException;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.ConnectionPendingException;
import java.nio.channels.NoConnectionPendingException;
import java.nio.channels.NotYetConnectedException;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.locks.ReentrantLock;
import sun.net.NetHooks;
import sun.net.ext.ExtendedSocketOptions;
import sun.net.util.SocketExceptions;
import static sun.net.ext.ExtendedSocketOptions.SOCK_STREAM;
/**
* An implementation of SocketChannels
*/
// Socket通道的本地实现
class SocketChannelImpl extends SocketChannel implements SelChImpl {
/** State, increases monotonically */
// Socket状态参数
private static final int ST_UNCONNECTED = 0; // 未连接
private static final int ST_CONNECTIONPENDING = 1; // 正在连接
private static final int ST_CONNECTED = 2; // 已连接
private static final int ST_CLOSING = 3; // 正在断开连接(关闭)
private static final int ST_KILLPENDING = 4; // 正在销毁
private static final int ST_KILLED = 5; // 已销毁
/** need stateLock to change */
private volatile int state; // SocketChannel状态
/** Socket adaptor, created on demand */
// 由当前Socket通道适配而成的Socket
private Socket socket;
private final FileDescriptor fd; // [客户端Socket]/[服务端Socket(通信)]在Java层的文件描述符
private final int fdVal; // [客户端Socket]/[服务端Socket(通信)]在本地(native层)的文件描述符
/*
* 本地地址
*
* [客户端Socket] : 客户端地址
* [服务端Socket(通信)]: 服务端地址
*/
private InetSocketAddress localAddress;
/*
* 远程地址
*
* [客户端Socket] : 服务端的地址
* [服务端Socket(通信)]: 客户端的地址
*/
private InetSocketAddress remoteAddress;
/** Used to make native read and write calls */
private static NativeDispatcher nd; // 完成Socket读写的工具类
/** Input/Output closed */
private volatile boolean isInputClosed; // 是否关闭了从当前通道读取数据的功能
private volatile boolean isOutputClosed; // 是否关闭了向当前通道写入数据的功能
/** set true when exclusive binding is on and SO_REUSEADDR is emulated */
private boolean isReuseAddress; // 是否允许立刻重用已关闭的socket端口
/** IDs of native threads doing reads and writes, for signalling */
private long readerThread; // 当前通道开始读/连接操作时所在的native线程号
private long writerThread; // 当前通道开始写操作时所在的native线程号
/** Lock held by current reading or connecting thread */
private final ReentrantLock readLock = new ReentrantLock(); // 读锁
/** Lock held by current writing or connecting thread */
private final ReentrantLock writeLock = new ReentrantLock(); // 写锁
/**
* Lock held by any thread that modifies the state fields declared below
* DO NOT invoke a blocking I/O operation while holding this lock!
*/
private final Object stateLock = new Object(); // 状态锁
static {
IOUtil.load();
nd = new SocketDispatcher();
}
/*▼ 构造器 ████████████████████████████████████████████████████████████████████████████████┓ */
/** Constructor for normal connecting sockets */
/*
* 构造一个未绑定的[客户端Socket],内部初始化了该Socket的文件描述符
*/
SocketChannelImpl(SelectorProvider provider) throws IOException {
super(provider);
// 创建[客户端Socket],并返回其在Java层的文件描述符
this.fd = Net.socket(true);
// 获取[客户端Socket]在本地(native层)的文件描述符
this.fdVal = IOUtil.fdVal(fd);
}
/** Constructor for sockets obtained from server sockets */
/*
* 构造一个[服务端Socket(通信)]
*
* 注:该方法是在ServerSocketChannelImpl中被调用的
*/
SocketChannelImpl(SelectorProvider provider, FileDescriptor fd, InetSocketAddress isa) throws IOException {
super(provider);
// 记录[服务端Socket(通信)]在Java层的文件描述符
this.fd = fd;
// 获取[服务端Socket(通信)]在本地(native层)的文件描述符
this.fdVal = IOUtil.fdVal(fd);
synchronized(stateLock) {
this.localAddress = Net.localAddress(fd);
this.remoteAddress = isa;
this.state = ST_CONNECTED;
}
}
/*
* 构造一个Socket
*/
SocketChannelImpl(SelectorProvider provider, FileDescriptor fd, boolean bound) throws IOException {
super(provider);
// 记录当前Socket在Java层的文件描述符
this.fd = fd;
// 获取当前Socket在本地(native层)的文件描述符
this.fdVal = IOUtil.fdVal(fd);
// 如果无需绑定,直接返回
if(!bound) {
return;
}
synchronized(stateLock) {
this.localAddress = Net.localAddress(fd);
}
}
/*▲ 构造器 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 适配 ████████████████████████████████████████████████████████████████████████████████┓ */
// 返回由当前Socket通道适配而成的Socket
@Override
public Socket socket() {
synchronized(stateLock) {
if(socket == null) {
socket = SocketAdaptor.create(this);
}
return socket;
}
}
/*▲ 适配 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ socket操作 ████████████████████████████████████████████████████████████████████████████████┓ */
// 对[客户端Socket]执行【bind】操作;local为null时,使用通配IP和随机端口
@Override
public SocketChannel bind(SocketAddress local) throws IOException {
readLock.lock();
try {
writeLock.lock();
try {
synchronized(stateLock) {
// 确保通道已经开启,否则抛异常
ensureOpen();
// 如果正在连接中,则抛出异常
if(state == ST_CONNECTIONPENDING) {
throw new ConnectionPendingException();
}
// 如果已绑定,则抛出异常
if(localAddress != null) {
throw new AlreadyBoundException();
}
// 确定绑定到哪个地址
InetSocketAddress isa = (local == null) ? new InetSocketAddress(0) // 如果未指定绑定地址,则使用通配IP和随机端口
: Net.checkAddress(local);
// 权限校验
SecurityManager sm = System.getSecurityManager();
if(sm != null) {
sm.checkListen(isa.getPort());
}
// Socket进行bind操作之前的回调
NetHooks.beforeTcpBind(fd, isa.getAddress(), isa.getPort());
// 为[客户端Socket]绑定IP地址与端口号
Net.bind(fd, isa.getAddress(), isa.getPort());
// 记录绑定的本地地址
localAddress = Net.localAddress(fd);
}
} finally {
writeLock.unlock();
}
} finally {
readLock.unlock();
}
return this;
}
/*
* 对[客户端Socket]执行【connect】操作,以便连接到远端Socket
*
* 在非阻塞模式下:如果成功建立了连接,则返回true;如果连接失败,则返回false,并且需要后续调用finishConnect()来完成连接操作。
* 在阻塞模式下,则连接操作会陷入阻塞,直到成功建立连接,或者发生IO异常。
*
* 在连接过程中发起的读写操作将被阻塞,直到连接完成
*/
@Override
public boolean connect(SocketAddress remote) throws IOException {
// 检查远程Socket地址是否合规
InetSocketAddress isa = Net.checkAddress(remote);
// 权限校验
SecurityManager sm = System.getSecurityManager();
if(sm != null) {
sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort());
}
InetAddress ia = isa.getAddress();
// 如果远程地址是通配地址
if(ia.isAnyLocalAddress()) {
// 获取本地主机地址,以便后续连接使用
ia = InetAddress.getLocalHost();
}
try {
readLock.lock();
try {
writeLock.lock();
try {
int n = 0;
// 判断当前通道是否为阻塞模式
boolean blocking = isBlocking();
try {
/*
* 标记可能阻塞的IO操作的开始:需要为阻塞通道所在的线程设置中断回调,该回调在遇到线程中断时会关闭通道;
* 此处会将通道设置为正在连接状态...
*/
beginConnect(blocking, isa);
do {
// 当前Socket向指定地址处的ServerSocket发起连接(如果采用堵塞模式,会一直等待,直到成功或出现异常)
n = Net.connect(fd, ia, isa.getPort());
} while(n == IOStatus.INTERRUPTED && isOpen());
} finally {
// 在当前Socket发起连接之后,移除为线程中断设置的回调,在连接成功的情形下,设置本地地址,并更新连接状态为已连接
endConnect(blocking, (n>0));
}
assert IOStatus.check(n);
return n>0;
} finally {
writeLock.unlock();
}
} finally {
readLock.unlock();
}
} catch(IOException ioe) {
// connect failed, close the channel
close();
throw SocketExceptions.of(ioe, isa);
}
}
/*
* 促进当前Socket完成与远程的连接
*
* 在非阻塞模式下:如果成功建立了连接,则返回true;如果连接失败,则返回false
* 在阻塞模式下 :不断检查连接是否成功,直到连接成功建立,或发生IO异常。
*/
@Override
public boolean finishConnect() throws IOException {
try {
readLock.lock();
try {
writeLock.lock();
try {
// 如果已经连接,立即返回true
if(isConnected()) {
return true;
}
// 判断当前通道是否为阻塞模式
boolean blocking = isBlocking();
boolean connected = false;
try {
/*
* 标记可能阻塞的IO操作的开始:需要为阻塞通道所在的线程设置中断回调,该回调在遇到线程中断时会关闭通道;
* 如果通道未处于"正在连接"状态,则抛异常...
*/
beginFinishConnect(blocking);
int n = 0;
if(blocking) {
do {
// 阻塞模式下,不断检查连接是否成功,直到连接成功建立,或发生异常
n = checkConnect(fd, true);
} while((n == 0 || n == IOStatus.INTERRUPTED) && isOpen());
} else {
// 非阻塞模式下,检查一次连接是否成功
n = checkConnect(fd, false);
}
connected = (n>0);
} finally {
// 在检测Socket是否完成连接之后,移除为线程中断设置的回调,在Socket成功连接的情形下,设置本地地址,并更新连接状态为已连接
endFinishConnect(blocking, connected);
}
assert (blocking && connected) ^ !blocking;
return connected;
} finally {
writeLock.unlock();
}
} finally {
readLock.unlock();
}
} catch(IOException ioe) {
// connect failed, close the channel
close();
throw SocketExceptions.of(ioe, remoteAddress);
}
}
/**
* Poll this channel's socket for a connection, up to the given timeout.
*
* @return {@code true} if the socket is polled
*/
/*
* 注册监听连接事件(Net.POLLCONN),连接成功后,当前Socket会收到通知。
* timeout是允许可阻塞socket等待连接的时间。
* 返回值指示是否在阻塞时间内收到了"成功建立连接"的信号。
*/
boolean pollConnected(long timeout) throws IOException {
boolean blocking = isBlocking();
assert Thread.holdsLock(blockingLock()) && blocking;
readLock.lock();
try {
writeLock.lock();
try {
boolean polled = false;
try {
/*
* 标记可能阻塞的IO操作的开始:需要为阻塞通道所在的线程设置中断回调,该回调在遇到线程中断时会关闭通道;
* 如果通道未处于"正在连接"状态,则抛异常...
*/
beginFinishConnect(blocking);
// 注册Net.POLLIN事件,远端发来"已连接"的消息时,会通知当前Socket
int events = Net.poll(fd, Net.POLLCONN, timeout);
polled = (events != 0);
} finally {
/*
* invoke endFinishConnect with completed=false so that the state is not changed to ST_CONNECTED.
* The socket adaptor will use finishConnect to finish.
*/
endFinishConnect(blocking, /*completed*/false);
}
return polled;
} finally {
writeLock.unlock();
}
} finally {
readLock.unlock();
}
}
/*▲ socket操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 关闭 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Invoked by implCloseChannel to close the channel.
*
* This method waits for outstanding I/O operations to complete.
* When in blocking mode, the socket is pre-closed and the threads in blocking I/O operations are signalled to ensure that the outstanding I/O operations complete quickly.
*
* If the socket is connected then it is shutdown by this method.
* The shutdown ensures that the peer reads EOF for the case that the socket is not pre-closed or closed by this method.
*
* The socket is closed by this method when it is not registered with a Selector.
* Note that a channel configured blocking may be registered with a Selector.
* This arises when a key is canceled and the channel configured to blocking mode before the key is flushed from the Selector.
*/
// 实现对"可选择"通道的关闭操作
@Override
protected void implCloseSelectableChannel() throws IOException {
assert !isOpen();
boolean blocking;
boolean connected;
boolean interrupted = false;
// 进入正在断开连接的状态
synchronized(stateLock) {
assert state<ST_CLOSING;
blocking = isBlocking();
connected = (state == ST_CONNECTED);
// 标记通道进入正在断开连接的状态
state = ST_CLOSING;
}
/* wait for any outstanding I/O operations to complete */
// 对于阻塞式通道,需要等到该通道上其他阻塞操作结束之后才能进行关闭
if(blocking) {
synchronized(stateLock) {
assert state == ST_CLOSING;
long reader = readerThread;
long writer = writerThread;
// 如果该通道上存在其他阻塞的读/写操作,则唤醒那些阻塞的操作,以促使它们结束运行
if(reader != 0 || writer != 0) {
nd.preClose(fd);
connected = false; // fd is no longer connected socket
if(reader != 0) {
// 唤醒阻塞的reader线程
NativeThread.signal(reader);
}
if(writer != 0) {
// 唤醒阻塞的writer线程
NativeThread.signal(writer);
}
// wait for blocking I/O operations to end
while(readerThread != 0 || writerThread != 0) {
try {
stateLock.wait();
} catch(InterruptedException e) {
interrupted = true;
}
}
}
}
// 对于非阻塞通道,确保其他读写操作已经完成
} else {
// non-blocking mode: wait for read/write to complete
readLock.lock();
try {
writeLock.lock();
writeLock.unlock();
} finally {
readLock.unlock();
}
}
synchronized(stateLock) {
assert state == ST_CLOSING;
/*
* if connected and the channel is registered with a Selector
* then shutdown the output if possible so that the peer reads EOF.
* If SO_LINGER is enabled and set to a non-zero value
* then it needs to be disabled so that the Selector does not wait
* when it closes the socket.
*/
if(connected && isRegistered()) {
try {
// 是否启用延时关闭
SocketOption<Integer> opt = StandardSocketOptions.SO_LINGER;
int interval = (int) Net.getSocketOption(fd, Net.UNSPEC, opt);
if(interval != 0) {
if(interval>0) {
// disable SO_LINGER
Net.setSocketOption(fd, Net.UNSPEC, opt, -1);
}
Net.shutdown(fd, Net.SHUT_WR);
}
} catch(IOException ignore) {
}
}
// 进入正在销毁的状态
state = ST_KILLPENDING;
}
/* close socket if not registered with Selector */
// 如果通道已经没有在任何选择器上注册,则销毁它
if(!isRegistered()) {
kill();
}
// 恢复中断状态
if(interrupted) {
Thread.currentThread().interrupt();
}
}
// 销毁通道,即释放对Socket文件描述符的引用
@Override
public void kill() throws IOException {
synchronized(stateLock) {
if(state == ST_KILLPENDING) {
// 通道进入销毁状态
state = ST_KILLED;
// 释放对本地文件描述符的引用
nd.close(fd);
}
}
}
// 关闭从当前通道读取数据的功能
@Override
public SocketChannel shutdownInput() throws IOException {
synchronized(stateLock) {
// 确保通道已经开启,否则抛异常
ensureOpen();
if(!isConnected()) {
throw new NotYetConnectedException();
}
if(!isInputClosed) {
Net.shutdown(fd, Net.SHUT_RD);
long reader = readerThread;
if(reader != 0) {
// 唤醒阻塞的thread线程
NativeThread.signal(reader);
}
isInputClosed = true;
}
return this;
}
}
// 关闭向当前通道写入数据的功能
@Override
public SocketChannel shutdownOutput() throws IOException {
synchronized(stateLock) {
// 确保通道已经开启,否则抛异常
ensureOpen();
if(!isConnected()) {
throw new NotYetConnectedException();
}
if(!isOutputClosed) {
Net.shutdown(fd, Net.SHUT_WR);
long writer = writerThread;
if(writer != 0) {
// 唤醒阻塞的thread线程
NativeThread.signal(writer);
}
isOutputClosed = true;
}
return this;
}
}
/*▲ 关闭 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 读/写操作 ████████████████████████████████████████████████████████████████████████████████┓ */
// 从当前通道中读取数据,读到的内容写入dst
@Override
public int read(ByteBuffer dst) throws IOException {
Objects.requireNonNull(dst);
readLock.lock();
try {
boolean blocking = isBlocking();
int n = 0;
try {
/*
* 标记可能阻塞的IO操作的开始:需要为阻塞通道所在的线程设置中断回调,该回调在遇到线程中断时会关闭通道;
* 此处要确保通道处于"已连接"状态...
*/
beginRead(blocking);
// check if input is shutdown
if(isInputClosed) {
return IOStatus.EOF;
}
/*
* 接下来,从当前通道中起始位置处读取,读到的内容写入dst后,
* 返回值表示从当前通道读取的字节数量
*/
// 阻塞模式下,如果当前通道中没有可读数据,则陷入阻塞
if(blocking) {
do {
n = IOUtil.read(fd, dst, -1, nd);
} while(n == IOStatus.INTERRUPTED && isOpen());
} else {
n = IOUtil.read(fd, dst, -1, nd);
}
} finally {
// 标记可能阻塞的I/O操作的结束:需要移除为阻塞通道所在的线程设置的中断回调
endRead(blocking, n>0);
if(n<=0 && isInputClosed) {
return IOStatus.EOF;
}
}
return IOStatus.normalize(n);
} finally {
readLock.unlock();
}
}
//【散射】从当前通道中读取数据,读到的内容依次写入dsts中offset处起的length个缓冲区
@Override
public long read(ByteBuffer[] dsts, int offset, int length) throws IOException {
Objects.checkFromIndexSize(offset, length, dsts.length);
readLock.lock();
try {
// 当前通道是否为阻塞模式
boolean blocking = isBlocking();
long n = 0;
try {
/*
* 标记可能阻塞的IO操作的开始:需要为阻塞通道所在的线程设置中断回调,该回调在遇到线程中断时会关闭通道;
* 此处要确保通道处于"已连接"状态...
*/
beginRead(blocking);
// check if input is shutdown
if(isInputClosed) {
return IOStatus.EOF;
}
/*
* 接下来,从当前通道中读取,读到的内容依次写入dsts[offset, offset+length-1]中各个缓冲区后,
* 返回值表示从当前通道读取的字节数量
*/
// 阻塞模式下,如果当前通道中没有可读数据,则陷入阻塞
if(blocking) {
do {
// 从文件描述符fd(关联的socket)中读取,读到的内容依次存入dsts中offset处起的length个缓冲区中,返回读到的字节数量
n = IOUtil.read(fd, dsts, offset, length, nd);
} while(n == IOStatus.INTERRUPTED && isOpen());
} else {
// 从文件描述符fd(关联的socket)中读取,读到的内容依次存入dsts中offset处起的length个缓冲区中,返回读到的字节数量
n = IOUtil.read(fd, dsts, offset, length, nd);
}
} finally {
// 标记可能阻塞的I/O操作的结束:需要移除为阻塞通道所在的线程设置的中断回调
endRead(blocking, n>0);
if(n<=0 && isInputClosed) {
return IOStatus.EOF;
}
}
return IOStatus.normalize(n);
} finally {
readLock.unlock();
}
}
// 从src中读取数据,读到的内容向当前通道中写入
@Override
public int write(ByteBuffer src) throws IOException {
Objects.requireNonNull(src);
writeLock.lock();
try {
boolean blocking = isBlocking();
int n = 0;
try {
/*
* 标记可能阻塞的IO操作的开始:需要为阻塞通道所在的线程设置中断回调,该回调在遇到线程中断时会关闭通道;
* 此处要确保通道处于"已连接"状态...
*/
beginWrite(blocking);
/*
* 接下来,从缓冲区src读取,读到的内容向当前通道中追加,返回值表示写入当前通道的字节数量
* 注:不使用DirectIO
*/
// 阻塞模式下,如果当前通道中没有可读数据,则陷入阻塞
if(blocking) {
do {
n = IOUtil.write(fd, src, -1, nd);
} while(n == IOStatus.INTERRUPTED && isOpen());
} else {
n = IOUtil.write(fd, src, -1, nd);
}
} finally {
// 标记可能阻塞的IO操作的结束:需要移除为阻塞通道所在的线程设置的中断回调
endWrite(blocking, n>0);
if(n<=0 && isOutputClosed) {
throw new AsynchronousCloseException();
}
}
return IOStatus.normalize(n);
} finally {
writeLock.unlock();
}
}
//【聚集】从srcs中offset处起的length个缓冲区读取数据,读到的内容向当前通道中写入
@Override
public long write(ByteBuffer[] srcs, int offset, int length) throws IOException {
Objects.checkFromIndexSize(offset, length, srcs.length);
writeLock.lock();
try {
boolean blocking = isBlocking();
long n = 0;
try {
/*
* 标记可能阻塞的IO操作的开始:需要为阻塞通道所在的线程设置中断回调,该回调在遇到线程中断时会关闭通道;
* 此处要确保通道处于"已连接"状态...
*/
beginWrite(blocking);
/*
* 接下来,从srcs[offset, offset+length-1]中各个缓冲区读取数据,读到的内容向当前通道中起始位置处写入,返回值表示写入的字节数量
* 注:不使用DirectIO
*/
if(blocking) {
do {
//
n = IOUtil.write(fd, srcs, offset, length, nd);
} while(n == IOStatus.INTERRUPTED && isOpen());
} else {
n = IOUtil.write(fd, srcs, offset, length, nd);
}
} finally {
// 标记可能阻塞的IO操作的结束:需要移除为阻塞通道所在的线程设置的中断回调
endWrite(blocking, n>0);
if(n<=0 && isOutputClosed) {
throw new AsynchronousCloseException();
}
}
return IOStatus.normalize(n);
} finally {
writeLock.unlock();
}
}
/**
* Poll this channel's socket for reading up to the given timeout.
*
* @return {@code true} if the socket is polled
*/
/*
* 注册监听可读事件(Net.POLLIN),当通道内有数据可读时,当前Socket会收到通知。
* timeout是允许可阻塞socket等待可读数据的时间。
* 返回值指示是否在阻塞时间内收到了"可读取数据"的信号。
*/
boolean pollRead(long timeout) throws IOException {
boolean blocking = isBlocking();
assert Thread.holdsLock(blockingLock()) && blocking;
readLock.lock();
try {
boolean polled = false;
try {
/*
* 标记可能阻塞的IO操作的开始:需要为阻塞通道所在的线程设置中断回调,该回调在遇到线程中断时会关闭通道;
* 此处要确保通道处于"已连接"状态...
*/
beginRead(blocking);
// 注册Net.POLLIN事件,远端发来"有可读数据"的消息时,会通知当前Socket
int events = Net.poll(fd, Net.POLLIN, timeout);
polled = (events != 0);
} finally {
// 标记可能阻塞的I/O操作的结束:需要移除为阻塞通道所在的线程设置的中断回调
endRead(blocking, polled);
}
return polled;
} finally {
readLock.unlock();
}
}
/*▲ 读/写操作 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 监听参数/事件 ████████████████████████████████████████████████████████████████████████████████┓ */
/**
* Translates an interest operation set into a native poll event set
*/
/*
* 翻译Socket通道注册的监听事件,返回对ops的翻译结果
*
* 方向:Java层 --> native层
* SelectionKey.XXX --> Net.XXX
*/
public int translateInterestOps(int ops) {
int newOps = 0;
if((ops & SelectionKey.OP_READ) != 0) {
newOps |= Net.POLLIN;
}
if((ops & SelectionKey.OP_WRITE) != 0) {
newOps |= Net.POLLOUT;
}
if((ops & SelectionKey.OP_CONNECT) != 0) {
newOps |= Net.POLLCONN;
}
return newOps;
}
/*▲ 监听参数/事件 ████████████████████████████████████████████████████████████████████████████████┛ */
/*▼ 就绪参数/事件 ████████████████████████████████████████████████████████████████████████████████┓ */
/*
*【增量更新】已就绪事件
*
* 将本地(native)反馈的就绪信号ops翻译并存储到Java层的就绪事件readyOps中,
* 返回值指示上一次反馈的事件与本次反馈的事件是否发生了改变。
*
* 通道收到有效的反馈事件后,会【增量更新】上次记录的已就绪事件,
* 如果本地(native)反馈了错误或挂起信号,则将已就绪事件直接设置为通道注册的监听事件。
*
* 方向:native层 --> Java层
* Net.XXX --> SelectionKey.XXX
*
* 参见:SelectionKeyImpl#readyOps
*/
public boolean translateAndUpdateReadyOps(int ops, SelectionKeyImpl ski) {
return translateReadyOps(ops, ski.nioReadyOps(), ski);
}
/*
*【覆盖更新】已就绪事件
*
* 将本地(native)反馈的就绪信号ops翻译并存储到Java层的就绪事件readyOps中,
* 返回值指示上一次反馈的事件与本次反馈的事件是否发生了改变。
*
* 通道收到有效的反馈事件后,会【覆盖】上次记录的已就绪事件,
* 如果本地(native)反馈了错误或挂起信号,则将已就绪事件直接设置为通道注册的监听事件。
*
* 方向:native层 --> Java层
* Net.XXX --> SelectionKey.XXX
*
* 参见:SelectionKeyImpl#readyOps
*/
public boolean translateAndSetReadyOps(int ops, SelectionKeyImpl selectionKey) {
return translateReadyOps(ops, 0, selectionKey);
}
/**
* Translates native poll revent ops into a ready operation ops
*/
/*
*【增量更新】已就绪事件(基于initialOps叠加)
*
* 将本地(native)反馈的就绪信号ops翻译并存储到Java层的就绪事件readyOps中,
* 返回值指示上一次反馈的事件与本次反馈的事件是否发生了改变。
*
* 通道收到有效的反馈事件后,会将其【叠加】在initialOps上,换句话说是对已就绪事件的增量更新。
* 如果本地(native)反馈了错误或挂起信号,则将已就绪事件直接设置为通道注册的监听事件
*
* 方向:native层 --> Java层
* Net.XXX --> SelectionKey.XXX
*
* 参见:SelectionKeyImpl#readyOps
*/
public boolean translateReadyOps(int ops, int initialOps, SelectionKeyImpl selectionKey) {
// 不合规的文件描述符
if((ops & Net.POLLNVAL) != 0) {
/*
* This should only happen if this channel is pre-closed while a selection operation is in progress
* ## Throw an error if this channel has not been pre-closed
*/
return false;
}
// 获取注册的监听事件:SelectionKey.XXX(不会验证当前"选择键"是否有效)
int intOps = selectionKey.nioInterestOps();
// 获取已就绪事件
int oldOps = selectionKey.nioReadyOps();
int newOps = initialOps;