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

@@ -81,7 +81,7 @@ func NewSession(server *Server, conn net.Conn) *Session {
logger: server.logger.Named(conn.RemoteAddr().String()),
server: server,
rawConn: conn,
cryptConn: network.NewCryptConn(conn, server.erupeConfig.RealClientMode),
cryptConn: network.NewCryptConn(conn, server.erupeConfig.RealClientMode, server.logger.Named(conn.RemoteAddr().String())),
sendPackets: make(chan packet, 20),
clientContext: &clientctx.ClientContext{RealClientMode: server.erupeConfig.RealClientMode},
lastPacket: time.Now(),
@@ -257,7 +257,12 @@ func (s *Session) handlePacketGroup(pktGroup []byte) {
return
}
// Handle the packet.
handlerTable[opcode](s, mhfPkt)
handler, ok := s.server.handlerTable[opcode]
if !ok {
s.logger.Warn("No handler for opcode", zap.Stringer("opcode", opcode))
return
}
handler(s, mhfPkt)
// If there is more data on the stream that the .Parse method didn't read, then read another packet off it.
remainingData := bf.DataFromCurrent()
if len(remainingData) >= 2 {