예제 #1
0
    // Use this for initialization
    void Start()
    {
        TestProtoClass test = new TestProtoClass();

        test.dependenies = new List <string>()
        {
            "123",
            "asd",
            "zxc",
            "dfg"
        };

        var bytes         = ProtoEx.Serialize(test);
        var testFromProto = ProtoEx.DeSerialize <TestProtoClass>(bytes);

        Debug.Log(testFromProto.dependenies.ConverToString());
    }
예제 #2
0
        /// <summary>
        /// 包创建工厂
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Package MakePakage <T>(int id, T value)
        {
            Package pkg = new Package();

            pkg.eventId = id;
            pkg.data    = ProtoEx.Serialize <T>(value);

            pkg.dataLength  = pkg.data.Length;
            pkg.totalLength = pkg.dataLength + idLength + headLength;

            byte[] lengthData = BitConverter.GetBytes(pkg.totalLength);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(lengthData);
            }
            byte[] eventIdData = BitConverter.GetBytes(pkg.eventId);

            pkg.totalData = lengthData.Concat(eventIdData).Concat(pkg.data);
            return(pkg);
        }
예제 #3
0
        /// <summary>
        /// 包创建工厂
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Package MakePakage <T>(int id, int channelId, T value, SendType sendType)
        {
            //防止Id溢出
            _currentRequestId = (_currentRequestId + 1) % int.MaxValue;

            Package pkg = new Package();

            pkg.eventId   = id;
            pkg.channelId = channelId;
            pkg.requestId = _currentRequestId;

            if (value != null)
            {
                pkg.data = ProtoEx.Serialize <T>(value);
            }
            else
            {
                pkg.data = new byte[0];
            }

            pkg.dataLength  = pkg.data.Length;
            pkg.totalLength =
                pkg.dataLength + idLength + headLength + channelIdLength + requestIdLength;

            byte[] lengthData = BitConverter.GetBytes(pkg.totalLength);

            if (BitConverter.IsLittleEndian && sendType == SendType.TCP)
            {
                Array.Reverse(lengthData);
            }

            byte[] eventIdData   = BitConverter.GetBytes(pkg.eventId);
            byte[] channelIdData = BitConverter.GetBytes(pkg.channelId);
            byte[] requestIdData = BitConverter.GetBytes(pkg.requestId);

            pkg.totalData = lengthData.Concat(eventIdData).Concat(channelIdData).Concat(requestIdData).Concat(pkg.data);
            return(pkg);
        }
예제 #4
0
    public byte[] Json2Proto <T>(string json)
    {
        var obj = JsonMapper.ToObject <T>(json);

        return(ProtoEx.Serialize(obj));
    }