add bind for relay

This commit is contained in:
ginuerzh
2021-11-25 17:29:54 +08:00
parent 98ef6c7492
commit 6daf0a4d0f
29 changed files with 600 additions and 352 deletions

View File

@ -23,7 +23,7 @@ func (r *Route) AddNode(node *Node) {
r.nodes = append(r.nodes, node)
}
func (r *Route) Connect(ctx context.Context) (conn net.Conn, err error) {
func (r *Route) connect(ctx context.Context) (conn net.Conn, err error) {
if r.IsEmpty() {
return nil, ErrEmptyRoute
}
@ -72,7 +72,7 @@ func (r *Route) Dial(ctx context.Context, network, address string) (net.Conn, er
return r.dialDirect(ctx, network, address)
}
conn, err := r.Connect(ctx)
conn, err := r.connect(ctx)
if err != nil {
return nil, err
}
@ -103,7 +103,7 @@ func (r *Route) Bind(ctx context.Context, network, address string, opts ...conne
return r.bindLocal(ctx, network, address, opts...)
}
conn, err := r.Connect(ctx)
conn, err := r.connect(ctx)
if err != nil {
return nil, err
}

View File

@ -36,7 +36,7 @@ func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Co
if count <= 0 {
count = 1
}
r.logger.Debugf("dial: %s/%s", address, network)
r.logger.Debugf("dial %s/%s", address, network)
for i := 0; i < count; i++ {
route := r.chain.GetRouteFor(network, address)
@ -47,41 +47,14 @@ func (r *Router) Dial(ctx context.Context, network, address string) (conn net.Co
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
}
fmt.Fprintf(&buf, "%s", address)
r.logger.Debugf("route(retry=%d): %s", i, buf.String())
r.logger.Debugf("route(retry=%d) %s", i, buf.String())
}
conn, err = route.Dial(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 {
count = 1
}
for i := 0; i < count; i++ {
route := r.chain.GetRoute()
if r.logger.IsLevelEnabled(logger.DebugLevel) {
buf := bytes.Buffer{}
for _, node := range route.Path() {
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
}
r.logger.Debugf("route(retry=%d): %s", i, buf.String())
}
conn, err = route.Connect(ctx)
if err == nil {
break
}
r.logger.Errorf("route(retry=%d): %s", i, err)
r.logger.Errorf("route(retry=%d) %s", i, err)
}
return
@ -92,7 +65,7 @@ func (r *Router) Bind(ctx context.Context, network, address string, opts ...conn
if count <= 0 {
count = 1
}
r.logger.Debugf("bind: %s/%s", address, network)
r.logger.Debugf("bind on %s/%s", address, network)
for i := 0; i < count; i++ {
route := r.chain.GetRouteFor(network, address)
@ -103,14 +76,14 @@ func (r *Router) Bind(ctx context.Context, network, address string, opts ...conn
fmt.Fprintf(&buf, "%s@%s > ", node.Name(), node.Addr())
}
fmt.Fprintf(&buf, "%s", address)
r.logger.Debugf("route(retry=%d): %s", i, buf.String())
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)
r.logger.Errorf("route(retry=%d) %s", i, err)
}
return

View File

@ -49,8 +49,12 @@ func (tr *Transport) dialOptions() []dialer.DialOption {
}
func (tr *Transport) Handshake(ctx context.Context, conn net.Conn) (net.Conn, error) {
var err error
if hs, ok := tr.dialer.(dialer.Handshaker); ok {
return hs.Handshake(ctx, conn)
conn, err = hs.Handshake(ctx, conn)
if err != nil {
return nil, err
}
}
if hs, ok := tr.connector.(connector.Handshaker); ok {
return hs.Handshake(ctx, conn)