예제 #1
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));
        }
예제 #2
0
        private static FieldSetting <T> CreateArray <InternalT>(int fieldNum, WireType wireType, Func <T, IEnumerable <InternalT> > valueGetter, Action <T, InternalT> oneValueSetter,
                                                                Action <T, BasicDeserializer> readActionWithoutTag, Func <T, bool> hasValueFunc,
                                                                Action <BasicSerializer, InternalT> oneValueWriter, Func <BasicDeserializer, InternalT> oneValueReader,
                                                                bool canReadPacked)
        {
            CheckFieldNum(fieldNum);
            return(new FieldSetting <T>(fieldNum, WireFormat.MakeTag(fieldNum, wireType),

                                        (value, serializer, rt) =>
            {
                foreach (var item in valueGetter(value))
                {
                    serializer.WriteRawTag(rt);
                    oneValueWriter(serializer, item);
                }
                ;
            },

                                        readActionWithoutTag,
                                        hasValueFunc,
                                        canReadPacked ? WireFormat.MakeTag(fieldNum, WireType.LengthDelimited) : 0,
                                        canReadPacked ? (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();
                }
            } : (Action <T, BasicDeserializer>)null));
        }