fix udp bind

This commit is contained in:
ginuerzh
2021-11-22 17:52:38 +08:00
parent 3fd3b5e801
commit 8861644bbc
18 changed files with 477 additions and 613 deletions

View File

@ -60,35 +60,6 @@ func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Co
return
}
func (r *Router) Bind(ctx context.Context, network, address string) (accepter connector.Accepter, err error) {
count := r.retries + 1
if count <= 0 {
count = 1
}
r.logger.Debugf("bind: %s/%s", address, network)
for i := 0; i < count; i++ {
route := r.chain.GetRouteFor(network, address)
if r.logger.IsLevelEnabled(logger.DebugLevel) {
buf := bytes.Buffer{}
for _, node := range route.Path() {
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
}
fmt.Fprintf(&buf, "%s", address)
r.logger.Debugf("route(retry=%d): %s", i, buf.String())
}
accepter, err = route.Bind(ctx, network, address)
if err == nil {
break
}
r.logger.Errorf("route(retry=%d): %s", i, err)
}
return
}
func (r *Router) Connect(ctx context.Context) (conn net.Conn, err error) {
count := r.retries + 1
if count <= 0 {
@ -115,3 +86,32 @@ func (r *Router) Connect(ctx context.Context) (conn net.Conn, err error) {
return
}
func (r *Router) Bind(ctx context.Context, network, address string, opts ...connector.BindOption) (ln net.Listener, err error) {
count := r.retries + 1
if count <= 0 {
count = 1
}
r.logger.Debugf("bind: %s/%s", address, network)
for i := 0; i < count; i++ {
route := r.chain.GetRouteFor(network, address)
if r.logger.IsLevelEnabled(logger.DebugLevel) {
buf := bytes.Buffer{}
for _, node := range route.Path() {
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
}
fmt.Fprintf(&buf, "%s", address)
r.logger.Debugf("route(retry=%d): %s", i, buf.String())
}
ln, err = route.Bind(ctx, network, address, opts...)
if err == nil {
break
}
r.logger.Errorf("route(retry=%d): %s", i, err)
}
return
}