type.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package tinymq
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. // 中间件函数
  7. // 如果返回为空,表示处理完成,通过
  8. // 如果返回 NEXT_MIDDLE,表示需要下一个中间件函数处理;如果没有下一函数则默认通过
  9. type MiddleFunc func(request *RequestData) (response *ResponseData)
  10. // 订阅频道响应函数
  11. type SubscribeBackFunc func(request *RequestData) (state uint8, result any)
  12. // GET 获取数据的回调函数,如果返回 false 则提前结束
  13. type GetBackFunc func(response *ResponseData) (ok bool)
  14. // 线路状态改变时调用
  15. type ConnectStatusFunc func(conn *Line)
  16. // 频道过滤器函数,如果返回true表示成功匹配
  17. type FilterFunc func(conn *Line) (ok bool)
  18. // 通过过滤函数获取一个频道信息
  19. type FilterToChannelFunc func(filter FilterFunc) (channel string)
  20. // 订阅频道数据结构
  21. type SubscribeData struct {
  22. Filter FilterFunc // 频道匹配过滤
  23. Cmd string // 请求的命令
  24. BackFunc SubscribeBackFunc //回调函数,如果状态为 NEXT_SUBSCRIBE 将继续下一个频道调用
  25. }
  26. // 获取数据使用的数据结构
  27. type GetData struct {
  28. Filter FilterFunc // 命令匹配过滤
  29. Cmd string
  30. Data any
  31. Max int // 获取数据的频道最多有几个,如果为0表示没有限制
  32. Timeout int // 超时时间(毫秒)
  33. Rand bool // 是否使用随机的数列
  34. backchan chan *ResponseData // 获取响应返回的数据
  35. }
  36. // 连接状态
  37. type ConnectState byte
  38. const (
  39. Disconnected ConnectState = iota
  40. Connected
  41. Closed
  42. )
  43. func (t ConnectState) String() string {
  44. switch t {
  45. case Disconnected:
  46. return "Disconnected"
  47. case Connected:
  48. return "Connected"
  49. case Closed:
  50. return "Closed"
  51. default:
  52. return fmt.Sprintf("Unknown ConnectState (%d)", t)
  53. }
  54. }
  55. // 主机类型
  56. type HostType byte
  57. const (
  58. Direct HostType = iota
  59. Proxy
  60. Both
  61. )
  62. // 请求数据包
  63. type RequestData struct {
  64. Id uint16
  65. Cmd string
  66. Data []byte
  67. timeout int // 超时时间,单位为毫秒
  68. backchan chan *ResponseData // 返回数据的管道
  69. conn *Line // 将连接传递出去是为了能够让上层找回来
  70. }
  71. func (r *RequestData) Conn() *Line {
  72. return r.conn
  73. }
  74. type ResponseData struct {
  75. Id uint16
  76. State uint8
  77. Data []byte
  78. conn *Line
  79. }
  80. // 获取当前的连接
  81. func (r *ResponseData) Conn() *Line {
  82. return r.conn
  83. }
  84. // 输出状态和数据,方便直接返回
  85. func (r *ResponseData) Out() (state uint8, data []byte) {
  86. return r.State, r.Data
  87. }
  88. // 输出可视化
  89. func (r *ResponseData) String() string {
  90. return fmt.Sprintf("id: %d, state: [%d]%s, data: %s", r.Id, r.State, IdMsg(r.State), string(r.Data))
  91. }
  92. type PingData struct {
  93. Id uint16
  94. }
  95. // 请求信息,得到回应通过管道传递信息
  96. type GetMsg struct {
  97. out chan *ResponseData
  98. timer *time.Timer
  99. }
  100. // 获取对应频道的一个连接地址
  101. type ConnectHostFunc func(channel string, hostType HostType) (hostInfo *HostInfo, err error)
  102. // 获取认证信息
  103. type AuthFunc func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte)
  104. // 认证合法性函数
  105. type CheckAuthFunc func(client bool, proto string, version uint8, channel string, auth []byte) bool
  106. // 验证发送的数据条件是否满足
  107. type CheckConnectOkFunc = func(line *Line, data *GetData) bool