type.go 2.9 KB

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