type.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. Proxing // 正在进行Proxy请求
  42. Proxied // Proxy 连接成功状态
  43. Closed
  44. )
  45. func (t ConnectState) String() string {
  46. switch t {
  47. case Disconnected:
  48. return "Disconnected"
  49. case Connected:
  50. return "Connected"
  51. case Proxing:
  52. return "Proxing"
  53. case Proxied:
  54. return "Proxied"
  55. case Closed:
  56. return "Closed"
  57. default:
  58. return fmt.Sprintf("Unknown ConnectState (%d)", t)
  59. }
  60. }
  61. // 主机类型
  62. type HostType byte
  63. const (
  64. Direct HostType = iota
  65. Proxy
  66. Both
  67. )
  68. // 请求数据包
  69. type RequestData struct {
  70. Id uint16
  71. Cmd string
  72. Data []byte
  73. timeout int // 超时时间,单位为毫秒
  74. backchan chan *ResponseData // 返回数据的管道
  75. conn *Line // 将连接传递出去是为了能够让上层找回来
  76. }
  77. func (r *RequestData) Conn() *Line {
  78. return r.conn
  79. }
  80. type ResponseData struct {
  81. Id uint16
  82. State uint8
  83. Data []byte
  84. conn *Line
  85. }
  86. // 获取当前的连接
  87. func (r *ResponseData) Conn() *Line {
  88. return r.conn
  89. }
  90. // 输出状态和数据,方便直接返回
  91. func (r *ResponseData) Out() (state uint8, data []byte) {
  92. return r.State, r.Data
  93. }
  94. // 输出可视化
  95. func (r *ResponseData) String() string {
  96. return fmt.Sprintf("id: %d, state: [%d]%s, data: %s", r.Id, r.State, IdMsg(r.State), string(r.Data))
  97. }
  98. type PingData struct {
  99. Id uint16
  100. }
  101. // 请求信息,得到回应通过管道传递信息
  102. type GetMsg struct {
  103. out chan *ResponseData
  104. timer *time.Timer
  105. }
  106. // 获取对应频道的一个连接地址
  107. type ConnectHostFunc func(channel string, hostType HostType) (hostInfo *HostInfo, err error)
  108. // 获取认证信息
  109. type AuthFunc func(client bool, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte)
  110. // 认证合法性函数
  111. type CheckAuthFunc func(client bool, proto string, version uint8, channel string, auth []byte) bool
  112. // 验证发送的数据条件是否满足
  113. type CheckConnectOkFunc = func(line *Line, data *GetData) bool