{"record":{"id":"c7ef33031133443b","repo":"egametang/ET","slug":"send-packet-too-large-stream-length-stream-pos","errorCode":null,"errorMessage":"send packet too large: {stream.Length} {stream.Position}","messagePattern":"send packet too large: (.+?) (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Network/TChannel.cs","lineNumber":107,"sourceCode":"\t\t\tthis.outArgs = null;\n\t\t\tthis.socket = null;\n\t\t}\n\n\t\tpublic void Send(MemoryBuffer stream)\n\t\t{\n\t\t\tif (this.IsDisposed)\n\t\t\t{\n\t\t\t\tthrow new Exception(\"TChannel已经被Dispose, 不能发送消息\");\n\t\t\t}\n\t\t\t\n\t\t\tswitch (this.Service.ServiceType)\n\t\t\t{\n\t\t\t\tcase ServiceType.Inner:\n\t\t\t\t{\n\t\t\t\t\tint messageSize = (int) (stream.Length - stream.Position);\n\t\t\t\t\tif (messageSize > ushort.MaxValue * 16)\n\t\t\t\t\t{\n\t\t\t\t\t\tthrow new Exception($\"send packet too large: {stream.Length} {stream.Position}\");\n\t\t\t\t\t}\n\n\t\t\t\t\tthis.sendCache.WriteTo(0, messageSize);\n\t\t\t\t\tthis.sendBuffer.Write(this.sendCache, 0, PacketParser.InnerPacketSizeLength);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t\tcase ServiceType.Outer:\n\t\t\t\t{\n\t\t\t\t\tushort messageSize = (ushort) (stream.Length - stream.Position);\n\t\t\t\t\tthis.sendCache.WriteTo(0, messageSize);\n\t\t\t\t\tthis.sendBuffer.Write(this.sendCache, 0, PacketParser.OuterPacketSizeLength);\n\t\t\t\t\tbreak;\n\t\t\t\t}\n\t\t\t}\n\t\t\t\n\t\t\tthis.sendBuffer.Write(stream.GetBuffer(), (int)stream.Position, (int)(stream.Length - stream.Position));\n\t\t\tif (!this.isSending)\n\t\t\t{","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Network/TChannel.cs#L89-L125","documentation":"TChannel.Send enforces an upper bound (ushort.MaxValue*16 ~= 1 MiB) on inner-channel messages by comparing stream.Length - stream.Position. Larger inner messages are rejected because the 4-byte size header / memory model cannot safely carry them.","triggerScenarios":"Serializing a very large object (big snapshot, blob, list) and sending it on an inner (server-to-server) channel in a single message.","commonSituations":"Full-state sync, large config push, or a log/binary blob sent as one message.","solutions":["Chunk large payloads into multiple messages under the limit.","Move bulk data out of band (shared store, file transfer) instead of one RPC.","Compress or trim the payload."],"exampleFix":"// before\ninnerChannel.Send(hugeMemoryBuffer);\n// after\nforeach (var chunk in Split(hugeMemoryBuffer, 512 * 1024))\n    innerChannel.Send(chunk);","handlingStrategy":"validation","validationCode":"const int InnerMax = ushort.MaxValue * 16;\nint msgSize = (int)(stream.Length - stream.Position);\nif (msgSize > InnerMax) throw new InvalidOperationException($\"message too large: {msgSize}\");\nchannel.Send(stream);","typeGuard":"static bool FitsInnerMessage(long length, long position) => length - position <= ushort.MaxValue * 16;","tryCatchPattern":"null","preventionTips":["Chunk payloads above ~1 MiB.","Move bulk data out of the RPC path.","Validate size before serializing the whole message."],"tags":["network","tcp","packet","csharp"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}