Files
hjha-server/ServerCore/NetWork/MemoryBuffer.cs
xiaoou e9616125ce feat: initial commit - HJHA game server full source
6 major server modules (PdkFriendServer/GlobalSever/ServerCore/GameModule/GameNetModule) +
game logic (GameFix/GameDAL/ServerData) +
network layer (NetWorkMessage) +
data layer (ObjectModel) +
utilities (MrWu/Core/Config/CloudAPI/dll) +
adapters (zyxAdapter/base)

.NET 8.0 C# solution, 16 projects, 958 source files
2026-07-07 12:02:15 +08:00

73 lines
1.9 KiB
C#

using System;
using System.Buffers;
using System.IO;
namespace Server.Net
{
public class MemoryBuffer : MemoryStream, IBufferWriter<byte>
{
/// <summary>
/// 原始位置
/// </summary>
private int origin;
public MemoryBuffer()
{
}
public MemoryBuffer(int capacity) : base(capacity)
{
}
public MemoryBuffer(byte[] buffer) : base(buffer)
{
}
public MemoryBuffer(byte[] buffer, int index, int length) : base(buffer, index, length)
{
this.origin = index;
}
public ReadOnlyMemory<byte> WrittenMemory => this.GetBuffer().AsMemory(this.origin, (int)this.Position);
public ReadOnlySpan<byte> WrittenSpan => this.GetBuffer().AsSpan(this.origin, (int)this.Position);
public void Advance(int count)
{
long newlength = this.Position + count;
if (newlength > this.Length)
{
this.SetLength(newlength);
}
this.Position = newlength;
}
public Memory<byte> GetMemory(int sizeHint = 0)
{
if (this.Length - this.Position < sizeHint)
{
this.SetLength(this.Position + sizeHint);
}
var memory = this.GetBuffer()
.AsMemory((int)this.Position + this.origin, (int)(this.Length - this.Position));
return memory;
}
public Span<byte> GetSpan(int sizeHint = 0)
{
if (this.Length - this.Position < sizeHint)
{
this.SetLength(this.Position + sizeHint);
}
var span = this.GetBuffer().AsSpan((int)this.Position + this.origin, (int)(this.Length - this.Position));
return span;
}
}
}