Zinx
  • Zinx--Golang轻量级并发服务器框架
  • 一、引言
    • 1、写在前面
    • 2、初探Zinx架构
  • 二、初识Zinx框架
    • 1. Zinx-V0.1-基础Server
    • 2.Zinx-V0.2-简单的连接封装与业务绑定
  • 三、Zinx框架基础路由模块
    • 3.1 IRequest 消息请求抽象类
    • 3.2 IRouter 路由配置抽象类
    • 3.3 Zinx-V0.3-集成简单路由功能
    • 3.4 Zinx-V0.3代码实现
    • 3.5 使用Zinx-V0.3完成应用程序
  • 四、Zinx的全局配置
    • 4.1 Zinx-V0.4增添全局配置代码实现
    • 4.2 使用Zinx-V0.4完成应用程序
  • 五、Zinx的消息封装
    • 5.1 创建消息封装类型
    • 5.2 消息的封包与拆包
    • 5.3 Zinx-V0.5代码实现
    • 5.4 使用Zinx-V0.5完成应用程序
  • 六、Zinx的多路由模式
    • 6.1 创建消息管理模块
    • 6.2 Zinx-V0.6代码实现
    • 6.3 使用Zinx-V0.6完成应用程序
Powered by GitBook
On this page
  • A) 创建消息管理模块抽象类
  • B) 实现消息管理模块

Was this helpful?

  1. 六、Zinx的多路由模式

6.1 创建消息管理模块

A) 创建消息管理模块抽象类

在zinx/ziface下创建imsghandler.go文件。

package ziface
/*
    消息管理抽象层
 */
type IMsgHandle interface{
    DoMsgHandler(request IRequest)            //马上以非阻塞方式处理消息
    AddRouter(msgId uint32, router IRouter)    //为消息添加具体的处理逻辑
}

这里面有两个方法,AddRouter()就是添加一个msgId和一个路由关系到Apis中,那么DoMsgHandler()则是调用Router中具体Handle()等方法的接口。

B) 实现消息管理模块

在zinx/znet下创建msghandler.go文件。

package znet

import (
    "fmt"
    "strconv"
    "zinx/ziface"
)

type MsgHandle struct{
    Apis map[uint32] ziface.IRouter //存放每个MsgId 所对应的处理方法的map属性
}

func NewMsgHandle() *MsgHandle {
    return &MsgHandle {
        Apis:make(map[uint32]ziface.IRouter),
    }
}

//马上以非阻塞方式处理消息
func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest)    {
    handler, ok := mh.Apis[request.GetMsgID()]
    if !ok {
        fmt.Println("api msgId = ", request.GetMsgID(), " is not FOUND!")
        return
    }

    //执行对应处理方法
    handler.PreHandle(request)
    handler.Handle(request)
    handler.PostHandle(request)
}
//为消息添加具体的处理逻辑
func (mh *MsgHandle) AddRouter(msgId uint32, router ziface.IRouter) {
    //1 判断当前msg绑定的API处理方法是否已经存在
    if _, ok := mh.Apis[msgId]; ok {
        panic("repeated api , msgId = " + strconv.Itoa(int(msgId)))
    }
    //2 添加msg与api的绑定关系
    mh.Apis[msgId] = router
    fmt.Println("Add api msgId = ", msgId)
}
Previous六、Zinx的多路由模式Next6.2 Zinx-V0.6代码实现

Last updated 6 years ago

Was this helpful?