refactor(channelserver): replace init() handler registration with explicit construction

The handler table was a package-level global populated by init(), making
registration implicit and untestable. Move it to buildHandlerTable()
which returns the map, store it as a Server struct field initialized in
NewServer(), and add a missing-handler guard in handlePacketGroup to log
a warning instead of panicking on unknown opcodes.
This commit is contained in:
Houmgaor
2026-02-20 18:58:32 +01:00
parent 45c29837a5
commit e5133e5dcf
6 changed files with 66 additions and 35 deletions

View File

@@ -7,10 +7,10 @@ import (
type handlerFunc func(s *Session, p mhfpacket.MHFPacket)
var handlerTable map[network.PacketID]handlerFunc
func init() {
handlerTable = make(map[network.PacketID]handlerFunc)
// buildHandlerTable constructs and returns the handler table mapping packet IDs
// to their handler functions. Called once during server construction.
func buildHandlerTable() map[network.PacketID]handlerFunc {
handlerTable := make(map[network.PacketID]handlerFunc)
handlerTable[network.MSG_HEAD] = handleMsgHead
handlerTable[network.MSG_SYS_reserve01] = handleMsgSysReserve01
handlerTable[network.MSG_SYS_reserve02] = handleMsgSysReserve02
@@ -443,4 +443,5 @@ func init() {
handlerTable[network.MSG_SYS_reserve1AD] = handleMsgSysReserve1AD
handlerTable[network.MSG_SYS_reserve1AE] = handleMsgSysReserve1AE
handlerTable[network.MSG_SYS_reserve1AF] = handleMsgSysReserve1AF
return handlerTable
}