From 82954841147ed6e258fefcff926697885ef65d7b Mon Sep 17 00:00:00 2001 From: endian11 <Dana_Lee1016@126.com> Date: 星期五, 13 十二月 2019 14:28:11 +0800 Subject: [PATCH] 目前可以走通连接,和发送数据;需要关注ByteBuf是否需要手动释放,什么时候释放; 修改了google json array解析数据 --- im_lib/src/main/java/com/anyun/im_lib/LoginAuthRespHandler.java | 2 im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java | 39 +++++++++++++++++++ im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java | 6 +- im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java | 14 +++++- im_lib/src/main/java/com/anyun/im_lib/ExecutorServiceFactory.java | 5 ++ im_lib/src/main/java/com/anyun/im_lib/netty/TCPReadHandler.java | 18 ++++++++- 6 files changed, 75 insertions(+), 9 deletions(-) diff --git a/im_lib/src/main/java/com/anyun/im_lib/ExecutorServiceFactory.java b/im_lib/src/main/java/com/anyun/im_lib/ExecutorServiceFactory.java index 410c052..37e9ede 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/ExecutorServiceFactory.java +++ b/im_lib/src/main/java/com/anyun/im_lib/ExecutorServiceFactory.java @@ -1,5 +1,7 @@ package com.anyun.im_lib; +import android.util.Log; + import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -10,6 +12,8 @@ * All Rights Saved! Chongqing AnYun Tech co. LTD */ public class ExecutorServiceFactory { + + private static final String TAG = ExecutorServiceFactory.class.getSimpleName(); /*** 绠$悊绾跨▼缁勶紝璐熻矗閲嶈繛***/ private ExecutorService bossPool; @@ -38,6 +42,7 @@ if (bossPool == null){ initBossLoopGroup(); } + Log.i(TAG, "execBossTask"); bossPool.execute(runnable); } /** diff --git a/im_lib/src/main/java/com/anyun/im_lib/LoginAuthRespHandler.java b/im_lib/src/main/java/com/anyun/im_lib/LoginAuthRespHandler.java index 6ebb52d..e0c0425 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/LoginAuthRespHandler.java +++ b/im_lib/src/main/java/com/anyun/im_lib/LoginAuthRespHandler.java @@ -36,7 +36,7 @@ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); - Log.i(TAG, "channelRead"+(String)msg); +// Log.i(TAG, "channelRead"+(String)msg); // TODO: 2019/12/4 } } diff --git a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java index 3867e0d..4306b86 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java +++ b/im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java @@ -12,10 +12,13 @@ import com.anyun.im_lib.listener.IMSConnectStatusCallback; import com.anyun.im_lib.listener.OnEventListener; +import java.nio.ByteBuffer; import java.util.Vector; import java.util.function.ToDoubleBiFunction; import io.netty.bootstrap.Bootstrap; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufAllocator; import io.netty.channel.Channel; import io.netty.channel.ChannelOption; import io.netty.channel.EventLoopGroup; @@ -176,13 +179,14 @@ try { channel = bootstrap.connect(currentHost,currentPort).sync().channel(); } catch (InterruptedException e) { + Log.i(TAG, String.format("杩炴帴Server(ip[%s],port[%d]澶辫触)",currentHost,currentPort)); try { Thread.sleep(500); } catch (InterruptedException ex) { ex.printStackTrace(); } - Log.i(TAG, String.format("杩炴帴Server(ip[%s],port[%d]澶辫触)",currentHost,currentPort)); + channel = null; } } @@ -318,7 +322,9 @@ Log.i(TAG, "sendMsg fail,channel涓虹┖"+msg); } try { - channel.writeAndFlush(msg); + ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(); + byteBuf.writeBytes(msg.getBytes()); + channel.writeAndFlush(byteBuf); } catch (Exception e) { Log.i(TAG, "鍙戦�佹秷鎭け璐ワ紝reason="+e.getMessage()+"\t"+msg); } @@ -388,6 +394,7 @@ @Override public void run() { + Log.i(TAG, "鎵ц閲嶈繛浠诲姟"); if (!isFirst){ onConnectStatusCallback(IMSConfig.CONNECT_STATE_FAILURE); } @@ -458,7 +465,7 @@ return IMSConfig.CONNECT_STATE_FAILURE; } String[] address = serverUrl.split(" "); - for (int j = 0; j < IMSConfig.DEFAULT_RECONNECT_COUNT; j++) { + for (int j = 1; j < IMSConfig.DEFAULT_RECONNECT_COUNT; j++) { //濡傛灉ims宸茬粡鍏抽棴锛屾垨缃戠粶涓嶅彲鐢紝鐩存帴鍥炶皟杩炴帴鐘舵�侊紝涓嶅啀杩涜杩炴帴 if (isClosed || !isNetworkAvaliable()){ return IMSConfig.CONNECT_STATE_FAILURE; @@ -471,6 +478,7 @@ try { currentPort = Integer.parseInt(address[1]); currentHost = address[0]; + Log.i(TAG, String.format("瑙f瀽鍦板潃[%s],port[%d]",currentHost,currentPort)); //杩炴帴鏈嶅姟鍣� toServer(); //channel涓嶄负绌猴紝鍒欒涓鸿繛鎺ュ凡缁忔垚鍔� diff --git a/im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java b/im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java index 721707e..5663664 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java +++ b/im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java @@ -50,13 +50,13 @@ // 涓轰簡鏇村姞娓呮鐨勮鏄庝竴涓嬩笂闈㈢殑瑙勫垯锛岃皟鏁翠竴涓嬩緥瀛愪腑鐨勪唬鐮併�傚湪鍐欏叆閫氶亾鍓嶏紝鍦ㄦ暟鎹� // pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,3 ,4,0,13)); - pipeline.addLast(new FixedLengthFrameDecoder(31)); // 娴嬭瘯鐢� 鍥哄畾闀垮害娑堟伅 + pipeline.addLast(new FixedLengthFrameDecoder(10)); // 娴嬭瘯鐢� 鍥哄畾闀垮害娑堟伅 //鎻℃墜璁よ瘉娑堟伅鐩稿簲澶勭悊handler - pipeline.addLast(LoginAuthRespHandler.class.getSimpleName(), new LoginAuthRespHandler(imsClient)); +// pipeline.addLast(LoginAuthRespHandler.class.getSimpleName(), new LoginAuthRespHandler(imsClient)); //蹇冭烦娑堟伅鍝嶅簲澶勭悊handler - pipeline.addLast(HeartbeatRespHandler.class.getSimpleName(), new HeartbeatRespHandler(imsClient)); +// pipeline.addLast(HeartbeatRespHandler.class.getSimpleName(), new HeartbeatRespHandler(imsClient)); //鎺ユ敹娑堟伅澶勭悊handler pipeline.addLast(TCPReadHandler.class.getSimpleName(),new TCPReadHandler(imsClient)); diff --git a/im_lib/src/main/java/com/anyun/im_lib/netty/TCPReadHandler.java b/im_lib/src/main/java/com/anyun/im_lib/netty/TCPReadHandler.java index 768ca32..2a7fe43 100644 --- a/im_lib/src/main/java/com/anyun/im_lib/netty/TCPReadHandler.java +++ b/im_lib/src/main/java/com/anyun/im_lib/netty/TCPReadHandler.java @@ -3,7 +3,12 @@ import android.util.Log; import com.anyun.im_lib.interf.IMSClientInteface; +import com.anyun.im_lib.util.ByteUtil; +import java.nio.ByteBuffer; +import java.nio.charset.Charset; + +import io.netty.buffer.ByteBuf; import io.netty.channel.Channel; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; @@ -45,6 +50,7 @@ Channel channel = ctx.channel(); if (channel != null){ channel.close(); + ctx.close(); } //瑙﹀彂閲嶈繛 imsClient.resetConnect(false); @@ -54,13 +60,21 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { super.channelRead(ctx, msg); // TODO: 2019/12/4 - Log.i(TAG, "channelRead: "); - imsClient.getMsgDispatcher().receivedMsg((String)msg); + //鏈嶅姟绔繑鍥炴秷鎭悗 + ByteBuf buf = (ByteBuf) msg; + byte[] req = new byte[buf.readableBytes()]; + buf.readBytes(req); + String str = new String(req, "UTF-8"); + System.out.println("鏈嶅姟绔暟鎹负 :" + str); + + Log.i(TAG, "channelRead: "+ str); + imsClient.getMsgDispatcher().receivedMsg( str ); } @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { super.channelReadComplete(ctx); Log.i(TAG, "channelReadComplete"); + } } diff --git a/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java b/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java new file mode 100644 index 0000000..b6b7989 --- /dev/null +++ b/im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java @@ -0,0 +1,39 @@ +package com.anyun.im_lib.util; + +import java.nio.ByteBuffer; +import java.nio.CharBuffer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetDecoder; + +/** + * MyApplication2 + * Created by lzw on 2019/12/13. 12:01:18 + * 閭锛�632393724@qq.com + * All Rights Saved! Chongqing AnYun Tech co. LTD + */ +public class ByteUtil { + /** + * ByteBuffer 杞崲 String + * @param buffer + * @return + */ + public static String getString(ByteBuffer buffer) + { + Charset charset = null; + CharsetDecoder decoder = null; + CharBuffer charBuffer = null; + try + { + charset = Charset.forName("UTF-8"); + decoder = charset.newDecoder(); + charBuffer = decoder.decode(buffer);//鐢ㄨ繖涓殑璇濓紝鍙兘杈撳嚭鏉ヤ竴娆$粨鏋滐紝绗簩娆℃樉绀轰负绌� +// charBuffer = decoder.decode(buffer.asReadOnlyBuffer()); + return charBuffer.toString(); + } + catch (Exception ex) + { + ex.printStackTrace(); + return ""; + } + } +} -- Gitblit v1.8.0