Search in sources :

Example 6 with Serialization

use of com.weibo.api.motan.codec.Serialization in project motan by weibocom.

the class DefaultRpcCodec method decode.

/**
     * decode data
     * 
     * <pre>
	 * 		对于client端:主要是来自server端的response or exception
	 * 		对于server端: 主要是来自client端的request
	 * </pre>
     * 
     * @param data
     * @return
     * @throws IOException
     */
@Override
public Object decode(Channel channel, String remoteIp, byte[] data) throws IOException {
    if (data.length <= RpcProtocolVersion.VERSION_1.getHeaderLength()) {
        throw new MotanFrameworkException("decode error: format problem", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    short type = ByteUtil.bytes2short(data, 0);
    if (type != MAGIC) {
        throw new MotanFrameworkException("decode error: magic error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    if (data[2] != RpcProtocolVersion.VERSION_1.getVersion()) {
        throw new MotanFrameworkException("decode error: version error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    int bodyLength = ByteUtil.bytes2int(data, 12);
    if (RpcProtocolVersion.VERSION_1.getHeaderLength() + bodyLength != data.length) {
        throw new MotanFrameworkException("decode error: content length error", MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    }
    byte flag = data[3];
    byte dataType = (byte) (flag & MASK);
    boolean isResponse = (dataType != MotanConstants.FLAG_REQUEST);
    byte[] body = new byte[bodyLength];
    System.arraycopy(data, RpcProtocolVersion.VERSION_1.getHeaderLength(), body, 0, bodyLength);
    long requestId = ByteUtil.bytes2long(data, 4);
    Serialization serialization = ExtensionLoader.getExtensionLoader(Serialization.class).getExtension(channel.getUrl().getParameter(URLParamType.serialize.getName(), URLParamType.serialize.getValue()));
    try {
        if (isResponse) {
            // response
            return decodeResponse(body, dataType, requestId, serialization);
        } else {
            return decodeRequest(body, requestId, serialization);
        }
    } catch (ClassNotFoundException e) {
        throw new MotanFrameworkException("decode " + (isResponse ? "response" : "request") + " error: class not found", e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
    } catch (Exception e) {
        if (ExceptionUtil.isMotanException(e)) {
            throw (RuntimeException) e;
        } else {
            throw new MotanFrameworkException("decode error: isResponse=" + isResponse, e, MotanErrorMsgConstant.FRAMEWORK_DECODE_ERROR);
        }
    }
}
Also used : Serialization(com.weibo.api.motan.codec.Serialization) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) IOException(java.io.IOException) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException)

Aggregations

Serialization (com.weibo.api.motan.codec.Serialization)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 ObjectOutput (java.io.ObjectOutput)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1