コード例 #1
0
ファイル: Examples.cs プロジェクト: snaami/RxSocket
        public async Task T00_Example()
        {
            // Create a socket server on the EndPoint.
            var server = IPEndPoint.CreateRxSocketServer(SocketServerLogger);

            // Start accepting connections from clients.
            server.AcceptObservable.Subscribe(acceptClient =>
            {
                // After the server accepts a client connection...
                acceptClient.ReceiveObservable.ToStrings().Subscribe(onNext: message =>
                {
                    // Echo each message received back to the client.
                    acceptClient.Send(message.ToByteArray());
                });
            });

            // Create a socket client by first connecting to the server at the EndPoint.
            var client = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

            // Start receiving messages from the server.
            client.ReceiveObservable.ToStrings().Subscribe(onNext: message =>
            {
                // The message received from the server is "Hello!".
                Assert.Equal("Hello!", message);
            });

            // Send the message "Hello" to the server (which will be echoed back to the client).
            client.Send("Hello!".ToByteArray());

            await Task.Delay(100);

            await client.DisposeAsync();

            await server.DisposeAsync();
        }
コード例 #2
0
        public async Task T01_Handshake()
        {
            NewThreadScheduler.Default.Schedule(async() =>
            {
                var server = IPEndPoint.CreateRxSocketServer(SocketServerLogger);
                var accept = await server.AcceptObservable.FirstAsync();

                var message1 = await accept.ReadBytesAsync().ReadStringAsync();
                Assert.Equal("API", message1);

                var message2 = await accept.ReadBytesAsync().ReadStringsAsync();
                Assert.Equal("HelloFromClient", message2.Single());

                accept.Send(new[] { "HelloFromServer" }.ToByteArrayWithLengthPrefix());

                await server.DisposeAsync();
            });

            var client = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

            // Send only the first message without prefix.
            client.Send("API".ToByteArray());

            // Start sending and receiving messages with an int32 message length prefix (UseV100Plus).
            client.Send(new[] { "HelloFromClient" }.ToByteArrayWithLengthPrefix());

            var message3 = await client.ReadBytesAsync().ReadStringsAsync();

            Assert.Equal("HelloFromServer", message3.Single());

            await client.DisposeAsync();
        }
コード例 #3
0
ファイル: Examples.cs プロジェクト: snaami/RxSocket
        public async Task T01_SendAndReceiveStringMessage()
        {
            // Create a socket server on the endpoint.
            var server = IPEndPoint.CreateRxSocketServer(SocketServerLogger);

            // Start a task to allow the server to accept the next client connection.
            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();

            // Create a socket client by successfully connecting to the server at EndPoint.
            var client = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

            // Get the client socket accepted by the server.
            var accept = await acceptTask;

            Assert.True(accept.Connected && client.Connected);

            // start a task to receive the first string from the server.
            var dataTask = client.ReceiveObservable.ToStrings().FirstAsync().ToTask();

            // The server sends a string to the client.
            accept.Send("Welcome!".ToByteArray());
            Assert.Equal("Welcome!", await dataTask);

            await client.DisposeAsync();

            await server.DisposeAsync();
        }
コード例 #4
0
ファイル: Examples.cs プロジェクト: snaami/RxSocket
        public async Task T30_Both()
        {
            var server = IPEndPoint.CreateRxSocketServer(SocketServerLogger);

            server.AcceptObservable.Subscribe(accepted =>
            {
                accepted.Send("Welcome!".ToByteArray());
                accepted
                .ReceiveObservable
                .ToStrings()
                .Subscribe(s => accepted.Send(s.ToByteArray()));
            });


            var clients = new List <IRxSocketClient>();

            for (var i = 0; i < 3; i++)
            {
                var client = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

                client.Send("Hello".ToByteArray());
                clients.Add(client);
            }

            foreach (var client in clients)
            {
                Assert.Equal("Hello", await client.ReceiveObservable.ToStrings().Skip(1).Take(1).FirstAsync());
            }

            foreach (var client in clients)
            {
                await client.DisposeAsync();
            }
            await server.DisposeAsync();
        }
コード例 #5
0
        public async Task T04_DisconnectWhileAccept()
        {
            var server     = IPEndPoint.CreateRxSocketServer(SocketServerLogger);
            var acceptTask = server.AcceptObservable.LastAsync().ToTask();
            await server.DisposeAsync();

            await Assert.ThrowsAnyAsync <Exception>(async() => await acceptTask);
        }
コード例 #6
0
        public async Task T03_DisconnectBeforeAccept()
        {
            var server = IPEndPoint.CreateRxSocketServer(SocketServerLogger);
            await server.DisposeAsync();

            await Assert.ThrowsAsync <OperationCanceledException>(async() => await server.AcceptObservable.LastOrDefaultAsync());

            //await server.AcceptObservable.LastOrDefaultAsync();
        }
