mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-03 22:45:13 +08:00
提交origin2.0版本
This commit is contained in:
80
event/event.go
Normal file
80
event/event.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package event
|
||||
|
||||
import (
|
||||
"github.com/duanhf2012/originnet/log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
const Default_EventChannelLen = 10000
|
||||
|
||||
//事件接受器
|
||||
type EventReciver func(event *Event) error
|
||||
|
||||
type Event struct {
|
||||
Type EventType
|
||||
Data interface{}
|
||||
}
|
||||
|
||||
type IEventProcessor interface {
|
||||
NotifyEvent(*Event)
|
||||
OnEventHandler(event *Event) error
|
||||
SetEventReciver(eventProcessor IEventProcessor)
|
||||
GetEventReciver() IEventProcessor
|
||||
SetEventChanNum(num int32) bool
|
||||
}
|
||||
|
||||
type EventProcessor struct {
|
||||
//事件管道
|
||||
EventChan chan *Event
|
||||
eventReciver IEventProcessor
|
||||
|
||||
eventChanNumLocker sync.RWMutex
|
||||
eventChanNum int32
|
||||
}
|
||||
|
||||
func (slf *EventProcessor) NotifyEvent(pEvent *Event) {
|
||||
if len(slf.EventChan) >= int(slf.eventChanNum) {
|
||||
log.Error("event queue is full!")
|
||||
}
|
||||
slf.EventChan <-pEvent
|
||||
}
|
||||
|
||||
func (slf *EventProcessor) OnEventHandler(event *Event) error{
|
||||
return nil
|
||||
}
|
||||
|
||||
func (slf *EventProcessor) GetEventChan() chan *Event{
|
||||
slf.eventChanNumLocker.Lock()
|
||||
defer slf.eventChanNumLocker.Unlock()
|
||||
|
||||
if slf.eventChanNum == 0 {
|
||||
slf.eventChanNum = Default_EventChannelLen
|
||||
}
|
||||
|
||||
if slf.EventChan == nil {
|
||||
slf.EventChan = make(chan *Event,slf.eventChanNum)
|
||||
}
|
||||
|
||||
return slf.EventChan
|
||||
}
|
||||
|
||||
//不允许重复设置
|
||||
func (slf *EventProcessor) SetEventChanNum(num int32) bool {
|
||||
slf.eventChanNumLocker.Lock()
|
||||
defer slf.eventChanNumLocker.Unlock()
|
||||
if slf.eventChanNum>0 {
|
||||
return false
|
||||
}
|
||||
|
||||
slf.eventChanNum = num
|
||||
return true
|
||||
}
|
||||
|
||||
func (slf *EventProcessor) SetEventReciver(eventProcessor IEventProcessor){
|
||||
slf.eventReciver = eventProcessor
|
||||
}
|
||||
|
||||
|
||||
func (slf *EventProcessor) GetEventReciver() IEventProcessor{
|
||||
return slf.eventReciver
|
||||
}
|
||||
14
event/eventtype.go
Normal file
14
event/eventtype.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package event
|
||||
|
||||
type EventType int
|
||||
|
||||
//大于Sys_Event_User_Define给用户定义
|
||||
const (
|
||||
Sys_Event_Tcp_Connected EventType= 1
|
||||
Sys_Event_Tcp_DisConnected EventType= 2
|
||||
Sys_Event_Tcp_RecvPack EventType = 3
|
||||
|
||||
|
||||
Sys_Event_User_Define EventType = 1000
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user