Search in sources :

Example 1 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class ProtocolV1Encoder method encode.

@Override
public void encode(ChannelHandlerContext ctx, Object msg, ByteBuf out) {
    try {
        if (msg instanceof RpcMessage) {
            RpcMessage rpcMessage = (RpcMessage) msg;
            int fullLength = ProtocolConstants.V1_HEAD_LENGTH;
            int headLength = ProtocolConstants.V1_HEAD_LENGTH;
            byte messageType = rpcMessage.getMessageType();
            out.writeBytes(ProtocolConstants.MAGIC_CODE_BYTES);
            out.writeByte(ProtocolConstants.VERSION);
            // full Length(4B) and head length(2B) will fix in the end.
            out.writerIndex(out.writerIndex() + 6);
            out.writeByte(messageType);
            out.writeByte(rpcMessage.getCodec());
            out.writeByte(rpcMessage.getCompressor());
            out.writeInt(rpcMessage.getId());
            // direct write head with zero-copy
            Map<String, String> headMap = rpcMessage.getHeadMap();
            if (headMap != null && !headMap.isEmpty()) {
                int headMapBytesLength = HeadMapSerializer.getInstance().encode(headMap, out);
                headLength += headMapBytesLength;
                fullLength += headMapBytesLength;
            }
            byte[] bodyBytes = null;
            if (messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST && messageType != ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE) {
                // heartbeat has no body
                Serializer serializer = EnhancedServiceLoader.load(Serializer.class, SerializerType.getByCode(rpcMessage.getCodec()).name());
                bodyBytes = serializer.serialize(rpcMessage.getBody());
                Compressor compressor = CompressorFactory.getCompressor(rpcMessage.getCompressor());
                bodyBytes = compressor.compress(bodyBytes);
                fullLength += bodyBytes.length;
            }
            if (bodyBytes != null) {
                out.writeBytes(bodyBytes);
            }
            // fix fullLength and headLength
            int writeIndex = out.writerIndex();
            // skip magic code(2B) + version(1B)
            out.writerIndex(writeIndex - fullLength + 3);
            out.writeInt(fullLength);
            out.writeShort(headLength);
            out.writerIndex(writeIndex);
        } else {
            throw new UnsupportedOperationException("Not support this class:" + msg.getClass());
        }
    } catch (Throwable e) {
        LOGGER.error("Encode request error!", e);
    }
}
Also used : Compressor(io.seata.core.compressor.Compressor) RpcMessage(io.seata.core.protocol.RpcMessage) Serializer(io.seata.core.serializer.Serializer)

Example 2 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class ProtocolV1Decoder method decodeFrame.

public Object decodeFrame(ByteBuf frame) {
    byte b0 = frame.readByte();
    byte b1 = frame.readByte();
    if (ProtocolConstants.MAGIC_CODE_BYTES[0] != b0 || ProtocolConstants.MAGIC_CODE_BYTES[1] != b1) {
        throw new IllegalArgumentException("Unknown magic code: " + b0 + ", " + b1);
    }
    byte version = frame.readByte();
    // TODO  check version compatible here
    int fullLength = frame.readInt();
    short headLength = frame.readShort();
    byte messageType = frame.readByte();
    byte codecType = frame.readByte();
    byte compressorType = frame.readByte();
    int requestId = frame.readInt();
    RpcMessage rpcMessage = new RpcMessage();
    rpcMessage.setCodec(codecType);
    rpcMessage.setId(requestId);
    rpcMessage.setCompressor(compressorType);
    rpcMessage.setMessageType(messageType);
    // direct read head with zero-copy
    int headMapLength = headLength - ProtocolConstants.V1_HEAD_LENGTH;
    if (headMapLength > 0) {
        Map<String, String> map = HeadMapSerializer.getInstance().decode(frame, headMapLength);
        rpcMessage.getHeadMap().putAll(map);
    }
    // read body
    if (messageType == ProtocolConstants.MSGTYPE_HEARTBEAT_REQUEST) {
        rpcMessage.setBody(HeartbeatMessage.PING);
    } else if (messageType == ProtocolConstants.MSGTYPE_HEARTBEAT_RESPONSE) {
        rpcMessage.setBody(HeartbeatMessage.PONG);
    } else {
        int bodyLength = fullLength - headLength;
        if (bodyLength > 0) {
            byte[] bs = new byte[bodyLength];
            frame.readBytes(bs);
            Compressor compressor = CompressorFactory.getCompressor(compressorType);
            bs = compressor.decompress(bs);
            Serializer serializer = EnhancedServiceLoader.load(Serializer.class, SerializerType.getByCode(rpcMessage.getCodec()).name());
            rpcMessage.setBody(serializer.deserialize(bs));
        }
    }
    return rpcMessage;
}
Also used : Compressor(io.seata.core.compressor.Compressor) RpcMessage(io.seata.core.protocol.RpcMessage) Serializer(io.seata.core.serializer.Serializer)

