Search in sources :

Example 1 with Cleanable

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

the class DecodeableRpcInvocation method decode.

public Object decode(Channel channel, InputStream input) throws IOException {
    ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType).deserialize(channel.getUrl(), input);
    setAttachment(Constants.DUBBO_VERSION_KEY, in.readUTF());
    setAttachment(Constants.PATH_KEY, in.readUTF());
    setAttachment(Constants.VERSION_KEY, in.readUTF());
    setMethodName(in.readUTF());
    try {
        Object[] args;
        Class<?>[] pts;
        String desc = in.readUTF();
        if (desc.length() == 0) {
            pts = DubboCodec.EMPTY_CLASS_ARRAY;
            args = DubboCodec.EMPTY_OBJECT_ARRAY;
        } else {
            pts = ReflectUtils.desc2classArray(desc);
            args = new Object[pts.length];
            for (int i = 0; i < args.length; i++) {
                try {
                    args[i] = in.readObject(pts[i]);
                } catch (Exception e) {
                    if (log.isWarnEnabled()) {
                        log.warn("Decode argument failed: " + e.getMessage(), e);
                    }
                }
            }
        }
        setParameterTypes(pts);
        Map<String, String> map = (Map<String, String>) in.readObject(Map.class);
        if (map != null && map.size() > 0) {
            Map<String, String> attachment = getAttachments();
            if (attachment == null) {
                attachment = new HashMap<String, String>();
            }
            attachment.putAll(map);
            setAttachments(attachment);
        }
        // decode argument ,may be callback
        for (int i = 0; i < args.length; i++) {
            args[i] = decodeInvocationArgument(channel, this, pts, i, args[i]);
        }
        setArguments(args);
    } catch (ClassNotFoundException e) {
        throw new IOException(StringUtils.toString("Read invocation data failed.", e));
    } finally {
        if (in instanceof Cleanable) {
            ((Cleanable) in).cleanup();
        }
    }
    return this;
}
Also used : IOException(java.io.IOException) IOException(java.io.IOException) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Cleanable(com.alibaba.dubbo.common.serialize.Cleanable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Cleanable

use of com.alibaba.dubbo.common.serialize.Cleanable 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 3 with Cleanable

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

the class TransportCodec method decode.

public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
    InputStream input = new ChannelBufferInputStream(buffer);
    ObjectInput objectInput = getSerialization(channel).deserialize(channel.getUrl(), input);
    Object object = decodeData(channel, objectInput);
    if (objectInput instanceof Cleanable) {
        ((Cleanable) objectInput).cleanup();
    }
    return object;
}
Also used : ChannelBufferInputStream(com.alibaba.dubbo.remoting.buffer.ChannelBufferInputStream) InputStream(java.io.InputStream) ChannelBufferInputStream(com.alibaba.dubbo.remoting.buffer.ChannelBufferInputStream) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) Cleanable(com.alibaba.dubbo.common.serialize.Cleanable)

Example 4 with Cleanable

use of com.alibaba.dubbo.common.serialize.Cleanable 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 5 with Cleanable

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

the class DecodeableRpcResult method decode.

public Object decode(Channel channel, InputStream input) throws IOException {
    ObjectInput in = CodecSupport.getSerialization(channel.getUrl(), serializationType).deserialize(channel.getUrl(), input);
    byte flag = in.readByte();
    switch(flag) {
        case DubboCodec.RESPONSE_NULL_VALUE:
            break;
        case DubboCodec.RESPONSE_VALUE:
            try {
                Type[] returnType = RpcUtils.getReturnTypes(invocation);
                setValue(returnType == null || returnType.length == 0 ? in.readObject() : (returnType.length == 1 ? in.readObject((Class<?>) returnType[0]) : in.readObject((Class<?>) returnType[0], returnType[1])));
            } catch (ClassNotFoundException e) {
                throw new IOException(StringUtils.toString("Read response data failed.", e));
            }
            break;
        case DubboCodec.RESPONSE_WITH_EXCEPTION:
            try {
                Object obj = in.readObject();
                if (obj instanceof Throwable == false)
                    throw new IOException("Response data error, expect Throwable, but get " + obj);
                setException((Throwable) obj);
            } catch (ClassNotFoundException e) {
                throw new IOException(StringUtils.toString("Read response data failed.", e));
            }
            break;
        default:
            throw new IOException("Unknown result flag, expect '0' '1' '2', get " + flag);
    }
    if (in instanceof Cleanable) {
        ((Cleanable) in).cleanup();
    }
    return this;
}
Also used : Type(java.lang.reflect.Type) ObjectInput(com.alibaba.dubbo.common.serialize.ObjectInput) IOException(java.io.IOException) 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