From 90475134a82fcda180ca99ab8b25fec3d38e63f3 Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Thu, 14 Nov 2024 15:00:05 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=87=8D=E8=AF=95?= =?UTF-8?q?=E4=B8=8D=E5=90=8C=E7=9A=84=E6=9C=8D=E5=8A=A1=E5=9C=B0=E5=9D=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dial.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 dial.go diff --git a/dial.go b/dial.go new file mode 100644 index 0000000..2c5e712 --- /dev/null +++ b/dial.go @@ -0,0 +1,29 @@ +package tdx + +import ( + "context" + "github.com/injoyai/ios" + "net" + "strings" + "time" +) + +func NewHostDial(hosts []string, timeout time.Duration) ios.DialFunc { + if len(hosts) == 0 { + hosts = Hosts + } + index := 0 + + return func(ctx context.Context) (ios.ReadWriteCloser, string, error) { + defer func() { index++ }() + if index >= len(hosts) { + index = 0 + } + addr := hosts[index] + if !strings.Contains(addr, ":") { + addr += ":7709" + } + c, err := net.DialTimeout("tcp", addr, timeout) + return c, hosts[index], err + } +} From d8eb8c3d1111adafa8c5534471b6206d5a824932 Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Thu, 14 Nov 2024 15:00:56 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0DialWith,=E5=8F=AF?= =?UTF-8?q?=E4=BB=A5=E8=87=AA=E5=AE=9A=E4=B9=89=E8=BF=9E=E6=8E=A5,?= =?UTF-8?q?=E9=85=8D=E7=BD=AENewHostDial,=E5=8F=AF=E4=BB=A5=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0=E6=96=AD=E5=BC=80=E8=BF=9E=E6=8E=A5=E5=88=87=E6=8D=A2?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/client.go b/client.go index 032edc0..56b6368 100644 --- a/client.go +++ b/client.go @@ -7,7 +7,7 @@ import ( "github.com/injoyai/conv" "github.com/injoyai/ios" "github.com/injoyai/ios/client" - "github.com/injoyai/ios/client/dial" + "github.com/injoyai/ios/module/tcp" "github.com/injoyai/logs" "github.com/injoyai/tdx/protocol" "runtime/debug" @@ -35,13 +35,18 @@ func Dial(addr string, op ...client.Option) (cli *Client, err error) { if !strings.Contains(addr, ":") { addr += ":7709" } + return DialWith(tcp.NewDial(addr), op...) +} + +// DialWith 与服务器建立连接 +func DialWith(dial ios.DialFunc, op ...client.Option) (cli *Client, err error) { cli = &Client{ Wait: wait.New(time.Second * 2), m: maps.NewSafe(), } - cli.Client, err = dial.TCP(addr, func(c *client.Client) { + cli.Client, err = client.Dial(dial, func(c *client.Client) { c.Logger.Debug(false) //关闭日志打印 c.Logger.WithHEX() //以HEX显示 c.SetOption(op...) //自定义选项 From 338e26af4ea5b9df53319f3e1c25ef18377e579f Mon Sep 17 00:00:00 2001 From: injoyai <1113655791@qq.com> Date: Thu, 14 Nov 2024 15:01:09 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0DialWith=E7=9A=84?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/common/common.go | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/example/common/common.go b/example/common/common.go index a9e13c7..30651fa 100644 --- a/example/common/common.go +++ b/example/common/common.go @@ -6,14 +6,22 @@ import ( ) func Test(f func(c *tdx.Client)) { - for _, v := range tdx.Hosts { - c, err := tdx.Dial(v, tdx.WithDebug()) - if err != nil { - logs.PrintErr(err) - continue - } - f(c) - <-c.Done() - break - } + + //重连方式1,优点,同一个客户端指针 + c, err := tdx.DialWith(tdx.NewHostDial(tdx.Hosts, 0), tdx.WithDebug()) + logs.PanicErr(err) + f(c) + <-c.Done() + + //重连方式2 + //for _, v := range tdx.Hosts { + // c, err := tdx.DialWith(v, tdx.WithDebug()) + // if err != nil { + // logs.PrintErr(err) + // continue + // } + // f(c) + // <-c.Done() + // break + //} }