Example 3 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class AbstractNettyRemotingClient method sendAsyncResponse.

@Override
public void sendAsyncResponse(String serverAddress, RpcMessage rpcMessage, Object msg) {
    RpcMessage rpcMsg = buildResponseMessage(rpcMessage, msg, ProtocolConstants.MSGTYPE_RESPONSE);
    Channel channel = clientChannelManager.acquireChannel(serverAddress);
    super.sendAsync(channel, rpcMsg);
}
Also used : Channel(io.netty.channel.Channel) RpcMessage(io.seata.core.protocol.RpcMessage)

Example 4 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class AbstractNettyRemoting method buildRequestMessage.

protected RpcMessage buildRequestMessage(Object msg, byte messageType) {
    RpcMessage rpcMessage = new RpcMessage();
    rpcMessage.setId(getNextMessageId());
    rpcMessage.setMessageType(messageType);
    rpcMessage.setCodec(ProtocolConstants.CONFIGURED_CODEC);
    rpcMessage.setCompressor(ProtocolConstants.CONFIGURED_COMPRESSOR);
    rpcMessage.setBody(msg);
    return rpcMessage;
}
Also used : RpcMessage(io.seata.core.protocol.RpcMessage)

Example 5 with RpcMessage

use of io.seata.core.protocol.RpcMessage in project seata by seata.

the class AbstractNettyRemotingServer method sendSyncRequest.

@Override
public Object sendSyncRequest(String resourceId, String clientId, Object msg) throws TimeoutException {
    Channel channel = ChannelManager.getChannel(resourceId, clientId);
    if (channel == null) {
        throw new RuntimeException("rm client is not connected. dbkey:" + resourceId + ",clientId:" + clientId);
    }
    RpcMessage rpcMessage = buildRequestMessage(msg, ProtocolConstants.MSGTYPE_RESQUEST_SYNC);
    return super.sendSync(channel, rpcMessage, NettyServerConfig.getRpcRequestTimeout());
}
Also used : Channel(io.netty.channel.Channel) RpcMessage(io.seata.core.protocol.RpcMessage)

Aggregations

RpcMessage (io.seata.core.protocol.RpcMessage)16 Channel (io.netty.channel.Channel)6 NamedThreadFactory (io.seata.common.thread.NamedThreadFactory)3 HashMap (java.util.HashMap)3 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)3 DefaultPromise (io.netty.util.concurrent.DefaultPromise)2 FrameworkException (io.seata.common.exception.FrameworkException)2 Compressor (io.seata.core.compressor.Compressor)2 HeartbeatMessage (io.seata.core.protocol.HeartbeatMessage)2 MessageFuture (io.seata.core.protocol.MessageFuture)2 BranchCommitRequest (io.seata.core.protocol.transaction.BranchCommitRequest)2 Serializer (io.seata.core.serializer.Serializer)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Future (java.util.concurrent.Future)2 TimeoutException (java.util.concurrent.TimeoutException)2 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 FrameworkErrorCode (io.seata.common.exception.FrameworkErrorCode)1 EnhancedServiceLoader (io.seata.common.loader.EnhancedServiceLoader)1