type.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package tinymq
  2. import (
  3. "fmt"
  4. "regexp"
  5. "time"
  6. )
  7. // 中间件函数
  8. // 如果返回为空,表示处理完成,通过
  9. // 如果返回 NEXT_MIDDLE,表示需要下一个中间件函数处理;如果没有下一函数则默认通过
  10. type MiddleFunc func(request *RequestData) (response *ResponseData)
  11. // 订阅频道响应函数
  12. type SubscribeBack func(request *RequestData) (state uint8, result []byte)
  13. // GET 获取数据的回调函数,如果返回 false 则提前结束
  14. type GetBack func(response *ResponseData) (ok bool)
  15. // 线路状态改变时调用
  16. type ConnectStatusFunc func(conn *Line)
  17. // 订阅频道数据结构
  18. type SubscribeData struct {
  19. Channel *regexp.Regexp //频道的正则表达式
  20. Cmd string // 请求的命令
  21. BackFunc SubscribeBack //回调函数,如果状态为 NEXT_SUBSCRIBE 将继续下一个频道调用
  22. }
  23. // 获取数据使用的数据结构
  24. type GetData struct {
  25. Channel *regexp.Regexp
  26. Cmd string
  27. Data []byte
  28. Max int // 获取数据的频道最多有几个,如果为0表示没有限制
  29. Timeout int // 超时时间(毫秒)
  30. backchan chan *ResponseData // 获取响应返回的数据
  31. }
  32. // 连接状态
  33. type ConnectState byte
  34. const (
  35. Disconnected ConnectState = iota
  36. Connected
  37. Closed
  38. )
  39. func (t ConnectState) String() string {
  40. switch t {
  41. case Disconnected:
  42. return "Disconnected"
  43. case Connected:
  44. return "Connected"
  45. case Closed:
  46. return "Closed"
  47. default:
  48. return fmt.Sprintf("Unknown ConnectState (%d)", t)
  49. }
  50. }
  51. // 请求数据包
  52. type RequestData struct {
  53. Id uint16
  54. Cmd string
  55. Data []byte
  56. timeout int // 超时时间,单位为毫秒
  57. backchan chan *ResponseData // 返回数据的管道
  58. conn *Line // 将连接传递出去是为了能够让上层找回来
  59. }
  60. func (r *RequestData) Conn() *Line {
  61. return r.conn
  62. }
  63. type ResponseData struct {
  64. Id uint16
  65. State uint8
  66. Data []byte
  67. conn *Line
  68. }
  69. func (r *ResponseData) Conn() *Line {
  70. return r.conn
  71. }
  72. type PingData struct {
  73. Id uint16
  74. }
  75. // 请求信息,得到回应通过管道传递信息
  76. type GetMsg struct {
  77. out chan *ResponseData
  78. timer *time.Timer
  79. }
  80. // 连接服务结构
  81. type HostInfo struct {
  82. Proto string `json:"proto" yaml:"proto"` // 协议
  83. Version uint8 `json:"version" yaml:"version"` // 版本
  84. Host string `json:"host" yaml:"host"` // 连接的IP地址或者域名
  85. Bind string `json:"bind" yaml:"bind"` // 绑定的地址
  86. Port uint16 `json:"port" yaml:"port"` // 连接的端口
  87. Path string `json:"path" yaml:"path"` // 连接的路径
  88. Hash string `json:"hash" yaml:"hash"` // 连接验证使用,格式 method:key
  89. Proxy bool `json:"proxy" yaml:"proxy"` // 是否代理转发
  90. Errors uint16 `json:"errors" yaml:"errors"` // 连接失败计数,如果成功了则重置为0
  91. Updated time.Time `json:"updated" yaml:"updated"` // 节点信息刷新时间
  92. }
  93. // 获取对应频道的一个连接地址
  94. type ConnectHostFunc func(channel string) (hostInfo *HostInfo, err error)
  95. // 获取认证信息
  96. type AuthFunc func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte)
  97. // 认证合法性函数
  98. type CheckAuthFunc func(proto string, version uint8, channel string, auth []byte) bool