Expose lower register functions
Added RegisterLow and RegisterUntilLow this gives more power to the user
This commit is contained in:
+19
-24
@@ -67,8 +67,6 @@ func (e MainHandlerErr) Unwrap() error {
|
|||||||
// DontForwardErr will stop the event from being forwarded to other handlers, including anything handlers.
|
// DontForwardErr will stop the event from being forwarded to other handlers, including anything handlers.
|
||||||
var DontForwardErr = errors.New("dont forward")
|
var DontForwardErr = errors.New("dont forward")
|
||||||
var WhileClosingErr = errors.New("error while closing")
|
var WhileClosingErr = errors.New("error while closing")
|
||||||
|
|
||||||
// CloseNonCriticalErr will close even when the event is non-critical
|
|
||||||
var ContextDoneErr = errors.New("context done")
|
var ContextDoneErr = errors.New("context done")
|
||||||
var RecvNotOkErr = errors.New("recv not ok")
|
var RecvNotOkErr = errors.New("recv not ok")
|
||||||
|
|
||||||
@@ -114,40 +112,37 @@ func (l *Loop) RegisterEventSource(ch <-chan any) {
|
|||||||
// Instead, it will log it to slog.Error
|
// Instead, it will log it to slog.Error
|
||||||
// Warning: If a bad handler is passed in the program will panic.
|
// Warning: If a bad handler is passed in the program will panic.
|
||||||
func (l *Loop) Register(h any) {
|
func (l *Loop) Register(h any) {
|
||||||
l.RegisterCustomType(ValidateHandler(h))
|
eventType, hv := ValidateHandler(h)
|
||||||
|
l.RegisterLow(eventType, hv, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterCustomType registers a handler that will not close the loop if it returns an error.
|
// RegisterLow registers a handler that will not close the loop if it returns an error.
|
||||||
// Instead, it will log it to slog.Error
|
// Instead, it will log it to slog.Error
|
||||||
|
// This does not hold true for signaling errors like HandlerDoneErr or DontForwardErr
|
||||||
// Warning: If a bad handler is passed in the program will panic.
|
// Warning: If a bad handler is passed in the program will panic.
|
||||||
func (l *Loop) RegisterCustomType(eventType reflect.Type, hv reflect.Value) {
|
func (l *Loop) RegisterLow(eventType reflect.Type, hv reflect.Value, critical bool) {
|
||||||
handleFunc := func(packet any) error {
|
handleFunc := hv
|
||||||
v := hv.Call([]reflect.Value{reflect.ValueOf(packet)})[0]
|
if !critical {
|
||||||
if !v.IsNil() {
|
handleFunc = reflect.ValueOf(func(packet any) error {
|
||||||
err := v.Interface().(error)
|
v := hv.Call([]reflect.Value{reflect.ValueOf(packet)})[0]
|
||||||
if errors.As(err, &HandlerDoneErr{}) || errors.Is(err, DontForwardErr) {
|
if !v.IsNil() {
|
||||||
return err
|
err := v.Interface().(error)
|
||||||
|
if errors.As(err, &HandlerDoneErr{}) || errors.Is(err, DontForwardErr) {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
slog.Error("handler failed", "err", err)
|
||||||
}
|
}
|
||||||
slog.Error("handler failed", "err", err)
|
return nil
|
||||||
}
|
})
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
l.RegisterDirect(eventType, reflect.ValueOf(handleFunc))
|
l.HandlerFunctions[eventType] = append(l.HandlerFunctions[eventType], handleFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterCritical registers a handler that will close the loop if it returns an error.
|
// RegisterCritical registers a handler that will close the loop if it returns an error.
|
||||||
// Warning: If a bad handler is passed in the program will panic.
|
// Warning: If a bad handler is passed in the program will panic.
|
||||||
func (l *Loop) RegisterCritical(h any) {
|
func (l *Loop) RegisterCritical(h any) {
|
||||||
eventType, hv := ValidateHandler(h)
|
eventType, hv := ValidateHandler(h)
|
||||||
l.RegisterDirect(eventType, hv)
|
l.HandlerFunctions[eventType] = append(l.HandlerFunctions[eventType], hv)
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Loop) RegisterDirect(k reflect.Type, h reflect.Value) {
|
|
||||||
l.HandlerFunctions[k] = append(l.HandlerFunctions[k], h)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (l *Loop) RegisterIgnore(t any) {
|
|
||||||
l.RegisterDirect(reflect.TypeOf(t), reflect.ValueOf(func(any) error { return nil }))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// FireFound triggers all handlers registered for the specified event type and returns if handlers were found or an error occurred.
|
// FireFound triggers all handlers registered for the specified event type and returns if handlers were found or an error occurred.
|
||||||
|
|||||||
+13
-18
@@ -63,36 +63,35 @@ func SmartConvert(from reflect.Value, to reflect.Type) (ret reflect.Value) {
|
|||||||
// RegisterUntilLatest registers a handler for all versions between the passed in packet version and the latest supported version.
|
// RegisterUntilLatest registers a handler for all versions between the passed in packet version and the latest supported version.
|
||||||
// Warning: If a bad handler is passed in, the program will panic.
|
// Warning: If a bad handler is passed in, the program will panic.
|
||||||
func (c *Conn) RegisterUntilLatest(h any) {
|
func (c *Conn) RegisterUntilLatest(h any) {
|
||||||
c.registerUntil(h, LatestVersion, false)
|
eventType, hv := event.ValidateHandler(h)
|
||||||
|
c.RegisterUntilLow(eventType, hv, LatestVersion, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterUntil registers a handler for all versions between the passed in packet version and the specified version.
|
// RegisterUntil registers a handler for all versions between the passed in packet version and the specified version.
|
||||||
// Warning: If a bad handler is passed in, the program will panic.
|
// Warning: If a bad handler is passed in, the program will panic.
|
||||||
func (c *Conn) RegisterUntil(h any, until string) {
|
func (c *Conn) RegisterUntil(h any, until string) {
|
||||||
c.registerUntil(h, until, false)
|
eventType, hv := event.ValidateHandler(h)
|
||||||
|
c.RegisterUntilLow(eventType, hv, until, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterCriticalUntilLatest registers a handler for all versions between the passed in packet version and the latest supported version.
|
// RegisterCriticalUntilLatest registers a handler for all versions between the passed in packet version and the latest supported version.
|
||||||
// If this handler returns an error, the connection will be closed.
|
// If this handler returns an error, the connection will be closed.
|
||||||
// Warning: If a bad handler is passed in, the program will panic.
|
// Warning: If a bad handler is passed in, the program will panic.
|
||||||
func (c *Conn) RegisterCriticalUntilLatest(h any) {
|
func (c *Conn) RegisterCriticalUntilLatest(h any) {
|
||||||
c.registerUntil(h, LatestVersion, true)
|
eventType, hv := event.ValidateHandler(h)
|
||||||
|
c.RegisterUntilLow(eventType, hv, LatestVersion, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterCriticalUntil registers a handler for all versions between the passed in packet version and the specified version.
|
// RegisterCriticalUntil registers a handler for all versions between the passed in packet version and the specified version.
|
||||||
// If this handler returns an error, the connection will be closed.
|
// If this handler returns an error, the connection will be closed.
|
||||||
// Warning: If a bad handler is passed in, the program will panic.
|
// Warning: If a bad handler is passed in, the program will panic.
|
||||||
func (c *Conn) RegisterCriticalUntil(h any, until string) {
|
func (c *Conn) RegisterCriticalUntil(h any, until string) {
|
||||||
c.registerUntil(h, until, true)
|
eventType, hv := event.ValidateHandler(h)
|
||||||
|
c.RegisterUntilLow(eventType, hv, until, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) registerUntil(h any, until string, critical bool) {
|
func (c *Conn) RegisterUntilLow(eventType reflect.Type, hv reflect.Value, until string, critical bool) {
|
||||||
eventType, hv := event.ValidateHandler(h)
|
c.RegisterLow(eventType, hv, critical)
|
||||||
if critical {
|
|
||||||
c.RegisterDirect(eventType, hv)
|
|
||||||
} else {
|
|
||||||
c.RegisterCustomType(eventType, hv)
|
|
||||||
}
|
|
||||||
if !eventType.Implements(reflect.TypeFor[proto_base.EncodeDecodeAble]()) {
|
if !eventType.Implements(reflect.TypeFor[proto_base.EncodeDecodeAble]()) {
|
||||||
panic("expected method with argument that implements proto_base.EncodeDecodeAble")
|
panic("expected method with argument that implements proto_base.EncodeDecodeAble")
|
||||||
}
|
}
|
||||||
@@ -110,12 +109,12 @@ func (c *Conn) registerUntil(h any, until string, critical bool) {
|
|||||||
versionName := proto_generated.SupportedVersions[i]
|
versionName := proto_generated.SupportedVersions[i]
|
||||||
versionInfo, err = data.LookUpVersionByName(versionName)
|
versionInfo, err = data.LookUpVersionByName(versionName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic("protocol version not found")
|
panic("protocol version not found this should not happen even if a wrong type is passed in")
|
||||||
}
|
}
|
||||||
var newPacketInfo proto_base.PacketInfo
|
var newPacketInfo proto_base.PacketInfo
|
||||||
newPacketInfo, found = LookupPacketInfoByNameProtocolVersionStateAndDirection(packetInfo.Name, versionInfo.Version, packetInfo.State, packetInfo.Direction)
|
newPacketInfo, found = LookupPacketInfoByNameProtocolVersionStateAndDirection(packetInfo.Name, versionInfo.Version, packetInfo.State, packetInfo.Direction)
|
||||||
if !found {
|
if !found {
|
||||||
panic("packet not found")
|
panic("packet not found in future version")
|
||||||
}
|
}
|
||||||
newT := reflect.TypeOf(newPacketInfo.Type)
|
newT := reflect.TypeOf(newPacketInfo.Type)
|
||||||
if !SmartConvertibleTo(newT, reflect.TypeOf(packetInfo.Type)) {
|
if !SmartConvertibleTo(newT, reflect.TypeOf(packetInfo.Type)) {
|
||||||
@@ -128,10 +127,6 @@ func (c *Conn) registerUntil(h any, until string, critical bool) {
|
|||||||
}
|
}
|
||||||
return v.Interface().(error)
|
return v.Interface().(error)
|
||||||
}
|
}
|
||||||
if critical {
|
c.RegisterLow(newT, reflect.ValueOf(handleFunc), critical)
|
||||||
c.RegisterDirect(newT, reflect.ValueOf(handleFunc))
|
|
||||||
} else {
|
|
||||||
c.RegisterCustomType(newT, reflect.ValueOf(handleFunc))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user