Flush when starting or finishing a message. (#4)

This is a no-op with BinaryWriter/MemoryStream,  but this is necesary if we do
any sort of transformation,  such as BitWriter, compression or encryption
This commit is contained in:
Paul Pacheco 2018-09-18 14:40:45 -05:00
parent ec48bcc496
commit cdd4eac7a1

View File

@ -23,6 +23,7 @@ public class NetworkWriter
// => .ToArray() would return 10 bytes because of the first write, which is exactly what we don't want. // => .ToArray() would return 10 bytes because of the first write, which is exactly what we don't want.
public byte[] ToArray() public byte[] ToArray()
{ {
writer.Flush();
byte[] slice = new byte[Position]; byte[] slice = new byte[Position];
Array.Copy(((MemoryStream)writer.BaseStream).ToArray(), slice, Position); Array.Copy(((MemoryStream)writer.BaseStream).ToArray(), slice, Position);
return slice; return slice;