rts-sim-testing-service/third_party/train_pc_sim/example/main.go

136 lines
3.3 KiB
Go
Raw Normal View History

2024-04-09 08:55:33 +08:00
package main
import (
"fmt"
"io"
"joylink.club/bj-rtsts-server/third_party/message"
"joylink.club/bj-rtsts-server/third_party/train_pc_sim"
"log/slog"
"net"
)
type TcpConnHandler = func(conn net.Conn)
type TcpMsgHandler = func(n int, data []byte)
func tcpRunAcceptTask(listen net.Listener, port int, connHandler TcpConnHandler, msgHandler TcpMsgHandler) {
go func() {
defer func() {
if err := recover(); err != nil {
slog.Error(fmt.Sprintf("TCP服务端[port:%d]接收连接任务异常:", port), err)
tcpRunAcceptTask(listen, port, connHandler, msgHandler)
}
}()
for {
conn, err := listen.Accept()
if err != nil {
slog.Error(fmt.Sprintf("TCP服务端[port:%d]接收连接出错:", port), err)
}
connHandler(conn)
tcpRunReadTask(conn, port, msgHandler)
}
}()
}
func tcpRunReadTask(conn net.Conn, port int, msgHandler TcpMsgHandler) {
go func() {
defer func() {
if err := recover(); err != nil {
slog.Error(fmt.Sprintf("TCP服务端[port:%d]读数据任务异常:", port), err)
tcpRunReadTask(conn, port, msgHandler)
}
}()
for {
data := make([]byte, 1024)
l, err := conn.Read(data)
if err != nil {
if err == io.EOF {
slog.Warn(fmt.Sprintf("TCP服务端[port:%d]断开[%s]连接:", port, conn.RemoteAddr().String()))
break
}
slog.Error(fmt.Sprintf("TCP服务端[port:%d]读取[%s]数据出错:", port, conn.RemoteAddr().String()), err)
serConn = nil
}
msgHandler(l, data)
}
}()
}
func StartTcpServer(port int, connHandler TcpConnHandler, msgHandler TcpMsgHandler) (net.Listener, error) {
listen, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
return nil, err
}
tcpRunAcceptTask(listen, port, connHandler, msgHandler)
return listen, err
}
var serConn net.Conn
func createServer(h TcpMsgHandler) {
StartTcpServer(5600, func(conn net.Conn) {
fmt.Println("TCP服务端接收到连接")
serConn = conn
}, h)
}
func connTrain() *message.TrainPcSimBaseMessage {
msg := &message.TrainPcSimBaseMessage{}
msg.Type = train_pc_sim.RECIVE_TRAIN_CREATE_REMOVE
msg.Data = []byte{0x01}
return msg
}
func changeDoorMode() *message.TrainPcSimBaseMessage {
msg := &message.TrainPcSimBaseMessage{}
msg.Type = train_pc_sim.RECIVE_TRAIN_DOOR_MODE
msg.Data = []byte{0x02}
return msg
}
// 测试创建连接
/*func TestConn(t *testing.T) {
createServer(func(n int, data []byte) {
})
select {}
}*/
func main() {
createServer(func(n int, data []byte) {
msg := &message.TrainPcSimBaseMessage{}
msg.Decode(data)
if msg.Type == train_pc_sim.SENDER_TRAIN_TC_ACTIVE {
fmt.Println("接收驾驶端激活")
} else if msg.Type == train_pc_sim.SENDER_TRAIN_TC_NOT_ACTIVE {
fmt.Println("接收驾驶端未激活")
} else if msg.Type == train_pc_sim.SENDER_TRAIN_OUTR_INFO {
fmt.Println("接受列车输出数字量", msg.Data[0], msg.Data[1])
}
})
//reader := bufio.NewReader(os.Stdin)
var command string
for {
if serConn == nil {
continue
}
fmt.Scanln(&command)
if command != "" {
fmt.Println(command)
}
if command == "create-train" {
msg := connTrain()
serConn.Write(msg.Encode())
} else if command == "door-mode" {
msg := changeDoorMode()
serConn.Write(msg.Encode())
}
command = ""
/*content, _ := reader.ReadString('\n')
if content == "create-train" {
}*/
}
select {}
}