static async Task <IotHubClientWebSocket> CreateLegacyClientWebSocketAsync(Uri webSocketUri, TimeSpan timeout)
        {
            var websocket = new IotHubClientWebSocket(WebSocketConstants.SubProtocols.Amqpwsb10);
            await websocket.ConnectAsync(webSocketUri.Host, webSocketUri.Port, WebSocketConstants.Scheme, timeout).ConfigureAwait(false);

            return(websocket);
        }
Esempio n. 2
0
        private async Task <TransportBase> CreateClientWebSocketTransport(TimeSpan timeout)
        {
            var timeoutHelper = new TimeoutHelper(timeout);
            var websocketUri  = new Uri(WebSocketConstants.Scheme + ConnectionString.HostName + ":" + WebSocketConstants.SecurePort + WebSocketConstants.UriSuffix);

#if NET451
            // Use Legacy WebSocket if it is running on Windows 7 or older. Windows 7/Windows 2008 R2 is version 6.1
            if (Environment.OSVersion.Version.Major < 6 || (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor <= 1))
            {
                IotHubClientWebSocket websocket = await CreateLegacyClientWebSocketAsync(
                    websocketUri,
                    timeoutHelper.RemainingTime())
                                                  .ConfigureAwait(false);

                return(new LegacyClientWebSocketTransport(
                           websocket,
                           DefaultOperationTimeout,
                           null,
                           null));
            }
            else
            {
#endif
            ClientWebSocket websocket = await CreateClientWebSocketAsync(websocketUri, timeoutHelper.RemainingTime()).ConfigureAwait(false);

            return(new ClientWebSocketTransport(
                       websocket,
                       null,
                       null));

#if NET451
        }
#endif
        }
 public LegacyClientWebSocketTransport(IotHubClientWebSocket webSocket, TimeSpan operationTimeout,  EndPoint localEndpoint, EndPoint remoteEndpoint)
     : base("legacyclientwebsocket")
 {
     this.webSocket = webSocket;
     this.operationTimeout = operationTimeout;
     this.localEndPoint = localEndpoint;
     this.remoteEndPoint = remoteEndpoint;
     this.asyncReadBufferSize = MaxReadBufferSize; // TODO: read from Config Settings
     this.asyncReadBuffer = new byte[this.asyncReadBufferSize];
 }
Esempio n. 4
0
 public LegacyClientWebSocketTransport(IotHubClientWebSocket webSocket, TimeSpan operationTimeout, EndPoint localEndpoint, EndPoint remoteEndpoint)
     : base("legacyclientwebsocket")
 {
     this.webSocket           = webSocket;
     this.operationTimeout    = operationTimeout;
     this.localEndPoint       = localEndpoint;
     this.remoteEndPoint      = remoteEndpoint;
     this.asyncReadBufferSize = MaxReadBufferSize; // TODO: read from Config Settings
     this.asyncReadBuffer     = new byte[this.asyncReadBufferSize];
 }
        private static async Task <IotHubClientWebSocket> CreateLegacyClientWebSocketAsync(Uri webSocketUri, TimeSpan timeout)
        {
            Logging.Enter(webSocketUri, timeout, nameof(CreateLegacyClientWebSocketAsync));

            try
            {
                var websocket = new IotHubClientWebSocket(WebSocketConstants.SubProtocols.Amqpwsb10);
                await websocket
                .ConnectAsync(webSocketUri.Host, webSocketUri.Port, WebSocketConstants.Scheme, timeout)
                .ConfigureAwait(false);

                return(websocket);
            }
            finally
            {
                Logging.Exit(webSocketUri, timeout, nameof(CreateLegacyClientWebSocketAsync));
            }
        }
