123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- 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
- 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)
- }
- 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
- }
|