diff --git a/client_sub.go b/client_sub.go index 8cb63561..024500c0 100644 --- a/client_sub.go +++ b/client_sub.go @@ -89,7 +89,7 @@ func (c *Client) SubscriptionIDs() []uint32 { } // recreateSubscriptions creates new subscriptions -// with the same parameters to replace the previous ones +// with the same parameters to replace the previous one func (c *Client) recreateSubscription(ctx context.Context, id uint32) error { c.subMux.Lock() defer c.subMux.Unlock() @@ -98,7 +98,10 @@ func (c *Client) recreateSubscription(ctx context.Context, id uint32) error { if !ok { return ua.StatusBadSubscriptionIDInvalid } - return sub.recreate_NeedsSubMuxLock(ctx) + + sub.recreate_delete(ctx) + c.forgetSubscription_NeedsSubMuxLock(ctx, id) + return sub.recreate_create(ctx) } // transferSubscriptions ask the server to transfer the given subscriptions diff --git a/subscription.go b/subscription.go index e8f981d8..5b5a3d27 100644 --- a/subscription.go +++ b/subscription.go @@ -391,29 +391,31 @@ func (p *SubscriptionParameters) setDefaults() { } } -// recreate_NeedsSubMuxLock creates a new subscription based on the previous subscription -// parameters and monitored items. -func (s *Subscription) recreate_NeedsSubMuxLock(ctx context.Context) error { - dlog := debug.NewPrefixLogger("sub %d: recreate: ", s.SubscriptionID) - - if s.SubscriptionID == terminatedSubscriptionID { - dlog.Printf("subscription is not in a valid state") - return nil +// recreate_delete is called by the client when it is trying to +// recreate an existing subscription. This function deletes the +// existing subscription from the server. +func (s *Subscription) recreate_delete(ctx context.Context) error { + dlog := debug.NewPrefixLogger("sub %d: recreate_delete: ", s.SubscriptionID) + req := &ua.DeleteSubscriptionsRequest{ + SubscriptionIDs: []uint32{s.SubscriptionID}, } + var res *ua.DeleteSubscriptionsResponse + _ = s.c.Send(ctx, req, func(v ua.Response) error { + return safeAssign(v, &res) + }) + dlog.Print("subscription deleted") + return nil +} + +// recreate_create is called by the client when it is trying to +// recreate an existing subscription. This function creates a +// new subscription with the same parameters as the previous one. +func (s *Subscription) recreate_create(ctx context.Context) error { + dlog := debug.NewPrefixLogger("sub %d: recreate_create: ", s.SubscriptionID) + s.paramsMu.Lock() params := s.params - { - req := &ua.DeleteSubscriptionsRequest{ - SubscriptionIDs: []uint32{s.SubscriptionID}, - } - var res *ua.DeleteSubscriptionsResponse - _ = s.c.Send(ctx, req, func(v ua.Response) error { - return safeAssign(v, &res) - }) - dlog.Print("subscription deleted") - } - s.c.forgetSubscription_NeedsSubMuxLock(ctx, s.SubscriptionID) - dlog.Printf("subscription forgotton") + s.paramsMu.Unlock() req := &ua.CreateSubscriptionRequest{ RequestedPublishingInterval: float64(params.Interval / time.Millisecond),