fix(watchdog): refactored tcp recvMsg function, now the watchdog can be connected correctly
- Refactor TCP message reading to use ReadBytes and TrimSpace for better reliability - Add connection success logging in main.go - Update .gitignore to include logs.db-journal
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -46,4 +46,5 @@ super-frpc
|
||||
super-frpc.exe
|
||||
*.db
|
||||
database.db
|
||||
logs.html
|
||||
logs.html
|
||||
logs.db-journal
|
||||
|
||||
5
main.go
5
main.go
@@ -105,11 +105,12 @@ func main() {
|
||||
} else {
|
||||
if !watchdog.Connect("127.0.0.1", config.Watchdog.Port) {
|
||||
postLog.Error(fmt.Sprintf("Failed to connect to Watchdog at %s:%d", "127.0.0.1", config.Watchdog.Port))
|
||||
} else {
|
||||
postLog.Info(fmt.Sprintf("Connected to Watchdog at %s:%d", "127.0.0.1", config.Watchdog.Port))
|
||||
is.watchdogConnected = true
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
addr := fmt.Sprintf("%s:%s", config.ListenAddr, config.ListenPort)
|
||||
server := &http.Server{
|
||||
Addr: addr,
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"super-frpc/postLog"
|
||||
"sync"
|
||||
"time"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -81,56 +82,61 @@ func sendMsg(message string, target int) (string, error) {
|
||||
}
|
||||
|
||||
func recvMsg() {
|
||||
defer recvWg.Done()
|
||||
defer recvWg.Done()
|
||||
|
||||
for {
|
||||
tcpConnMutex.Lock()
|
||||
if tcpConn == nil {
|
||||
tcpConnMutex.Unlock()
|
||||
return
|
||||
}
|
||||
conn := tcpConn
|
||||
tcpConnMutex.Unlock()
|
||||
tcpConnMutex.Lock()
|
||||
if tcpConn == nil {
|
||||
tcpConnMutex.Unlock()
|
||||
return
|
||||
}
|
||||
conn := tcpConn
|
||||
tcpConnMutex.Unlock()
|
||||
|
||||
select {
|
||||
case <-stopRecvChan:
|
||||
return
|
||||
default:
|
||||
conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||
reader := bufio.NewReader(conn)
|
||||
line, err := reader.ReadString('\n')
|
||||
if err != nil {
|
||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
tcpConnMutex.Lock()
|
||||
if tcpConn != nil {
|
||||
tcpConn.Close()
|
||||
tcpConn = nil
|
||||
isConnected = false
|
||||
}
|
||||
tcpConnMutex.Unlock()
|
||||
return
|
||||
}
|
||||
reader := bufio.NewReader(conn)
|
||||
|
||||
line = line[:len(line)-1]
|
||||
if len(line) > 0 {
|
||||
select {
|
||||
case recvChan <- line:
|
||||
default:
|
||||
select {
|
||||
case <-recvChan:
|
||||
recvChan <- line
|
||||
default:
|
||||
}
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-stopRecvChan:
|
||||
return
|
||||
default:
|
||||
conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
|
||||
|
||||
// ⭐ 改为读取到换行符 \n 为止
|
||||
data, err := reader.ReadBytes('\n')
|
||||
|
||||
if len(data) > 0 {
|
||||
// ⭐ 用 strings.TrimSpace 去掉末尾的 \n
|
||||
line := strings.TrimSpace(string(data))
|
||||
|
||||
if len(line) > 0 {
|
||||
select {
|
||||
case recvChan <- line:
|
||||
default:
|
||||
}
|
||||
|
||||
if len(line) > 0 && line != "success" && !isResponseMessage(line) {
|
||||
postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !isResponseMessage(line) {
|
||||
postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
|
||||
continue
|
||||
}
|
||||
|
||||
// 处理非超时的严重错误(如服务端断开)
|
||||
tcpConnMutex.Lock()
|
||||
if tcpConn != nil {
|
||||
tcpConn.Close()
|
||||
tcpConn = nil
|
||||
isConnected = false
|
||||
}
|
||||
tcpConnMutex.Unlock()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func isResponseMessage(msg string) bool {
|
||||
|
||||
Reference in New Issue
Block a user