From e9ea621b93fbb58d9edfca8375918791637bbd52 Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 30 Dec 2020 20:59:04 +0800 Subject: +init --- .../Recorder/PacketSerializationContext.cs | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Impostor-dev/src/Impostor.Server/Recorder/PacketSerializationContext.cs (limited to 'Impostor-dev/src/Impostor.Server/Recorder/PacketSerializationContext.cs') diff --git a/Impostor-dev/src/Impostor.Server/Recorder/PacketSerializationContext.cs b/Impostor-dev/src/Impostor.Server/Recorder/PacketSerializationContext.cs new file mode 100644 index 0000000..07755f6 --- /dev/null +++ b/Impostor-dev/src/Impostor.Server/Recorder/PacketSerializationContext.cs @@ -0,0 +1,56 @@ +using System.IO; +using System.Text; + +namespace Impostor.Server.Recorder +{ + public class PacketSerializationContext + { + private const int InitialStreamSize = 0x100; + private const int MaximumStreamSize = 0x100000; + + private MemoryStream _memory; + private BinaryWriter _writer; + + public MemoryStream Stream + { + get + { + if (_memory == null) + { + _memory = new MemoryStream(InitialStreamSize); + } + + return _memory; + } + private set => _memory = value; + } + + public BinaryWriter Writer + { + get + { + if (_writer == null) + { + _writer = new BinaryWriter(Stream, Encoding.UTF8, true); + } + + return _writer; + } + private set => _writer = value; + } + + public void Reset() + { + if (Stream.Capacity > MaximumStreamSize) + { + Stream = null; + Writer = null; + } + else + { + Stream.Position = 0L; + Stream.SetLength(0L); + } + } + } +} \ No newline at end of file -- cgit v1.1-26-g67d0