type.go 3.7 KB

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