1
1
package top .meethigher .proxy .tcp .tunnel ;
2
2
3
3
import io .vertx .core .AsyncResult ;
4
- import io .vertx .core .Handler ;
5
4
import io .vertx .core .Vertx ;
6
5
import io .vertx .core .net .NetClient ;
7
6
import io .vertx .core .net .NetSocket ;
@@ -81,42 +80,7 @@ public TunnelMessageParser decode(final NetSocket socket) {
81
80
*/
82
81
public void connect (final String host , final int port ) {
83
82
log .debug ("client connect {}:{} ..." , host , port );
84
- Handler <AsyncResult <NetSocket >> asyncResultHandler = ar -> {
85
- if (ar .succeeded ()) {
86
- setReconnectDelay (this .minDelay );
87
-
88
- NetSocket socket = ar .result ();
89
- this .netSocket = socket ;
90
- socket .pause ();
91
- socket .closeHandler (v -> {
92
- log .warn ("closed {} -- {}, after {} ms will reconnect" ,
93
- socket .localAddress (),
94
- socket .remoteAddress (),
95
- reconnectDelay );
96
- this .netSocket = null ;
97
- reconnect (host , port );
98
- });
99
- socket .handler (decode (socket ));
100
- log .debug ("client connected {}:{}" , host , port );
101
-
102
- // 执行连接成功的Handler
103
- TunnelHandler tunnelHandler = tunnelHandlers .get (null );
104
- if (tunnelHandler != null ) {
105
- tunnelHandler .handle (vertx , socket , null );
106
- }
107
- socket .resume ();
108
- } else {
109
- Throwable e = ar .cause ();
110
- log .error ("client connect {}:{} error, after {} ms will reconnect" ,
111
- host ,
112
- port ,
113
- reconnectDelay ,
114
- e );
115
- this .netSocket = null ;
116
- reconnect (host , port );
117
- }
118
- };
119
- netClient .connect (port , host ).onComplete (asyncResultHandler );
83
+ netClient .connect (port , host ).onComplete (ar -> handleConnectCompleteAsyncResult (ar , host , port ));
120
84
}
121
85
122
86
/**
@@ -133,6 +97,50 @@ public void emit(final TunnelMessageType type, final byte[] bodyBytes) {
133
97
}
134
98
}
135
99
100
+ /**
101
+ * client完成连接后的业务逻辑
102
+ *
103
+ * @param ar 完成结果
104
+ * @param host 连接主机地址
105
+ * @param port 连接端口
106
+ */
107
+ protected void handleConnectCompleteAsyncResult (final AsyncResult <NetSocket > ar ,
108
+ final String host , final int port ) {
109
+ if (ar .succeeded ()) {
110
+ setReconnectDelay (this .minDelay );
111
+
112
+ NetSocket socket = ar .result ();
113
+ this .netSocket = socket ;
114
+ socket .pause ();
115
+ socket .closeHandler (v -> {
116
+ log .warn ("closed {} -- {}, after {} ms will reconnect" ,
117
+ socket .localAddress (),
118
+ socket .remoteAddress (),
119
+ reconnectDelay );
120
+ this .netSocket = null ;
121
+ reconnect (host , port );
122
+ });
123
+ socket .handler (decode (socket ));
124
+ log .debug ("client connected {}:{}" , host , port );
125
+
126
+ // 执行连接成功的Handler
127
+ TunnelHandler tunnelHandler = tunnelHandlers .get (null );
128
+ if (tunnelHandler != null ) {
129
+ tunnelHandler .handle (vertx , socket , null );
130
+ }
131
+ socket .resume ();
132
+ } else {
133
+ Throwable e = ar .cause ();
134
+ log .error ("client connect {}:{} error, after {} ms will reconnect" ,
135
+ host ,
136
+ port ,
137
+ reconnectDelay ,
138
+ e );
139
+ this .netSocket = null ;
140
+ reconnect (host , port );
141
+ }
142
+ }
143
+
136
144
protected void setReconnectDelay (final long delay ) {
137
145
this .reconnectDelay = delay ;
138
146
}
0 commit comments