vince
2024-04-26 b1c572949997a5d82d9b609163ff280a1c49627d
src/main/java/com/fzzy/protocol/wujia/client/ClientEngine.java
@@ -16,7 +16,7 @@
import java.util.Map;
/**
 * 通讯协议
 * 游仙主库,通讯协议
 */
@Slf4j
public class ClientEngine implements Runnable {
@@ -29,7 +29,7 @@
    private String host;
    private int port;
    private Channel defaultChannel;
    public static Channel defaultChannel;
    public ClientEngine(String host, int port) {
@@ -45,6 +45,7 @@
    @Override
    public void run() {
        try {
            startClient();
        } catch (Exception e) {
            e.printStackTrace();
@@ -52,7 +53,10 @@
    }
    public void startClient() throws Exception {
        if(defaultChannel != null){
            log.info("-----IP={},连接存在,直接使用",host);
            return;
        }
        EventLoopGroup group = new OioEventLoopGroup();
        Bootstrap b = new Bootstrap();
        //默认长连接
@@ -82,9 +86,9 @@
            @Override
            public void operationComplete(ChannelFuture arg0) throws Exception {
                if (channelFuture.isSuccess()) {
                    log.info("-----IP={},连接成功", host);
                    log.info("-----IP={},连接成功",host);
                } else {
                    log.info("-----IP={},连接失败,自动关闭线程", host);
                    log.info("-----IP={},连接失败,自动关闭线程",host);
                    channelFuture.cause().printStackTrace();
                    group.shutdownGracefully(); // 关闭线程组
                }
@@ -107,11 +111,11 @@
    }
    public Channel getChannel() {
    public static Channel getChannel() {
        return defaultChannel;
    }
    public static void add2ChannelMap(String key, Channel channel) {
    public  void add2ChannelMap(String key, Channel channel) {
        clientChannelMap.put(key, channel);
    }
@@ -121,18 +125,19 @@
     * @param key
     * @return
     */
    public static Channel getChannel(String key) {
    public static  Channel getChannel(String key) {
        Channel channel = clientChannelMap.get(key);
        if (null == channel) return null;
        if (channel.isActive()) {
            return channel;
        } else {
            channel.close();
            defaultChannel = null;
        }
        return null;
    }
    public static InvokeResult send2(String hex, Channel channel) throws InterruptedException {
    public static InvokeResult send2(String hex,Channel channel) throws InterruptedException {
        if (null == channel) {
            return InvokeResult.SOCKET_NOT_CREATE;
        }