|
@@ -4,6 +4,7 @@ import (
|
|
|
"context"
|
|
"context"
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"net"
|
|
"net"
|
|
|
|
|
+ "pbx-api-gin/internal/app/stc/active"
|
|
|
"pbx-api-gin/internal/app/stc/broadcast"
|
|
"pbx-api-gin/internal/app/stc/broadcast"
|
|
|
msgdata "pbx-api-gin/internal/app/stc/data"
|
|
msgdata "pbx-api-gin/internal/app/stc/data"
|
|
|
"pbx-api-gin/internal/app/stc/socket"
|
|
"pbx-api-gin/internal/app/stc/socket"
|
|
@@ -14,22 +15,18 @@ import (
|
|
|
"time"
|
|
"time"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-const RemotePort = 10100
|
|
|
|
|
-const LocalPort = 10201
|
|
|
|
|
-
|
|
|
|
|
-// const RemoteAddr = "192.168.17.14"
|
|
|
|
|
-// const RemoteAddr = "10.0.0.51"
|
|
|
|
|
-const RemoteAddr = "10.0.11.11"
|
|
|
|
|
-
|
|
|
|
|
-func StartStcConnection(conn net.Conn) {
|
|
|
|
|
|
|
+func StartStcConnection(conn net.Conn, cab string) {
|
|
|
|
|
|
|
|
var connMux sync.Mutex // 保护 conn 的读写
|
|
var connMux sync.Mutex // 保护 conn 的读写
|
|
|
|
|
+ var conn1 net.Conn
|
|
|
|
|
+ var err error
|
|
|
|
|
|
|
|
for {
|
|
for {
|
|
|
- // 尝试建立连接
|
|
|
|
|
- conn1, err := CreateConnection()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
|
|
+ // 尝试建立连接MC
|
|
|
|
|
+ conn1, err = CreateConnection(cab)
|
|
|
|
|
+ if err != nil || conn1 == nil {
|
|
|
time.Sleep(2 * time.Second)
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("===========Reconnecting====Cab:%s=======", cab)
|
|
|
continue
|
|
continue
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -76,22 +73,77 @@ func StartStcConnection(conn net.Conn) {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// 返回错误而不是终止程序
|
|
// 返回错误而不是终止程序
|
|
|
-func CreateConnection() (net.Conn, error) {
|
|
|
|
|
- lfshook.NewLogger().Logger.Infof("========Connect server IP:%s :Port:%d", socket.RemoteAddr, socket.RemotePort)
|
|
|
|
|
- // 创建 Dialer
|
|
|
|
|
- dialer := &net.Dialer{
|
|
|
|
|
- LocalAddr: &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: socket.LocalPort}, // 固定本地端口
|
|
|
|
|
- Control: controlTCPConn,
|
|
|
|
|
- }
|
|
|
|
|
|
|
+func CreateConnection(RemoteCab string) (net.Conn, error) {
|
|
|
|
|
+
|
|
|
|
|
+ if RemoteCab == "1" { // connect to MC1
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("========Connect Server MC1 IP:%s :Port:%d", socket.RemoteAddr, socket.RemotePort)
|
|
|
|
|
+ // 创建 Dialer
|
|
|
|
|
+ if active.CabNum == "1" { //in cab1
|
|
|
|
|
+ dialer := &net.Dialer{
|
|
|
|
|
+ LocalAddr: &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: socket.LocalPort}, // 固定本地端口
|
|
|
|
|
+ Control: controlTCPConn,
|
|
|
|
|
+ Timeout: 5 * time.Second,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DialAddr := fmt.Sprintf("%s:%d", socket.RemoteAddr, socket.RemotePort)
|
|
|
|
|
+ conn, err := dialer.Dial("tcp", DialAddr)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("========Connect server err :%+v", err)
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("Connect success :%s:%d", socket.RemoteAddr, socket.RemotePort)
|
|
|
|
|
+ return conn, nil
|
|
|
|
|
+ } else { //in cab 8
|
|
|
|
|
+ dialer := &net.Dialer{
|
|
|
|
|
+ LocalAddr: &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: socket.LocalPort8}, // 固定本地端口
|
|
|
|
|
+ Control: controlTCPConn,
|
|
|
|
|
+ Timeout: 5 * time.Second,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DialAddr := fmt.Sprintf("%s:%d", socket.RemoteAddr, socket.RemotePort)
|
|
|
|
|
+ conn, err := dialer.Dial("tcp", DialAddr)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("========Connect server err :%+v", err)
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("Connect success :%s:%d", socket.RemoteAddr, socket.RemotePort)
|
|
|
|
|
+ return conn, nil
|
|
|
|
|
+ }
|
|
|
|
|
+ } else { // connect to MC8
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("========Connect server MC8 IP:%s :Port:%d", socket.RemoteAddr8, socket.RemotePort)
|
|
|
|
|
+ // 创建 Dialer
|
|
|
|
|
+ if active.CabNum == "1" { //in cab1
|
|
|
|
|
+ dialer := &net.Dialer{
|
|
|
|
|
+ LocalAddr: &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: socket.LocalPort}, // 固定本地端口
|
|
|
|
|
+ Control: controlTCPConn,
|
|
|
|
|
+ Timeout: 5 * time.Second,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ DialAddr := fmt.Sprintf("%s:%d", socket.RemoteAddr8, socket.RemotePort)
|
|
|
|
|
+ conn, err := dialer.Dial("tcp", DialAddr)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("========Connect server err :%+v", err)
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("Connect success MC8:%s:%d", socket.RemoteAddr8, socket.RemotePort)
|
|
|
|
|
+ return conn, nil
|
|
|
|
|
+ } else { //in cab 8
|
|
|
|
|
+ dialer := &net.Dialer{
|
|
|
|
|
+ LocalAddr: &net.TCPAddr{IP: net.ParseIP("0.0.0.0"), Port: socket.LocalPort8}, // 固定本地端口
|
|
|
|
|
+ Control: controlTCPConn,
|
|
|
|
|
+ Timeout: 5 * time.Second,
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- DialAddr := fmt.Sprintf("%s:%d", socket.RemoteAddr, socket.RemotePort)
|
|
|
|
|
- conn, err := dialer.Dial("tcp", DialAddr)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- lfshook.NewLogger().Logger.Infof("========Connect server err :%+v", err)
|
|
|
|
|
- return nil, err
|
|
|
|
|
|
|
+ DialAddr := fmt.Sprintf("%s:%d", socket.RemoteAddr8, socket.RemotePort)
|
|
|
|
|
+ conn, err := dialer.Dial("tcp", DialAddr)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("========Connect server err :%+v", err)
|
|
|
|
|
+ return nil, err
|
|
|
|
|
+ }
|
|
|
|
|
+ lfshook.NewLogger().Logger.Infof("Connect success MC8:%s:%d", socket.RemoteAddr8, socket.RemotePort)
|
|
|
|
|
+ return conn, nil
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
- lfshook.NewLogger().Logger.Infof("Connect success :%s:%d", socket.RemoteAddr, socket.RemotePort)
|
|
|
|
|
- return conn, nil
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func controlTCPConn(network, address string, c syscall.RawConn) error {
|
|
func controlTCPConn(network, address string, c syscall.RawConn) error {
|