minor refactoring, fixed go-staticcheck warnings

This commit is contained in:
CPunch 2023-08-23 18:03:14 -05:00
parent 670d4a514c
commit 83b664da93
3 changed files with 15 additions and 18 deletions

View File

@ -31,7 +31,7 @@ func (pkt Packet) encodeStructField(field reflect.StructField, value reflect.Val
case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to encode it case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to encode it
sz, err := strconv.Atoi(field.Tag.Get("size")) sz, err := strconv.Atoi(field.Tag.Get("size"))
if err != nil { if err != nil {
return fmt.Errorf("Failed to grab string 'size' tag!!") return fmt.Errorf("failed to grab string 'size' tag")
} }
buf16 := utf16.Encode([]rune(value.String())) buf16 := utf16.Encode([]rune(value.String()))
@ -100,7 +100,7 @@ func (pkt Packet) decodeStructField(field reflect.StructField, value reflect.Val
case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to decode it case reflect.String: // all strings in fusionfall packets are encoded as utf16, we'll need to decode it
sz, err := strconv.Atoi(field.Tag.Get("size")) sz, err := strconv.Atoi(field.Tag.Get("size"))
if err != nil { if err != nil {
return fmt.Errorf("Failed to grab string 'size' tag!!") return fmt.Errorf("failed to grab string 'size' tag")
} }
buf16 := make([]uint16, sz) buf16 := make([]uint16, sz)

View File

@ -11,9 +11,9 @@ import (
func (server *ShardServer) attachPlayer(peer *protocol.CNPeer, meta redis.LoginMetadata) (*entity.Player, error) { func (server *ShardServer) attachPlayer(peer *protocol.CNPeer, meta redis.LoginMetadata) (*entity.Player, error) {
// resending a shard enter packet? // resending a shard enter packet?
old, err := server.getPlayer(peer) old, _ := server.getPlayer(peer)
if old != nil { if old != nil {
return nil, fmt.Errorf("resent enter packet!") return nil, fmt.Errorf("resent enter packet")
} }
// attach player // attach player

View File

@ -16,8 +16,6 @@ import (
type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error
func stubbedPacket(_ *protocol.CNPeer, _ protocol.Packet) error { /* stubbed */ return nil }
type ShardServer struct { type ShardServer struct {
listener net.Listener listener net.Listener
port int port int
@ -67,18 +65,17 @@ func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port
} }
func (server *ShardServer) handleEvents() { func (server *ShardServer) handleEvents() {
for { for event := range server.eRecv {
select { switch event.Type {
case event := <-server.eRecv: case protocol.EVENT_CLIENT_DISCONNECT:
switch event.Type { server.disconnect(event.Peer)
case protocol.EVENT_CLIENT_DISCONNECT: case protocol.EVENT_CLIENT_PACKET:
server.disconnect(event.Peer) if err := server.handlePacket(event.Peer, event.PktID, protocol.NewPacket(event.Pkt)); err != nil {
case protocol.EVENT_CLIENT_PACKET: event.Peer.Kill()
defer pool.Put(event.Pkt)
if err := server.handlePacket(event.Peer, event.PktID, protocol.NewPacket(event.Pkt)); err != nil {
event.Peer.Kill()
}
} }
// the packet is given to us by the event, so we'll need to make sure to return it to the pool
pool.Put(event.Pkt)
} }
} }
} }
@ -143,7 +140,7 @@ func (server *ShardServer) connect(peer *protocol.CNPeer) {
func (server *ShardServer) getPlayer(peer *protocol.CNPeer) (*entity.Player, error) { func (server *ShardServer) getPlayer(peer *protocol.CNPeer) (*entity.Player, error) {
plr, ok := server.peers[peer] plr, ok := server.peers[peer]
if !ok { if !ok {
return nil, fmt.Errorf("Player not found") return nil, fmt.Errorf("player not found")
} }
return plr, nil return plr, nil