mirror of
https://github.com/WiiLink24/wfc-server.git
synced 2026-12-04 22:19:00 -06:00
27
common/errors.go
Normal file
27
common/errors.go
Normal 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)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
|
||||
@@ -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{
|
||||
|
||||
@@ -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
12
main.go
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user