1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package stc
- import (
- "fmt"
- "log"
- "net"
- "pbx-api-gin/internal/app/stc/broadcast"
- msgdata "pbx-api-gin/internal/app/stc/data"
- "syscall"
- "time"
- )
- func StartStcConnection(conn net.Conn) {
- //read msg
- go func(conn net.Conn) {
- broadcast.HandleStcCmd(conn)
- }(conn)
- //heartbeat
- go func(conn net.Conn) {
- Sendheartbeat(conn)
- }(conn)
- }
- func CreateConnection(ServerAddr string, Port int) net.Conn {
- // connect server
- fmt.Println("==========conn server============")
- conn, err := net.DialTCP("tcp", &net.TCPAddr{
- IP: net.ParseIP("0.0.0.0"),
- Port: 10201,
- }, &net.TCPAddr{
- IP: net.ParseIP(ServerAddr),
- Port: Port,
- })
- if err != nil {
- fmt.Println("Error conn server:", err)
- log.Fatal(err)
- return nil
- }
- fileDesc, _ := conn.File()
- fd := fileDesc.Fd()
- syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1)
- return conn
- }
- func Sendheartbeat(conn net.Conn) {
- var count uint8
- //init heartbeat data ============================
- //return
- protocol := msgdata.NewProtocol()
- protocol.SourceID = 0x02
- protocol.DestinationID = 0x01
- protocol.MessageID = 0x21
- protocol.DataLength = 0x04
- ticker := time.NewTicker(2 * time.Second)
- defer ticker.Stop()
- for range ticker.C {
- count = count + 1
- protocol.Data = []byte{count, 0x00, 0x00, 0x00}
- encoded, errEn := protocol.Encode()
- if errEn != nil {
- fmt.Println("Encode error:", errEn)
- return
- }
- _, err := conn.Write(encoded)
- if err != nil {
- fmt.Println("send heartbeat err:", err)
- return
- }
- }
- }
- // 检查PA server是主状态还是从状态
- func CheckMaster(conn net.Conn) bool {
- // var count uint8
- //init heartbeat data
- protocol := msgdata.NewProtocol()
- protocol.SourceID = 0x02
- protocol.DestinationID = 0x01
- protocol.MessageID = 0x21
- protocol.DataLength = 0x04
- return false
- }
|