Unverified Commit 1dd6d60a authored by Kevin's avatar Kevin Committed by GitHub
Browse files

Fix cancel via context and respect context deadline (#14)

* Fix cancel via context and respect context deadline

* Remove timeout overwrite and remove go1.7 from travis
parent c8fd5401
...@@ -4,7 +4,6 @@ os: ...@@ -4,7 +4,6 @@ os:
- linux - linux
go: go:
- 1.7
- 1.8 - 1.8
- 1.9 - 1.9
- tip - tip
......
...@@ -209,18 +209,26 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) { ...@@ -209,18 +209,26 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) {
if h.dialerTmo != time.Duration(0) { if h.dialerTmo != time.Duration(0) {
tmo = time.After(h.dialerTmo) tmo = time.After(h.dialerTmo)
} }
select { select {
case <-ctx.Done(): case <-ctx.Done():
return nil, ctx.Err() return h.cancelDial()
case <-tmo:
return h.cancelDial()
case <-h.done: case <-h.done:
return nil, h.err return nil, h.err
case c := <-h.chMasterConn: case c := <-h.chMasterConn:
return gatt.NewClient(c) return gatt.NewClient(c)
case <-tmo:
}
}
// cancelDial cancels the Dialing
func (h *HCI) cancelDial() (ble.Client, error) {
err := h.Send(&h.params.connCancel, nil) err := h.Send(&h.params.connCancel, nil)
if err == nil { if err == nil {
// The pending connection was canceled successfully. // The pending connection was canceled successfully.
return nil, fmt.Errorf("connection timed out") return nil, fmt.Errorf("connection canceled")
} }
// The connection has been established, the cancel command // The connection has been established, the cancel command
// failed with ErrDisallowed. // failed with ErrDisallowed.
...@@ -228,7 +236,6 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) { ...@@ -228,7 +236,6 @@ func (h *HCI) Dial(ctx context.Context, a ble.Addr) (ble.Client, error) {
return gatt.NewClient(<-h.chMasterConn) return gatt.NewClient(<-h.chMasterConn)
} }
return nil, errors.Wrap(err, "cancel connection failed") return nil, errors.Wrap(err, "cancel connection failed")
}
} }
// Advertise starts advertising. // Advertise starts advertising.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment