22
33import io .vproxy .base .selector .SelectorEventLoop ;
44import io .vproxy .base .util .Logger ;
5- import io .vproxy .base .util .anno .Comment ;
6- import io .vproxy .base .util .promise .Promise ;
75import io .vproxy .vfd .FD ;
86import io .vproxy .vfd .IPPort ;
97import io .vproxy .vfd .SocketFD ;
1311import java .net .SocketOption ;
1412import java .net .StandardSocketOptions ;
1513import java .nio .ByteBuffer ;
16- import java .util .function . Function ;
14+ import java .util .Objects ;
1715
18- public abstract class AbstractBaseVirtualSocketFD extends AbstractBaseFD implements SocketFD , VirtualFD {
19- private final VirtualFD _self ;
16+ public abstract class AbstractBaseVirtualSocketFD extends AbstractBaseFD implements SocketFD , VirtualFD , DelegatingSourceFD {
2017 private SelectorEventLoop loop ;
2118 protected final boolean isAccepted ;
2219
@@ -30,16 +27,11 @@ public abstract class AbstractBaseVirtualSocketFD extends AbstractBaseFD impleme
3027 private IPPort remote ;
3128 private IPPort local ;
3229
30+ private boolean shutdownOutputIsCalled ;
3331 private boolean eof ;
3432 private IOException error ;
3533
3634 public AbstractBaseVirtualSocketFD (boolean isAccepted , IPPort local , IPPort remote ) {
37- this (null , isAccepted , local , remote );
38- }
39-
40- @ Comment ("this constructor will make an util object" )
41- public AbstractBaseVirtualSocketFD (VirtualFD _self , boolean isAccepted , IPPort local , IPPort remote ) {
42- this ._self = _self ;
4335 this .isAccepted = isAccepted ;
4436 this .connected = isAccepted ;
4537 this .local = local ;
@@ -98,6 +90,11 @@ public boolean isConnected() {
9890 public void shutdownOutput () throws IOException {
9991 checkOpen ();
10092 checkConnected ();
93+ shutdownOutputIsCalled = true ;
94+ }
95+
96+ public boolean isShutdownOutput () {
97+ return shutdownOutputIsCalled ;
10198 }
10299
103100 @ Override
@@ -161,7 +158,7 @@ public int read(ByteBuffer dst) throws IOException {
161158
162159 protected abstract int doRead (ByteBuffer dst ) throws IOException ;
163160
164- public void setEof () {
161+ protected void setEof () {
165162 this .eof = true ;
166163 setReadable ();
167164 }
@@ -227,7 +224,7 @@ public <T> void setOption(SocketOption<T> name, T value) {
227224
228225 @ Override
229226 public FD real () {
230- throw new UnsupportedOperationException () ;
227+ return null ;
231228 }
232229
233230 @ Override
@@ -262,17 +259,6 @@ private void superClose() {
262259
263260 protected abstract void doClose (boolean reset );
264261
265- protected void asyncClose (Function <Boolean , Promise <Void >> promiseFunc ) {
266- if (closed ) {
267- return ;
268- }
269- closed = true ;
270- promiseFunc .apply (resetWhenClosing ).then (v -> {
271- closeSelf ();
272- return Promise .resolve (null );
273- });
274- }
275-
276262 @ Override
277263 public String toString () {
278264 return formatToString ();
@@ -284,8 +270,14 @@ public String toString() {
284270 // events
285271 // ======
286272
287- private VirtualFD self () {
288- return _self == null ? this : _self ;
273+ private DelegatingTargetFD delegatingTargetFD ;
274+
275+ @ Override
276+ public void setDelegatingTargetFD (DelegatingTargetFD fd ) {
277+ Objects .requireNonNull (fd );
278+ if (delegatingTargetFD != null )
279+ throw new IllegalStateException ("delegatingTargetFD is already set" );
280+ delegatingTargetFD = fd ;
289281 }
290282
291283 private boolean readable = false ;
@@ -295,15 +287,23 @@ protected void setReadable() {
295287 if (loop == null ) {
296288 return ;
297289 }
298- loop .runOnLoop (() -> loop .selector .registerVirtualReadable (self ()));
290+ if (delegatingTargetFD != null ) {
291+ delegatingTargetFD .setReadable ();
292+ return ;
293+ }
294+ loop .runOnLoop (() -> loop .selector .registerVirtualReadable (this ));
299295 }
300296
301297 protected void cancelReadable () {
302298 readable = false ;
303299 if (loop == null ) {
304300 return ;
305301 }
306- loop .runOnLoop (() -> loop .selector .removeVirtualReadable (self ()));
302+ if (delegatingTargetFD != null ) {
303+ delegatingTargetFD .cancelReadable ();
304+ return ;
305+ }
306+ loop .runOnLoop (() -> loop .selector .removeVirtualReadable (this ));
307307 }
308308
309309 private boolean writable = false ;
@@ -313,15 +313,23 @@ protected void setWritable() {
313313 if (loop == null ) {
314314 return ;
315315 }
316- loop .runOnLoop (() -> loop .selector .registerVirtualWritable (self ()));
316+ if (delegatingTargetFD != null ) {
317+ delegatingTargetFD .setWritable ();
318+ return ;
319+ }
320+ loop .runOnLoop (() -> loop .selector .registerVirtualWritable (this ));
317321 }
318322
319323 protected void cancelWritable () {
320324 writable = false ;
321325 if (loop == null ) {
322326 return ;
323327 }
324- loop .runOnLoop (() -> loop .selector .removeVirtualWritable (self ()));
328+ if (delegatingTargetFD != null ) {
329+ delegatingTargetFD .cancelWritable ();
330+ return ;
331+ }
332+ loop .runOnLoop (() -> loop .selector .removeVirtualWritable (this ));
325333 }
326334
327335 @ Override
@@ -338,14 +346,4 @@ public void onRegister() {
338346 public void onRemove () {
339347 // do nothing
340348 }
341-
342- protected void tryToRunOnLoop (Runnable r ) {
343- if (loop == null ) {
344- assert Logger .lowLevelDebug ("no loop yet, direct run" );
345- r .run ();
346- } else {
347- assert Logger .lowLevelDebug ("run on loop" );
348- loop .runOnLoop (r );
349- }
350- }
351349}
0 commit comments