优化
This commit is contained in:
parent
a30b918377
commit
930e08bbdc
@ -3,10 +3,16 @@ package processor
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/fox/fox/ipb"
|
"github.com/fox/fox/ipb"
|
||||||
|
"github.com/fox/fox/log"
|
||||||
|
"github.com/fox/fox/timer"
|
||||||
"github.com/golang/protobuf/proto"
|
"github.com/golang/protobuf/proto"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func initLog() {
|
||||||
|
log.Open("test.log", log.DebugL)
|
||||||
|
}
|
||||||
|
|
||||||
func onChat(userId int64, req *ipb.InternalMsg) {
|
func onChat(userId int64, req *ipb.InternalMsg) {
|
||||||
_ = userId
|
_ = userId
|
||||||
fmt.Println("onChat.", string(req.Msg))
|
fmt.Println("onChat.", string(req.Msg))
|
||||||
@ -46,3 +52,30 @@ func TestProcessorAny(t *testing.T) {
|
|||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TimerType int
|
||||||
|
|
||||||
|
const (
|
||||||
|
TtSecond TimerType = 5 // 每秒定时器
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t TimerType) Number() int32 {
|
||||||
|
return int32(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t TimerType) String() string {
|
||||||
|
switch t {
|
||||||
|
case TtSecond:
|
||||||
|
return "TtSecond"
|
||||||
|
}
|
||||||
|
return "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTimerProcessor(t *testing.T) {
|
||||||
|
initLog()
|
||||||
|
p := NewTimerProcessor()
|
||||||
|
p.RegisterMessages(map[timer.ITimeType]any{
|
||||||
|
TtSecond: func(t *testing.T) { t.Log(TtSecond) },
|
||||||
|
})
|
||||||
|
_ = p.Dispatch(TimerType(5), t)
|
||||||
|
}
|
||||||
|
49
processor/timeProcessor.go
Normal file
49
processor/timeProcessor.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package processor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/fox/fox/log"
|
||||||
|
"github.com/fox/fox/timer"
|
||||||
|
"reflect"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RegisterTimerMetas map[timer.ITimeType]any
|
||||||
|
|
||||||
|
func NewTimerProcessor() *TimerProcessor {
|
||||||
|
return &TimerProcessor{
|
||||||
|
delegates: make(map[timer.ITimeType]reflect.Value),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type TimerProcessor struct {
|
||||||
|
delegates map[timer.ITimeType]reflect.Value
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *TimerProcessor) RegisterMessages(metas RegisterTimerMetas) {
|
||||||
|
for cmd, delegate := range metas {
|
||||||
|
h.RegisterMessage(cmd, delegate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *TimerProcessor) RegisterMessage(cmd timer.ITimeType, delegate any) {
|
||||||
|
h.delegates[cmd] = reflect.ValueOf(delegate)
|
||||||
|
log.DebugF("processor register message %v", cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *TimerProcessor) UnregisterMessage(cmd timer.ITimeType) {
|
||||||
|
delete(h.delegates, cmd)
|
||||||
|
log.DebugF("processor unregister message %v", cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *TimerProcessor) Dispatch(cmd timer.ITimeType, params ...any) error {
|
||||||
|
in := make([]reflect.Value, len(params))
|
||||||
|
for i, param := range params {
|
||||||
|
in[i] = reflect.ValueOf(param)
|
||||||
|
}
|
||||||
|
delegate, ok := h.delegates[cmd]
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("cmd %d delegates not found", cmd)
|
||||||
|
}
|
||||||
|
delegate.Call(in)
|
||||||
|
return nil
|
||||||
|
}
|
@ -9,3 +9,8 @@ type ITimer interface {
|
|||||||
CancelTimer(timerId uint32)
|
CancelTimer(timerId uint32)
|
||||||
CancelAllTimer()
|
CancelAllTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ITimeType interface {
|
||||||
|
Number() int32
|
||||||
|
String() string
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user