Search in sources :

Example 6 with Cleanable

use of com.alibaba.dubbo.common.serialize.Cleanable in project dubbo by alibaba.

the class ExchangeCodec method encodeRequest.

protected void encodeRequest(Channel channel, ChannelBuffer buffer, Request req) throws IOException {
    Serialization serialization = getSerialization(channel);
    // header.
    byte[] header = new byte[HEADER_LENGTH];
    // set magic number.
    Bytes.short2bytes(MAGIC, header);
    // set request and serialization flag.
    header[2] = (byte) (FLAG_REQUEST | serialization.getContentTypeId());
    if (req.isTwoWay())
        header[2] |= FLAG_TWOWAY;
    if (req.isEvent())
        header[2] |= FLAG_EVENT;
    // set request id.
    Bytes.long2bytes(req.getId(), header, 4);
    // encode request data.
    int savedWriteIndex = buffer.writerIndex();
    buffer.writerIndex(savedWriteIndex + HEADER_LENGTH);
    ChannelBufferOutputStream bos = new ChannelBufferOutputStream(buffer);
    ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
    if (req.isEvent()) {
        encodeEventData(channel, out, req.getData());
    } else {
        encodeRequestData(channel, out, req.getData());
    }
    out.flushBuffer();
    if (out instanceof Cleanable) {
        ((Cleanable) out).cleanup();
    }
    bos.flush();
    bos.close();
    int len = bos.writtenBytes();
    checkPayload(channel, len);
    Bytes.int2bytes(len, header, 12);
    // write
    buffer.writerIndex(savedWriteIndex);
    // write header.
    buffer.writeBytes(header);
    buffer.writerIndex(savedWriteIndex + HEADER_LENGTH + len);
}
Also used : Serialization(com.alibaba.dubbo.common.serialize.Serialization) ChannelBufferOutputStream(com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream) ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) Cleanable(com.alibaba.dubbo.common.serialize.Cleanable)

Aggregations

Cleanable (com.alibaba.dubbo.common.serialize.Cleanable)6 ObjectInput (com.alibaba.dubbo.common.serialize.ObjectInput)3 ObjectOutput (com.alibaba.dubbo.common.serialize.ObjectOutput)3 ChannelBufferOutputStream (com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream)3 IOException (java.io.IOException)3 Serialization (com.alibaba.dubbo.common.serialize.Serialization)2 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 ChannelBufferInputStream (com.alibaba.dubbo.remoting.buffer.ChannelBufferInputStream)1 Response (com.alibaba.dubbo.remoting.exchange.Response)1 ExceedPayloadLimitException (com.alibaba.dubbo.remoting.transport.ExceedPayloadLimitException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1