修改udp服务未启动监听问题

This commit is contained in:
walker 2023-10-19 14:02:20 +08:00
parent fa0167271f
commit 7542ee473c
3 changed files with 5 additions and 1 deletions

View File

@ -78,6 +78,7 @@ func newDynamics() Dynamics {
}, },
} }
d.trainInfoUdpServer = udp.NewServer(fmt.Sprintf(":%d", config.Config.Dynamics.UdpLocalPort), d.handleDynamicsTrainInfo) d.trainInfoUdpServer = udp.NewServer(fmt.Sprintf(":%d", config.Config.Dynamics.UdpLocalPort), d.handleDynamicsTrainInfo)
d.trainInfoUdpServer.Listen()
return d return d
} }

View File

@ -49,6 +49,7 @@ func newSemiPhysicalTrain() SemiPhysicalTrain {
trainSpeedInfoUdpClient: udp.NewClient("127.0.0.1:7777"), trainSpeedInfoUdpClient: udp.NewClient("127.0.0.1:7777"),
} }
s.trainControlUdpServer = udp.NewServer(fmt.Sprintf(":%d", config.Config.Dynamics.UdpLocalPort), s.handleTrainControlMsg) s.trainControlUdpServer = udp.NewServer(fmt.Sprintf(":%d", config.Config.Dynamics.UdpLocalPort), s.handleTrainControlMsg)
s.trainControlUdpServer.Listen()
return s return s
} }

View File

@ -3,6 +3,7 @@ package udp
import ( import (
"log/slog" "log/slog"
"net" "net"
"runtime/debug"
) )
type UdpServer interface { type UdpServer interface {
@ -54,7 +55,8 @@ func (s *server) listenAndHandle() {
func (s *server) handle(b []byte) { func (s *server) handle(b []byte) {
defer func() { defer func() {
if err := recover(); err != nil { if err := recover(); err != nil {
slog.Error("udp server handle error", err) slog.Error("udp server handle error", "error", err, "stack", string(debug.Stack()))
debug.PrintStack()
} }
}() }()
// slog.Info("udp server handle", "msg", string(b)) // slog.Info("udp server handle", "msg", string(b))