|
| 1 | +package io.github.cloudintheking.jt809.client; |
| 2 | + |
| 3 | +import io.github.cloudintheking.jt809.client.handler.HeartBeatHandler; |
| 4 | +import io.github.cloudintheking.jt809.client.handler.LoginResponseHandler; |
| 5 | +import io.github.cloudintheking.jt809.codec.MessageDecoder; |
| 6 | +import io.github.cloudintheking.jt809.codec.MessageEncoder; |
| 7 | +import io.netty.bootstrap.Bootstrap; |
| 8 | +import io.netty.channel.Channel; |
| 9 | +import io.netty.channel.ChannelFuture; |
| 10 | +import io.netty.channel.ChannelInitializer; |
| 11 | +import io.netty.channel.ChannelOption; |
| 12 | +import io.netty.channel.nio.NioEventLoopGroup; |
| 13 | +import io.netty.channel.socket.SocketChannel; |
| 14 | +import io.netty.channel.socket.nio.NioSocketChannel; |
| 15 | +import io.netty.handler.logging.LogLevel; |
| 16 | +import io.netty.handler.logging.LoggingHandler; |
| 17 | +import lombok.Data; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import java.net.InetSocketAddress; |
| 22 | + |
| 23 | + |
| 24 | +@Data |
| 25 | +public class TcpClient809 { |
| 26 | + private Logger LOG = LoggerFactory.getLogger(getClass()); |
| 27 | + private static final int DEFAULT_PORT = 9000; |
| 28 | + |
| 29 | + private long connectTimeoutMillis = 3000; |
| 30 | + |
| 31 | + private int port = DEFAULT_PORT; |
| 32 | + |
| 33 | + private boolean tcpNoDelay = false; |
| 34 | + |
| 35 | + private boolean reuseAddress = true; |
| 36 | + |
| 37 | + private boolean keepAlive = true; |
| 38 | + |
| 39 | + private int workerCount = 4; |
| 40 | + |
| 41 | + public static TcpClient809 INSTANCE = new TcpClient809(); |
| 42 | + |
| 43 | + public Channel getChannel(String address, int port) { |
| 44 | + NioEventLoopGroup workerGroup = new NioEventLoopGroup(); |
| 45 | + Bootstrap bootstrap = new Bootstrap(); |
| 46 | + bootstrap.group(workerGroup) |
| 47 | + .channel(NioSocketChannel.class) |
| 48 | + .option(ChannelOption.TCP_NODELAY, tcpNoDelay) |
| 49 | + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeoutMillis) |
| 50 | + .option(ChannelOption.SO_REUSEADDR, reuseAddress) |
| 51 | + .option(ChannelOption.SO_KEEPALIVE, keepAlive) |
| 52 | + .option(ChannelOption.WRITE_BUFFER_HIGH_WATER_MARK, 64 * 1024) |
| 53 | + .option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 32 * 1024) |
| 54 | + .handler(new ChannelInitializer<SocketChannel>() { |
| 55 | + @Override |
| 56 | + protected void initChannel(SocketChannel socketChannel) throws Exception { |
| 57 | + socketChannel.pipeline().addLast(new LoggingHandler(LogLevel.ERROR)); |
| 58 | + socketChannel.pipeline().addLast(new HeartBeatHandler());//心跳发送包处理handler |
| 59 | + socketChannel.pipeline().addLast(new MessageDecoder());//解码 |
| 60 | + socketChannel.pipeline().addLast(MessageEncoder.INSTANCE);//编码 |
| 61 | + socketChannel.pipeline().addLast(new LoginResponseHandler());//反馈数据处理 |
| 62 | + } |
| 63 | + }); |
| 64 | + ChannelFuture future = bootstrap.connect(new InetSocketAddress(address, port)); |
| 65 | + future.awaitUninterruptibly(); |
| 66 | + if (future.isSuccess()) { |
| 67 | + return future.channel(); |
| 68 | + } else { |
| 69 | + return null; |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments