Exemple #1
0
        public Task ExecuteAsync(IWebSocketsClient client, byte[] data)
        {
            if (_clientType == ClientType.Consumer)
            {
                return(Task.CompletedTask);
            }

            if (data is null || data.Length == 0)
            {
                return(Task.CompletedTask);
            }

            var(dataHeaderId, headerData) = GetDataInfo(data);
            if (dataHeaderId is null || string.IsNullOrWhiteSpace(headerData))
            {
                return(Task.CompletedTask);
            }

            var dataResult = new Dictionary <string, string>
            {
                [string.Empty] = headerData
            };

            _dataPublisher.Publish(new DataHeaderData(dataHeaderId, dataResult));

            return(Task.CompletedTask);
        }
Exemple #2
0
        public void Can_remove_client()
        {
            IWebSocketsClient client = _module.CreateClient(WebSocket.CreateFromStream(Stream.Null, false, "subprotocol", TimeSpan.FromMinutes(1)), "test");

            client.Should().NotBeNull();
            _module.RemoveClient(client.Id);
        }
Exemple #3
0
        public void Channel_type_is_web_sockets()
        {
            IWebSocketsClient            webSocketsClient = Substitute.For <IWebSocketsClient>();
            NdmWebSocketsConsumerChannel channel          = new NdmWebSocketsConsumerChannel(webSocketsClient);

            channel.Type.Should().Be(NdmConsumerChannelType.WebSockets);
        }
Exemple #4
0
        public void Can_send_message()
        {
            IWebSocketsClient client = _module.CreateClient(WebSocket.CreateFromStream(Stream.Null, false, "subprotocol", TimeSpan.FromMinutes(1)), "test");

            client.Should().NotBeNull();
            _module.SendAsync(new WebSocketsMessage("test", "client", "data"));
        }
 public JsonRpcWebSocketsClient(IWebSocketsClient client,
                                JsonRpcProcessor jsonRpcProcessor, IJsonSerializer jsonSerializer)
 {
     _client           = client;
     _jsonRpcProcessor = jsonRpcProcessor;
     _jsonSerializer   = jsonSerializer;
 }
Exemple #6
0
        public void Can_publish()
        {
            IWebSocketsClient            webSocketsClient = Substitute.For <IWebSocketsClient>();
            NdmWebSocketsConsumerChannel channel          = new NdmWebSocketsConsumerChannel(webSocketsClient);

            channel.PublishAsync(Keccak.Zero, "client", "data");
            webSocketsClient.Received().SendAsync(Arg.Is <WebSocketsMessage>(ws => ws.Client == "client" && ws.Type == "data_received"));
        }
Exemple #7
0
        public void Id_is_copied_from_ws_client()
        {
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            webSocketsClient.Id.Returns(nameof(NdmWebSocketsClientTests) + "_id");

            INdmDataPublisher   dataPublisher = Substitute.For <INdmDataPublisher>();
            NdmWebSocketsClient client        = new NdmWebSocketsClient(webSocketsClient, dataPublisher);

            client.Id.Should().Be(nameof(NdmWebSocketsClientTests) + "_id");
        }
Exemple #8
0
        public void Can_receive_invalid_data_asset_id()
        {
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            webSocketsClient.Id.Returns(nameof(NdmWebSocketsClientTests) + "_id");

            INdmDataPublisher   dataPublisher = Substitute.For <INdmDataPublisher>();
            NdmWebSocketsClient client        = new NdmWebSocketsClient(webSocketsClient, dataPublisher);

            client.ReceiveAsync(Encoding.ASCII.GetBytes("a|b|c"));
        }
        public void Can_receive_data_without_asset_id()
        {
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            webSocketsClient.Id.Returns(nameof(NdmWebSocketsClientTests) + "_id");

            INdmDataPublisher   dataPublisher = Substitute.For <INdmDataPublisher>();
            NdmWebSocketsClient client        = new NdmWebSocketsClient(webSocketsClient, dataPublisher);

            client.ReceiveAsync(Encoding.ASCII.GetBytes("|b|c"));
            dataPublisher.DidNotReceiveWithAnyArgs().Publish(null);
        }
Exemple #10
0
        public void Can_receive_null_or_empty_data()
        {
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            webSocketsClient.Id.Returns(nameof(NdmWebSocketsClientTests) + "_id");

            INdmDataPublisher   dataPublisher = Substitute.For <INdmDataPublisher>();
            NdmWebSocketsClient client        = new NdmWebSocketsClient(webSocketsClient, dataPublisher);

            client.ReceiveAsync(null);
            client.ReceiveAsync(Bytes.Empty);
        }
Exemple #11
0
        public void Can_receive_data()
        {
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            webSocketsClient.Id.Returns(nameof(NdmWebSocketsClientTests) + "_id");

            INdmDataPublisher   dataPublisher = Substitute.For <INdmDataPublisher>();
            NdmWebSocketsClient client        = new NdmWebSocketsClient(webSocketsClient, dataPublisher);

            client.ReceiveAsync(Encoding.ASCII.GetBytes($"{Keccak.Zero.Bytes.ToHexString(false)}|b|c"));
            dataPublisher.Received().Publish(Arg.Is <DataAssetData>(dad => dad.Data == "c" && dad.AssetId == Keccak.Zero));
        }
Exemple #12
0
 public JsonRpcWebSocketsClient(IWebSocketsClient client,
                                JsonRpcProcessor jsonRpcProcessor,
                                JsonRpcService jsonRpcService,
                                IJsonSerializer jsonSerializer,
                                IJsonRpcLocalStats jsonRpcLocalStats)
 {
     _client            = client;
     _jsonRpcProcessor  = jsonRpcProcessor;
     _jsonRpcService    = jsonRpcService;
     _jsonSerializer    = jsonSerializer;
     _jsonRpcLocalStats = jsonRpcLocalStats;
 }
Exemple #13
0
        public void Forwards_raw_messages_to_ws_client()
        {
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            webSocketsClient.Id.Returns(nameof(NdmWebSocketsClientTests) + "_id");

            INdmDataPublisher   dataPublisher = Substitute.For <INdmDataPublisher>();
            NdmWebSocketsClient client        = new NdmWebSocketsClient(webSocketsClient, dataPublisher);

            client.SendRawAsync("raw");

            webSocketsClient.Received().SendRawAsync("raw");
        }
Exemple #14
0
        public async Task Stops_on_dirty_disconnect()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            receiveResult.Enqueue(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true));
            WebSocketMock mock = new WebSocketMock(receiveResult);

            mock.ReturnTaskWithFaultOnEmptyQueue = true;

            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            await mock.ReceiveAsync(webSocketsClient);
        }
Exemple #15
0
        public static void ResetWebSocketsClientUrl(IWebSocketsClient client, string url)
        {
            ExceptionUtility.Try(() =>
            {
                if (Environment.Providers.WebSocketsClient != null)
                {
                    Environment.Providers.WebSocketsClient.Dispose();
                }

                client.SetUrl(url);
                Environment.Providers.WebSocketsClient = client;
            });
        }
        public async Task ExecuteAsync(IWebSocketsClient client, byte[] data)
        {
            var result = await _jsonRpcProcessor.ProcessAsync(Encoding.UTF8.GetString(data));

            if (result.IsCollection)
            {
                await client.SendAsync(_jsonSerializer.Serialize(result.Responses));

                return;
            }

            await client.SendAsync(_jsonSerializer.Serialize(result.Responses.SingleOrDefault()));
        }
Exemple #17
0
 public JsonRpcWebSocketsClient(IWebSocketsClient client,
                                JsonRpcProcessor jsonRpcProcessor,
                                JsonRpcService jsonRpcService,
                                IJsonSerializer jsonSerializer,
                                IJsonRpcLocalStats jsonRpcLocalStats)
 {
     _client            = client;
     _jsonRpcProcessor  = jsonRpcProcessor;
     _jsonRpcService    = jsonRpcService;
     _jsonSerializer    = jsonSerializer;
     _jsonRpcLocalStats = jsonRpcLocalStats;
     _jsonRpcContext    = new JsonRpcContext(RpcEndpoint.WebSocket, this);
 }
Exemple #18
0
        public void Forwards_messages_to_ws_client()
        {
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            webSocketsClient.Id.Returns(nameof(NdmWebSocketsClientTests) + "_id");

            INdmDataPublisher   dataPublisher = Substitute.For <INdmDataPublisher>();
            NdmWebSocketsClient client        = new NdmWebSocketsClient(webSocketsClient, dataPublisher);
            WebSocketsMessage   message       = new WebSocketsMessage("type", "client", "data");

            client.SendAsync(message);

            webSocketsClient.Received().SendAsync(message);
        }
Exemple #19
0
        private static async Task ReceiveAsync(WebSocket webSocket, IWebSocketsClient client)
        {
            var buffer = new byte[1024 * 4];
            var result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);

            while (!result.CloseStatus.HasValue)
            {
                var data = buffer.Slice(0, result.Count);
                await client.ReceiveAsync(data);

                result = await webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);
            }

            await webSocket.CloseAsync(result.CloseStatus.Value, result.CloseStatusDescription, CancellationToken.None);
        }
        public async Task Can_receive_whole_message()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            receiveResult.Enqueue(new WebSocketReceiveResult(4096, WebSocketMessageType.Text, false));
            receiveResult.Enqueue(new WebSocketReceiveResult(4096, WebSocketMessageType.Text, false));
            receiveResult.Enqueue(new WebSocketReceiveResult(1024, WebSocketMessageType.Text, true));
            receiveResult.Enqueue(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));
            WebSocketMock mock = new WebSocketMock(receiveResult);

            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            await mock.ReceiveAsync(webSocketsClient);

            await webSocketsClient.Received().ReceiveAsync(Arg.Is <Memory <byte> >(ba => ba.Length == 2 * 4096 + 1024));
        }
Exemple #21
0
        /// <summary>
        /// Handles a failed websocket request
        /// </summary>
        /// <param name="client">The websocket client which sent the request</param>
        /// <param name="args">Arguments associated with failure</param>
        private static void HandleFailedRequest(IWebSocketsClient client, RequestFailureEventArgs args)
        {
            LogUtility.LogMessage(String.Format("Request Failure (reason: {0})", Enum.GetName(typeof(RequestFailureReason), args.FailureReason)));

            bool   tryReconnect = false;
            string message      = null;

            switch (args.FailureReason)
            {
            case RequestFailureReason.Auth:
                tryReconnect = false;
                message      = "authentication problem";
                NotificationUtility.PostNotification(NotificationType.AuthFailure);
                break;

            case RequestFailureReason.Timeout:
                tryReconnect = ReconnectIsAllowed();
                message      = "timeout";
                break;

            case RequestFailureReason.ServerDown:
                tryReconnect = ReconnectIsAllowed();
                message      = "server down";
                break;

            case RequestFailureReason.Network:
                tryReconnect = ReconnectIsAllowed();
                message      = "network issue";
                break;

            case RequestFailureReason.ServerRequestedReconnect:
                tryReconnect = ReconnectIsAllowed();
                message      = "server requested reconnect";
                break;

            case RequestFailureReason.Error:
                break;
            }

            if (tryReconnect)
            {
                SetState(ConnectionState.Disconnected, message);
                ReconnectProcess.Begin(args.ForegroundAction, args.OnResume, args.Request, args.FailureReason);
            }
        }
        public async Task Can_receive_many_messages()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            for (int i = 0; i < 1000; i++)
            {
                receiveResult.Enqueue(new WebSocketReceiveResult(1234, WebSocketMessageType.Text, true));
            }

            receiveResult.Enqueue(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));

            WebSocketMock     mock             = new WebSocketMock(receiveResult);
            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            await mock.ReceiveAsync(webSocketsClient);

            await webSocketsClient.Received(1000).ReceiveAsync(Arg.Is <Memory <byte> >(ba => ba.Length == 1234));
        }
        public async Task Throws_on_too_long_message()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            for (int i = 0; i < 1024; i++)
            {
                receiveResult.Enqueue(new WebSocketReceiveResult(1024, WebSocketMessageType.Text, false));
            }

            receiveResult.Enqueue(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true));
            receiveResult.Enqueue(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));
            WebSocketMock mock = new WebSocketMock(receiveResult);

            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            Assert.ThrowsAsync <InvalidOperationException>(async() => await mock.ReceiveAsync(webSocketsClient));
            await webSocketsClient.DidNotReceive().ReceiveAsync(Arg.Any <Memory <byte> >());
        }
        public void Can_publish_on_various_channel_types()
        {
            NdmConsumerChannelManager manager = new NdmConsumerChannelManager();

            IWebSocketsClient client = Substitute.For <IWebSocketsClient>();

            INdmConsumerChannel[] channels = new INdmConsumerChannel[]
            {
                new JsonRpcNdmConsumerChannel(LimboLogs.Instance),
                new GrpcNdmConsumerChannel(Substitute.For <IGrpcServer>()),
                new NdmWebSocketsConsumerChannel(client),
            };

            ((JsonRpcNdmConsumerChannel)channels[0]).Pull(Keccak.Zero).Should().BeNull();

            for (int i = 0; i < 3; i++)
            {
                manager.Add(channels[i]);
            }

            channels[0].Type.Should().Be(NdmConsumerChannelType.JsonRpc);
            channels[1].Type.Should().Be(NdmConsumerChannelType.Grpc);

            manager.PublishAsync(Keccak.Zero, "client1", "data1");
            manager.PublishAsync(Keccak.Zero, "client2", "data2");

            for (int i = 0; i < 3; i++)
            {
                manager.Remove(channels[i]);
            }

            manager.PublishAsync(Keccak.Zero, "client3", "data3");

            for (int i = 0; i < 3; i++)
            {
                client.Received().SendAsync(Arg.Is <WebSocketsMessage>(wm => wm.Client == "client1"));
                client.Received().SendAsync(Arg.Is <WebSocketsMessage>(wm => wm.Client == "client2"));
                client.DidNotReceive().SendAsync(Arg.Is <WebSocketsMessage>(wm => wm.Client == "client3"));
            }

            ((JsonRpcNdmConsumerChannel)channels[0]).Pull(Keccak.Zero).Should().NotBeNull();
            ((JsonRpcNdmConsumerChannel)channels[0]).Pull(Keccak.Zero).Should().NotBeNull();
            ((JsonRpcNdmConsumerChannel)channels[0]).Pull(Keccak.Zero).Should().BeNull();
        }
        public async Task Can_receive_whole_message_non_buffer_sizes()
        {
            Queue <WebSocketReceiveResult> receiveResult = new Queue <WebSocketReceiveResult>();

            for (int i = 0; i < 6; i++)
            {
                receiveResult.Enqueue(new WebSocketReceiveResult(2000, WebSocketMessageType.Text, false));
            }

            receiveResult.Enqueue(new WebSocketReceiveResult(1, WebSocketMessageType.Text, true));
            receiveResult.Enqueue(new WebSocketReceiveResult(0, WebSocketMessageType.Close, true));
            WebSocketMock mock = new WebSocketMock(receiveResult);

            IWebSocketsClient webSocketsClient = Substitute.For <IWebSocketsClient>();

            await mock.ReceiveAsync(webSocketsClient);

            await webSocketsClient.Received().ReceiveAsync(Arg.Is <Memory <byte> >(ba => ba.Length == 6 * 2000 + 1));
        }
