org.tio.core.GroupContext Java Examples

The following examples show how to use org.tio.core.GroupContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TioRpcServerEndpoint.java    From nutzcloud with Apache License 2.0 6 votes vote down vote up
@Override
public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) {
    LiteRpcPacket rpcPacket = (LiteRpcPacket) packet;
    // 理论上只会编码resp对象吧
    if (rpcPacket.opType == OP_RPC_RESP) {
        int bodySize = 2 + 16;
        if (rpcPacket.body != null) {
            bodySize += rpcPacket.body.length;
        }
        ByteBuffer buffer = ByteBuffer.allocate(HEADER_LENGHT + bodySize);
        buffer.order(groupContext.getByteOrder());
        buffer.putInt(bodySize);
        buffer.put(VERSION);
        buffer.put(OP_RPC_RESP);
        buffer.putLong(rpcPacket.uuidMost);
        buffer.putLong(rpcPacket.uuidLeast);
        if (rpcPacket.body != null)
            buffer.put(rpcPacket.body);
        return buffer;
    }
    return null;
}
 
Example #2
Source File: AbstractAioHandler.java    From md_blockchain with Apache License 2.0 5 votes vote down vote up
/**
 * 编码:把业务消息包编码为可以发送的ByteBuffer
 * 消息头:type + bodyLength
 * 消息体:byte[]
 */
@Override
public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) {
    BlockPacket showcasePacket = (BlockPacket) packet;
    byte[] body = showcasePacket.getBody();
    int bodyLen = 0;
    if (body != null) {
        bodyLen = body.length;
    }

    //总长度是消息头的长度+消息体的长度
    int allLen = BlockPacket.HEADER_LENGTH + bodyLen;

    ByteBuffer buffer = ByteBuffer.allocate(allLen);
    buffer.order(groupContext.getByteOrder());

    //写入消息类型
    buffer.put(showcasePacket.getType());
    //写入消息体长度
    buffer.putInt(bodyLen);

    //写入消息体
    if (body != null) {
        buffer.put(body);
    }
    return buffer;
}
 
Example #3
Source File: BaseAioHandler.java    From blockchain with Apache License 2.0 5 votes vote down vote up
/**
 * 编码:把业务消息包编码为可以发送的ByteBuffer
 * 总的消息结构:消息头 + 消息类别 + 消息体
 * 消息头结构:    4个字节,存储消息体的长度
 * 消息类别: 1 个字节, 存储类别,S => 字符串, B => 区块, T => 交易
 * 消息体结构:   对象的json串的byte[]
 */
public ByteBuffer encode(Packet packet, GroupContext groupContext, ChannelContext channelContext) {

	MessagePacket messagePacket = (MessagePacket) packet;
	byte[] body = messagePacket.getBody();
	int bodyLen = 0;
	if (body != null) {
		bodyLen = body.length;
	}

	//bytebuffer的总长度是 = 消息头的长度 + 消息体的长度
	int allLen = MessagePacket.HEADER_LENGTH + bodyLen;
	//创建一个新的bytebuffer
	ByteBuffer buffer = ByteBuffer.allocate(allLen);
	//设置字节序
	buffer.order(groupContext.getByteOrder());

	//写入消息类型
	buffer.put(messagePacket.getType());
	//写入消息头----消息头的内容就是消息体的长度
	buffer.putInt(bodyLen);

	//写入消息体
	if (body != null) {
		buffer.put(body);
	}
	return buffer;
}
 
Example #4
Source File: SpringBootIpStatListener.java    From tio-starters with MIT License 2 votes vote down vote up
@Override
public void onExpired(GroupContext groupContext, IpStat ipStat) {

}