修改NettyTcp;1.增加ByteUtil方法;2,增加服务端通用消息应答;3.增加客户端通用消息应答;4.服务器端消息封装;5,所有平台消息重构放入专用包下;6.所有消息打印TAG为PlatFormMessage 7.增加客户端获取学员信息、鉴权消息 8.MessageProcessor增加解析转义、验证之后的服务消息
1个文件已删除
7个文件已修改
7个文件已添加
4 文件已重命名
| | |
| | | return null; |
| | | } |
| | | |
| | | public void sendMessage(String message){ |
| | | public void sendMessage(byte[] message){ |
| | | if (isActive){ |
| | | imsClient.sendMsg(message); |
| | | } |
| | |
| | | |
| | | import com.anyun.im_lib.listener.OnEventListener; |
| | | |
| | | import safeluck.drive.evaluation.bean.RegisterMessage; |
| | | import safeluck.drive.evaluation.platformMessage.RegisterMessage; |
| | | |
| | | /** |
| | | * MyApplication2 |
| | |
| | | */ |
| | | public interface IMessageProcessor { |
| | | void receiveMsg(byte[] message); |
| | | void sendMessage(String msg); |
| | | void sendMessage(byte[] msg); |
| | | } |
| | |
| | | |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | import safeluck.drive.evaluation.platformMessage.RegisterResp; |
| | | import safeluck.drive.evaluation.util.CThreadPoolExecutor; |
| | | import safeluck.drive.evaluation.util.Utils; |
| | | |
| | |
| | | byte checkcolde=Utils.calCheckCode(ByteUtil.subArray(datas,0,datas.length-1)); |
| | | if (checkcolde == datas[datas.length-1]){ |
| | | Log.i(TAG, "receiveMsg: 消息正确"); |
| | | parseMessage(datas); |
| | | } |
| | | } |
| | | |
| | | private void parseMessage(byte[] datas) { |
| | | byte[] msgidBytes = ByteUtil.subArray(datas,1,2); |
| | | short msgid = ByteUtil.getShort(msgidBytes); |
| | | |
| | | switch (msgid){ |
| | | case (short) 0x8100: |
| | | RegisterResp registerResp = new RegisterResp(datas); |
| | | break; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void sendMessage(final String msg) { |
| | | public void sendMessage(final byte[] msg) { |
| | | CThreadPoolExecutor.runInBackground(new Runnable() { |
| | | @Override |
| | | public void run() { |
New file |
| | |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | import android.util.Log; |
| | | |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | public class AuthMessage extends DriveExamProtocol { |
| | | |
| | | private static final String TAG = "AuthMessage"; |
| | | private static final int BODY_LENGTH = 12; |
| | | private long timeStamp; |
| | | private String hexStrPwd; |
| | | |
| | | public String getHexStrPwd() { |
| | | return hexStrPwd; |
| | | } |
| | | |
| | | public void setHexStrPwd(String hexStrPwd) { |
| | | this.hexStrPwd = hexStrPwd; |
| | | } |
| | | |
| | | /** |
| | | * 构造函数 |
| | | * |
| | | * @param msg_id 消息ID |
| | | */ |
| | | public AuthMessage(short msg_id) { |
| | | super(msg_id); |
| | | } |
| | | |
| | | @Override |
| | | protected byte[] createMessageBody() { |
| | | byte[] messageBody = new byte[BODY_LENGTH]; |
| | | int pos = 0; |
| | | timeStamp = System.currentTimeMillis(); |
| | | Log.i(TAG, "createMessageBody: timeStamp="+timeStamp); |
| | | byte[] timeStampBytes = ByteUtil.intGetBytes((int)timeStamp); |
| | | System.arraycopy(timeStampBytes,0,messageBody,pos,timeStampBytes.length); |
| | | pos+= 4; |
| | | byte [] hexPwdBytes = ByteUtil.hexStr2Bytes(hexStrPwd); |
| | | System.arraycopy(hexPwdBytes,0,messageBody,pos,hexPwdBytes.length); |
| | | return messageBody; |
| | | } |
| | | |
| | | @Override |
| | | protected short msgBodyLength() { |
| | | return BODY_LENGTH; |
| | | } |
| | | } |
New file |
| | |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | public class ClientCommonRsp extends DriveExamProtocol { |
| | | private static final int BODY_LENGTH = 5; |
| | | |
| | | private short result;//结果 |
| | | private short msgId;//对应的服务器端消息id |
| | | private short msg_serial;//服务器消息给的流水号 |
| | | |
| | | private int curPos = 0; |
| | | |
| | | /** |
| | | * 构造函数 |
| | | * |
| | | * |
| | | */ |
| | | public ClientCommonRsp() { |
| | | super((short) 0x0001); |
| | | } |
| | | |
| | | @Override |
| | | protected byte[] createMessageBody() { |
| | | byte[] messageBody = new byte[BODY_LENGTH]; |
| | | byte[] msg_serialBytes= ByteUtil.shortGetBytes(msg_serial); |
| | | byte[] msgidBytes = ByteUtil.shortGetBytes(msgId); |
| | | System.arraycopy(msg_serialBytes,0,messageBody,curPos,2); |
| | | curPos += 2; |
| | | System.arraycopy(msgidBytes,0,messageBody,curPos,2); |
| | | curPos += 2; |
| | | System.arraycopy(ByteUtil.shortGetByte(result),0,messageBody,curPos,1); |
| | | return messageBody; |
| | | } |
| | | |
| | | @Override |
| | | protected short msgBodyLength() { |
| | | return BODY_LENGTH; |
| | | } |
| | | |
| | | public short getResult() { |
| | | return result; |
| | | } |
| | | |
| | | public void setResult(short result) { |
| | | this.result = result; |
| | | } |
| | | |
| | | public short getMsgId() { |
| | | return msgId; |
| | | } |
| | | |
| | | public void setMsgId(short msgId) { |
| | | this.msgId = msgId; |
| | | } |
| | | |
| | | public short getMsg_serial() { |
| | | return msg_serial; |
| | | } |
| | | |
| | | public void setMsg_serial(short msg_serial) { |
| | | this.msg_serial = msg_serial; |
| | | } |
| | | } |
File was renamed from app/src/main/java/safeluck/drive/evaluation/bean/DriveExamProtocol.java |
| | |
| | | package safeluck.drive.evaluation.bean; |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | import android.util.Log; |
| | | |
New file |
| | |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | import android.text.TextUtils; |
| | | |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | /** |
| | | * 获取学员信息消息(发送身份证ID上去给平台) |
| | | * MyApplication2 |
| | | * Created by lzw on 2019/12/19. 18:26:35 |
| | | * 邮箱:632393724@qq.com |
| | | * All Rights Saved! Chongqing AnYun Tech co. LTD |
| | | */ |
| | | public class GainStuMessage extends DriveExamProtocol { |
| | | |
| | | private static final int BODY_LENGTH = 17; |
| | | |
| | | private String id;//卡ID |
| | | private short examaCourse = 0; |
| | | |
| | | public GainStuMessage(short msg_id) { |
| | | super(msg_id); |
| | | } |
| | | |
| | | @Override |
| | | protected short msgBodyLength() { |
| | | return BODY_LENGTH; |
| | | } |
| | | |
| | | @Override |
| | | protected byte[] createMessageBody() { |
| | | int pos = 0; |
| | | byte[] messageBody = new byte[BODY_LENGTH]; |
| | | byte[] idBytes = !TextUtils.isEmpty(id)?id.getBytes():"".getBytes(); |
| | | System.arraycopy(idBytes,0,messageBody,pos,idBytes.length); |
| | | pos+= 16; |
| | | byte[] examBytes = ByteUtil.shortGetByte(examaCourse); |
| | | System.arraycopy(examBytes,0,messageBody,pos,examBytes.length); |
| | | |
| | | return messageBody; |
| | | } |
| | | |
| | | public String getId() { |
| | | return id; |
| | | } |
| | | |
| | | public void setId(String id) { |
| | | this.id = id; |
| | | } |
| | | |
| | | public short getExamaCourse() { |
| | | return examaCourse; |
| | | } |
| | | |
| | | public void setExamaCourse(short examaCourse) { |
| | | this.examaCourse = examaCourse; |
| | | } |
| | | } |
File was renamed from app/src/main/java/safeluck/drive/evaluation/bean/KeepaliveMessage.java |
| | |
| | | package safeluck.drive.evaluation.bean; |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | import safeluck.drive.evaluation.platformMessage.DriveExamProtocol; |
| | | |
| | | /** |
| | | * 心跳消息 |
New file |
| | |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | public class PlatFormConstant { |
| | | public static final String TAG = "PlatFormMessage"; |
| | | |
| | | public static final int SUCCESS = 0; |
| | | } |
File was renamed from app/src/main/java/safeluck/drive/evaluation/bean/RegisterMessage.java |
| | |
| | | package safeluck.drive.evaluation.bean; |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | |
| | | import android.text.TextUtils; |
| | | |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | import static com.anyun.im_lib.util.ByteUtil.shortGetBytes; |
| | | import safeluck.drive.evaluation.platformMessage.DriveExamProtocol; |
| | | |
| | | /** |
| | | * 注册消息 |
New file |
| | |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | import com.anyun.exam.lib.MyLog; |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | |
| | | import safeluck.drive.evaluation.im.MessageProcessor; |
| | | |
| | | public class RegisterResp extends ServerProtocol { |
| | | private String hexpwd; |
| | | private short result; |
| | | private int currPos = 0; |
| | | |
| | | public RegisterResp(byte[] rspBytes) { |
| | | super(rspBytes); |
| | | } |
| | | |
| | | @Override |
| | | protected void parseMsgBody(byte[] msgbodyData) { |
| | | currPos += 2; |
| | | result = ByteUtil.getShort(ByteUtil.subArray(msgbodyData,currPos,1)); |
| | | if (result == PlatFormConstant.SUCCESS){ |
| | | MyLog.i(PlatFormConstant.TAG,"设备注册成功"); |
| | | AuthMessage authMessage = new AuthMessage((short) 0x101); |
| | | |
| | | currPos +=1; |
| | | hexpwd = ByteUtil.byte2HexStr(ByteUtil.subArray(msgbodyData,currPos,msgBodyLength-3)); |
| | | MyLog.i(PlatFormConstant.TAG,"des pwd:"+hexpwd); |
| | | authMessage.setHexStrPwd(hexpwd); |
| | | MessageProcessor.getInstance().sendMessage(authMessage.toBytes()); |
| | | }else{ |
| | | MyLog.i(PlatFormConstant.TAG,"设备注册失败"); |
| | | } |
| | | } |
| | | } |
New file |
| | |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | /** |
| | | * 服务端通用应答 |
| | | */ |
| | | public class ServerCommonRsp extends ServerProtocol { |
| | | |
| | | /** |
| | | * 80 |
| | | * 80 01 |
| | | * 00 05 |
| | | * 00 08 61 85 13 02 12 45 |
| | | * 30 AF |
| | | * FF |
| | | * |
| | | * 00 02 |
| | | * 01 01 |
| | | * 00 |
| | | * |
| | | * CC |
| | | * |
| | | * 出去头尾标识位0x7e后的 通用服务器应答消息 |
| | | */ |
| | | private short result; |
| | | private int currPos=0; |
| | | |
| | | private short msgId;//对应客户端的消息ID; |
| | | |
| | | private short msg_serial_num;//对应客户端给的消息流水号 |
| | | /** |
| | | * 除了头尾标示位0x7e的消息 |
| | | * @param message |
| | | */ |
| | | public ServerCommonRsp(byte[] message) { |
| | | super(message); |
| | | } |
| | | |
| | | @Override |
| | | protected void parseMsgBody(byte[] msgbodyData) { |
| | | byte[] msg_serial_numBytes = ByteUtil.subArray(msgbodyData,currPos,2); |
| | | currPos += 2; |
| | | byte[] msgIdBytes = ByteUtil.subArray(msgbodyData,currPos,2); |
| | | currPos += 2; |
| | | result = ByteUtil.getShort(ByteUtil.subArray(msgbodyData,currPos,1)); |
| | | } |
| | | |
| | | public short getResult() { |
| | | return result; |
| | | } |
| | | } |
New file |
| | |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | import com.anyun.im_lib.util.ByteUtil; |
| | | |
| | | public abstract class ServerProtocol { |
| | | /**注册应答消息为例 |
| | | * 80 版本号 |
| | | * 81 00 消息id |
| | | * 00 0B 消息体属性(里面消息体长度有用) |
| | | * 00 00 00 00 00 00 00 00 电话 |
| | | * 1E 5E 流水号 |
| | | * FF 保留字节 |
| | | * |
| | | * |
| | | * 下面是应答内容 |
| | | * 00 00 |
| | | * 00 |
| | | * 01 02 03 04 05 06 07 08 |
| | | * BD |
| | | */ |
| | | private short version ;//版本号 一个字节 |
| | | protected short msgId;//消息id 两个字节 大端 |
| | | protected short msgBodyLength; //消息体长度 |
| | | private String phone; // 8个字节的string |
| | | private short message_serial_num;//流水号,两个字节 |
| | | private byte reserved;//保留字段 一个字节 |
| | | private int pos = 0; |
| | | |
| | | public ServerProtocol(byte[] rspBytes) { |
| | | |
| | | byte[] versionByte = ByteUtil.subArray(rspBytes,pos,1); |
| | | this.version = ByteUtil.getShort(versionByte); |
| | | pos+=1;//消息ID开始的位置 |
| | | |
| | | |
| | | pos += 2;// 消息属性开始的位置 |
| | | byte[] msgProperty = ByteUtil.subArray(rspBytes,pos,2); |
| | | msgBodyLength |= (msgProperty[1]&0x3f); |
| | | pos+=2; //电话开始的字节位置 |
| | | |
| | | pos += 8;// liusshui号开始的位置 |
| | | |
| | | pos +=2;//保留字节开始的位置 |
| | | |
| | | pos+=1;//消息体第一个字节开始的位置 子类可以直接取pos位置 |
| | | |
| | | parseMsgBody(ByteUtil.subArray(rspBytes,pos,rspBytes.length-(pos))); |
| | | } |
| | | |
| | | protected abstract void parseMsgBody(byte[] msgbodyData); |
| | | } |
File was renamed from app/src/main/java/safeluck/drive/evaluation/bean/StartExamMessage.java |
| | |
| | | package safeluck.drive.evaluation.bean; |
| | | package safeluck.drive.evaluation.platformMessage; |
| | | |
| | | /** |
| | | * 给平台发送开始考试消息 |
| | |
| | | * 发送消息 |
| | | * @param msg |
| | | */ |
| | | void sendMsg(String msg); |
| | | void sendMsg(byte[] msg); |
| | | |
| | | /** |
| | | * 发送消息 |
| | |
| | | * @param msg |
| | | * @param isJoinTimeoutManager 是否加入超时管理器 |
| | | */ |
| | | void sendMsg(String msg, boolean isJoinTimeoutManager); |
| | | void sendMsg(byte[] msg, boolean isJoinTimeoutManager); |
| | | |
| | | /** |
| | | * 获取重连间隔时长 |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void sendMsg(String msg) { |
| | | public void sendMsg(byte[] msg) { |
| | | this.sendMsg(msg,true); |
| | | } |
| | | |
| | |
| | | * @param isJoinTimeoutManager 是否加入超时管理器 |
| | | */ |
| | | @Override |
| | | public void sendMsg(String msg, boolean isJoinTimeoutManager) { |
| | | public void sendMsg(byte[] msg, boolean isJoinTimeoutManager) { |
| | | if (msg==null ){ |
| | | return; |
| | | } |
| | |
| | | } |
| | | try { |
| | | ByteBuf byteBuf = ByteBufAllocator.DEFAULT.ioBuffer(); |
| | | byteBuf.writeBytes(msg.getBytes()); |
| | | byteBuf.writeBytes(msg); |
| | | channel.writeAndFlush(byteBuf); |
| | | } catch (Exception e) { |
| | | Log.i(TAG, "发送消息失败,reason="+e.getMessage()+"\t"+msg); |
| | |
| | | */ |
| | | public static byte[] hexStr2Bytes(String src) |
| | | { |
| | | src = src.replace(" ",""); |
| | | int m=0,n=0; |
| | | int l=src.length()/2; |
| | | System.out.println(l); |
| | |
| | | } |
| | | |
| | | /** |
| | | * short转为字节数组 |
| | | * short转为字节数组 大端 |
| | | * @param data |
| | | * @return 包含2个字节的字节数组 |
| | | */ |
| | |
| | | byte[] bytes = new byte[2]; |
| | | bytes[1] = (byte) (data & 0xff); |
| | | bytes[0] = (byte) ((data & 0xff00) >> 8); |
| | | return bytes; |
| | | } |
| | | |
| | | /** |
| | | * 大端 int 转成 4个字节的byte数组 |
| | | * @param data |
| | | * @return |
| | | */ |
| | | public static byte[] intGetBytes(int data) { |
| | | byte[] bytes = new byte[4]; |
| | | bytes[3] = (byte) ((data & 0xff)); |
| | | bytes[2] = (byte) ((data & 0xff00) >> 8); |
| | | bytes[1] = (byte) ((data & 0xff0000) >> 16); |
| | | bytes[0] = (byte) ((data & 0xff000000) >> 24); |
| | | return bytes; |
| | | } |
| | | |
| | |
| | | System.arraycopy(shortGetBytes((short)3),0,messageBody,22,2); |
| | | System.out.println(byte2HexStr(messageBody)); |
| | | |
| | | |
| | | byte[] bytes =new byte[]{(byte) 0x80}; |
| | | short aShort= getShort(bytes); |
| | | System.out.println(aShort); |
| | | |
| | | } |
| | | |
| | | public static byte[] subArray(byte[] srcBytes, int begin, int length) { |
| | |
| | | Log.i(TAG, "subArray: "+byte2HexStr(bytes)); |
| | | return bytes; |
| | | } |
| | | |
| | | public static short getShort(byte[] bytes) { |
| | | short s =0; |
| | | if (bytes.length==1){ |
| | | s|=(bytes[0]&0xff); |
| | | return s; |
| | | } |
| | | s =(short) ((0xff & bytes[1]) | (0xff00 & (bytes[0] << 8))); |
| | | Log.i(TAG, "getShort: "+s); |
| | | return s ; |
| | | } |
| | | } |