Пример #1
0
 internal MessageWriter(Action <T, BasicSerializer> writeAction, Stream stream, bool ownStream)
 {
     this.writeAction        = writeAction;
     this.internalStream     = new MemoryStream();
     this.internalSerializer = new BasicSerializer(this.internalStream);
     this.stream             = stream;
     this.serializer         = new BasicSerializer(stream);
     this.ownStream          = ownStream;
 }
Пример #2
0
        private static FieldSetting <T> CreatePackedArray <InternalT>(int fieldNum, WireType wireType, Func <T, IEnumerable <InternalT> > valueGetter, Action <T, InternalT> oneValueSetter,
                                                                      Action <T, BasicDeserializer> altReadActionWithoutTag, Func <T, bool> hasValueFunc,
                                                                      Action <BasicSerializer, InternalT> oneValueWriter, Func <BasicDeserializer, InternalT> oneValueReader,
                                                                      bool canReadUnpacked)
        {
            CheckFieldNum(fieldNum);
            return(new FieldSetting <T>(fieldNum, WireFormat.MakeTag(fieldNum, WireType.LengthDelimited),

                                        (value, serializer, rt) =>
            {
                serializer.WriteRawTag(rt);
                using (MemoryStream ms = new MemoryStream())
                {
                    BasicSerializer bs = new BasicSerializer(ms);
                    foreach (var item in valueGetter(value))
                    {
                        oneValueWriter(bs, item);
                    }
                    ;

                    ms.Flush();

                    var len = (int)ms.Position;
                    serializer.WriteLength(len);
                    ArraySegment <byte> buff;
                    if (ms.TryGetBuffer(out buff))
                    {
                        serializer.stream.Write(buff.Array, 0, len);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Unable to get buffer from {nameof(ms)}");
                    }
                }
            },

                                        (value, serializer) =>
            {
                var len = serializer.ReadLength();
                var positionLimit = serializer.stream.Position + len;

                while (serializer.stream.Position < positionLimit)
                {
                    oneValueSetter(value, oneValueReader(serializer));
                }

                if (serializer.stream.Position > positionLimit)
                {
                    throw InvalidProtocolBufferException.AllowableFieldLengthWasExceeded();
                }
            },

                                        hasValueFunc,
                                        canReadUnpacked ? WireFormat.MakeTag(fieldNum, wireType) : 0,
                                        canReadUnpacked ? altReadActionWithoutTag : null));
        }
Пример #3
0
        public static byte[] GetTagBytes(uint tag)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BasicSerializer bs = new BasicSerializer(ms);
                bs.WriteUInt32(tag);

                ms.Flush();

                return ms.ToArray();
            }
        }
Пример #4
0
        public static byte[] GetTagBytes(uint tag)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                BasicSerializer bs = new BasicSerializer(ms);
                bs.WriteUInt32(tag);

                ms.Flush();

                return(ms.ToArray());
            }
        }