Example #1
0
        public async Task RunAsync(ChannelIdentifier channelIdentifier, WebSocket webSocket, CancellationToken cancellationToken)
        {
            if (webSocket == null)
            {
                throw new ArgumentNullException(nameof(webSocket));
            }

            var channelOptions = new ConnectorChannelOptions
            {
                UseCompression = true
            };

            var channel = new ConnectorChannel(channelOptions, webSocket, _cloudMessageSerializer, _logger);

            try
            {
                var openChannel = new OpenChannel(channelIdentifier, channel, _logger);
                lock (_openChannels)
                {
                    _openChannels[channelIdentifier] = openChannel;
                }

                await openChannel.RunAsync(cancellationToken).ConfigureAwait(false);
            }
            finally
            {
                lock (_openChannels)
                {
                    _openChannels.Remove(channelIdentifier);
                }
            }
        }
Example #2
0
        public void ResetChannelStatistics(ChannelIdentifier channelIdentifier)
        {
            lock (_openChannels)
            {
                if (!_openChannels.TryGetValue(channelIdentifier, out var deviceSession))
                {
                    return;
                }

                deviceSession.ResetStatistics();
            }
        }
Example #3
0
        public ConnectorChannelStatistics GetChannelStatistics(ChannelIdentifier channelIdentifier)
        {
            lock (_openChannels)
            {
                if (!_openChannels.TryGetValue(channelIdentifier, out var deviceSession))
                {
                    return(null);
                }

                return(deviceSession.GetStatistics());
            }
        }
Example #4
0
        public async Task <CloudMessage> Invoke(ChannelIdentifier channelIdentifier, CloudMessage requestMessage, CancellationToken cancellationToken)
        {
            if (channelIdentifier == null)
            {
                throw new ArgumentNullException(nameof(channelIdentifier));
            }
            if (requestMessage == null)
            {
                throw new ArgumentNullException(nameof(requestMessage));
            }

            OpenChannel deviceSession;

            lock (_openChannels)
            {
                if (!_openChannels.TryGetValue(channelIdentifier, out deviceSession))
                {
                    throw new OpenChannelNotFoundException(channelIdentifier);
                }
            }

            requestMessage.CorrelationId = Guid.NewGuid().ToString();

            var resultAwaiter = new TaskCompletionSource <CloudMessage>();

            try
            {
                deviceSession.AddAwaiter(requestMessage.CorrelationId, resultAwaiter);

                using (cancellationToken.Register(() =>
                {
                    resultAwaiter.TrySetCanceled();
                }))
                {
                    await deviceSession.SendAsync(requestMessage, cancellationToken).ConfigureAwait(false);

                    return(await resultAwaiter.Task.ConfigureAwait(false));
                }
            }
            finally
            {
                deviceSession.RemoveAwaiter(requestMessage.CorrelationId);
            }
        }
Example #5
0
 public OpenChannel(ChannelIdentifier identifier, ConnectorChannel channel, ILogger logger)
 {
     _identifier = identifier ?? throw new ArgumentNullException(nameof(identifier));
     _channel    = channel ?? throw new ArgumentNullException(nameof(channel));
     _logger     = logger ?? throw new ArgumentNullException(nameof(logger));
 }
Example #6
0
 public OpenChannelNotFoundException(ChannelIdentifier identifier)
     : base($"No open channel with ID '{identifier}' found.")
 {
 }