Esempio n. 6
0
        private async Task <TransportBase> CreateClientWebSocketTransportAsync(TimeSpan timeout)
        {
            Logging.Enter(this, timeout, nameof(CreateClientWebSocketTransportAsync));

            try
            {
                var timeoutHelper = new TimeoutHelper(timeout);
                var websocketUri  = new Uri($"{ WebSocketConstants.Scheme }{ Credential.HostName}:{ WebSocketConstants.SecurePort}{WebSocketConstants.UriSuffix}");

                Logging.Info(this, websocketUri, nameof(CreateClientWebSocketTransportAsync));

#if NET451
                // Use Legacy WebSocket if it is running on Windows 7 or older. Windows 7/Windows 2008 R2 is version 6.1
                if (Environment.OSVersion.Version.Major < 6 ||
                    (Environment.OSVersion.Version.Major == 6 &&
                     Environment.OSVersion.Version.Minor <= 1))
                {
                    IotHubClientWebSocket websocket = await CreateLegacyClientWebSocketAsync(
                        websocketUri,
                        timeoutHelper.RemainingTime())
                                                      .ConfigureAwait(false);

                    return(new LegacyClientWebSocketTransport(
                               websocket,
                               DefaultOperationTimeout,
                               null,
                               null));
                }
                else
                {
#endif
                ClientWebSocket websocket = await CreateClientWebSocketAsync(websocketUri, timeoutHelper.RemainingTime()).ConfigureAwait(false);

                return(new ClientWebSocketTransport(websocket, null, null));

#if NET451
            }
#endif
            }
            finally
            {
                Logging.Exit(this, timeout, nameof(CreateClientWebSocketTransportAsync));
            }
        }
        public async Task LegacyWebSocketWriteAfterAbortTest()
        {
            var websocket = new IotHubClientWebSocket(WebSocketConstants.SubProtocols.Amqpwsb10);
            await websocket.ConnectAsync(IotHubName, Port, "ws://", TimeSpan.FromMinutes(1));
            legacyClientWebSocketTransport = new LegacyClientWebSocketTransport(websocket, TimeSpan.FromMinutes(1), null, null);
            legacyClientWebSocketTransport.Abort();

            var args = new TransportAsyncCallbackArgs();
            args.SetBuffer(byteArray, 0, byteArray.Length);
            legacyClientWebSocketTransport.WriteAsync(args);
            Assert.Fail("Did not throw object disposed exception");
        }
 public async Task LegacyWebSocketWriteAfterCloseTest()
 {
     var websocket = new IotHubClientWebSocket(WebSocketConstants.SubProtocols.Amqpwsb10);
     await websocket.ConnectAsync(IotHubName, Port, "ws://", TimeSpan.FromMinutes(1));
     legacyClientWebSocketTransport = new LegacyClientWebSocketTransport(websocket, TimeSpan.FromMinutes(1), null, null);
     await legacyClientWebSocketTransport.CloseAsync(TimeSpan.FromSeconds(30));
     var args = new TransportAsyncCallbackArgs();
     args.SetBuffer(byteArray, 0, byteArray.Length);
     legacyClientWebSocketTransport.WriteAsync(args);
 }
        public async Task LegacyWebSocketReadWriteTest()
        {
            var websocket = new IotHubClientWebSocket(WebSocketConstants.SubProtocols.Amqpwsb10);
            await websocket.ConnectAsync(IotHubName, Port, "ws://", TimeSpan.FromMinutes(1));

            legacyClientWebSocketTransport = new LegacyClientWebSocketTransport(websocket, TimeSpan.FromSeconds(60), null, null);

            // Test Write API
            TransportAsyncCallbackArgs args = new TransportAsyncCallbackArgs();
            args.CompletedCallback = onWriteOperationComplete;
            args.SetBuffer(byteArray, 0, byteArray.Length);
            legacyClientWebSocketTransport.WriteAsync(args);

            // Test Read API
            args.CompletedCallback = onReadOperationComplete;
            if (legacyClientWebSocketTransport.ReadAsync(args))
            {
                while (!readComplete)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }

            // Once Read operation is complete, close websocket transport
            legacyClientWebSocketTransport.CloseAsync(TimeSpan.FromSeconds(30)).Wait(CancellationToken.None);
        }
        public async Task LegacyClientWebSocketTransportReadWithoutConnectTest()
        {
            var websocket = new IotHubClientWebSocket(WebSocketConstants.SubProtocols.Amqpwsb10);
            var clientWebSocketTransport = new LegacyClientWebSocketTransport(websocket, TimeSpan.FromSeconds(60), null, null);
            var args = new TransportAsyncCallbackArgs();
            var byteArray = new byte[10];
            args.SetBuffer(byteArray, 0, 10);
            if (clientWebSocketTransport.ReadAsync(args))
            {
                while (!readComplete)
                {
                    Thread.Sleep(TimeSpan.FromSeconds(1));
                }
            }

            await websocket.CloseAsync();
        }
 public void LegacyClientWebSocketTransportWriteWithoutConnectTest()
 {
     var websocket = new IotHubClientWebSocket(WebSocketConstants.SubProtocols.Amqpwsb10);
     var clientWebSocketTransport = new LegacyClientWebSocketTransport(websocket, TimeSpan.FromSeconds(60), null, null);
     var args = new TransportAsyncCallbackArgs();
     args.SetBuffer(byteArray, 0, byteArray.Length);
     clientWebSocketTransport.WriteAsync(args);
 }