github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/network/udp_dialer.go (about) 1 package network 2 3 import ( 4 "context" 5 "github.com/15mga/kiwi" 6 7 "github.com/15mga/kiwi/util" 8 "github.com/xtaci/kcp-go/v5" 9 ) 10 11 type udpDialer struct { 12 name string 13 agent kiwi.IAgent 14 } 15 16 func NewUdpDialer(name, addr string, receiver kiwi.FnAgentBytes, options ...kiwi.AgentOption) kiwi.IDialer { 17 d := &udpDialer{ 18 name: name, 19 agent: NewUdpAgent(addr, receiver, options...), 20 } 21 return d 22 } 23 24 func (d *udpDialer) Name() string { 25 return d.name 26 } 27 28 func (d *udpDialer) Connect(ctx context.Context) *util.Err { 29 addr := d.agent.Addr() 30 c, err := kcp.Dial(addr) 31 if err != nil { 32 return util.NewErr(util.EcConnectErr, util.M{ 33 "addr": addr, 34 "error": err.Error(), 35 }) 36 } 37 38 d.agent.(*udpAgent).Start(ctx, c) 39 return nil 40 } 41 42 func (d *udpDialer) Agent() kiwi.IAgent { 43 return d.agent 44 }