|
|
@@ -168,10 +168,10 @@ func (h *Hub) ConnectRange(fn func(id int, line *Line) bool) {
|
|
|
}
|
|
|
|
|
|
// 获取当前在线的数量
|
|
|
-func (h *Hub) ConnectNum() int {
|
|
|
+func (h *Hub) ConnectedNum() int {
|
|
|
var count int
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == Connected {
|
|
|
+ if line.state == StateConnected {
|
|
|
count++
|
|
|
}
|
|
|
return true
|
|
|
@@ -180,10 +180,10 @@ func (h *Hub) ConnectNum() int {
|
|
|
}
|
|
|
|
|
|
// 获取所有的在线连接频道
|
|
|
-func (h *Hub) AllChannel() []string {
|
|
|
+func (h *Hub) AllConnectedChannel() []string {
|
|
|
cs := make([]string, 0)
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == Connected {
|
|
|
+ if line.state == StateConnected {
|
|
|
cs = append(cs, line.channel)
|
|
|
}
|
|
|
return true
|
|
|
@@ -193,10 +193,10 @@ func (h *Hub) AllChannel() []string {
|
|
|
|
|
|
// 获取所有连接频道和连接时长
|
|
|
// 为了避免定义数据结构麻烦,采用|隔开, 频道名|连接开始时间
|
|
|
-func (h *Hub) AllChannelWithStarted() []string {
|
|
|
+func (h *Hub) AllConnectedChannelWithStarted() []string {
|
|
|
cs := make([]string, 0)
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == Connected {
|
|
|
+ if line.state == StateConnected {
|
|
|
cs = append(cs, fmt.Sprintf("%s|%d", line.channel, line.updated.UnixMilli()))
|
|
|
}
|
|
|
return true
|
|
|
@@ -205,9 +205,9 @@ func (h *Hub) AllChannelWithStarted() []string {
|
|
|
}
|
|
|
|
|
|
// 获取频道并通过函数过滤,如果返回 false 将终止
|
|
|
-func (h *Hub) ChannelToFunc(fn func(string) bool) {
|
|
|
+func (h *Hub) ConnectedChannelToFunc(fn func(string) bool) {
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == Connected {
|
|
|
+ if line.state == StateConnected {
|
|
|
return fn(line.channel)
|
|
|
}
|
|
|
return true
|
|
|
@@ -255,9 +255,10 @@ func (h *Hub) sendRequest(gd *GetData) (count int, err error) {
|
|
|
log.Println(err)
|
|
|
return 0, err
|
|
|
}
|
|
|
+ // 发送数据到网络
|
|
|
doit := func(_ int, line *Line) bool {
|
|
|
// 检查连接是否OK
|
|
|
- if line.state != Connected {
|
|
|
+ if line.state != StateConnected {
|
|
|
return true
|
|
|
}
|
|
|
// 验证连接是否达到发送数据的要求
|
|
|
@@ -635,7 +636,7 @@ func (h *Hub) BindForServer(info *conn.HostInfo) (err error) {
|
|
|
// 将连接加入现有连接中
|
|
|
done := false
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == Disconnected && line.host == nil && line.IsChannelEqual(channel) {
|
|
|
+ if line.state == StateDisconnected && line.host == nil && line.IsChannelEqual(channel) {
|
|
|
line.Start(channel, conn, nil)
|
|
|
done = true
|
|
|
return false
|
|
|
@@ -668,7 +669,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
// 检查当前channel是否已经存在
|
|
|
if !force {
|
|
|
line := h.ChannelToLine(remoteChannel)
|
|
|
- if line != nil && line.state == Connected {
|
|
|
+ if line != nil && line.state == StateConnected {
|
|
|
// err = fmt.Errorf("[ConnectToServer ERROR] existed channel: %s", channel)
|
|
|
log.Println("[ConnectToServer] channel existed:", remoteChannel)
|
|
|
return
|
|
|
@@ -684,7 +685,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
return err
|
|
|
}
|
|
|
if host == nil {
|
|
|
- // 如果地址为空表示不需要连接,自己返回
|
|
|
+ // 如果地址为空表示不需要连接,直接返回
|
|
|
return
|
|
|
}
|
|
|
}
|
|
|
@@ -738,7 +739,7 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
// if host.Proxy {
|
|
|
// localChannel = localChannel + "?proxy=" + host.Host
|
|
|
// }
|
|
|
- err = CheckAuthInfo(conn, host, h.channel, remoteChannel, h.authFunc, h.checkAuthFunc)
|
|
|
+ err = ClientCheckAuthInfo(conn, host, h.channel, remoteChannel, h.authFunc, h.checkAuthFunc)
|
|
|
if err != nil {
|
|
|
conn.Close()
|
|
|
host.Errors++
|
|
|
@@ -748,59 +749,12 @@ func (h *Hub) ConnectToServer(remoteChannel string, force bool, host *conn.HostI
|
|
|
host.Errors = 0
|
|
|
host.Updated = time.Now()
|
|
|
}
|
|
|
- // 发送验证信息
|
|
|
- // if err := conn.WriteAuthInfo(localChannel, h.authFunc(true, rawProto, host.Version, channel, nil)); err != nil {
|
|
|
- // log.Println("[ConnectToServer] WriteAuthInfo failed:", err)
|
|
|
- // conn.Close()
|
|
|
- // host.Errors++
|
|
|
- // host.Updated = time.Now()
|
|
|
- // return err
|
|
|
- // }
|
|
|
- // // 接收频道信息
|
|
|
- // proto, version, channel2, auth, err := conn.ReadAuthInfo()
|
|
|
- // if err != nil {
|
|
|
- // log.Println("[ConnectToServer] ReadAuthInfo failed:", err)
|
|
|
- // conn.Close()
|
|
|
- // host.Errors++
|
|
|
- // host.Updated = time.Now()
|
|
|
- // return err
|
|
|
- // }
|
|
|
- // // 检查版本和协议是否一致
|
|
|
- // if version != host.Version || proto != rawProto {
|
|
|
- // err = fmt.Errorf("[ConnectToServer] version or protocol wrong: %d, %s", version, proto)
|
|
|
- // log.Println(err)
|
|
|
- // conn.Close()
|
|
|
- // host.Errors++
|
|
|
- // host.Updated = time.Now()
|
|
|
- // return err
|
|
|
- // }
|
|
|
- // // 检查频道名称是否匹配
|
|
|
- // if !strings.Contains(channel2, channel) {
|
|
|
- // err = fmt.Errorf("[ConnectToServer] channel want %s, get %s", channel, channel2)
|
|
|
- // log.Println(err)
|
|
|
- // conn.Close()
|
|
|
- // host.Errors++
|
|
|
- // host.Updated = time.Now()
|
|
|
- // return err
|
|
|
- // }
|
|
|
- // // 检查验证是否合法
|
|
|
- // if !h.checkAuthFunc(true, proto, version, channel, auth) {
|
|
|
- // err = fmt.Errorf("[ConnectToServer] client checkAuthFunc in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
|
|
|
- // log.Println(err)
|
|
|
- // conn.Close()
|
|
|
- // host.Errors++
|
|
|
- // host.Updated = time.Now()
|
|
|
- // return err
|
|
|
- // }
|
|
|
- // // 更新服务主机信息
|
|
|
- // host.Errors = 0
|
|
|
- // host.Updated = time.Now()
|
|
|
|
|
|
// 将连接加入现有连接中
|
|
|
done = false
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
if line.channel == remoteChannel {
|
|
|
- if line.state == Connected {
|
|
|
+ if line.state == StateConnected {
|
|
|
if force {
|
|
|
line.Close(true)
|
|
|
} else {
|
|
|
@@ -890,7 +844,7 @@ func (h *Hub) checkConnect() {
|
|
|
})
|
|
|
case <-connectTicker.C:
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.autoReconnect && line.host != nil && line.state == Disconnected {
|
|
|
+ if line.autoReconnect && line.host != nil && line.state == StateDisconnected {
|
|
|
err := h.ConnectToServer(line.channel, true, nil, true)
|
|
|
if err != nil {
|
|
|
log.Println("[connectTicker ConnectToServer]", err)
|
|
|
@@ -908,7 +862,7 @@ func (h *Hub) checkConnect() {
|
|
|
func (h *Hub) Quit() {
|
|
|
h.cancel()
|
|
|
h.lines.Range(func(id int, line *Line) bool {
|
|
|
- if line.state == Connected {
|
|
|
+ if line.state == StateConnected {
|
|
|
line.Close(true)
|
|
|
}
|
|
|
return true
|