コード例 #7
0
        protected async override Task ExecuteAsync(CancellationToken cancellationToken)
        {
            logger.LogInformation("UniFeeder starting...");
            var end = new IPEndPoint(IPAddress.Parse(option.Value.Ip), option.Value.Port);

            this.server = end.CreateRxSocketServer();
            UniFeederServer(server);

            Loop(token: cancellationToken);
            await Task.CompletedTask;

            logger.LogInformation("UniFeeder started");
        }
コード例 #8
0
        public async Task T02_AcceptSuccess()
        {
            var server = IPEndPoint.CreateRxSocketServer(SocketServerLogger);

            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();

            var clientSocket = Utilities.CreateSocket();

            clientSocket.Connect(IPEndPoint);

            var acceptedSocket = await acceptTask;

            Assert.True(clientSocket.Connected && acceptedSocket.Connected);

            clientSocket.Disconnect(false);
            await server.DisposeAsync();
        }
コード例 #9
0
ファイル: PerformanceTests.cs プロジェクト: snaami/RxSocket
        public async Task T02_ReceiveStringsFromPrefixedBytes()
        {
            var server     = IPEndPoint.CreateRxSocketServer();
            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();

            var client = await IPEndPoint.ConnectRxSocketClientAsync();

            Assert.True(client.Connected);

            var countTask = client.ReceiveObservable.RemoveLengthPrefix().ToStringArray().Count().ToTask();

            var accept = await acceptTask;

            Assert.True(accept.Connected);

            var message = new [] { "Welcome!" }.ToByteArrayWithLengthPrefix();

            var watch = new Stopwatch();

            watch.Start();

            for (var i = 0; i < messages; i++)
            {
                accept.Send(message);
            }

            // end count task
            await accept.DisposeAsync();

            int count = await countTask;

            watch.Stop();

            Assert.Equal(messages, count);

            var frequency = Stopwatch.Frequency * messages / watch.ElapsedTicks;

            Write($"{frequency:N0} messages / second");

            await client.DisposeAsync();

            await server.DisposeAsync();
        }
コード例 #10
0
ファイル: Examples.cs プロジェクト: snaami/RxSocket
        public async Task T40_ClientDisconnect()
        {
            var semaphore = new SemaphoreSlim(0, 1);

            var             server       = IPEndPoint.CreateRxSocketServer(SocketServerLogger);
            IRxSocketClient?acceptClient = null;

            server.AcceptObservable.Subscribe(ac =>
            {
                acceptClient = ac;
                semaphore.Release();
                acceptClient.ReceiveObservable.ToStrings().Subscribe(onNext: message =>
                {
                    acceptClient.Send(message.ToByteArray());
                });
            });

            var client = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

            client.ReceiveObservable.ToStrings().Subscribe(onNext: message =>
            {
                Write(message);
            });

            client.Send("Hello!".ToByteArray());

            await semaphore.WaitAsync();

            if (acceptClient == null)
            {
                throw new NullReferenceException(nameof(acceptClient));
            }

            await client.DisposeAsync();

            // should throw!
            acceptClient.Send("Anybody there?".ToByteArray());
            //client.Send("Anybody there?".ToByteArray());

            await server.DisposeAsync();
        }
コード例 #11
0
ファイル: Examples.cs プロジェクト: snaami/RxSocket
        public async Task T10_ReceiveObservable()
        {
            var server     = IPEndPoint.CreateRxSocketServer(SocketServerLogger);
            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();
            var client     = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

            var accept = await acceptTask;

            client.ReceiveObservable.ToStrings().Subscribe(str =>
            {
                Write(str);
            });

            accept.Send("Welcome!".ToByteArray());
            accept.Send("Welcome Again!".ToByteArray());

            await Task.Delay(100);

            await client.DisposeAsync();

            await server.DisposeAsync();
        }
コード例 #12
0
ファイル: PerformanceTests.cs プロジェクト: snaami/RxSocket
        public async Task T01_ReceiveStrings()
        {
            var server     = IPEndPoint.CreateRxSocketServer();
            var acceptTask = server.AcceptObservable.FirstAsync().ToTask();
            var client     = await IPEndPoint.ConnectRxSocketClientAsync();

            var countTask = client.ReceiveObservable.ToStrings().Count().ToTask();
            var accept    = await acceptTask;

            var watch = new Stopwatch();

            watch.Start();

            // send messages from server to client
            var message = "Welcome!".ToByteArray();

            for (var i = 0; i < messages; i++)
            {
                accept.Send(message);
            }

            // end count task
            await accept.DisposeAsync();

            var count = await countTask;

            watch.Stop();

            Assert.Equal(messages, count);

            var frequency = Stopwatch.Frequency * messages / watch.ElapsedTicks;

            Write($"{frequency:N0} messages / second");

            await client.DisposeAsync();

            await server.DisposeAsync();
        }
コード例 #13
0
ファイル: Examples.cs プロジェクト: snaami/RxSocket
        public async Task T20_AcceptObservable()
        {
            var server = IPEndPoint.CreateRxSocketServer(SocketServerLogger);

            server.AcceptObservable.Subscribe(accepted => accepted.Send("Welcome!".ToByteArray()));

            var client1 = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

            var client2 = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

            var client3 = await IPEndPoint.ConnectRxSocketClientAsync(SocketClientLogger);

            Assert.Equal("Welcome!", await client1.ReceiveObservable.ToStrings().Take(1).FirstAsync());
            Assert.Equal("Welcome!", await client2.ReceiveObservable.ToStrings().Take(1).FirstAsync());
            Assert.Equal("Welcome!", await client3.ReceiveObservable.ToStrings().Take(1).FirstAsync());

            await client1.DisposeAsync();

            await client2.DisposeAsync();

            await client3.DisposeAsync();

            await server.DisposeAsync();
        }
コード例 #14
0
        public void T01_InvalidEndPoint()
        {
            var endPoint = new IPEndPoint(IPAddress.Parse("111.111.111.111"), 1111);

            Assert.Throws <SocketException>(() => endPoint.CreateRxSocketServer(SocketServerLogger));
        }
コード例 #15
0
ファイル: Server.cs プロジェクト: Timezone-design/InterReact
        internal async Task Run()
        {
            //var server = EndPoint.CreateRxSocketServer(Logger);
            var server = EndPoint.CreateRxSocketServer();

            Logger.LogInformation("Waiting for client.");
            var accept = await server.AcceptObservable.FirstAsync();

            Logger.LogInformation("Client connection accepted.");

            var firstString = await accept.ReceiveObservable.ToStrings().FirstAsync();

            if (firstString != "API")
            {
                throw new InvalidDataException("'API' not received.");
            }
            Logger.LogInformation("Received 'API'.");

            // Start receiving with length prefix.
            // hangs on next line
            var messages1 = await accept.ReceiveObservable.ToByteArrayOfLengthPrefix().ToStringArray().FirstAsync();

            var versions = messages1.Single();

            if (!versions.StartsWith("v"))
            {
                throw new InvalidDataException("Versions not received.");
            }
            Logger.LogInformation($"Received supported server versions: '{versions}'.");

            var messages2 = await accept.ReceiveObservable.ToByteArrayOfLengthPrefix().ToStringArray().FirstAsync();

            if (messages2[0] != "71") // receive StartApi message
            {
                throw new InvalidDataException("StartApi message not received.");
            }
            Logger.LogInformation("Received StartApi message.");

            new RequestMessage(accept, Limiter)
            .Write(149)     // server version
            .Write(DateTime.Now.ToString("yyyyMMdd HH:mm:ss XXX"))
            .Send();

            new RequestMessage(accept, Limiter)
            .Write("15")
            .Write("1")
            .Write("123,456,789")
            .Send();

            new RequestMessage(accept, Limiter)
            .Write("9")
            .Write("1")
            .Write("10")
            .Send();

            Logger.LogInformation("Client login complete.");

            ////////////////////////////////////////////////////

            var obs = accept.ReceiveObservable.ToByteArrayOfLengthPrefix().ToStringArray().Publish().RefCount();

            // receive test start signal
            await obs.FirstAsync();

            var watch = new Stopwatch();

            watch.Start();
            var count = await obs.TakeWhile(m => m[0] == "2").Count();

            watch.Stop();
            var frequency = Stopwatch.Frequency * (count + 1) / watch.ElapsedTicks;

            Logger.LogInformation($"Received {frequency:N0} messages/second.");

            var ms = new MemoryStream();

            for (var i = 0; i < 500_000; i++)
            {
                new RequestMessage(ms.Write, Limiter)
                .Write("2", "3", 1, TickType.LastSize, 300)
                .Send();
            }

            // message to indicate test stop
            new RequestMessage(ms.Write, Limiter)
            .Write("1", "3", 1, TickType.LastPrice, 100, 200, true)
            .Send();

            Logger.LogInformation("Sending messages...");
            accept.Send(ms.ToArray(), 0, (int)ms.Position);

            // wait for OnCompleted()
            await obs.LastOrDefaultAsync();

            Logger.LogInformation("Disconnecting.");
            server.Dispose();
            Logger.LogInformation("Disconnected.");
        }