新增服务间事件通知机制

This commit is contained in:
duanhf2012
2020-04-20 15:38:42 +08:00
parent 990fec396a
commit 742ffc410e
10 changed files with 311 additions and 123 deletions

View File

@@ -57,7 +57,6 @@ type IHttpRouter interface {
POST(url string, handle HttpHandle) bool
Router(session *HttpSession)
PutHttpSession(httpSession *HttpSession)
SetServeFile(method HTTP_METHOD, urlpath string, dirname string) error
SetFormFileKey(formFileKey string)
GetFormFileKey()string
@@ -67,7 +66,7 @@ type IHttpRouter interface {
type HttpRouter struct {
pathRouter map[HTTP_METHOD] map[string] routerMatchData //url地址对应本service地址
serveFileData map[string] *routerServeFileData
eventReciver event.IEventProcessor
//eventReciver event.IEventHandler
httpFiltrateList [] HttpFiltrate
formFileKey string
@@ -104,9 +103,9 @@ type HttpService struct {
func NewHttpHttpRouter(eventReciver event.IEventProcessor) IHttpRouter {
func NewHttpHttpRouter() IHttpRouter {
httpRouter := &HttpRouter{}
httpRouter.eventReciver = eventReciver
//httpRouter.eventReciver = eventHandler
httpRouter.pathRouter =map[HTTP_METHOD] map[string] routerMatchData{}
httpRouter.serveFileData = map[string] *routerServeFileData{}
httpRouter.formFileKey = "file"
@@ -185,8 +184,6 @@ func (slf *HttpSession) WriteJsonDone(statusCode int,msgJson interface{}) error
return err
}
func (slf *HttpSession) flush() {
slf.w.WriteHeader(slf.statusCode)
if slf.msg!=nil {
@@ -235,9 +232,6 @@ func (slf *HttpRouter) GetFormFileKey()string{
return slf.formFileKey
}
func (slf *HttpRouter) PutHttpSession(httpSession *HttpSession){
slf.eventReciver.NotifyEvent(&event.Event{Type:event.Sys_Event_Http_Event,Data:httpSession})
}
func (slf *HttpRouter) GET(url string, handle HttpHandle) bool {
return slf.regRouter(METHOD_GET, url, handle)
@@ -297,8 +291,14 @@ func (slf *HttpRouter) Router(session *HttpSession){
session.Done()
}
func (slf *HttpService) SetHttpRouter(httpRouter IHttpRouter) {
func (slf *HttpService) HttpEventHandler(ev *event.Event) {
ev.Data.(*HttpSession).Handle()
}
func (slf *HttpService) SetHttpRouter(httpRouter IHttpRouter,eventHandler event.IEventHandler) {
slf.httpRouter = httpRouter
slf.RegEventReciverFunc(event.Sys_Event_Http_Event,eventHandler,slf.HttpEventHandler)
}
@@ -521,7 +521,7 @@ func (slf *HttpService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
session.body = body
slf.httpRouter.PutHttpSession(session)
slf.GetEventHandler().NotifyEvent(&event.Event{Type:event.Sys_Event_Http_Event,Data:session})
ticker := time.NewTicker(slf.processTimeout)
select {
case <-ticker.C:

View File

@@ -71,8 +71,41 @@ func (slf *TcpService) OnInit() error{
return nil
}
func (slf *TcpService) SetProcessor(process network.Processor){
type TcpPackType int8
const(
TPT_Connected TcpPackType = 0
TPT_DisConnected TcpPackType = 1
TPT_Pack TcpPackType = 2
TPT_UnknownPack TcpPackType = 3
)
type TcpPack struct {
Type TcpPackType //0表示连接 1表示断开 2表示数据
MsgProcessor network.Processor
ClientId uint64
Data interface{}
}
func (slf *TcpService) TcpEventHandler(ev *event.Event) {
pack := ev.Data.(*TcpPack)
if pack.Type == TPT_Connected {
pack.MsgProcessor.ConnectedRoute(pack.ClientId)
}else if pack.Type == TPT_DisConnected {
pack.MsgProcessor.DisConnectedRoute(pack.ClientId)
} else if pack.Type == TPT_UnknownPack{
pack.MsgProcessor.UnknownMsgRoute(pack.Data,pack.ClientId)
} else if pack.Type == TPT_Pack {
pack.MsgProcessor.MsgRoute(pack.Data, pack.ClientId)
}
}
func (slf *TcpService) SetProcessor(process network.Processor,handler event.IEventHandler){
slf.process = process
slf.RegEventReciverFunc(event.Sys_Event_Tcp,handler,slf.TcpEventHandler)
}
func (slf *TcpService) NewClient(conn *network.TCPConn) network.Agent {
@@ -103,17 +136,14 @@ type Client struct {
tcpService *TcpService
}
type TcpPack struct {
ClientId uint64
Data interface{}
}
func (slf *Client) GetId() uint64 {
return slf.id
}
func (slf *Client) Run() {
slf.tcpService.GetEventReciver().NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp_Connected,Data:&TcpPack{ClientId:slf.id}})
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:&TcpPack{ClientId:slf.id,Type:TPT_Connected,MsgProcessor:slf.tcpService.process}})
for{
bytes,err := slf.tcpConn.ReadMsg()
if err != nil {
@@ -122,17 +152,15 @@ func (slf *Client) Run() {
}
data,err:=slf.tcpService.process.Unmarshal(bytes)
if err != nil {
slf.tcpService.GetEventReciver().NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp_PackException,Data:&TcpPack{ClientId:slf.id,Data:bytes}})
//log.Debug("process.Unmarshal is error:%+v",err)
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:&TcpPack{ClientId:slf.id,Type:TPT_UnknownPack,Data:bytes,MsgProcessor:slf.tcpService.process}})
continue
}
slf.tcpService.GetEventReciver().NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp_RecvPack,Data:&TcpPack{ClientId:slf.id,Data:data}})
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:&TcpPack{ClientId:slf.id,Type:TPT_Pack,Data:data,MsgProcessor:slf.tcpService.process}})
}
}
func (slf *Client) OnClose(){
slf.tcpService.GetEventReciver().NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp_DisConnected,Data:&TcpPack{ClientId:slf.id}})
slf.tcpService.NotifyEvent(&event.Event{Type:event.Sys_Event_Tcp,Data:&TcpPack{ClientId:slf.id,Type:TPT_DisConnected,MsgProcessor:slf.tcpService.process}})
slf.tcpService.mapClientLocker.Lock()
defer slf.tcpService.mapClientLocker.Unlock()
delete (slf.tcpService.mapClient,slf.GetId())