Exemple #26
0
 public NdmWebSocketsConsumerChannel(IWebSocketsClient client)
 {
     _client = client;
 }
Exemple #27
0
        public static async Task ReceiveAsync(this WebSocket webSocket, IWebSocketsClient client)
        {
            int currentMessageLength = 0;

            byte[] buffer       = new byte[1024 * 4];
            byte[] combinedData = Array.Empty <byte>();

            WebSocketReceiveResult        result = null;
            Task <WebSocketReceiveResult> receiveBeforeTheLoop = webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);
            await receiveBeforeTheLoop.ContinueWith(t =>
            {
                if (t.IsFaulted)
                {
                    // TODO: how to log it from here
                }

                if (t.IsCompletedSuccessfully)
                {
                    result = t.Result;
                }
            });

            if (result == null)
            {
                // TODO: how to log it from here
                return;
            }

            while (result.MessageType != WebSocketMessageType.Close)
            {
                int newMessageLength = currentMessageLength + result.Count;
                if (newMessageLength > _maxPooledSize)
                {
                    throw new InvalidOperationException("Message too long");
                }

                byte[] newBytes = ArrayPool <byte> .Shared.Rent(newMessageLength);

                buffer.AsSpan(0, result.Count).CopyTo(newBytes.AsSpan(currentMessageLength, result.Count));
                if (!ReferenceEquals(combinedData, Array.Empty <byte>()))
                {
                    combinedData.AsSpan(0, currentMessageLength).CopyTo(newBytes.AsSpan(0, currentMessageLength));
                    ArrayPool <byte> .Shared.Return(combinedData);
                }

                combinedData         = newBytes;
                currentMessageLength = newMessageLength;

                if (result.EndOfMessage)
                {
                    Memory <byte> data = combinedData.AsMemory().Slice(0, currentMessageLength);
                    await client.ReceiveAsync(data);

                    currentMessageLength = 0;
                    ArrayPool <byte> .Shared.Return(combinedData);

                    combinedData = Array.Empty <byte>();
                }

                Task <WebSocketReceiveResult> receiveInTheLoop = webSocket.ReceiveAsync(new ArraySegment <byte>(buffer), CancellationToken.None);
                await receiveInTheLoop.ContinueWith(t =>
                {
                    if (t.IsFaulted)
                    {
                        result = null;
                        // TODO: how to log it from here
                    }

                    if (t.IsCompletedSuccessfully)
                    {
                        result = t.Result;
                    }
                });

                if (result == null)
                {
                    // TODO: how to log it from here
                    return;
                }
            }

            await webSocket.CloseAsync(result.CloseStatus ?? WebSocketCloseStatus.Empty, result.CloseStatusDescription, CancellationToken.None);
        }
 public NdmWebSocketsClient(IWebSocketsClient client, INdmDataPublisher dataPublisher)
 {
     _client        = client;
     _dataPublisher = dataPublisher;
     Client         = client.Client;
 }
 public void Cleanup(IWebSocketsClient client)
 {
 }
 public void AddClient(IWebSocketsClient client)
 {
 }