rts-sim-testing-service/dynamics/udp.go

155 lines
4.0 KiB
Go

package dynamics
import (
"container/list"
"encoding/binary"
"fmt"
"github.com/panjf2000/gnet/v2"
"go.uber.org/zap"
"joylink.club/bj-rtsts-server/config"
"math"
"net"
"sync"
"time"
)
func init() {
runSendTurnoutStateTask()
}
var (
m sync.Map
)
type TurnoutInfoFunc func() []*TurnoutInfo
func runSendTurnoutStateTask() {
go func() {
tick := time.Tick(50 * time.Millisecond)
for range tick {
m.Range(func(key, value any) bool {
slice := value.(TurnoutInfoFunc)()
for _, turnoutInfo := range slice {
err := SendTurnoutInfo(turnoutInfo)
if err != nil {
zap.S().Error(err)
}
}
return true
})
}
}()
}
func AddTurnoutInfoSource(simId string, tiFunc TurnoutInfoFunc) {
m.Store(simId, tiFunc)
}
func RemoveTurnoutInfoSource(simId string) {
m.Delete(simId)
}
// SendTurnoutInfo 发送道岔信息
func SendTurnoutInfo(info *TurnoutInfo) error {
defer func() {
if r := recover(); r != nil {
zap.S().Error("发送道岔信息失败", r)
}
}()
addr := fmt.Sprintf("%v:%v", config.Config.Dynamics.Ip, config.Config.Dynamics.UdpRemotePort)
remoteAddr, _ := net.ResolveUDPAddr("udp", addr)
conn, err := net.DialUDP("udp", nil, remoteAddr)
if err != nil {
zap.S().Error("UDP通信失败", err)
return err
}
defer func(conn *net.UDPConn) {
err := conn.Close()
if err != nil {
zap.S().Error(err)
}
}(conn)
var data []byte
data = binary.BigEndian.AppendUint16(data, info.Code)
var b byte
if info.NPosition {
b |= 1 << 7
}
if info.RPosition {
b |= 1 << 6
}
data = append(data, b)
_, err = conn.Write(data)
return err
}
type udpServer struct {
gnet.BuiltinEventEngine
eng gnet.Engine
addr string
multicore bool
eventHandlers []gnet.EventHandler
}
func (server *udpServer) OnBoot(eng gnet.Engine) gnet.Action {
server.eng = eng
fmt.Printf("udp server with multi-core=%t is listening on %s\n", server.multicore, server.addr)
zap.S().Infof("udp server with multi-core=%t is listening on %s\n", server.multicore, server.addr)
return gnet.None
}
// OnTraffic 接收到数据后的解析
func (server *udpServer) OnTraffic(c gnet.Conn) gnet.Action {
defer func() {
if r := recover(); r != nil {
zap.S().Error("udp服务数据解析异常", r)
}
}()
buf, _ := c.Next(-1)
trainInfo := TrainInfo{}
trainInfo.LifeSignal = binary.BigEndian.Uint16(buf[0:2])
trainInfo.Number = buf[2]
trainInfo.Len = binary.BigEndian.Uint16(buf[3:5])
trainInfo.Link = buf[5]
trainInfo.LinkOffset = binary.BigEndian.Uint32(buf[6:10])
trainInfo.Slope = binary.BigEndian.Uint16(buf[10:12])
b := buf[12]
trainInfo.UpSlope = (b & (1 << 7)) != 0
trainInfo.Up = (b & (1 << 6)) != 0
trainInfo.TotalResistance = binary.BigEndian.Uint32(buf[14:18])
trainInfo.AirResistance = binary.BigEndian.Uint32(buf[18:22])
trainInfo.SlopeResistance = binary.BigEndian.Uint32(buf[22:26])
trainInfo.CurveResistance = binary.BigEndian.Uint32(buf[26:30])
trainInfo.Speed = math.Float32frombits(binary.BigEndian.Uint32(buf[30:34]))
trainInfo.HeadSpeed1 = math.Float32frombits(binary.BigEndian.Uint32(buf[34:38]))
trainInfo.HeadSpeed2 = math.Float32frombits(binary.BigEndian.Uint32(buf[38:42]))
trainInfo.TailSpeed1 = math.Float32frombits(binary.BigEndian.Uint32(buf[42:46]))
trainInfo.TailSpeed2 = math.Float32frombits(binary.BigEndian.Uint32(buf[46:50]))
trainInfo.HeadRadarSpeed = math.Float32frombits(binary.BigEndian.Uint32(buf[50:54]))
trainInfo.TailRadarSpeed = math.Float32frombits(binary.BigEndian.Uint32(buf[54:58]))
for e := handlerList.Front(); e != nil; e = e.Next() {
handler := e.Value.(TrainInfoHandler)
handler(&trainInfo)
}
return gnet.None
}
var handlerList list.List
type TrainInfoHandler func(info *TrainInfo)
func RegisterTrainInfoHandler(handler TrainInfoHandler) {
handlerList.PushBack(handler)
}
func RunUdpServer() {
server := &udpServer{addr: fmt.Sprintf("udp://:%d", config.Config.Dynamics.UdpLocalPort), multicore: false}
err := gnet.Run(server, server.addr, gnet.WithMulticore(server.multicore))
zap.L().Fatal("udp服务启动失败", zap.Error(err))
}