|
|
@@ -23,7 +23,7 @@ var (
|
|
|
isRunning1 bool
|
|
|
exitCh1 chan struct{}
|
|
|
wg1 sync.WaitGroup
|
|
|
- curNetType NetType // 当前使用的网络类型
|
|
|
+ curNetType NetType
|
|
|
)
|
|
|
|
|
|
type NetType int
|
|
|
@@ -46,7 +46,7 @@ func ModuleInit() {
|
|
|
// 联网保持服务
|
|
|
func serviceRun() {
|
|
|
// 1, 首次连接网络
|
|
|
- network_reconnect()
|
|
|
+ openNetwork()
|
|
|
|
|
|
// 2, 监听有线插拔
|
|
|
eth0PlugCycleCh := make(chan bool, 1)
|
|
|
@@ -93,15 +93,15 @@ func serviceRun() {
|
|
|
offlineStartTs.Store(time.Now().UnixNano()) // 记录离线开始时间
|
|
|
}
|
|
|
|
|
|
- if OfflineDuration() >= (time.Duration(60) * time.Second) {
|
|
|
- baseapp.Logger.Warnf("[%s] 检测到网络长时间断开, 将重新尝试连接!", MODULE_NAME)
|
|
|
- network_reconnect()
|
|
|
+ if offlineDuration() >= (time.Duration(60) * time.Second) {
|
|
|
+ baseapp.Logger.Warnf("[%s] 检测到网络长时间断开, 将重新尝试连接网络!", MODULE_NAME)
|
|
|
+ openNetwork()
|
|
|
offlineStartTs.Store(time.Now().UnixNano()) // 重置离线开始时间
|
|
|
}
|
|
|
|
|
|
t.Reset(interval1)
|
|
|
- case <-eth0PlugCycleCh: // 有线网口插拔事件, 重新连接网络, 有线网优先
|
|
|
- network_reconnect()
|
|
|
+ case <-eth0PlugCycleCh: // 有线网口插拔事件, 重新连接网络, 优先有线网
|
|
|
+ openNetwork()
|
|
|
continue
|
|
|
case <-baseapp.IsExit2():
|
|
|
return
|
|
|
@@ -140,7 +140,7 @@ func WaitAllOK(timeout time.Duration) bool {
|
|
|
}
|
|
|
|
|
|
// 返回断网时长
|
|
|
-func OfflineDuration() time.Duration {
|
|
|
+func offlineDuration() time.Duration {
|
|
|
if IsInetAvailable() {
|
|
|
return 0
|
|
|
}
|
|
|
@@ -151,8 +151,8 @@ func OfflineDuration() time.Duration {
|
|
|
return time.Since(time.Unix(0, ts))
|
|
|
}
|
|
|
|
|
|
-// 连接有线网络
|
|
|
-func OpenEth0Net() bool {
|
|
|
+// 打开有线网络
|
|
|
+func openEth0Net() bool {
|
|
|
mu1.Lock()
|
|
|
defer mu1.Unlock()
|
|
|
|
|
|
@@ -196,7 +196,7 @@ func OpenEth0Net() bool {
|
|
|
}
|
|
|
|
|
|
// 断开有线网络
|
|
|
-func CloseEth0Net() {
|
|
|
+func closeEth0Net() {
|
|
|
mu1.Lock()
|
|
|
defer mu1.Unlock()
|
|
|
|
|
|
@@ -215,21 +215,18 @@ func CloseEth0Net() {
|
|
|
isRunning1 = false
|
|
|
}
|
|
|
|
|
|
-// 重新连接网络
|
|
|
-func network_reconnect() {
|
|
|
- // 1, 断开所有网络
|
|
|
- CloseEth0Net()
|
|
|
+// 打开连接网络
|
|
|
+func openNetwork() {
|
|
|
+ closeEth0Net()
|
|
|
modem.CloseEth2Net()
|
|
|
|
|
|
- // 2, 尝试有线网络
|
|
|
eth0CableOK, _ := isEth0CableConnected()
|
|
|
- if eth0CableOK && curNetType != NetEth && OpenEth0Net() {
|
|
|
+ if eth0CableOK && curNetType != NetEth && openEth0Net() {
|
|
|
curNetType = NetEth
|
|
|
- baseapp.Logger.Infof("[%s] ✅ 已连接有线网络", MODULE_NAME)
|
|
|
+ baseapp.Logger.Infof("[%s] ✅ 有线网络已连接", MODULE_NAME)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 3, 尝试蜂窝网络
|
|
|
tryOpenLTE := func() bool {
|
|
|
if eth2CableOK, _ := modem.IsEth2CableConnected(); eth2CableOK {
|
|
|
if modem.OpenEth2Net() {
|
|
|
@@ -246,12 +243,12 @@ func network_reconnect() {
|
|
|
if tryOpenLTE() {
|
|
|
curNetType = NetLTE
|
|
|
enableEth0() // 保持 eth0 可用, 以便监听网线的插拔, 触发切换网络
|
|
|
- baseapp.Logger.Infof("[%s] ✅ 已连接蜂窝网络", MODULE_NAME)
|
|
|
+ baseapp.Logger.Infof("[%s] ✅ 蜂窝网络已连接", MODULE_NAME)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- // 4, 都不可用
|
|
|
curNetType = NetNone
|
|
|
+ baseapp.Logger.Warnf("[%s] 没有可用的网络连接!", MODULE_NAME)
|
|
|
}
|
|
|
|
|
|
// 监控网线插拔
|
|
|
@@ -285,7 +282,11 @@ func monitorEth0PlugCycle(plugCycle chan bool, done chan struct{}) error {
|
|
|
waitingForPlug = true
|
|
|
} else if waitingForPlug {
|
|
|
baseapp.Logger.Infof("[%s] ✅ eth0 cable plugged in", MODULE_NAME)
|
|
|
- plugCycle <- true
|
|
|
+ select {
|
|
|
+ case plugCycle <- true:
|
|
|
+ case <-done:
|
|
|
+ return nil
|
|
|
+ }
|
|
|
waitingForPlug = false
|
|
|
}
|
|
|
}
|