Skip to content

Commit

Permalink
uacp: honor the context deadline during the handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
magiconair committed Jan 19, 2023
1 parent 218a2b7 commit 437537a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions uacp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net"
"sync"
"sync/atomic"
"time"

"github.com/gopcua/opcua/debug"
"github.com/gopcua/opcua/errors"
Expand Down Expand Up @@ -89,7 +90,7 @@ func (d *Dialer) Dial(ctx context.Context, endpoint string) (*Conn, error) {
}

debug.Printf("uacp %d: start HEL/ACK handshake", conn.id)
if err := conn.Handshake(endpoint); err != nil {
if err := conn.Handshake(ctx, endpoint); err != nil {
debug.Printf("uacp %d: HEL/ACK handshake failed: %s", conn.id, err)
conn.Close()
return nil, err
Expand Down Expand Up @@ -217,7 +218,7 @@ func (c *Conn) close() error {
return c.TCPConn.Close()
}

func (c *Conn) Handshake(endpoint string) error {
func (c *Conn) Handshake(ctx context.Context, endpoint string) error {
hel := &Hello{
Version: c.ack.Version,
ReceiveBufSize: c.ack.ReceiveBufSize,
Expand All @@ -227,6 +228,11 @@ func (c *Conn) Handshake(endpoint string) error {
EndpointURL: endpoint,
}

// set a deadline if there is one
if dl, ok := ctx.Deadline(); ok {
c.SetDeadline(dl)
}

if err := c.Send("HELF", hel); err != nil {
return err
}
Expand All @@ -236,6 +242,9 @@ func (c *Conn) Handshake(endpoint string) error {
return err
}

// clear the deadline
c.SetDeadline(time.Time{})

msgtyp := string(b[:4])
switch msgtyp {
case "ACKF":
Expand Down

0 comments on commit 437537a

Please sign in to comment.