package tinymq import ( "fmt" "time" "git.me9.top/git/tinymq/conn" ) // 中间件函数 // 如果返回为空,表示处理完成,通过 // 如果返回 NEXT_MIDDLE,表示需要下一个中间件函数处理;如果没有下一函数则默认通过 type MiddleFunc func(request *RequestData) (response *ResponseData) // 订阅频道响应函数 type SubscribeBackFunc func(request *RequestData) (state uint8, result any) // GET 获取数据的回调函数,如果返回 false 则提前结束 type GetBackFunc func(response *ResponseData) (ok bool) // 线路状态改变时调用 type ConnectStatusFunc func(conn *Line) // 频道过滤器函数,如果返回true表示成功匹配 type FilterFunc func(conn *Line) (ok bool) // 通过过滤函数获取一个频道信息 type FilterToChannelFunc func(filter FilterFunc) (channel string) // 订阅频道数据结构 type SubscribeData struct { Filter FilterFunc // 频道匹配过滤 Cmd string // 请求的命令 BackFunc SubscribeBackFunc //回调函数,如果状态为 NEXT_SUBSCRIBE 将继续下一个频道调用 } // 获取数据使用的数据结构 type GetData struct { Filter FilterFunc // 命令匹配过滤 Cmd string Data any Max int // 获取数据的频道最多有几个,如果为0表示没有限制 Timeout int // 超时时间(毫秒) Rand bool // 是否使用随机的数列 backchan chan *ResponseData // 获取响应返回的数据 } // 连接状态 type ConnectState byte const ( StateDisconnected ConnectState = iota StateConnected StateProxing // 正在进行Proxy请求 StateProxied // Proxy 连接成功状态 StateClosed // 已经被关闭的连接 ) func (t ConnectState) String() string { switch t { case StateDisconnected: return "Disconnected" case StateConnected: return "Connected" case StateProxing: return "Proxing" case StateProxied: return "Proxied" case StateClosed: return "Closed" default: return fmt.Sprintf("Unknown ConnectState (%d)", t) } } // 主机类型 type HostType byte const ( HostTypeDirect HostType = iota HostTypeProxy HostTypeBoth ) // 请求数据包 type RequestData struct { Id uint16 Cmd string Data []byte timeout int // 超时时间,单位为毫秒 backchan chan *ResponseData // 返回数据的管道 conn *Line // 将连接传递出去是为了能够让上层找回来 } func (r *RequestData) Conn() *Line { return r.conn } type ResponseData struct { Id uint16 State uint8 Data []byte conn *Line } // 获取当前的连接 func (r *ResponseData) Conn() *Line { return r.conn } // 输出状态和数据,方便直接返回 func (r *ResponseData) Out() (state uint8, data []byte) { return r.State, r.Data } // 输出可视化 func (r *ResponseData) String() string { return fmt.Sprintf("id: %d, state: [%d]%s, data: %s", r.Id, r.State, StateIdString(r.State), string(r.Data)) } type PingData struct { Id uint16 } // 请求信息,得到回应通过管道传递信息 type GetMsg struct { out chan *ResponseData timer *time.Timer } // 获取对应频道的一个连接地址 type ConnectHostFunc func(channel string, hostType HostType) (hostInfo *conn.HostInfo, err error) // 获取认证信息 type AuthFunc func(host *conn.HostInfo, proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) // 认证合法性函数,只有客户端有host信息 // host 后面的部分是远程返回回来的信息 type CheckAuthFunc func(host *conn.HostInfo, proto string, version uint8, channel string, auth []byte) bool // 验证发送的数据条件是否满足 type CheckConnectOkFunc = func(line *Line, data *GetData) bool