diff --git a/event/event.go b/event/event.go index 26f699f..f5b20ac 100644 --- a/event/event.go +++ b/event/event.go @@ -10,7 +10,7 @@ import ( const Default_EventChannelLen = 10000 //事件接受器 -type EventReciver func(event *Event) error +type EventReciverFunc func(event *Event) type Event struct { Type EventType @@ -19,10 +19,12 @@ type Event struct { type IEventProcessor interface { NotifyEvent(*Event) - OnEventHandler(event *Event) error + SetEventReciver(eventProcessor IEventProcessor) GetEventReciver() IEventProcessor SetEventChanNum(num int32) bool + RegEventReciverFunc(eventType EventType,reciverFunc EventReciverFunc) + UnRegEventReciverFun(eventType EventType) } type EventProcessor struct { @@ -32,6 +34,15 @@ type EventProcessor struct { eventChanNumLocker sync.RWMutex eventChanNum int32 + mapEventReciverFunc map[EventType]EventReciverFunc +} + +func (slf *EventProcessor) RegEventReciverFunc(eventType EventType,reciverFunc EventReciverFunc){ + slf.mapEventReciverFunc[eventType] = reciverFunc +} + +func (slf *EventProcessor) UnRegEventReciverFun(eventType EventType){ + delete(slf.mapEventReciverFunc,eventType) } func (slf *EventProcessor) NotifyEvent(pEvent *Event) { @@ -41,9 +52,7 @@ func (slf *EventProcessor) NotifyEvent(pEvent *Event) { slf.EventChan <-pEvent } -func (slf *EventProcessor) OnEventHandler(event *Event) error{ - return nil -} + func (slf *EventProcessor) GetEventChan() chan *Event{ slf.eventChanNumLocker.Lock() @@ -85,7 +94,7 @@ type IHttpEventData interface { Handle() } -func (slf *EventProcessor) EventHandler(processor IEventProcessor,ev *Event) { +func (slf *EventProcessor) EventHandler(ev *Event) { defer func() { if r := recover(); r != nil { buf := make([]byte, 4096) @@ -99,7 +108,11 @@ func (slf *EventProcessor) EventHandler(processor IEventProcessor,ev *Event) { return } - processor.OnEventHandler(ev) + if fun,ok := slf.mapEventReciverFunc[ev.Type];ok == false{ + return + }else{ + fun(ev) + } } func (slf *EventProcessor) innerEventHandler(ev *Event) bool { diff --git a/service/service.go b/service/service.go index db8ab8c..31ed0f6 100644 --- a/service/service.go +++ b/service/service.go @@ -2,7 +2,6 @@ package service import ( "fmt" - "github.com/duanhf2012/origin/event" "github.com/duanhf2012/origin/log" "github.com/duanhf2012/origin/profiler" "github.com/duanhf2012/origin/rpc" @@ -130,7 +129,7 @@ func (slf *Service) Run() { if slf.profiler!=nil { analyzer = slf.profiler.Push(fmt.Sprintf("Event_%d", int(ev.Type))) } - slf.EventHandler(slf.this.(event.IEventProcessor),ev) + slf.EventHandler(ev) if analyzer!=nil { analyzer.Pop() analyzer = nil