예제 #1
0
        public static FieldSetting <T> CreateMessageArray <EmbeddedT>(int fieldNum, Func <T, IEnumerable <EmbeddedT> > valueGetter, Action <T, EmbeddedT> oneValueSetter,
                                                                      MessageDescriptor <EmbeddedT> embeddedDescriptor, Func <T, bool> hasValueFunc = null) where EmbeddedT : new()
        {
            CheckFieldNum(fieldNum);
            uint tag = WireFormat.MakeTag(fieldNum, WireType.LengthDelimited);

            return(new FieldSetting <T>(fieldNum, tag,

                                        (value, serializer, rt) =>
            {
                using (var writer = embeddedDescriptor.CreateWriter(serializer.stream))
                {
                    foreach (var item in valueGetter(value))
                    {
                        serializer.WriteRawTag(rt);
                        writer.WriteWithLength(item);
                    }
                    ;
                }
            },

                                        (value, serializer) =>
            {
                using (var reader = embeddedDescriptor.CreateReader(serializer.stream))
                {
                    oneValueSetter(value, reader.ReadWithLen());
                }
            },
                                        hasValueFunc));
        }
예제 #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
        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));
        }
예제 #4
0
 private static FieldSetting <T> CreateValue(int fieldNum, WireType wireType, Action <T, BasicSerializer, byte[]> writeAction, Action <T, BasicDeserializer> readActionWithoutTag, Func <T, bool> hasValueFunc)
 {
     CheckFieldNum(fieldNum);
     return(new FieldSetting <T>(fieldNum, WireFormat.MakeTag(fieldNum, wireType), writeAction, readActionWithoutTag, hasValueFunc));
 }