Add DebugPrintPackets and fix not translating packet id

This commit is contained in:
Admin Else
2026-03-29 14:15:31 +02:00
parent e386a45d96
commit 8dd566c0da
+15 -7
View File
@@ -2,11 +2,14 @@ package proto
import ( import (
"bytes" "bytes"
"context"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
"reflect" "reflect"
"slices"
"strings"
"sync" "sync"
"github.com/admin-else/strom/data" "github.com/admin-else/strom/data"
@@ -60,7 +63,7 @@ type Conn struct {
Actor proto_base.Actor Actor proto_base.Actor
Version string Version string
ProtocolVersion int32 ProtocolVersion int32
DecodeEvenIfUnused bool DebugPrintPackets []string
sendTickAlways bool sendTickAlways bool
} }
@@ -80,7 +83,6 @@ func (c *Conn) OnLoginTick(_ LoginTick) (err error) {
if err != nil { if err != nil {
return return
} }
slog.Debug("logintick recv", "actor", c.Actor, "packet", fmt.Sprintf("%#v", packet))
err = c.Fire(packet) err = c.Fire(packet)
return return
} }
@@ -136,7 +138,6 @@ func (c *Conn) ActivateReceiveRoutine() (chSending <-chan any) {
ch <- err ch <- err
return return
} }
slog.Debug("recv", "actor", c.Actor, "packet", fmt.Sprintf("%#v", packet))
ch <- packet ch <- packet
} }
} }
@@ -146,7 +147,7 @@ func (c *Conn) ActivateReceiveRoutine() (chSending <-chan any) {
return return
} }
func (c *Conn) translatePacketVersion(packet proto_base.EncodeDecodeAble, packetInfo proto_base.PacketInfo) (convertedPacket proto_base.EncodeDecodeAble, err error) { func (c *Conn) translatePacketVersion(packet proto_base.EncodeDecodeAble, packetInfo proto_base.PacketInfo) (convertedPacket proto_base.EncodeDecodeAble, packetInfoReal proto_base.PacketInfo, err error) {
packetInfoReal, found := LookupPacketInfoByNameProtocolVersionAndState(packetInfo.Name, c.ProtocolVersion, c.State()) packetInfoReal, found := LookupPacketInfoByNameProtocolVersionAndState(packetInfo.Name, c.ProtocolVersion, c.State())
if !found { if !found {
err = PacketDoesntExistInFutureVersionErr err = PacketDoesntExistInFutureVersionErr
@@ -178,7 +179,7 @@ func (c *Conn) sendRegisteredPacket(packet proto_base.EncodeDecodeAble) (err err
return return
} }
if i.ProtocolVersion != c.ProtocolVersion { if i.ProtocolVersion != c.ProtocolVersion {
packet, err = c.translatePacketVersion(packet, i) packet, i, err = c.translatePacketVersion(packet, i)
if err != nil { if err != nil {
return return
} }
@@ -216,12 +217,16 @@ func (c *Conn) Receive() (packet proto_base.EncodeDecodeAble, err error) {
if err != nil { if err != nil {
return return
} }
packet, ok := LookUpTypeByPacketInfoAndCopyType(c.Actor.ReceiveDirection(), c.State(), id, c.ProtocolVersion)
i, ok := LookUpTypeByPacketInfo(c.Actor.ReceiveDirection(), c.State(), id, c.ProtocolVersion)
if !ok { if !ok {
packet = &UnCodablePacket{Err: BadPacketIdErr, Data: packetBytes, Direction: c.Actor.ReceiveDirection()} packet = &UnCodablePacket{Err: BadPacketIdErr, Data: packetBytes, Direction: c.Actor.ReceiveDirection()}
return return
} }
if !c.DecodeEvenIfUnused && !c.IsTypeRegistered(reflect.TypeOf(packet)) { packet = reflect.New(reflect.TypeOf(i.Type).Elem()).Interface().(proto_base.EncodeDecodeAble)
debugPrint := slog.Default().Enabled(context.Background(), slog.LevelDebug) &&
slices.ContainsFunc(c.DebugPrintPackets, func(s string) bool { return strings.Contains(i.Name, s) })
if !c.IsTypeRegistered(reflect.TypeOf(packet)) && !debugPrint {
packet = &UnCodablePacket{Err: NoHandlerRegisteredErr, Data: packetBytes, Direction: c.Actor.ReceiveDirection()} packet = &UnCodablePacket{Err: NoHandlerRegisteredErr, Data: packetBytes, Direction: c.Actor.ReceiveDirection()}
return return
} }
@@ -236,5 +241,8 @@ func (c *Conn) Receive() (packet proto_base.EncodeDecodeAble, err error) {
packet = &UnCodablePacket{Err: PacketNotFullyDecodedErr, Data: packetBytes, Direction: c.Actor.ReceiveDirection()} packet = &UnCodablePacket{Err: PacketNotFullyDecodedErr, Data: packetBytes, Direction: c.Actor.ReceiveDirection()}
return return
} }
if debugPrint {
slog.Debug("recv", "packet", fmt.Sprint("#v%", packet))
}
return return
} }