mirror of
https://github.com/duanhf2012/origin.git
synced 2026-05-13 11:07:34 +08:00
优化Event增加接口支持
This commit is contained in:
@@ -10,18 +10,25 @@ import (
|
|||||||
const DefaultEventChannelLen = 10000
|
const DefaultEventChannelLen = 10000
|
||||||
|
|
||||||
//事件接受器
|
//事件接受器
|
||||||
type EventCallBack func(event *Event)
|
type EventCallBack func(event IEvent)
|
||||||
|
|
||||||
|
type IEvent interface {
|
||||||
|
GetEventType() EventType
|
||||||
|
}
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
Type EventType
|
Type EventType
|
||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *Event) GetEventType() EventType{
|
||||||
|
return e.Type
|
||||||
|
}
|
||||||
|
|
||||||
type IEventHandler interface {
|
type IEventHandler interface {
|
||||||
Init(processor IEventProcessor)
|
Init(processor IEventProcessor)
|
||||||
GetEventProcessor() IEventProcessor //获得事件
|
GetEventProcessor() IEventProcessor //获得事件
|
||||||
NotifyEvent(*Event)
|
NotifyEvent(IEvent)
|
||||||
Destroy()
|
Destroy()
|
||||||
//注册了事件
|
//注册了事件
|
||||||
addRegInfo(eventType EventType,eventProcessor IEventProcessor)
|
addRegInfo(eventType EventType,eventProcessor IEventProcessor)
|
||||||
@@ -29,19 +36,19 @@ type IEventHandler interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type IEventProcessor interface {
|
type IEventProcessor interface {
|
||||||
EventHandler(ev *Event)
|
EventHandler(ev IEvent)
|
||||||
//同一个IEventHandler,只能接受一个EventType类型回调
|
//同一个IEventHandler,只能接受一个EventType类型回调
|
||||||
RegEventReciverFunc(eventType EventType,reciver IEventHandler,callback EventCallBack)
|
RegEventReciverFunc(eventType EventType,reciver IEventHandler,callback EventCallBack)
|
||||||
UnRegEventReciverFun(eventType EventType,reciver IEventHandler)
|
UnRegEventReciverFun(eventType EventType,reciver IEventHandler)
|
||||||
SetEventChannel(channelNum int) bool
|
SetEventChannel(channelNum int) bool
|
||||||
|
|
||||||
castEvent(event *Event) //广播事件
|
castEvent(event IEvent) //广播事件
|
||||||
pushEvent(event *Event)
|
pushEvent(event IEvent)
|
||||||
addBindEvent(eventType EventType,reciver IEventHandler,callback EventCallBack)
|
addBindEvent(eventType EventType,reciver IEventHandler,callback EventCallBack)
|
||||||
addListen(eventType EventType,reciver IEventHandler)
|
addListen(eventType EventType,reciver IEventHandler)
|
||||||
removeBindEvent(eventType EventType,reciver IEventHandler)
|
removeBindEvent(eventType EventType,reciver IEventHandler)
|
||||||
removeListen(eventType EventType,reciver IEventHandler)
|
removeListen(eventType EventType,reciver IEventHandler)
|
||||||
GetEventChan() chan Event
|
GetEventChan() chan IEvent
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventHandler struct {
|
type EventHandler struct {
|
||||||
@@ -55,7 +62,7 @@ type EventHandler struct {
|
|||||||
|
|
||||||
|
|
||||||
type EventProcessor struct {
|
type EventProcessor struct {
|
||||||
eventChannel chan Event
|
eventChannel chan IEvent
|
||||||
|
|
||||||
locker sync.RWMutex
|
locker sync.RWMutex
|
||||||
mapListenerEvent map[EventType]map[IEventProcessor]int //监听者信息
|
mapListenerEvent map[EventType]map[IEventProcessor]int //监听者信息
|
||||||
@@ -100,7 +107,7 @@ func (handler *EventHandler) GetEventProcessor() IEventProcessor{
|
|||||||
return handler.eventProcessor
|
return handler.eventProcessor
|
||||||
}
|
}
|
||||||
|
|
||||||
func (handler *EventHandler) NotifyEvent(ev *Event){
|
func (handler *EventHandler) NotifyEvent(ev IEvent){
|
||||||
handler.GetEventProcessor().castEvent(ev)
|
handler.GetEventProcessor().castEvent(ev)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,7 +127,7 @@ func (processor *EventProcessor) SetEventChannel(channelNum int) bool{
|
|||||||
channelNum = DefaultEventChannelLen
|
channelNum = DefaultEventChannelLen
|
||||||
}
|
}
|
||||||
|
|
||||||
processor.eventChannel = make(chan Event,channelNum)
|
processor.eventChannel = make(chan IEvent,channelNum)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,18 +201,18 @@ func (handler *EventHandler) Destroy(){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (processor *EventProcessor) GetEventChan() chan Event{
|
func (processor *EventProcessor) GetEventChan() chan IEvent{
|
||||||
processor.locker.Lock()
|
processor.locker.Lock()
|
||||||
defer processor.locker.Unlock()
|
defer processor.locker.Unlock()
|
||||||
|
|
||||||
if processor.eventChannel == nil {
|
if processor.eventChannel == nil {
|
||||||
processor.eventChannel =make(chan Event,DefaultEventChannelLen)
|
processor.eventChannel =make(chan IEvent,DefaultEventChannelLen)
|
||||||
}
|
}
|
||||||
|
|
||||||
return processor.eventChannel
|
return processor.eventChannel
|
||||||
}
|
}
|
||||||
|
|
||||||
func (processor *EventProcessor) EventHandler(ev *Event) {
|
func (processor *EventProcessor) EventHandler(ev IEvent) {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
buf := make([]byte, 4096)
|
buf := make([]byte, 4096)
|
||||||
@@ -215,7 +222,7 @@ func (processor *EventProcessor) EventHandler(ev *Event) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
mapCallBack,ok := processor.mapBindHandlerEvent[ev.Type]
|
mapCallBack,ok := processor.mapBindHandlerEvent[ev.GetEventType()]
|
||||||
if ok == false {
|
if ok == false {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -224,24 +231,24 @@ func (processor *EventProcessor) EventHandler(ev *Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (processor *EventProcessor) pushEvent(event *Event){
|
func (processor *EventProcessor) pushEvent(event IEvent){
|
||||||
if len(processor.eventChannel)>=cap(processor.eventChannel){
|
if len(processor.eventChannel)>=cap(processor.eventChannel){
|
||||||
log.Error("event process channel is full.")
|
log.Error("event process channel is full.")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
processor.eventChannel<-*event
|
processor.eventChannel<-event
|
||||||
}
|
}
|
||||||
|
|
||||||
func (processor *EventProcessor) castEvent(event *Event){
|
func (processor *EventProcessor) castEvent(event IEvent){
|
||||||
if processor.mapListenerEvent == nil {
|
if processor.mapListenerEvent == nil {
|
||||||
log.Error("mapListenerEvent not init!")
|
log.Error("mapListenerEvent not init!")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
eventProcessor,ok := processor.mapListenerEvent[event.Type]
|
eventProcessor,ok := processor.mapListenerEvent[event.GetEventType()]
|
||||||
if ok == false || processor == nil{
|
if ok == false || processor == nil{
|
||||||
log.Debug("event type %d not listen.",event.Type)
|
log.Debug("event type %d not listen.",event.GetEventType())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -128,9 +128,9 @@ func (s *Service) Run() {
|
|||||||
}
|
}
|
||||||
case ev := <- eventChan:
|
case ev := <- eventChan:
|
||||||
if s.profiler!=nil {
|
if s.profiler!=nil {
|
||||||
analyzer = s.profiler.Push(fmt.Sprintf("[Event]%d", int(ev.Type)))
|
analyzer = s.profiler.Push(fmt.Sprintf("[Event]%d", int(ev.GetEventType())))
|
||||||
}
|
}
|
||||||
s.eventProcessor.EventHandler(&ev)
|
s.eventProcessor.EventHandler(ev)
|
||||||
if analyzer!=nil {
|
if analyzer!=nil {
|
||||||
analyzer.Pop()
|
analyzer.Pop()
|
||||||
analyzer = nil
|
analyzer = nil
|
||||||
|
|||||||
@@ -286,8 +286,8 @@ func (slf *HttpRouter) Router(session *HttpSession){
|
|||||||
session.Done()
|
session.Done()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (httpService *HttpService) HttpEventHandler(ev *event.Event) {
|
func (httpService *HttpService) HttpEventHandler(ev event.IEvent) {
|
||||||
ev.Data.(*HttpSession).Handle()
|
ev.(*event.Event).Data.(*HttpSession).Handle()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (httpService *HttpService) SetHttpRouter(httpRouter IHttpRouter,eventHandler event.IEventHandler) {
|
func (httpService *HttpService) SetHttpRouter(httpRouter IHttpRouter,eventHandler event.IEventHandler) {
|
||||||
|
|||||||
@@ -135,8 +135,8 @@ func (tcpService *TcpService) OnInit() error{
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tcpService *TcpService) TcpEventHandler(ev *event.Event) {
|
func (tcpService *TcpService) TcpEventHandler(ev event.IEvent) {
|
||||||
pack := ev.Data.(TcpPack)
|
pack := ev.(*event.Event).Data.(TcpPack)
|
||||||
switch pack.Type {
|
switch pack.Type {
|
||||||
case TPT_Connected:
|
case TPT_Connected:
|
||||||
tcpService.process.ConnectedRoute(pack.ClientId)
|
tcpService.process.ConnectedRoute(pack.ClientId)
|
||||||
|
|||||||
Reference in New Issue
Block a user