CZT
2023-09-27 3a97ed3e03c8969aef34d172bf46116596683bd3
igds-protocol-beibo/src/main/java/com/ld/igds/protocol/beibo/grainv1/client/BeiboClientEngine.java
ÎļþÃû´Ó igds-protocol-beibo/src/main/java/com/ld/igds/protocol/beibo/grain/client/BeiboClientEngine.java ÐÞ¸Ä
@@ -1,4 +1,4 @@
package com.ld.igds.protocol.beibo.grain.client;
package com.ld.igds.protocol.beibo.grainv1.client;
import com.ld.igds.util.BytesUtil;
import com.ld.io.api.InvokeResult;
@@ -10,6 +10,8 @@
import io.netty.channel.socket.oio.OioSocketChannel;
import io.netty.handler.codec.bytes.ByteArrayDecoder;
import io.netty.handler.codec.bytes.ByteArrayEncoder;
import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory;
/**
 * è´åšç²®æƒ…协议,分机为服务端,平台为客户端主动连接
@@ -17,6 +19,8 @@
 * @author czt
 */
public class BeiboClientEngine implements Runnable {
   private final InternalLogger log = InternalLoggerFactory.getInstance(this.getClass());
   private String host;
   private int port;
@@ -44,14 +48,14 @@
   public void startRun() throws Exception {
      EventLoopGroup group = new OioEventLoopGroup();
      Bootstrap b = new Bootstrap();
      // b.option(ChannelOption.SO_KEEPALIVE, true);
//      b.option(ChannelOption.SO_KEEPALIVE, true);
      b.group(group).channel(OioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {
               @Override
               protected void initChannel(SocketChannel ch)
                     throws Exception {
                  ChannelPipeline p = ch.pipeline();
                  // å­—符串解码 å’Œ ç¼–码
                  // å­—符串解码和编码
                  p.addLast("decoder", new ByteArrayDecoder());
                  p.addLast("encoder", new ByteArrayEncoder());
                  // è‡ªå·±çš„逻辑Handler
@@ -62,15 +66,15 @@
      // å‘起异步连接请求,绑定连接端口和host信息
      ChannelFuture channelFuture = b.connect(host, port);
      this.channel = channelFuture.channel();
      // channelFuture.channel().closeFuture().sync();
//       channelFuture.channel().closeFuture().sync();
      channelFuture.addListener(new ChannelFutureListener() {
         @Override
         public void operationComplete(ChannelFuture arg0) throws Exception {
            if (channelFuture.isSuccess()) {
               System.out.println("连接服务器成功");
               log.info("连接服务器成功");
            } else {
               System.out.println("连接服务器失败");
               log.info("连接服务器失败");
               channelFuture.cause().printStackTrace();
               group.shutdownGracefully(); // å…³é—­çº¿ç¨‹ç»„
            }