14
14
import io .netty .bootstrap .Bootstrap ;
15
15
import io .netty .channel .*;
16
16
import io .netty .handler .ssl .SslContext ;
17
+ import io .netty .handler .ssl .SslContextBuilder ;
17
18
import io .netty .handler .ssl .util .InsecureTrustManagerFactory ;
18
19
import lombok .AccessLevel ;
19
20
import lombok .Getter ;
@@ -35,9 +36,9 @@ public final class NetworkConnection implements PacketSender {
35
36
private long connectionTrys = 0 ;
36
37
37
38
private final PacketManager packetManager = new PacketManager ();
38
- private EventLoopGroup eventLoopGroup = NetworkUtils .eventLoopGroup (4 );
39
- private Runnable task ;
39
+ private final EventLoopGroup eventLoopGroup = NetworkUtils .eventLoopGroup (4 );
40
40
41
+ private Runnable task ;
41
42
private SslContext sslContext ;
42
43
43
44
@ Override
@@ -66,8 +67,7 @@ public boolean tryConnect(boolean ssl, SimpleChannelInboundHandler<Packet> defau
66
67
{
67
68
try
68
69
{
69
- eventLoopGroup = NetworkUtils .eventLoopGroup (4 );
70
- if (ssl ) sslContext = SslContext .newClientContext (InsecureTrustManagerFactory .INSTANCE );
70
+ if (ssl ) sslContext = SslContextBuilder .forClient ().trustManager (InsecureTrustManagerFactory .INSTANCE ).build ();
71
71
72
72
Bootstrap bootstrap = new Bootstrap ()
73
73
.option (ChannelOption .AUTO_READ , true )
@@ -95,10 +95,11 @@ protected void initChannel(Channel channel) throws Exception
95
95
System .out .println ("Failed to connect... [" + connectionTrys + "]" );
96
96
System .out .println ("Error: " + ex .getMessage ());
97
97
98
- if (eventLoopGroup != null )
99
- eventLoopGroup .shutdownGracefully ();
100
-
101
- eventLoopGroup = null ;
98
+ if (this .channel != null )
99
+ {
100
+ this .channel .close ();
101
+ this .channel = null ;
102
+ }
102
103
103
104
if (cancelTask != null )
104
105
{
@@ -113,6 +114,7 @@ public boolean tryDisconnect()
113
114
{
114
115
if (channel != null )
115
116
channel .close ();
117
+
116
118
eventLoopGroup .shutdownGracefully ();
117
119
return false ;
118
120
}
0 commit comments