Пример #1
0
        private byte[] MainWrite(object obj)
        {
            MemoryStream          ms        = new MemoryStream();
            PrimitiveValueWrapper mainStage = new PrimitiveValueWrapper(ms);

            Serializers[obj.GetType()].Serialize(mainStage, obj);
            mainStage.CompleteWrite();
            mainStage.SetInvalid();
            //Read Payload
            ms.Position = 0;
            byte[] buf = new byte[ms.Length];
            ms.Read(buf, 0, buf.Length);
            ms.Close();
            return(buf);
        }
Пример #2
0
        private bool TryBaseRead(Stream stream, out BasePacket packet)
        {
            PrimitiveValueWrapper baseStage = new PrimitiveValueWrapper(stream);

            try
            {
                packet = BaseSerializer.DeserializePacket(baseStage);
                baseStage.SetInvalid();
                return(true);
            }
            catch (Exception)
            {
                packet = null;
                return(false);
            }
        }
Пример #3
0
        private object MainRead(BasePacket basePacket)
        {
            MemoryStream ms = new MemoryStream(basePacket.Payload)
            {
                Position = 0
            };
            Type packetType = GetTypeByKey(basePacket.PacketType);

            PrimitiveValueWrapper mainStage = new PrimitiveValueWrapper(ms);
            object ret = Serializers[packetType].Deserialize(mainStage);

            mainStage.SetInvalid();


            ms.Close();
            return(ret);
        }
Пример #4
0
        private static bool TryBaseWrite(Stream stream, BasePacket packet)
        {
            PrimitiveValueWrapper baseStage = new PrimitiveValueWrapper(stream);
            bool ret = true;

            try
            {
                BaseSerializer.Serialize(baseStage, packet);
                baseStage.CompleteWrite();
            }
            catch (Exception)
            {
                ret = false;
            }
            baseStage.SetInvalid();
            return(ret);
        }