From aedbfdb641cbcce7f3de35b156b804ac224a505a Mon Sep 17 00:00:00 2001
From: endian11 <Dana_Lee1016@126.com>
Date: 星期三, 18 十二月 2019 12:00:41 +0800
Subject: [PATCH] 应用层添加测试注册消息;im_lib添加注册消息;在tcp连接成功时发注册消息(测试)
---
app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java | 7 +++
im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java | 26 +++++++++++++
im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java | 18 +++++++--
app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java | 6 +-
im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java | 15 +++++++
im_lib/src/main/java/com/anyun/im_lib/listener/OnEventListener.java | 2 +
app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java | 5 ++
im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java | 3 +
8 files changed, 73 insertions(+), 9 deletions(-)
diff --git a/app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java b/app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java
index 38c2a8a..4448329 100644
--- a/app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java
+++ b/app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java
@@ -57,7 +57,7 @@
this.msg_id = msg_id;
}
- protected byte[] toBytes(){
+ public byte[] toBytes(){
byte[] desBytes = new byte[1+16+2+1+1];
int pos = 0;
@@ -80,7 +80,7 @@
System.arraycopy(msg_pro_bytes,0,desBytes,pos,msg_pro_bytes.length);
pos+=msg_pro_bytes.length;
//缁堢鎵嬫満鍙�
- byte[] phoneBytes = ByteUtil.str2Bcd("12345678");
+ byte[] phoneBytes = ByteUtil.str2Bcd("1234567890121518");
System.arraycopy(phoneBytes,0,desBytes,pos,phoneBytes.length);
pos+=phoneBytes.length;
//娑堟伅娴佹按鍙�
@@ -102,7 +102,7 @@
//鏈熬缁撴潫鏍囪瘑浣�
desBytes[pos] = MESSAGE_TAIL;
- Log.i(TAG, "鍖呴暱搴�="+pos);
+ Log.i(TAG, "鍖呴暱搴�="+(pos+1));
Log.i(TAG, "鍖呭唴瀹�: "+ByteUtil.byte2HexStr(desBytes));
return desBytes;
}
diff --git a/app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java b/app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java
index f5b9878..df4e056 100644
--- a/app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java
+++ b/app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java
@@ -10,6 +10,7 @@
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
+import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.AppCompatEditText;
@@ -47,9 +48,9 @@
private static final String TAG = TcpFragment.class.getSimpleName();
private TextInputEditText ip;
private TextInputEditText port;
+ private TextView tv_content;
private Button btn_connect;
private Button btn_send;
- private ConnectThread connectThread;
private AppCompatEditText sendEditText;
private CriteriaIViewModel workViewModel;
private int item_id=0;
@@ -73,6 +74,7 @@
ip = view.findViewById(R.id.input_ip);
port = view.findViewById(R.id.input_port);
sendEditText = view.findViewById(R.id.sendtxt);
+ tv_content = view.findViewById(R.id.content);
btn_connect = view.findViewById(R.id.btn_connect);
btn_send = view.findViewById(R.id.btn_send);
@@ -126,6 +128,7 @@
MessageProcessor.getInstance().sendMessage(sendEditText.getText().toString().trim());
+ sendEditText.getText().clear();
break;
}
diff --git a/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java b/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java
index 4461802..18a7590 100644
--- a/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java
+++ b/app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java
@@ -2,6 +2,8 @@
import com.anyun.im_lib.listener.OnEventListener;
+import safeluck.drive.evaluation.bean.RegisterMessage;
+
/**
* MyApplication2
* Created by lzw on 2019/12/12. 16:12:40
@@ -66,4 +68,9 @@
public int getReConnectInterval() {
return 0;
}
+
+ @Override
+ public byte[] getRegisterMessage() {
+ return new RegisterMessage((short) 0x802).toBytes();
+ }
}
diff --git a/im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java b/im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java
index 2f04a29..9007df3 100644
--- a/im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java
+++ b/im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java
@@ -110,5 +110,6 @@
MsgTimeOutTimerManager getMsgTimeOutTimerManager();
-
+//鑾峰彇娉ㄥ唽娑堟伅
+ byte[] getRegisterMessage();
}
diff --git a/im_lib/src/main/java/com/anyun/im_lib/listener/OnEventListener.java b/im_lib/src/main/java/com/anyun/im_lib/listener/OnEventListener.java
index be3e606..ae61ce2 100644
--- a/im_lib/src/main/java/com/anyun/im_lib/listener/OnEventListener.java
+++ b/im_lib/src/main/java/com/anyun/im_lib/listener/OnEventListener.java
@@ -57,4 +57,6 @@
int getResendInterval();
int getReConnectInterval();
+//娉ㄥ唽娑堟伅
+ byte[] getRegisterMessage();
}
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 abb179a..73332d6 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
@@ -217,6 +217,13 @@
imsConnectStatusCallback.onConnected();
}
// TODO: 2019/12/12 杩炴帴鎴愬姛 锛屽彂閫佹彙鎵嬫秷鎭紙闇�瑕佺殑璇濃�斺�旓級
+ try {
+ ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer();
+ byteBuf.writeBytes(mOnEventListener.getRegisterMessage());
+ channel.writeAndFlush(byteBuf);
+ } catch (Exception e) {
+ Log.i(TAG, "鍙戦�佹秷鎭け璐ワ紝reason="+e.getMessage()+"\t");
+ }
break;
case IMSConfig.CONNECT_STATE_FAILURE:
default:
@@ -385,6 +392,14 @@
return msgTimeOutTimerManager;
}
+ @Override
+ public byte[] getRegisterMessage() {
+ if (mOnEventListener != null){
+ return mOnEventListener.getRegisterMessage();
+ }
+ return null;
+ }
+
/**
* 閲嶈繛浠诲姟
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 9413b23..37f0d28 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
@@ -1,15 +1,18 @@
package com.anyun.im_lib.netty;
+import android.util.Log;
+
import com.anyun.im_lib.HeartbeatRespHandler;
import com.anyun.im_lib.LoginAuthRespHandler;
import com.anyun.im_lib.interf.IMSClientInteface;
+import io.netty.buffer.ByteBuf;
+import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
-import io.netty.handler.codec.FixedLengthFrameDecoder;
-import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
-import io.netty.handler.codec.LineBasedFrameDecoder;
+import io.netty.handler.codec.DelimiterBasedFrameDecoder;
+
/**
* MyApplication2
@@ -20,6 +23,8 @@
*/
public class TCPChannelInitializerHandler extends ChannelInitializer<Channel> {
+ private static final String TAG = TCPChannelInitializerHandler.class.getSimpleName();
+
private IMSClientInteface imsClient;
public TCPChannelInitializerHandler(NettyTcpClient nettyTcpClient) {
@@ -29,6 +34,7 @@
@Override
protected void initChannel(Channel channel) throws Exception {
ChannelPipeline pipeline = channel.pipeline();
+ Log.i(TAG, "initChannel: ");
//netty鎻愪緵鐨勮嚜瀹氫箟闀垮害瑙g爜鍣紝瑙e喅TP鎷嗗寘/绮樺寘闂
@@ -53,12 +59,16 @@
// 绗簲涓弬鏁颁负4锛岃〃绀烘渶缁堢殑鍙栧埌鐨勭洰鏍囨暟鎹寘锛屾姏寮冩渶鍓嶉潰鐨�4涓瓧鑺傛暟鎹紝闀垮害鍩熺殑鍊艰鎶涘純銆�
//
// 涓轰簡鏇村姞娓呮鐨勮鏄庝竴涓嬩笂闈㈢殑瑙勫垯锛岃皟鏁翠竴涓嬩緥瀛愪腑鐨勪唬鐮併�傚湪鍐欏叆閫氶亾鍓嶏紝鍦ㄦ暟鎹�
- pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,3 ,2,1,0));
+// pipeline.addLast(new LengthFieldBasedFrameDecoder(1024,3 ,2,1,0));
// pipeline.addLast(new FixedLengthFrameDecoder(10)); // 娴嬭瘯鐢� 鍥哄畾闀垮害娑堟伅
// pipeline.addLast(new LineBasedFrameDecoder(1024));
+ byte[] bytes = new byte[]{0x7e};
+ ByteBuf byteBuf = Unpooled.copiedBuffer(bytes);
+ pipeline.addLast(new DelimiterBasedFrameDecoder(1024,byteBuf));
+
//鎻℃墜璁よ瘉娑堟伅鐩稿簲澶勭悊handler
pipeline.addLast(LoginAuthRespHandler.class.getSimpleName(), new LoginAuthRespHandler(imsClient));
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
index 4b3dcf4..7c93921 100644
--- 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
@@ -180,4 +180,30 @@
return "";
}
}
+
+ /**
+ * short杞负瀛楄妭鏁扮粍
+ * @param data
+ * @return 鍖呭惈2涓瓧鑺傜殑瀛楄妭鏁扮粍
+ */
+ public static byte[] shortGetBytes(short data) {
+ byte[] bytes = new byte[2];
+ bytes[0] = (byte) (data & 0xff);
+ bytes[1] = (byte) ((data & 0xff00) >> 8);
+ return bytes;
+ }
+
+ public static byte[] shortGetByte(short data){
+ byte[] bytes = new byte[1];
+ bytes[0] = (byte)(data & 0xff);
+ return bytes;
+ }
+
+ public static void main(String[] args){
+ System.out.println(byte2HexStr(shortGetBytes((short) 65535)));
+ short b = (short) 32768;
+ b++;
+ System.out.println(byte2HexStr(shortGetBytes((short) b)));
+ }
+
}
--
Gitblit v1.8.0