左晓为主开发手持机充值管理机
zuoxiao
2023-11-14 5a843ba11b991722f8f2af386f0e546e2769f7c3
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
package com.dayu.recharge.net;
 
import com.dayu.recharge.tools.HexUtil;
import com.dayu.recharge.utils.LogUtil;
import com.dayu.recharge.utils.SocketUtil;
import com.easysocket.interfaces.config.IMessageProtocol;
 
import java.nio.ByteOrder;
 
public class ScoketMessageProtocol implements IMessageProtocol {
    @Override
    public int getHeaderLength() {
        return 3;
    }
 
    @Override
    public int getBodyLength(byte[] header, ByteOrder byteOrder) {
        LogUtil.i("headerLength>>" + header.length);
        if (header == null || header.length < getHeaderLength()) {
            return 0;
        }
//        ByteBuffer bb = ByteBuffer.wrap(header);
//        bb.order(byteOrder);
 
//        String length = "";
//        try {
//            String head = new String(header);
//            LogUtil.i("getBodyLength>>" + head);
//            length = head.split("68")[1];
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
 
        int length = SocketUtil.get16to10(HexUtil.byteToHex(header[1]));
 
//        EasySocket.getInstance().getDefOptions().getMaxWriteBytes();
 
 
        return length + 2; // body的长度以int的形式写在header那里
    }
 
 
//    @Override
//    public byte[] pack(ISender sender) {
//        // 默认为utf-8 Charset.forName(EasySocket.getInstance().getOptions().getCharsetName())
//        byte[] body = new Gson().toJson(sender).getBytes(Charset.forName(EasySocket.getInstance().getOptions().getCharsetName()));
//        // 消息头的长度,指多少个byte
//        int headerLength = getHeaderLength();
//        ByteBuffer bb = ByteBuffer.allocate(headerLength + body.length);
//        bb.order(ByteOrder.BIG_ENDIAN);
//        bb.putInt(body.length); // header,保存body的length
//        bb.put(body); // body
//        return bb.array();
//    }
 
 
}