type.go 2.6 KB

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