我想知道如何优化这种方法。是否可以在列表中添加字节而不覆盖它?
public void Flush(NetworkStream netStream)
{
MinecraftPacketWriter writer = new MinecraftPacketWriter();
writer.WriteVarInt(buffer.Count);
byte[] lengthbytes = writer.Buffer.ToArray();
byte[] bufferbytes = buffer.ToArray();
byte[] doublebuffer = lengthbytes.Concat(bufferbytes).ToArray();
writer.Reset();
buffer = doublebuffer.ToList<byte>();
RawFlush(netStream);
}
这是我的全班。
public class MinecraftPacketWriter
{
private List<byte> buffer = new List<byte>();
public List<byte> Buffer { get { return buffer; } }
public void Reset()
{
buffer.Clear();
}
//.....
public void Write(byte b)
{
buffer.Add(b);
}
public void RawFlush(NetworkStream netStream)
{
Console.WriteLine("Buffer Bytes Count: {0}", buffer.Count);
netStream.Write(buffer.ToArray(), 0, buffer.Count);
Reset();
}
public void Flush(NetworkStream netStream)
{
MinecraftPacketWriter writer = new MinecraftPacketWriter();
writer.WriteVarInt(buffer.Count);
byte[] lengthbytes = writer.Buffer.ToArray();
byte[] bufferbytes = buffer.ToArray();
byte[] doublebuffer = lengthbytes.Concat(bufferbytes).ToArray();
writer.Reset();
buffer = doublebuffer.ToList<byte>();
RawFlush(netStream);
}
}