Skip to content

Commit

Permalink
Merge pull request #555 from gopcua/add-missing-with-context-methods
Browse files Browse the repository at this point in the history
Add missing with context methods
  • Loading branch information
magiconair authored Jan 27, 2022
2 parents 4dee16b + 17436a2 commit 71a904b
Show file tree
Hide file tree
Showing 24 changed files with 165 additions and 81 deletions.
18 changes: 13 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func GetEndpoints(ctx context.Context, endpoint string, opts ...Option) ([]*ua.E
if err := c.Dial(ctx); err != nil {
return nil, err
}
defer c.Close()
defer c.CloseWithContext(ctx)
res, err := c.GetEndpointsWithContext(ctx)
if err != nil {
return nil, err
Expand Down Expand Up @@ -195,14 +195,14 @@ func (c *Client) Connect(ctx context.Context) (err error) {

s, err := c.CreateSessionWithContext(ctx, c.cfg.session)
if err != nil {
c.Close()
c.CloseWithContext(ctx)
stats.RecordError(err)

return err
}

if err := c.ActivateSessionWithContext(ctx, s); err != nil {
c.Close()
c.CloseWithContext(ctx)
stats.RecordError(err)

return err
Expand All @@ -221,7 +221,7 @@ func (c *Client) Connect(ctx context.Context) (err error) {
// todo(fs): see the discussion in https://github.com/gopcua/opcua/pull/512
// todo(fs): and you should find a commit that implements this option.
if err := c.UpdateNamespacesWithContext(ctx); err != nil {
c.Close()
c.CloseWithContext(ctx)
stats.RecordError(err)

return err
Expand Down Expand Up @@ -552,12 +552,20 @@ func (c *Client) Dial(ctx context.Context) error {
}

// Close closes the session and the secure channel.
//
// Note: Starting with v0.5 this method will require a context
// and the corresponding XXXWithContext(ctx) method will be removed.
func (c *Client) Close() error {
return c.CloseWithContext(context.Background())
}

// Note: Starting with v0.5 this method is superseded by the non 'WithContext' method.
func (c *Client) CloseWithContext(ctx context.Context) error {
stats.Client().Add("Close", 1)

// try to close the session but ignore any error
// so that we close the underlying channel and connection.
c.CloseSession()
c.CloseSessionWithContext(ctx)
c.setState(Closed)

if c.mcancel != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/accesslevel/accesslevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

id, err := ua.ParseNodeID(*nodeID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/browse/browse.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

id, err := ua.ParseNodeID(*nodeID)
if err != nil {
Expand Down
16 changes: 8 additions & 8 deletions examples/crypto/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ import (
"strings"
"syscall"

"golang.org/x/crypto/ssh/terminal"

"github.com/gopcua/opcua"
"github.com/gopcua/opcua/debug"
"github.com/gopcua/opcua/errors"
"github.com/gopcua/opcua/ua"

"golang.org/x/crypto/ssh/terminal"
)

var (
Expand Down Expand Up @@ -65,10 +65,10 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

// Use our connection (read the server's time)
v, err := c.Node(ua.NewNumericNodeID(0, 2258)).Value()
v, err := c.Node(ua.NewNumericNodeID(0, 2258)).ValueWithContext(ctx)
if err != nil {
log.Fatal(err)
}
Expand All @@ -79,7 +79,7 @@ func main() {
}

// Detach our session and try re-establish it on a different secure channel
s, err := c.DetachSession()
s, err := c.DetachSessionWithContext(ctx)
if err != nil {
log.Fatalf("Error detaching session: %s", err)
}
Expand All @@ -88,16 +88,16 @@ func main() {

// Create a channel only and do not activate it automatically
d.Dial(ctx)
defer d.Close()
defer d.CloseWithContext(ctx)

// Activate the previous session on the new channel
err = d.ActivateSession(s)
err = d.ActivateSessionWithContext(ctx, s)
if err != nil {
log.Fatalf("Error reactivating session: %s", err)
}

// Read the time again to prove our session is still OK
v, err = d.Node(ua.NewNumericNodeID(0, 2258)).Value()
v, err = d.Node(ua.NewNumericNodeID(0, 2258)).ValueWithContext(ctx)
if err != nil {
log.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/datetime/datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

v, err := c.Node(ua.NewNumericNodeID(0, 2258)).ValueWithContext(ctx)
switch {
Expand Down
2 changes: 1 addition & 1 deletion examples/history-read/history-read.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

id, err := ua.ParseNodeID(*nodeID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/method/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

in := int64(12)
req := &ua.CallMethodRequest{
Expand Down
2 changes: 1 addition & 1 deletion examples/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func main() {
log.Fatal(err)
}

defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

m, err := monitor.NewNodeMonitor(c)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/read/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

id, err := ua.ParseNodeID(*nodeID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/regread/regread.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

id, err := ua.ParseNodeID(*nodeID)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion examples/subscribe/subscribe.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

notifyCh := make(chan *opcua.PublishNotificationData)

Expand Down
2 changes: 1 addition & 1 deletion examples/translate/translate.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

root := c.Node(ua.NewTwoByteNodeID(id.ObjectsFolder))
nodeID, err := root.TranslateBrowsePathInNamespaceToNodeIDWithContext(ctx, uint16(*ns), *nodePath)
Expand Down
2 changes: 1 addition & 1 deletion examples/trigger/trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

notifyCh := make(chan *opcua.PublishNotificationData)

Expand Down
2 changes: 1 addition & 1 deletion examples/udt/udt.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

v, err := c.Node(id).ValueWithContext(ctx)
switch {
Expand Down
2 changes: 1 addition & 1 deletion examples/write/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func main() {
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.CloseSessionWithContext(ctx)
defer c.CloseWithContext(ctx)

id, err := ua.ParseNodeID(*nodeID)
if err != nil {
Expand Down
Loading

0 comments on commit 71a904b

Please sign in to comment.