type.go 3.0 KB

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