You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
999 B

  1. package znet
  2. import (
  3. "fmt"
  4. "strconv"
  5. "zinx/ziface"
  6. )
  7. type MsgHandle struct{
  8. Apis map[uint32] ziface.IRouter //存放每个MsgId 所对应的处理方法的map属性
  9. }
  10. func NewMsgHandle() *MsgHandle {
  11. return &MsgHandle {
  12. Apis:make(map[uint32]ziface.IRouter),
  13. }
  14. }
  15. //马上以非阻塞方式处理消息
  16. func (mh *MsgHandle) DoMsgHandler(request ziface.IRequest) {
  17. handler, ok := mh.Apis[request.GetMsgID()]
  18. if !ok {
  19. fmt.Println("api msgId = ", request.GetMsgID(), " is not FOUND!")
  20. return
  21. }
  22. //执行对应处理方法
  23. handler.PreHandle(request)
  24. handler.Handle(request)
  25. handler.PostHandle(request)
  26. }
  27. //为消息添加具体的处理逻辑
  28. func (mh *MsgHandle) AddRouter(msgId uint32, router ziface.IRouter) {
  29. //1 判断当前msg绑定的API处理方法是否已经存在
  30. if _, ok := mh.Apis[msgId]; ok {
  31. panic("repeated api , msgId = " + strconv.Itoa(int(msgId)))
  32. }
  33. //2 添加msg与api的绑定关系
  34. mh.Apis[msgId] = router
  35. fmt.Println("Add api msgId = ", msgId)
  36. }