添加拨号重试功能
This commit is contained in:
parent
0490b11224
commit
6eb855ee9a
20
tcp.go
20
tcp.go
|
|
@ -1,6 +1,8 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"github.com/TransX/log"
|
"github.com/TransX/log"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -16,14 +18,24 @@ func NewTransTCP() *TransTCP {
|
||||||
|
|
||||||
func (this *TransTCP) createTCPClient(ip, port string) (conn net.Conn, err error) {
|
func (this *TransTCP) createTCPClient(ip, port string) (conn net.Conn, err error) {
|
||||||
conn, err = net.Dial("tcp4", ip+":"+port)
|
conn, err = net.Dial("tcp4", ip+":"+port)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
|
|
||||||
} else {
|
|
||||||
conn = nil
|
conn = nil
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *TransTCP) createTCPClientWithRetry(ip, port string, retry int) (conn net.Conn, err error) {
|
||||||
|
for i := 0; i < retry; i++ {
|
||||||
|
c, e := this.createTCPClient(ip, port)
|
||||||
|
if e == nil {
|
||||||
|
return c, e
|
||||||
|
}
|
||||||
|
log.Error("Create Client Error: %s", e.Error())
|
||||||
|
}
|
||||||
|
//failed with retry
|
||||||
|
return nil, errors.New(fmt.Sprintln("failed to create client after %d retry", retry))
|
||||||
|
}
|
||||||
|
|
||||||
func (this *TransTCP) createTCPListener(ip, port string) (listen net.Listener, err error) {
|
func (this *TransTCP) createTCPListener(ip, port string) (listen net.Listener, err error) {
|
||||||
listener, _err := net.Listen("tcp4", ip+":"+port)
|
listener, _err := net.Listen("tcp4", ip+":"+port)
|
||||||
if _err == nil {
|
if _err == nil {
|
||||||
|
|
@ -48,7 +60,7 @@ func (this *TransTCP) Start(listenPort, destIP, destPort string, clientOrServer
|
||||||
go func() {
|
go func() {
|
||||||
log.Info("Incoming %s", listenerConn.RemoteAddr().String())
|
log.Info("Incoming %s", listenerConn.RemoteAddr().String())
|
||||||
//创建到目标的连接
|
//创建到目标的连接
|
||||||
destConn, err := this.createTCPClient(destIP, destPort)
|
destConn, err := this.createTCPClientWithRetry(destIP, destPort, 3)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic("Failed to connect to destination. %s", err)
|
log.Panic("Failed to connect to destination. %s", err)
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue