{"record":{"id":"e21486e1116dd88e","repo":"egametang/ET","slug":"bufferlist-length-count-length-count","errorCode":null,"errorMessage":"bufferList length < count, {Length} {count}","messagePattern":"bufferList length < count, (.+?) (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Network/Circularbuffer.cs","lineNumber":120,"sourceCode":"\t\t//\t    sendSize = (int)buffLength;\n\t\t//    }\n\t\t//\t\n\t\t//    await stream.WriteAsync(this.First, this.FirstIndex, sendSize);\n\t\t//    \n\t\t//    this.FirstIndex += sendSize;\n\t\t//    if (this.FirstIndex == this.ChunkSize)\n\t\t//    {\n\t\t//\t    this.FirstIndex = 0;\n\t\t//\t    this.RemoveFirst();\n\t\t//    }\n\t\t//}\n\n\t    // 从CircularBuffer读到stream\n\t    public void Read(Stream stream, int count)\n\t    {\n\t\t    if (count > this.Length)\n\t\t    {\n\t\t\t    throw new Exception($\"bufferList length < count, {Length} {count}\");\n\t\t    }\n\n\t\t    int alreadyCopyCount = 0;\n\t\t    while (alreadyCopyCount < count)\n\t\t    {\n\t\t\t    int n = count - alreadyCopyCount;\n\t\t\t    if (ChunkSize - this.FirstIndex > n)\n\t\t\t    {\n\t\t\t\t    stream.Write(this.First, this.FirstIndex, n);\n\t\t\t\t    this.FirstIndex += n;\n\t\t\t\t    alreadyCopyCount += n;\n\t\t\t    }\n\t\t\t    else\n\t\t\t    {\n\t\t\t\t    stream.Write(this.First, this.FirstIndex, ChunkSize - this.FirstIndex);\n\t\t\t\t    alreadyCopyCount += ChunkSize - this.FirstIndex;\n\t\t\t\t    this.FirstIndex = 0;\n\t\t\t\t    this.RemoveFirst();","sourceCodeStart":102,"sourceCodeEnd":138,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Network/Circularbuffer.cs#L102-L138","documentation":"CircularBuffer.Read(Stream, int) copies `count` bytes into a destination stream. It throws when the requested count exceeds the buffer's current Length, i.e. the caller asked for data that has not arrived yet.","triggerScenarios":"Reading a packet body before all its bytes are buffered, miscalculating packetSize, or a producer/consumer race where the reader outran the network writer.","commonSituations":"Packet parser using a stale packetSize after a malformed header, or two readers draining the same buffer.","solutions":["Always check buffer.Length >= count before calling Read (the PacketParser already does this pattern).","Return/false-wait until enough bytes accumulate instead of throwing.","Re-derive count from a freshly parsed size header."],"exampleFix":"// before\nbuffer.Read(stream, expectedCount);\n// after\nif (buffer.Length < expectedCount) return false;\nbuffer.Read(stream, expectedCount);","handlingStrategy":"validation","validationCode":"if (buffer.Length < count) return false; // not enough data yet\nbuffer.Read(stream, count);","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Always gate reads on buffer.Length >= count.","Re-derive count from a freshly parsed header each iteration.","Never share one buffer between two concurrent readers."],"tags":["network","buffer","csharp"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}