2020-08-25 18:53:35 -07:00
|
|
|
/*************************************************************************
|
|
|
|
|
* ModernUO *
|
2024-06-23 10:51:20 -07:00
|
|
|
* Copyright 2019-2024 - ModernUO Development Team *
|
2020-08-25 18:53:35 -07:00
|
|
|
* Email: hi@modernuo.com *
|
2022-10-11 22:17:22 -07:00
|
|
|
* File: BufferWriter.cs *
|
2020-08-25 18:53:35 -07:00
|
|
|
* *
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
|
|
using System;
|
2023-03-10 00:05:16 -08:00
|
|
|
using System.Buffers.Binary;
|
2022-10-11 22:17:22 -07:00
|
|
|
using System.Collections.Concurrent;
|
2020-12-24 14:21:17 -08:00
|
|
|
using System.Diagnostics;
|
2020-10-31 17:38:29 -07:00
|
|
|
using System.IO;
|
|
|
|
|
using System.Runtime.CompilerServices;
|
2024-03-28 13:34:12 -07:00
|
|
|
using System.Runtime.InteropServices;
|
2020-08-25 18:53:35 -07:00
|
|
|
using System.Text;
|
2021-01-13 18:56:32 -08:00
|
|
|
using Server.Text;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
namespace Server;
|
|
|
|
|
|
|
|
|
|
public class BufferWriter : IGenericWriter
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2024-06-23 10:51:20 -07:00
|
|
|
private readonly ConcurrentQueue<Type> _types;
|
|
|
|
|
private readonly Encoding _encoding;
|
|
|
|
|
private readonly bool _prefixStrings;
|
2022-10-10 21:47:08 -07:00
|
|
|
private long _bytesWritten;
|
|
|
|
|
private long _index;
|
2020-12-24 14:21:17 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
protected long Index
|
|
|
|
|
{
|
|
|
|
|
get => _index;
|
|
|
|
|
set
|
2020-12-24 14:21:17 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
if (value < 0 || value > _buffer.Length)
|
2020-12-24 14:21:17 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
// If you are receiving this exception and your value is too large, you may need to use `Resize`
|
|
|
|
|
// If you are receiving this exception and your value is negative, you probably used Seek incorrectly.
|
|
|
|
|
throw new ArgumentOutOfRangeException(nameof(value));
|
2020-12-24 14:21:17 -08:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
_index = value;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (value > _bytesWritten)
|
|
|
|
|
{
|
|
|
|
|
_bytesWritten = value;
|
|
|
|
|
}
|
2020-11-01 17:22:18 -08:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-11-01 17:22:18 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
private byte[] _buffer;
|
2020-10-31 17:38:29 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
public BufferWriter(byte[] buffer, bool prefixStr, ConcurrentQueue<Type> types = null)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2022-10-11 22:17:22 -07:00
|
|
|
_prefixStrings = prefixStr;
|
|
|
|
|
_encoding = TextEncoding.UTF8;
|
2022-10-10 21:47:08 -07:00
|
|
|
_buffer = buffer;
|
2022-10-11 22:17:22 -07:00
|
|
|
_types = types;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-12-23 07:11:41 -08:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
public BufferWriter(bool prefixStr, ConcurrentQueue<Type> types = null) : this(0, prefixStr, types)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
|
|
|
|
}
|
2020-11-01 17:22:18 -08:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
public BufferWriter(int count, bool prefixStr, ConcurrentQueue<Type> types = null)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2022-10-11 22:17:22 -07:00
|
|
|
_prefixStrings = prefixStr;
|
|
|
|
|
_encoding = TextEncoding.UTF8;
|
2022-10-10 21:47:08 -07:00
|
|
|
_buffer = GC.AllocateUninitializedArray<byte>(count < 1 ? BufferSize : count);
|
2022-10-11 22:17:22 -07:00
|
|
|
_types = types;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-10-31 17:38:29 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public virtual long Position => Index;
|
2020-10-31 17:38:29 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
protected virtual int BufferSize => 256;
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public byte[] Buffer => _buffer;
|
2020-12-13 13:15:48 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
public virtual void Close()
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-12-24 14:21:17 -08:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void Resize(int size)
|
|
|
|
|
{
|
|
|
|
|
// We shouldn't ever resize to a 0 length buffer. That is dangerous
|
|
|
|
|
if (size <= 0)
|
|
|
|
|
{
|
|
|
|
|
size = BufferSize;
|
2020-10-31 17:38:29 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
if (size < _buffer.Length)
|
2020-10-31 17:38:29 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
_bytesWritten = size;
|
2020-12-13 13:15:48 -08:00
|
|
|
}
|
|
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
var newBuffer = GC.AllocateUninitializedArray<byte>(size);
|
|
|
|
|
_buffer.AsSpan(0, Math.Min(size, _buffer.Length)).CopyTo(newBuffer);
|
|
|
|
|
_buffer = newBuffer;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-14 09:57:43 -07:00
|
|
|
public virtual void Flush() => Resize(Math.Clamp(_buffer.Length * 2, BufferSize, _buffer.Length + 1024 * 1024 * 64));
|
2022-10-10 21:47:08 -07:00
|
|
|
|
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
private void FlushIfNeeded(int amount)
|
|
|
|
|
{
|
|
|
|
|
if (Index + amount > _buffer.Length)
|
2020-12-13 13:15:48 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
Flush();
|
2020-11-01 17:22:18 -08:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-10-31 17:38:29 -07:00
|
|
|
|
2024-05-24 14:36:43 -07:00
|
|
|
public virtual void Write(byte[] bytes) => Write(bytes.AsSpan());
|
|
|
|
|
|
|
|
|
|
public virtual void Write(byte[] bytes, int offset, int count) => Write(bytes.AsSpan(offset, count));
|
|
|
|
|
|
|
|
|
|
public virtual void Write(ReadOnlySpan<byte> bytes)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2023-03-10 00:05:16 -08:00
|
|
|
var length = bytes.Length;
|
2020-12-13 13:15:48 -08:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
while (_buffer.Length - _index < length)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2023-03-10 00:05:16 -08:00
|
|
|
Flush();
|
2020-12-13 13:15:48 -08:00
|
|
|
}
|
2023-03-10 00:05:16 -08:00
|
|
|
|
|
|
|
|
bytes.CopyTo(_buffer.AsSpan((int)_index));
|
|
|
|
|
Index += length;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-12-13 13:15:48 -08:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
public virtual long Seek(long offset, SeekOrigin origin)
|
|
|
|
|
{
|
|
|
|
|
Debug.Assert(
|
|
|
|
|
origin != SeekOrigin.End || offset <= 0 && offset > -_buffer.Length,
|
|
|
|
|
"Attempting to seek to an invalid position using SeekOrigin.End"
|
|
|
|
|
);
|
|
|
|
|
Debug.Assert(
|
|
|
|
|
origin != SeekOrigin.Begin || offset >= 0 && offset < _buffer.Length,
|
|
|
|
|
"Attempting to seek to an invalid position using SeekOrigin.Begin"
|
|
|
|
|
);
|
|
|
|
|
Debug.Assert(
|
|
|
|
|
origin != SeekOrigin.Current || Index + offset >= 0 && Index + offset < _buffer.Length,
|
|
|
|
|
"Attempting to seek to an invalid position using SeekOrigin.Current"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Index = Math.Max(0, origin switch
|
2020-11-01 17:22:18 -08:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
SeekOrigin.Current => Index + offset,
|
|
|
|
|
SeekOrigin.End => _bytesWritten + offset,
|
|
|
|
|
_ => offset // Begin
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
public void Write(string value)
|
|
|
|
|
{
|
2022-10-11 22:17:22 -07:00
|
|
|
if (_prefixStrings)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
if (value == null)
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
Write(false);
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
Write(true);
|
2020-08-25 18:53:35 -07:00
|
|
|
InternalWriteString(value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
else
|
2020-08-25 18:53:35 -07:00
|
|
|
{
|
2022-10-10 21:47:08 -07:00
|
|
|
InternalWriteString(value);
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
public void Write(long value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(8);
|
|
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
BinaryPrimitives.WriteInt64LittleEndian(_buffer.AsSpan((int)_index), value);
|
|
|
|
|
Index += 8;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
public void Write(ulong value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(8);
|
|
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
BinaryPrimitives.WriteUInt64LittleEndian(_buffer.AsSpan((int)_index), value);
|
|
|
|
|
Index += 8;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
public void Write(int value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(4);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
BinaryPrimitives.WriteInt32LittleEndian(_buffer.AsSpan((int)_index), value);
|
|
|
|
|
Index += 4;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
public void Write(uint value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(4);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
BinaryPrimitives.WriteUInt32LittleEndian(_buffer.AsSpan((int)_index), value);
|
|
|
|
|
Index += 4;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
public void Write(short value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(2);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
BinaryPrimitives.WriteInt16LittleEndian(_buffer.AsSpan((int)_index), value);
|
|
|
|
|
Index += 2;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
public void Write(ushort value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(2);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
BinaryPrimitives.WriteUInt16LittleEndian(_buffer.AsSpan((int)_index), value);
|
|
|
|
|
Index += 2;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-03-10 00:05:16 -08:00
|
|
|
public void Write(double value)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
|
|
|
|
FlushIfNeeded(8);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
BinaryPrimitives.WriteDoubleLittleEndian(_buffer.AsSpan((int)_index), value);
|
2022-10-10 21:47:08 -07:00
|
|
|
Index += 8;
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2023-03-15 00:37:01 -07:00
|
|
|
public void Write(float value)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
|
|
|
|
FlushIfNeeded(4);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
BinaryPrimitives.WriteSingleLittleEndian(_buffer.AsSpan((int)_index), value);
|
2023-03-15 00:37:01 -07:00
|
|
|
Index += 4;
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void Write(byte value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(1);
|
|
|
|
|
_buffer[Index++] = value;
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void Write(sbyte value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(1);
|
|
|
|
|
_buffer[Index++] = (byte)value;
|
|
|
|
|
}
|
2021-08-25 00:27:19 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public unsafe void Write(bool value)
|
|
|
|
|
{
|
|
|
|
|
FlushIfNeeded(1);
|
|
|
|
|
_buffer[Index++] = *(byte*)&value; // up to 30% faster to dereference the raw value on the stack
|
|
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2022-10-10 21:47:08 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void Write(Serial serial) => Write(serial.Value);
|
2020-12-23 07:11:41 -08:00
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void Write(Type type)
|
|
|
|
|
{
|
|
|
|
|
if (type == null)
|
|
|
|
|
{
|
|
|
|
|
Write((byte)0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Write((byte)0x2); // xxHash3 64bit
|
|
|
|
|
Write(AssemblyHandler.GetTypeHash(type));
|
|
|
|
|
_types?.Enqueue(type);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 13:34:12 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
|
|
|
public void Write(decimal value)
|
|
|
|
|
{
|
|
|
|
|
Span<int> buffer = stackalloc int[sizeof(decimal) / 4];
|
|
|
|
|
decimal.GetBits(value, buffer);
|
|
|
|
|
|
|
|
|
|
Write(MemoryMarshal.Cast<int, byte>(buffer));
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-11 22:17:22 -07:00
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
2022-10-10 21:47:08 -07:00
|
|
|
internal void InternalWriteString(string value)
|
|
|
|
|
{
|
2023-03-10 00:05:16 -08:00
|
|
|
var length = _encoding.GetByteCount(value);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
((IGenericWriter)this).WriteEncodedInt(length);
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
while (_buffer.Length - _index < length)
|
2022-10-10 21:47:08 -07:00
|
|
|
{
|
2023-03-10 00:05:16 -08:00
|
|
|
Flush();
|
2022-10-10 21:47:08 -07:00
|
|
|
}
|
2020-08-25 18:53:35 -07:00
|
|
|
|
2023-03-10 00:05:16 -08:00
|
|
|
// We don't use spans here since that incurs extra allocations for safety.
|
|
|
|
|
Index += _encoding.GetBytes(value, 0, value.Length, _buffer, (int)_index);
|
2020-08-25 18:53:35 -07:00
|
|
|
}
|
|
|
|
|
}
|