private void handleWelcome(JsonObject message) { // a non-null reconnect token indicates a prior connection bool invokeOnReconnect = (reconnectId != null); bool resume = false; clientId = (String)message[ProtocolConstants.CLIENT_ID_FIELD]; reconnectId = (String)message[ProtocolConstants.ID_TOKEN_FIELD]; maxMessageSize = (long)message[ProtocolConstants.MAX_SIZE_FIELD]; try { if (message.ContainsKey(ProtocolConstants.PROTOCOL_FIELD)) { protocol = (long)message[ProtocolConstants.PROTOCOL_FIELD]; } else { protocol = 0; } } catch (Exception e) { Console.WriteLine(e); } if (message.ContainsKey(ProtocolConstants.QOS_FIELD)) { qos = System.Boolean.Parse((String)message[ProtocolConstants.QOS_FIELD]); } else { qos = false; } if (message.ContainsKey(ProtocolConstants.RESUME_FIELD)) { resume = System.Boolean.Parse((String)message[ProtocolConstants.RESUME_FIELD]); } // connected Interlocked.Exchange(ref connected, 1); // reset reconnect attempts reconnectAttempts = 0; // reset URL list to start auto-reconnect attempts from the beginning resetURLList(); // synchronized to ensure message sequence numbers are ordered lock (writeLock) { // clear the queue while (writeQueue.Count > 0) { writeQueue.Take(); } // start writer thread writer = new Thread(new ThreadStart(Run)); writer.Name = "EFTL Writer"; writer.IsBackground = true; writer.Start(); // repair subscriptions foreach (BasicSubscription subscription in subscriptions.Values) { if (!resume) { subscription.LastSeqNum = 0; } Subscribe(subscription); } // re-send unacknowledged messages foreach (PublishContext ctx in requests.Values()) { Queue(ctx); } } // invoke callback if not auto-reconnecting if (!invokeOnReconnect) { listener.OnConnect(this); } else if (Interlocked.Read(ref reconnecting) == 0) { listener.OnReconnect(this); } }
private void handleWelcome(JsonObject message) { // a non-null reconnect token indicates a prior connection bool invokeOnReconnect = (reconnectId != null); bool resume = false; clientId = (String)message[ProtocolConstants.CLIENT_ID_FIELD]; reconnectId = (String)message[ProtocolConstants.ID_TOKEN_FIELD]; maxMessageSize = (long)message[ProtocolConstants.MAX_SIZE_FIELD]; if (message.ContainsKey(ProtocolConstants.QOS_FIELD)) { qos = System.Boolean.Parse((String)message[ProtocolConstants.QOS_FIELD]); } else { qos = false; } if (message.ContainsKey(ProtocolConstants.RESUME_FIELD)) { resume = System.Boolean.Parse((String)message[ProtocolConstants.RESUME_FIELD]); } // connected Interlocked.Exchange(ref connected, 1); // reset reconnect attempts reconnectAttempts = 0; // synchronized to ensure message sequence numbers are ordered lock (writeLock) { lock (writeQueueLock) { // clear the queue while (writeQueue.Count > 0) { writeQueue.Dequeue(); } } // start writer thread writer = new Thread(new ThreadStart(Run)); writer.Name = "EFTL Writer"; writer.IsBackground = true; writerInterrupted = false; writer.Start(); // repair subscriptions foreach (BasicSubscription subscription in subscriptions.Values) { // invoke callback if not auto-reconnecting if (Interlocked.Read(ref reconnecting) != 1) { subscription.setPending(true); } Subscribe(subscription); } if (resume) { // re-send unacknowledged messages foreach (PublishContext ctx in sendList.Values()) { Queue(ctx); } } else { // clear unacknowledged messages publishClear(CompletionListenerConstants.PUBLISH_FAILED, "Reconnect"); // reset the last sequence number if not a reconnect lastSequenceNumber = 0; } } // invoke callback if not auto-reconnecting if (!invokeOnReconnect) { listener.OnConnect(this); } else if (Interlocked.Read(ref reconnecting) == 1) { listener.OnReconnect(this); } }