def.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package rocommon
  2. //连接session
  3. type Session interface {
  4. //获得net.Conn
  5. Raw() interface{}
  6. //todo...
  7. Node() ServerNode //tcpAcceptor / tcpConnector
  8. Send(msg interface{})
  9. Close()
  10. //表示ID
  11. ID() uint64
  12. //aes密码(加密解密)
  13. GetAES() *[]byte
  14. SetAES(aes string)
  15. GetHandCode() string
  16. SetHandCode(code string)
  17. GetSessionOpt() interface{}
  18. GetSessionOptFlag() bool
  19. SetSessionOptFlag(flag bool)
  20. HeartBeat(msg interface{})
  21. IncRecvPingNum(incNum int)
  22. RecvPingNum() int
  23. }
  24. //事件处理队列
  25. type NetEventQueue interface {
  26. StartQueue() NetEventQueue
  27. StopQueue() NetEventQueue
  28. Wait()
  29. PostCb(callback func())
  30. AttachUpdateModule(update UpdateModule)
  31. }
  32. //处理主逻辑的更新操作
  33. type UpdateModule interface {
  34. //传入的时间为毫秒
  35. Update(ms uint64)
  36. Init()
  37. }
  38. type UpdateLogic interface {
  39. //传入的时间为毫秒
  40. Update(ms uint64)
  41. }
  42. //event相关
  43. type ProcEvent interface {
  44. Session() Session //会话信息
  45. Msg() interface{} //消息
  46. SeqId() uint32 //消息序列号
  47. KVTime() uint64 //接受到消息时的时间
  48. }
  49. //输入返回输出
  50. type EventHook interface {
  51. InEvent(in ProcEvent) ProcEvent //获得接收事件
  52. OutEvent(out ProcEvent) ProcEvent //获得发送事件
  53. }
  54. type EventCallBack func(e ProcEvent)
  55. //消息处理
  56. type MessageProcessor interface {
  57. //recv
  58. OnRecvMsg(s Session) (interface{}, uint32, error)
  59. //send
  60. OnSendMsg(s Session, msg interface{}) error
  61. }
  62. ///////////////////////////////////
  63. //recv send event -> ProcEvent
  64. type RecvMsgEvent struct {
  65. Sess Session
  66. Message interface{}
  67. Err error
  68. MsgSeqId uint32
  69. KvTime uint64
  70. }
  71. func (this *RecvMsgEvent) Session() Session {
  72. return this.Sess
  73. }
  74. func (this *RecvMsgEvent) Msg() interface{} {
  75. return this.Message
  76. }
  77. func (this *RecvMsgEvent) SeqId() uint32 {
  78. return this.MsgSeqId
  79. }
  80. func (this *RecvMsgEvent) KVTime() uint64 {
  81. return this.KvTime
  82. }
  83. //接收到消息处理后并回复消息(如果需要回复调用该接口)
  84. func (this *RecvMsgEvent) Replay(msg interface{}) error {
  85. this.Sess.Send(msg)
  86. return nil
  87. }
  88. type SendMsgEvent struct {
  89. Sess Session
  90. Message interface{}
  91. }
  92. func (this *SendMsgEvent) Session() Session {
  93. return this.Sess
  94. }
  95. func (this *SendMsgEvent) Msg() interface{} {
  96. return this.Message
  97. }
  98. func (this *SendMsgEvent) SeqId() uint32 {
  99. return 0
  100. }
  101. func (this *SendMsgEvent) KVTime() uint64 {
  102. return 0
  103. }
  104. type ReplayEvent interface {
  105. Replay(msg interface{}) error
  106. }
  107. //直接发送数据,例如game,发给gate,然后gate直接发送
  108. type TransmitPacket struct {
  109. MsgData []byte
  110. MsgId uint32
  111. SeqId uint32
  112. }
  113. ///////////////////////////////////
  114. //http处理
  115. type HTTPRequest struct {
  116. ReqMsg interface{} //request
  117. ResMsg interface{} //response
  118. ReqCodecName string //默认为json
  119. }