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:
2026-04-02 22:22:44 +08:00
parent 78bd1cb3cc
commit de6a8cdcb0
3 changed files with 57 additions and 49 deletions
+1
View File
@@ -47,3 +47,4 @@ super-frpc.exe
*.db *.db
database.db database.db
logs.html logs.html
logs.db-journal
+3 -2
View File
@@ -105,11 +105,12 @@ func main() {
} else { } else {
if !watchdog.Connect("127.0.0.1", config.Watchdog.Port) { 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)) 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) addr := fmt.Sprintf("%s:%s", config.ListenAddr, config.ListenPort)
server := &http.Server{ server := &http.Server{
Addr: addr, Addr: addr,
+26 -20
View File
@@ -7,6 +7,7 @@ import (
"super-frpc/postLog" "super-frpc/postLog"
"sync" "sync"
"time" "time"
"strings"
) )
var ( var (
@@ -83,7 +84,6 @@ func sendMsg(message string, target int) (string, error) {
func recvMsg() { func recvMsg() {
defer recvWg.Done() defer recvWg.Done()
for {
tcpConnMutex.Lock() tcpConnMutex.Lock()
if tcpConn == nil { if tcpConn == nil {
tcpConnMutex.Unlock() tcpConnMutex.Unlock()
@@ -92,17 +92,40 @@ func recvMsg() {
conn := tcpConn conn := tcpConn
tcpConnMutex.Unlock() tcpConnMutex.Unlock()
reader := bufio.NewReader(conn)
for {
select { select {
case <-stopRecvChan: case <-stopRecvChan:
return return
default: default:
conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
reader := bufio.NewReader(conn)
line, err := reader.ReadString('\n') // ⭐ 改为读取到换行符 \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 !isResponseMessage(line) {
postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line))
}
}
}
if err != nil { if err != nil {
if netErr, ok := err.(net.Error); ok && netErr.Timeout() { if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
continue continue
} }
// 处理非超时的严重错误(如服务端断开)
tcpConnMutex.Lock() tcpConnMutex.Lock()
if tcpConn != nil { if tcpConn != nil {
tcpConn.Close() tcpConn.Close()
@@ -112,23 +135,6 @@ func recvMsg() {
tcpConnMutex.Unlock() tcpConnMutex.Unlock()
return return
} }
line = line[:len(line)-1]
if len(line) > 0 {
select {
case recvChan <- line:
default:
select {
case <-recvChan:
recvChan <- line
default:
}
}
if len(line) > 0 && line != "success" && !isResponseMessage(line) {
postLog.Debug(fmt.Sprintf("[Watchdog] TCP Socket received message: %s", line))
}
}
} }
} }
} }