Beispiel #1
0
    public ReceiveData Decode(BufferCache cache)
    {
        if (cache.Length < sizeof(Int16) * 2)
        {
            return(null);
        }
        int startPtr      = cache.ReadPtr;
        int packageLength = cache.ReadUInt16();

#if net_log
        Debug.Log("decode read first 2 byte packageLength:" + packageLength);
#endif
        if (packageLength > Transporter.MAX_PACKAGE_LENGTH)
        {
            Debug.Log("Transporter receive package length over max length :" + packageLength);
            cache.ReadPtr = startPtr;
            return(null);
        }
        if (cache.Length < packageLength)
        {
            Debug.Log("decode packageLenght over cache length packageLength:" + packageLength + "cache.Length:" + cache.Length);
            cache.ReadPtr = startPtr;
            return(null);
        }
        UInt16 typeId = cache.ReadUInt16();
#if net_log
        Debug.Log("decode typeId:" + typeId);
#endif
        Type packageType = ClientProtocol.GetTypeById(typeId);
        if (packageType == null)
        {
            Debug.Log("Transporter receive package can not find id" + typeId);
            return(null);
        }
        int packageBodyLength = packageLength - 2 - 4;
        cache.ReadUInt16();
        cache.ReadUInt16();
        ReceiveData data = new ReceiveData();
        data.MsgId     = typeId;
        data.MsgObject = Activator.CreateInstance(packageType);
        MemoryStream mStream = cache.GetMemoryStream(packageBodyLength);
        RuntimeTypeModel.Default.Deserialize(mStream, data.MsgObject, packageType);
        return(data);
    }