Dial/Hangup Hack. If the connection is closed because of an error, re-dial it from within the dial function itself by incrementing the ID and creating a new session on top of the old one.
This commit is contained in:
24
dial.go
24
dial.go
@ -25,12 +25,10 @@ func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Con
|
||||
}()
|
||||
select {
|
||||
case err := <-errCh:
|
||||
c.Close()
|
||||
return nil, err
|
||||
case conn := <-connCh:
|
||||
return conn, nil
|
||||
case <-ctx.Done():
|
||||
c.Close()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
@ -38,16 +36,16 @@ func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Con
|
||||
func (c *Client) dialCheck(addr string) (int32, bool) {
|
||||
if c.lastaddr == "invalid" {
|
||||
return c.NewID(), true
|
||||
} else if addr == "" {
|
||||
return c.id, false
|
||||
// } else if addr == "" {
|
||||
// return c.id, false
|
||||
} else if c.lastaddr != addr {
|
||||
return c.NewID(), true
|
||||
}
|
||||
return c.id, false
|
||||
return c.NewID(), false
|
||||
}
|
||||
|
||||
func (c *Client) Dial(network, addr string) (net.Conn, error) {
|
||||
return c.DialContext(context.Background(), network, addr)
|
||||
return c.DialContextFree(network, addr)
|
||||
}
|
||||
|
||||
// Dial implements the net.Dial function and can be used for http.Transport
|
||||
@ -60,16 +58,17 @@ func (c *Client) DialContextFree(network, addr string) (net.Conn, error) {
|
||||
}
|
||||
addr, err := c.Lookup(addr)
|
||||
if err != nil {
|
||||
log.Printf("LOOKUP DIALER ERROR %s %s", addr, err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// var test bool
|
||||
c.id, _ = c.dialCheck(addr)
|
||||
// var test bool
|
||||
//c.id, _ = c.dialCheck(addr)
|
||||
// log.Println("Address indicates we need to dial a new session.")
|
||||
// if test {
|
||||
c.destination, err = c.CreateStreamSession(c.id, c.destination)
|
||||
if err != nil {
|
||||
c.id += 1
|
||||
c, err = c.NewClient()
|
||||
c, err = c.NewClient(c.id + 1)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -78,15 +77,16 @@ func (c *Client) DialContextFree(network, addr string) (net.Conn, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
c, err = c.NewClient()
|
||||
// }
|
||||
c, err = c.NewClient(c.id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.lastaddr = addr
|
||||
//}
|
||||
err = c.StreamConnect(c.id, addr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//}
|
||||
return c.SamConn, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user