Joyit hai 4 semanas
pai
achega
84bb40ae6c
Modificáronse 5 ficheiros con 16 adicións e 7 borrados
  1. 1 1
      examples/client-tcp2.go
  2. 1 1
      examples/client-ws2.go
  3. 1 1
      examples/server.go
  4. 3 3
      hub.go
  5. 10 1
      type.go

+ 1 - 1
examples/client-tcp2.go

@@ -23,7 +23,7 @@ func main() {
 		Hash:    "xor:1qaz2wsx3",
 	}
 
-	hub := tinymq.NewHub(cf, channel, func(channel string, proxy bool) (hostInfo *tinymq.HostInfo, err error) {
+	hub := tinymq.NewHub(cf, channel, func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
 		return host, nil
 	}, func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
 		// 从 remoteAuth 是否为空来判断是否需要返回信息

+ 1 - 1
examples/client-ws2.go

@@ -26,7 +26,7 @@ func main() {
 		Hash: "xor:1qaz2wsx3edc",
 	}
 
-	hub := tinymq.NewHub(cf, localChannel, func(channel string, proxy bool) (hostInfo *tinymq.HostInfo, err error) {
+	hub := tinymq.NewHub(cf, localChannel, func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
 		return host, nil
 	}, func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
 		// 从 remoteAuth 是否为空来判断是否需要返回信息

+ 1 - 1
examples/server.go

@@ -24,7 +24,7 @@ func main() {
 	var hub *tinymq.Hub
 
 	hub = tinymq.NewHub(cf, localChannel,
-		func(channel string, proxy bool) (hostInfo *tinymq.HostInfo, err error) {
+		func(channel string, hostType tinymq.HostType) (hostInfo *tinymq.HostInfo, err error) {
 			return nil, errors.New("not host found")
 		}, func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte) {
 			// 从 remoteAuth 是否为空来判断是否需要返回信息

+ 3 - 3
hub.go

@@ -576,7 +576,7 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo) (err e
 	}
 	if host == nil {
 		// 获取服务地址等信息
-		host, err = h.connectHostFunc(channel, true)
+		host, err = h.connectHostFunc(channel, Both)
 		if err != nil {
 			return err
 		}
@@ -713,7 +713,7 @@ func (h *Hub) ConnectToServer(channel string, force bool, host *HostInfo) (err e
 // 重试方式连接服务
 // 将会一直阻塞直到连接成功
 func (h *Hub) ConnectToServerX(channel string, force bool) {
-	host, _ := h.connectHostFunc(channel, false)
+	host, _ := h.connectHostFunc(channel, Direct)
 	for {
 		err := h.ConnectToServer(channel, force, host)
 		if err == nil {
@@ -739,7 +739,7 @@ func (h *Hub) checkProxyConnect() {
 		h.connects.Range(func(key, _ any) bool {
 			line := key.(*Line)
 			if line.host != nil && line.host.Proxy && now-line.updated.UnixMilli() > int64(h.cf.ProxyTimeout) {
-				host, err := h.connectHostFunc(line.channel, false)
+				host, err := h.connectHostFunc(line.channel, Direct)
 				if err != nil {
 					log.Println("[checkProxyConnect connectHostFunc ERROR]", err)
 					return false

+ 10 - 1
type.go

@@ -62,6 +62,15 @@ func (t ConnectState) String() string {
 	}
 }
 
+// 主机类型
+type HostType byte
+
+const (
+	Direct HostType = iota
+	Proxy
+	Both
+)
+
 // 请求数据包
 type RequestData struct {
 	Id   uint16
@@ -150,7 +159,7 @@ func (h *HostInfo) Key() string {
 }
 
 // 获取对应频道的一个连接地址
-type ConnectHostFunc func(channel string, proxy bool) (hostInfo *HostInfo, err error)
+type ConnectHostFunc func(channel string, hostType HostType) (hostInfo *HostInfo, err error)
 
 // 获取认证信息
 type AuthFunc func(proto string, version uint8, channel string, remoteAuth []byte) (auth []byte)