Exemple #1
0
        public void UnsafeMemoryInputBuffer()
        {
            using (var buffer = new UnsafeMemoryInputBuffer(new Memory <byte>(Input)))
            {
                this.InputBufferTest(buffer);
            }

            using (var buffer = new UnsafeMemoryInputBuffer(new Memory <byte>(StringInput)))
            {
                this.StringInputBufferTest(buffer);
            }
        }
Exemple #2
0
        public void UnsafeMemoryInputBuffer()
        {
            using (var buffer = new UnsafeMemoryInputBuffer(new Memory <byte>(Input)))
            {
                this.InputBufferTest(buffer);
            }

            using (var buffer = new UnsafeMemoryInputBuffer(new Memory <byte>(StringInput)))
            {
                this.StringInputBufferTest(buffer);
            }

            this.TestDeserializeBoth(b => new UnsafeMemoryInputBuffer(b));
        }
Exemple #3
0
        public static void Run()
        {
            Dog tony = new Dog
            {
                Breed  = DogBreed.BostonTerrier,
                Vitals = new AnimalVitals {
                    Age = 11, Gender = Gender.Male, Name = "Tony"
                }
            };
            Dog rocket = new Dog
            {
                Breed  = DogBreed.GoldenRetriever,
                Vitals = new AnimalVitals {
                    Age = 8, Gender = Gender.Female, Name = "Rocket"
                }
            };
            Dog peaches = new Dog
            {
                Breed  = DogBreed.GermanShepard,
                Vitals = new AnimalVitals {
                    Age = 14, Gender = Gender.Female, Name = "Peaches"
                }
            };

            // RIP
            Cat grumpyCat = new Cat
            {
                Breed  = CatBreed.GrumpyCat,
                Vitals = new AnimalVitals {
                    Age = 17, Gender = Gender.Female, Name = "Tardar Sauce"
                }
            };

            Person person = new Person
            {
                Age         = 24,
                Cats        = new[] { grumpyCat },
                Dogs        = new[] { tony, rocket, peaches },
                FavoritePet = new FlatBufferUnion <Dog, Cat>(rocket),
                Name        = "Nikola Tesla"
            };

            // SpanWriter is the core code that writes data to a span. Flatsharp provides a couple:
            SpanWriter spanWriter       = new SpanWriter();
            SpanWriter unsafeSpanWriter = new UnsafeSpanWriter();

            byte[] buffer = new byte[Person.Serializer.GetMaxSize(person)];

            int bytesWritten = Person.Serializer.Write(spanWriter, buffer, person);

            bytesWritten = Person.Serializer.Write(unsafeSpanWriter, buffer, person);

            // For reading data, we use InputBuffer. There are more options here:
            var p1 = Person.Serializer.Parse(new ArrayInputBuffer(buffer));
            var p3 = Person.Serializer.Parse(new MemoryInputBuffer(new Memory <byte>(buffer)));
            var p2 = Person.Serializer.Parse(new UnsafeArrayInputBuffer(buffer));

            using (var unsafeMemoryInput = new UnsafeMemoryInputBuffer(new Memory <byte>(buffer)))
            {
                // Unsafe memory input buffer must be disposed of because it pins the memory in place.
                var p4 = Person.Serializer.Parse(unsafeMemoryInput);
            }
        }
Exemple #4
0
        public static void Run()
        {
            Dog tony = new Dog
            {
                Breed  = DogBreed.BostonTerrier,
                Vitals = new AnimalVitals {
                    Age = 11, Gender = Gender.Male, Name = "Tony"
                }
            };
            Dog rocket = new Dog
            {
                Breed  = DogBreed.GoldenRetriever,
                Vitals = new AnimalVitals {
                    Age = 8, Gender = Gender.Female, Name = "Rocket"
                }
            };
            Dog peaches = new Dog
            {
                Breed  = DogBreed.GermanShepard,
                Vitals = new AnimalVitals {
                    Age = 14, Gender = Gender.Female, Name = "Peaches"
                }
            };

            // RIP
            Cat grumpyCat = new Cat
            {
                Breed  = CatBreed.GrumpyCat,
                Vitals = new AnimalVitals {
                    Age = 17, Gender = Gender.Female, Name = "Tardar Sauce"
                }
            };

            Person person = new Person
            {
                Age         = 24,
                Cats        = new[] { grumpyCat },
                Dogs        = new[] { tony, rocket, peaches },
                FavoritePet = new FlatBufferUnion <Dog, Cat>(rocket),
                Name        = "Nikola Tesla"
            };

            // SpanWriter is the core code that writes data to a span. Flatsharp provides a couple:
            SpanWriter spanWriter       = new SpanWriter();
            SpanWriter unsafeSpanWriter = new UnsafeSpanWriter();

            byte[] buffer = new byte[Person.Serializer.GetMaxSize(person)];

            int bytesWritten = Person.Serializer.Write(spanWriter, buffer, person);

            bytesWritten = Person.Serializer.Write(unsafeSpanWriter, buffer, person);

            // For reading data, we use InputBuffer. There are more options here:

            // Array and Memory input buffers are general purpose and support all scenarios.
            var p1 = Person.Serializer.Parse(new ArrayInputBuffer(buffer));
            var p2 = Person.Serializer.Parse(new MemoryInputBuffer(new Memory <byte>(buffer)));

            // ReadOnlyMemory input buffer will fail to Parse any objects that have Memory<T> in them (that is -- non read only memory).
            var p3 = Person.Serializer.Parse(new ReadOnlyMemoryInputBuffer(new ReadOnlyMemory <byte>(buffer)));

            // The unsafe variants are available in the FlatSharp.Unsafe package and use pointers and other unsafe code to squeeze
            // out some more performance.
            var p4 = Person.Serializer.Parse(new UnsafeArrayInputBuffer(buffer));

            using (var unsafeMemoryInput = new UnsafeMemoryInputBuffer(new Memory <byte>(buffer)))
            {
                // Unsafe memory input buffer must be disposed of because it pins the memory in place.
                var p5 = Person.Serializer.Parse(unsafeMemoryInput);
            }
        }