Пример #1
0
 public void Dispose() {
     if (_subscriptionClient != null)
         _subscriptionClient.Dispose();
     _subscriptionClient = null;
 }
Пример #2
0
        /// <summary>
        /// StartSubscriptionsAsync must be called if you want subscription change notifications.
        /// This starts the subscription engine. We always create one subscription for
        /// Home.Name to start (but ignore any updates).
        /// </summary>
        public async Task StartSubscriptionsAsync(IPremiseSocket premiseSocket) {
            try {
                // Always stop the previous subscription client first
                StopSubscriptions();

                if (_readCts != null) {
                    // If we've just cancelled the connection and are reconnecting
                    // we need to wait a bit for the socket to be closed.
                    if (_readCts.IsCancellationRequested)
                        await TaskEx.Delay(1000);
                    _readCts.Dispose();
                }
                _readCts = new CancellationTokenSource();

                _subscriptionClient = premiseSocket;
                Debug.WriteLine("Connecting to IPremiseSocket");
                await _subscriptionClient.ConnectAsync(Host, Port, SSL, Username, Password);
                Debug.WriteLine("Connected to socket");
                Error = false;
                LastError = "";
                LastErrorContent = "";

                // Assign the resulting task to a local variable to get around
                // the compiler warning about not awaiting this.
                // http://stackoverflow.com/questions/18577054/alternative-to-task-run-that-doesnt-throw-warning
                //ThreadPool.QueueUserWorkItem(state => ReadSubscriptionResponses());
                var readTask = Task.Factory.StartNew(ReadSubscriptionResponses, _readCts.Token);

                // We do a GetValue so we know we have a good connection
                if (await SendRequest("sys://Home?f??" + "Name")) {
                    if (FastMode) EnableFastMode();
                    foreach (var subscription in _subscriptions) {
                        subscription.Value.RequestTask = SendSubscriptionRequest(subscription.Value);
                    }
                }
                else {
                    Debug.WriteLine("SendRequest returned false");
                }
            }
            catch (Exception ex) {
                Debug.WriteLine("StartSubscriptionAsync: " + ex.ToString());
                Error = true;
                LastError = ex.GetType().ToString();
                LastErrorContent = ex.Message;
                Dispose();
                throw ex;
            }
        }
Пример #3
0
        /// <summary>
        /// StartSubscriptionsAsync must be called if you want subscription change notifications.
        /// This starts the subscription engine. We always create one subscription for
        /// Home DisplayName to start (but ignore any updates).
        /// </summary>
        public async Task StartSubscriptionsAsync(IPremiseSocket premiseSocket) {
            try {
                _subscriptionClient = premiseSocket;
                await _subscriptionClient.ConnectAsync(Host, Port, SSL, Username, Password);

                // Assign the resulting task to a local variable to get around
                // the compiler warning about not awaiting this.
                // http://stackoverflow.com/questions/18577054/alternative-to-task-run-that-doesnt-throw-warning
                Task task = Task.Run(() => ReadSubscriptionResponses());

                // We do a GetValue so we know we have a good connection
                if (await SendRequest("sys://Home?f??" + "Name")) {

                    if (FastMode) EnableFastMode();

                    foreach (var subscription in _subscriptions) {
                        Task t = SendSubscriptionRequest(subscription.Value);
                    }
                }
                else {
                    Debug.WriteLine("SendRequest returned false");
                }
            }
            catch (Exception ex) {
                Debug.WriteLine(ex.ToString());
                Dispose();
                throw ex;
            }
        }