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(); // } }