ÎļþÃû´Ó igds-protocol-beibo/src/main/java/com/ld/igds/protocol/beibo/grain/client/BeiboClientEngine.java ÐÞ¸Ä |
| | |
| | | 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; |
| | |
| | | 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; |
| | | |
| | | /** |
| | | * è´åç²®æ
åè®®ï¼åæºä¸ºæå¡ç«¯ï¼å¹³å°ä¸ºå®¢æ·ç«¯ä¸»å¨è¿æ¥ |
| | |
| | | * @author czt |
| | | */ |
| | | public class BeiboClientEngine implements Runnable { |
| | | |
| | | private final InternalLogger log = InternalLoggerFactory.getInstance(this.getClass()); |
| | | |
| | | private String host; |
| | | private int port; |
| | |
| | | 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 |
| | |
| | | // åèµ·å¼æ¥è¿æ¥è¯·æ±ï¼ç»å®è¿æ¥ç«¯å£å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(); // å
³é线ç¨ç» |
| | | } |