GPCM: Don't panic if CloseConnection errors

Fixed #97
This commit is contained in:
Palapeli
2026-12-02 16:51:23 -05:00
parent 4f15ea0db5
commit 6676c2ece4
5 changed files with 36 additions and 20 deletions

27
common/errors.go Normal file
View File

@@ -0,0 +1,27 @@
package common
import "errors"
var (
ErrFrontendBadIndex = errors.New("incorrect connection index")
ErrBackendBusy = errors.New("backend is busy")
)
// ShouldNotError panics if the error is not nil.
func ShouldNotError(err error) {
if err != nil {
panic(err)
}
}
// ShouldNotErrorUnless panics if the error is not nil and not in the allowed list.
func ShouldNotErrorUnless(err error, allowed ...error) {
if err != nil {
for _, a := range allowed {
if err == a {
return
}
}
panic(err)
}
}

View File

@@ -1,19 +1,14 @@
package common
import "reflect"
import (
"reflect"
)
// MaybeUnused is a helper function to mark variables as used to avoid compiler warnings.
func MaybeUnused(v ...interface{}) struct{} {
return struct{}{}
}
// ShouldNotError panics if the error is not nil.
func ShouldNotError(err error) {
if err != nil {
panic(err)
}
}
// ReverseMap takes a map and returns a new map with the keys and values reversed.
func ReverseMap(m interface{}) interface{} {
inputType := reflect.TypeOf(m)

View File

@@ -483,7 +483,7 @@ func (g *GameSpySession) replyError(gpErr GPError) {
if err != nil {
logging.Error("GPCM", "Failed to send packet:", err)
}
common.ShouldNotError(common.CloseConnection(ServerName, g.ConnIndex))
_ = common.CloseConnection(ServerName, g.ConnIndex)
}
logging.Event("gpcm_returned_error", map[string]any{
"profile_id": g.User.ProfileId,
@@ -506,7 +506,7 @@ func (g *GameSpySession) replyError(gpErr GPError) {
if err != nil {
logging.Error("GPCM", "Failed to send packet:", err)
}
common.ShouldNotError(common.CloseConnection(ServerName, g.ConnIndex))
_ = common.CloseConnection(ServerName, g.ConnIndex)
}
logging.Event("gpcm_returned_error", map[string]any{

View File

@@ -27,7 +27,7 @@ func kickPlayer(profileID uint32, reason string) {
case "network_error":
// No error message
common.ShouldNotError(common.CloseConnection(ServerName, session.ConnIndex))
_ = common.CloseConnection(ServerName, session.ConnIndex)
return
}

12
main.go
View File

@@ -1,7 +1,6 @@
package main
import (
"errors"
"io"
"net"
"net/rpc"
@@ -555,11 +554,6 @@ func handleConnection(server serverInfo, conn net.Conn, index uint64) {
}
}
var (
ErrBadIndex = errors.New("incorrect connection index")
ErrorBusy = errors.New("backend is busy")
)
// RPCFrontendPacket.SendPacket is called by the backend to send a packet to a connection
func (r *RPCFrontendPacket) SendPacket(args RPCFrontendPacket, _ *struct{}) error {
rpcMutex.Lock()
@@ -567,7 +561,7 @@ func (r *RPCFrontendPacket) SendPacket(args RPCFrontendPacket, _ *struct{}) erro
conn := connections[args.Server][args.Index]
if conn == nil {
return ErrBadIndex
return common.ErrFrontendBadIndex
}
_, err := (*conn).Write(args.Data)
@@ -582,7 +576,7 @@ func (r *RPCFrontendPacket) CloseConnection(args RPCFrontendPacket, _ *struct{})
conn := connections[args.Server][args.Index]
if conn == nil {
return ErrBadIndex
return common.ErrFrontendBadIndex
}
return (*conn).Close()
@@ -636,7 +630,7 @@ func (r *RPCFrontendPacket) VerifyState(uuid string, reload *bool) error {
rpcMutex.Unlock()
logging.Error("FRONTEND", "Failed to verify UUID, backend is active")
*reload = false
return ErrorBusy
return common.ErrBackendBusy
}
if uuid != frontendUuid {