11package org .tron .core .net ;
22
3- import io .netty .bootstrap .Bootstrap ;
4- import io .netty .channel .Channel ;
5- import io .netty .channel .ChannelInitializer ;
6- import io .netty .channel .ChannelOption ;
7- import io .netty .channel .DefaultMessageSizeEstimator ;
8- import io .netty .channel .FixedRecvByteBufAllocator ;
9- import io .netty .channel .nio .NioEventLoopGroup ;
10- import io .netty .channel .socket .nio .NioSocketChannel ;
11- import io .netty .handler .codec .ByteToMessageDecoder ;
12- import io .netty .handler .codec .protobuf .ProtobufVarint32FrameDecoder ;
13- import io .netty .handler .codec .protobuf .ProtobufVarint32LengthFieldPrepender ;
14- import io .netty .handler .timeout .ReadTimeoutHandler ;
15- import io .netty .handler .timeout .WriteTimeoutHandler ;
16- import java .io .File ;
173import java .io .IOException ;
18- import java .util .Collection ;
194import java .util .concurrent .ExecutorService ;
205import java .util .concurrent .Executors ;
21- import java .util .concurrent .TimeUnit ;
226import lombok .extern .slf4j .Slf4j ;
237import org .junit .AfterClass ;
248import org .junit .Assert ;
2913import org .tron .common .application .ApplicationFactory ;
3014import org .tron .common .application .TronApplicationContext ;
3115import org .tron .common .parameter .CommonParameter ;
32- import org .tron .common .utils .FileUtil ;
3316import org .tron .common .utils .PublicMethod ;
34- import org .tron .common .utils .ReflectUtils ;
3517import org .tron .core .Constant ;
3618import org .tron .core .config .DefaultConfig ;
3719import org .tron .core .config .args .Args ;
38- import org .tron .core .net .peer .PeerConnection ;
3920
4021@ Slf4j
4122public class BaseNet {
@@ -53,30 +34,6 @@ public class BaseNet {
5334
5435 private static ExecutorService executorService = Executors .newFixedThreadPool (1 );
5536
56- public static Channel connect (ByteToMessageDecoder decoder ) throws InterruptedException {
57- NioEventLoopGroup group = new NioEventLoopGroup (1 );
58- Bootstrap b = new Bootstrap ();
59- b .group (group ).channel (NioSocketChannel .class )
60- .handler (new ChannelInitializer <Channel >() {
61- @ Override
62- protected void initChannel (Channel ch ) throws Exception {
63- ch .config ().setRecvByteBufAllocator (new FixedRecvByteBufAllocator (256 * 1024 ));
64- ch .config ().setOption (ChannelOption .SO_RCVBUF , 256 * 1024 );
65- ch .config ().setOption (ChannelOption .SO_BACKLOG , 1024 );
66- ch .pipeline ()
67- .addLast ("readTimeoutHandler" , new ReadTimeoutHandler (600 , TimeUnit .SECONDS ))
68- .addLast ("writeTimeoutHandler" , new WriteTimeoutHandler (600 , TimeUnit .SECONDS ));
69- ch .pipeline ().addLast ("protoPender" , new ProtobufVarint32LengthFieldPrepender ());
70- ch .pipeline ().addLast ("lengthDecode" , new ProtobufVarint32FrameDecoder ());
71- ch .pipeline ().addLast ("handshakeHandler" , decoder );
72- ch .closeFuture ();
73- }
74- }).option (ChannelOption .SO_KEEPALIVE , true )
75- .option (ChannelOption .CONNECT_TIMEOUT_MILLIS , 60000 )
76- .option (ChannelOption .MESSAGE_SIZE_ESTIMATOR , DefaultMessageSizeEstimator .DEFAULT );
77- return b .connect (Constant .LOCAL_HOST , port ).sync ().channel ();
78- }
79-
8037 @ BeforeClass
8138 public static void init () throws Exception {
8239 executorService .execute (() -> {
@@ -123,11 +80,6 @@ public static void init() throws Exception {
12380
12481 @ AfterClass
12582 public static void destroy () {
126- Collection <PeerConnection > peerConnections = ReflectUtils
127- .invokeMethod (tronNetDelegate , "getActivePeer" );
128- for (PeerConnection peer : peerConnections ) {
129- peer .getChannel ().close ();
130- }
13183 Args .clearParam ();
13284 context .destroy ();
13385 }
0 commit comments