{"record":{"id":"de0648d618bcbb87","repo":"EllanJiang/GameFramework","slug":"packet-header-length-is-invalid","errorCode":null,"errorMessage":"Packet header length is invalid.","messagePattern":"Packet header length is invalid\\.","errorType":"validation","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/Network/NetworkManager.cs","lineNumber":234,"sourceCode":"        }\n\n        /// <summary>\n        /// 创建网络频道。\n        /// </summary>\n        /// <param name=\"name\">网络频道名称。</param>\n        /// <param name=\"serviceType\">网络服务类型。</param>\n        /// <param name=\"networkChannelHelper\">网络频道辅助器。</param>\n        /// <returns>要创建的网络频道。</returns>\n        public INetworkChannel CreateNetworkChannel(string name, ServiceType serviceType, INetworkChannelHelper networkChannelHelper)\n        {\n            if (networkChannelHelper == null)\n            {\n                throw new GameFrameworkException(\"Network channel helper is invalid.\");\n            }\n\n            if (networkChannelHelper.PacketHeaderLength < 0)\n            {\n                throw new GameFrameworkException(\"Packet header length is invalid.\");\n            }\n\n            if (HasNetworkChannel(name))\n            {\n                throw new GameFrameworkException(Utility.Text.Format(\"Already exist network channel '{0}'.\", name ?? string.Empty));\n            }\n\n            NetworkChannelBase networkChannel = null;\n            switch (serviceType)\n            {\n                case ServiceType.Tcp:\n                    networkChannel = new TcpNetworkChannel(name, networkChannelHelper);\n                    break;\n\n                case ServiceType.TcpWithSyncReceive:\n                    networkChannel = new TcpWithSyncReceiveNetworkChannel(name, networkChannelHelper);\n                    break;\n","sourceCodeStart":216,"sourceCodeEnd":252,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/Network/NetworkManager.cs#L216-L252","documentation":"The network channel helper's PacketHeaderLength must be zero or positive because it determines how many bytes are read to learn each packet's size. A negative value is meaningless, so CreateNetworkChannel throws immediately. The guard validates the helper contract before any channel is constructed.","triggerScenarios":"A custom INetworkChannelHelper implementation returns a negative value from its PacketHeaderLength property, often due to an uninitialized backing field or an inverted sign in a computed header size.","commonSituations":"Writing a custom packet protocol helper and accidentally returning -1 as a 'not set' sentinel; copying a helper template and hard-coding the wrong constant; struct field never initialized before use.","solutions":["Fix the helper's PacketHeaderLength to return a non-negative byte count matching your protocol header size","If the value is configurable, clamp or validate it at helper initialization time","Inspect the helper implementation for sentinel values like -1"],"exampleFix":"// before\npublic int PacketHeaderLength => _headerLength; // _headerLength never set (-1)\n// after\npublic int PacketHeaderLength => _headerLength >= 0 ? _headerLength : 4;","handlingStrategy":"validation","validationCode":"if (helper.PacketHeaderLength < 0) throw new InvalidOperationException(\"PacketHeaderLength must be >= 0\");\nvar channel = networkManager.CreateNetworkChannel(name, serviceType, helper);","typeGuard":"bool HasValidHeaderLength(INetworkChannelHelper h) => h != null && h.PacketHeaderLength >= 0;","tryCatchPattern":"try { var channel = networkManager.CreateNetworkChannel(name, serviceType, helper); }\ncatch (GameFrameworkException ex) { Log.Error(\"Invalid packet header length: {0}\", ex.Message); }","preventionTips":["Initialize PacketHeaderLength backing fields explicitly","Assert header length in the helper's constructor","Unit-test custom helpers before use"],"tags":["network","validation","packet-header","gameframework"],"backgroundTag":"invalid-argument-value","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}