Search in sources :

Example 1 with ChannelBufferOutputStream

use of com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream in project dubbo by alibaba.

the class TransportCodec method encode.

public void encode(Channel channel, ChannelBuffer buffer, Object message) throws IOException {
    OutputStream output = new ChannelBufferOutputStream(buffer);
    ObjectOutput objectOutput = getSerialization(channel).serialize(channel.getUrl(), output);
    encodeData(channel, objectOutput, message);
    objectOutput.flushBuffer();
    if (objectOutput instanceof Cleanable) {
        ((Cleanable) objectOutput).cleanup();
    }
}
Also used : ChannelBufferOutputStream(com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream) ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) OutputStream(java.io.OutputStream) ChannelBufferOutputStream(com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream) Cleanable(com.alibaba.dubbo.common.serialize.Cleanable)

Example 2 with ChannelBufferOutputStream

use of com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream in project dubbo by alibaba.

the class ExchangeCodec method encodeResponse.

protected void encodeResponse(Channel channel, ChannelBuffer buffer, Response res) throws IOException {
    int savedWriteIndex = buffer.writerIndex();
    try {
        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] = serialization.getContentTypeId();
        if (res.isHeartbeat())
            header[2] |= FLAG_EVENT;
        // set response status.
        byte status = res.getStatus();
        header[3] = status;
        // set request id.
        Bytes.long2bytes(res.getId(), header, 4);
        buffer.writerIndex(savedWriteIndex + HEADER_LENGTH);
        ChannelBufferOutputStream bos = new ChannelBufferOutputStream(buffer);
        ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
        // encode response data or error message.
        if (status == Response.OK) {
            if (res.isHeartbeat()) {
                encodeHeartbeatData(channel, out, res.getResult());
            } else {
                encodeResponseData(channel, out, res.getResult());
            }
        } else
            out.writeUTF(res.getErrorMessage());
        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);
    } catch (Throwable t) {
        // clear buffer
        buffer.writerIndex(savedWriteIndex);
        // send error message to Consumer, otherwise, Consumer will wait till timeout.
        if (!res.isEvent() && res.getStatus() != Response.BAD_RESPONSE) {
            Response r = new Response(res.getId(), res.getVersion());
            r.setStatus(Response.BAD_RESPONSE);
            if (t instanceof ExceedPayloadLimitException) {
                logger.warn(t.getMessage(), t);
                try {
                    r.setErrorMessage(t.getMessage());
                    channel.send(r);
                    return;
                } catch (RemotingException e) {
                    logger.warn("Failed to send bad_response info back: " + t.getMessage() + ", cause: " + e.getMessage(), e);
                }
            } else {
                // FIXME log error message in Codec and handle in caught() of IoHanndler?
                logger.warn("Fail to encode response: " + res + ", send bad_response info instead, cause: " + t.getMessage(), t);
                try {
                    r.setErrorMessage("Failed to send response: " + res + ", cause: " + StringUtils.toString(t));
                    channel.send(r);
                    return;
                } catch (RemotingException e) {
                    logger.warn("Failed to send bad_response info back: " + res + ", cause: " + e.getMessage(), e);
                }
            }
        }
        // Rethrow exception
        if (t instanceof IOException) {
            throw (IOException) t;
        } else if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else if (t instanceof Error) {
            throw (Error) t;
        } else {
            throw new RuntimeException(t.getMessage(), t);
        }
    }
}
Also used : ChannelBufferOutputStream(com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream) ObjectOutput(com.alibaba.dubbo.common.serialize.ObjectOutput) IOException(java.io.IOException) Serialization(com.alibaba.dubbo.common.serialize.Serialization) Response(com.alibaba.dubbo.remoting.exchange.Response) RemotingException(com.alibaba.dubbo.remoting.RemotingException) ExceedPayloadLimitException(com.alibaba.dubbo.remoting.transport.ExceedPayloadLimitException) Cleanable(com.alibaba.dubbo.common.serialize.Cleanable)

Example 3 with ChannelBufferOutputStream

use of com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream 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)3 ObjectOutput (com.alibaba.dubbo.common.serialize.ObjectOutput)3 ChannelBufferOutputStream (com.alibaba.dubbo.remoting.buffer.ChannelBufferOutputStream)3 Serialization (com.alibaba.dubbo.common.serialize.Serialization)2 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 Response (com.alibaba.dubbo.remoting.exchange.Response)1 ExceedPayloadLimitException (com.alibaba.dubbo.remoting.transport.ExceedPayloadLimitException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1