// The unsubscribe method handles both the subscripition // unsubscribe and close operations. private void unsubscribe(bool close) { string linbox = null; string lAckInbox = null; Connection lsc = null; rwLock.EnterWriteLock(); try { if (sc == null) { throw new StanBadSubscriptionException(); } lsc = sc; sc = null; linbox = inboxSub.Subject; inboxSub.Unsubscribe(); inboxSub = null; lAckInbox = ackInbox; ackInbox = null; } catch { throw; } finally { rwLock.ExitWriteLock(); } lsc.unsubscribe(subject, linbox, lAckInbox, close); }
// in STAN, much of this code is in the connection module. internal void subscribe(string subRequestSubject, string subject, string qgroup, EventHandler <StanMsgHandlerArgs> handler) { rwLock.EnterWriteLock(); try { this.handler += handler; this.subject = subject; if (sc == null) { throw new StanConnectionClosedException(); } // Listen for actual messages. inboxSub = sc.NATSConnection.SubscribeAsync(inbox, sc.processMsg); SubscriptionRequest sr = new SubscriptionRequest(); sr.ClientID = sc.ClientID; sr.Subject = subject; sr.QGroup = (qgroup == null ? "" : qgroup); sr.Inbox = inbox; sr.MaxInFlight = options.MaxInflight; sr.AckWaitInSecs = options.AckWait / 1000; sr.StartPosition = options.startAt; sr.DurableName = (options.DurableName == null ? "" : options.DurableName); // Conditionals switch (sr.StartPosition) { case StartPosition.TimeDeltaStart: sr.StartTimeDelta = convertTimeSpan( options.useStartTimeDelta ? options.startTimeDelta : (DateTime.UtcNow - options.startTime)); break; case StartPosition.SequenceStart: sr.StartSequence = options.startSequence; break; } byte[] b = ProtocolSerializer.marshal(sr); Msg m = sc.NATSConnection.Request(subRequestSubject, b, sc.opts.ConnectTimeout); SubscriptionResponse r = new SubscriptionResponse(); ProtocolSerializer.unmarshal(m.Data, r); if (string.IsNullOrWhiteSpace(r.Error) == false) { throw new StanException(r.Error); } ackInbox = r.AckInbox; } catch { if (inboxSub != null) { try { inboxSub.Unsubscribe(); } catch (NATSTimeoutException) { // NOOP - this is unrecoverable. } } throw; } finally { rwLock.ExitWriteLock(); } }