lizhanwei
2020-02-17 e549203e1132e151b3fc83a76cbd8b54d1efcd35
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package safeluck.drive.evaluation.im;
 
import android.util.Log;
 
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;
 
/**
 * MyApplication2
 * Created by lzw on 2019/12/12. 16:14:33
 * 邮箱:632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class MessageProcessor implements IMessageProcessor {
 
    private static final String TAG = MessageProcessor.class.getSimpleName();
 
    private MessageProcessor(){
 
    }
 
    private static class MessageProcessorInstance{
        private static final IMessageProcessor INSTANCE = new MessageProcessor();
    }
 
    public static IMessageProcessor getInstance(){
        return MessageProcessorInstance.INSTANCE;
    }
 
    @Override
    public void receiveMsg(byte[] message) {
        byte[] datas = Utils.parseMsg(message);
        Log.i(TAG, "receiveMsg: "+ ByteUtil.byte2HexStr(datas));
        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 byte[] msg) {
        CThreadPoolExecutor.runInBackground(new Runnable() {
            @Override
            public void run() {
                if (IMSClientBootstrap.getInstance().isActive()){
 
                    IMSClientBootstrap.getInstance().sendMessage(msg);
                }else{
                    Log.e(TAG, "run: 发送消息失败,未初始化连接NettyTcp");
                }
            }
        });
    }
}