github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/network/tcp_dialer.go (about) 1 package network 2 3 import ( 4 "context" 5 "github.com/15mga/kiwi" 6 "net" 7 8 "github.com/15mga/kiwi/util" 9 ) 10 11 type tcpDialer struct { 12 name string 13 agent *tcpAgent 14 } 15 16 func NewTcpDialer(name, addr string, receiver kiwi.FnAgentBytes, options ...kiwi.AgentOption) kiwi.IDialer { 17 d := &tcpDialer{ 18 name: name, 19 agent: NewTcpAgent(addr, receiver, options...), 20 } 21 return d 22 } 23 24 func (d *tcpDialer) Name() string { 25 return d.name 26 } 27 28 func (d *tcpDialer) Connect(ctx context.Context) *util.Err { 29 addr := d.agent.Addr() 30 ta, err := net.ResolveTCPAddr("tcp", addr) 31 if err != nil { 32 return util.NewErr(util.EcConnectErr, util.M{ 33 "addr": addr, 34 "error": err.Error(), 35 }) 36 } 37 38 c, err := net.DialTCP("tcp", nil, ta) 39 if err != nil { 40 return util.NewErr(util.EcConnectErr, util.M{ 41 "addr": addr, 42 "error": err.Error(), 43 }) 44 } 45 _ = c.SetNoDelay(true) 46 d.agent.Start(ctx, c) 47 return nil 48 } 49 50 func (d *tcpDialer) Agent() kiwi.IAgent { 51 return d.agent 52 }