应用层添加测试注册消息;im_lib添加注册消息;在tcp连接成功时发注册消息(测试)
8个文件已修改
82 ■■■■ 已修改文件
app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/fragment/TcpFragment.java 5 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
app/src/main/java/safeluck/drive/evaluation/im/IMSEventListener.java 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
im_lib/src/main/java/com/anyun/im_lib/listener/OnEventListener.java 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
im_lib/src/main/java/com/anyun/im_lib/netty/NettyTcpClient.java 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
im_lib/src/main/java/com/anyun/im_lib/netty/TCPChannelInitializerHandler.java 18 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
im_lib/src/main/java/com/anyun/im_lib/util/ByteUtil.java 26 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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;
    }
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;
        }
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();
    }
}
im_lib/src/main/java/com/anyun/im_lib/interf/IMSClientInteface.java
@@ -110,5 +110,6 @@
    MsgTimeOutTimerManager getMsgTimeOutTimerManager();
//获取注册消息
    byte[] getRegisterMessage();
}
im_lib/src/main/java/com/anyun/im_lib/listener/OnEventListener.java
@@ -57,4 +57,6 @@
    int getResendInterval();
    int getReConnectInterval();
//注册消息
    byte[] getRegisterMessage();
}
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;
    }
    /**
     * 重连任务
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提供的自定义长度解码器,解决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));
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)));
    }
}