Ejemplo n.º 1
0
    private async Task <int> SharedStringsTest_Common()
    {
        int length = 0;

        await this.EchoTest(async client =>
        {
            var requestStream = SChannel.CreateUnbounded <StringMessage>();

            byte[] random = new byte[1024];
            new Random().NextBytes(random);
            string randomB64 = Convert.ToBase64String(random);

            for (int i = 0; i < 100; ++i)
            {
                await requestStream.Writer.WriteAsync(new StringMessage {
                    Value = randomB64
                });
            }

            requestStream.Writer.Complete();

            MultiStringMessage response = await((IEchoService)client).EchoClientStreaming(requestStream, default);

            // Test that the string is only written once. This confirms that our update to the serializer worked as intended.
            Assert.IsAssignableFrom <IFlatBufferDeserializedObject>(response);

            IFlatBufferDeserializedObject obj = (IFlatBufferDeserializedObject)response;
            length = obj.InputBuffer.Length;
        });

        return(length);
    }
Ejemplo n.º 2
0
    public Task EchoDuplexStreaming_Interface_Canceled_BeforeCompletion()
    {
        return(this.EchoTest_Interface(async client =>
        {
            CancellationTokenSource cts = new();

            var sourceChannel = SChannel.CreateUnbounded <StringMessage>();
            var destChannel = SChannel.CreateUnbounded <StringMessage>();

            var duplexCall = client.EchoDuplexStreaming(sourceChannel.Reader, destChannel.Writer, cts.Token);

            for (int i = 0; i < 100; ++i)
            {
                string str = Guid.NewGuid().ToString();
                await sourceChannel.Writer.WriteAsync(new StringMessage {
                    Value = str
                });

                var item = await destChannel.Reader.ReadAsync();
                Assert.Equal(str, item.Value);
            }

            cts.Cancel();
            await Task.Delay(50);
            sourceChannel.Writer.Complete();
            await this.AssertCanceled(() => duplexCall);

            Assert.True(destChannel.Reader.Completion.IsCompleted);
            Assert.False(destChannel.Reader.Completion.IsCompletedSuccessfully);
        }));
    }
Ejemplo n.º 3
0
    public Task EchoDuplexStreaming_Interface()
    {
        return(this.EchoTest_Interface(async client =>
        {
            var sourceChannel = SChannel.CreateUnbounded <StringMessage>();
            var destChannel = SChannel.CreateUnbounded <StringMessage>();
            var duplexCall = client.EchoDuplexStreaming(sourceChannel.Reader, destChannel.Writer, CancellationToken.None);

            for (int i = 0; i < 100; ++i)
            {
                string str = Guid.NewGuid().ToString();
                await sourceChannel.Writer.WriteAsync(new StringMessage {
                    Value = str
                });

                var item = await destChannel.Reader.ReadAsync();
                Assert.Equal(str, item.Value);
            }

            sourceChannel.Writer.Complete();
            await duplexCall;

            Assert.True(destChannel.Reader.Completion.IsCompletedSuccessfully);
        }));
    }
Ejemplo n.º 4
0
    public Task EchoServerStreaming_Interface()
    {
        return(this.EchoTest_Interface(async client =>
        {
            MultiStringMessage message = new MultiStringMessage
            {
                Value = Enumerable.Range(0, 100).Select(x => Guid.NewGuid().ToString()).ToList()
            };

            var channel = SChannel.CreateUnbounded <StringMessage>();

            var task = client.EchoServerStreaming(message, channel, CancellationToken.None);

            int i = 0;
            while (await channel.Reader.WaitToReadAsync())
            {
                Assert.True(channel.Reader.TryRead(out var item));
                Assert.Equal(message.Value[i++], item.Value);
            }

            Assert.Equal(100, i);

            await task;

            Assert.True(channel.Reader.Completion.IsCompleted);
            Assert.True(channel.Reader.Completion.IsCompletedSuccessfully);
        }));
    }
Ejemplo n.º 5
0
    public Task EchoClientStreaming_Interface()
    {
        return(this.EchoTest_Interface(async client =>
        {
            var requestChannel = SChannel.CreateUnbounded <StringMessage>();

            List <string> messages = new();
            for (int i = 0; i < 100; ++i)
            {
                string msg = Guid.NewGuid().ToString();
                await requestChannel.Writer.WriteAsync(new StringMessage {
                    Value = msg
                });
                messages.Add(msg);
            }

            requestChannel.Writer.Complete();

            MultiStringMessage response = await client.EchoClientStreaming(requestChannel, CancellationToken.None);

            Assert.Equal(100, response.Value.Count);
            for (int i = 0; i < 100; ++i)
            {
                Assert.Equal(messages[i], response.Value[i]);
            }
        }));
    }
Ejemplo n.º 6
0
    public Task EchoDuplexStreaming_Interface_Canceled_OnWrite()
    {
        return(this.EchoTest_Interface(async client =>
        {
            CancellationTokenSource cts = new();

            var sourceChannel = SChannel.CreateUnbounded <StringMessage>();
            var destChannel = SChannel.CreateUnbounded <StringMessage>();

            var duplexCall = client.EchoDuplexStreaming(sourceChannel.Reader, destChannel.Writer, cts.Token);

            for (int i = 0; i < 100; ++i)
            {
                string str = Guid.NewGuid().ToString();
                await sourceChannel.Writer.WriteAsync(new StringMessage {
                    Value = str
                });

                if (i == 50)
                {
                    cts.Cancel();
                    await Task.Delay(50);

                    try
                    {
                        await destChannel.Reader.ReadAsync();
                        Assert.False(true, "Exception not thrown");
                    }
                    catch (System.Threading.Channels.ChannelClosedException)
                    {
                    }
                    catch (TaskCanceledException)
                    {
                    }

                    break;
                }
                else
                {
                    var item = await destChannel.Reader.ReadAsync();
                    Assert.Equal(str, item.Value);
                }
            }

            await this.AssertCanceled(() => duplexCall);

            Assert.True(destChannel.Reader.Completion.IsCompleted);
            Assert.False(destChannel.Reader.Completion.IsCompletedSuccessfully);
        }));
    }
Ejemplo n.º 7
0
    public Task EchoClientStreaming_Interface_Canceled()
    {
        return(this.EchoTest_Interface(async client =>
        {
            CancellationTokenSource cts = new();
            var channel = SChannel.CreateUnbounded <StringMessage>();
            await channel.Writer.WriteAsync(new StringMessage {
                Value = "foo"
            });
            await channel.Writer.WriteAsync(new StringMessage {
                Value = "bar"
            });

            var responseTask = client.EchoClientStreaming(channel, cts.Token);

            cts.Cancel();

            await this.AssertCanceled(() => responseTask);
        }));
    }
        public SuperSocketSocketClient(ILogger <SuperSocketSocketClient> logger, IFluentSocketApplication app, ISocketSessionBuilder socketSessionBuilder, ClientSetting setting)
        {
            _logger = logger;
            _app    = app;
            _socketSessionBuilder = socketSessionBuilder;

            _setting = setting;
            if (!_setting.ExtraSettings.Any())
            {
                _setting.ExtraSettings.Add(new SuperSocketClientSetting());
            }
            _extraSetting = (SuperSocketClientSetting)_setting.ExtraSettings.FirstOrDefault();

            _cts = new CancellationTokenSource();

            _semaphoreSlim          = new SemaphoreSlim(1);
            _manualResetEventSlim   = new ManualResetEventSlim(true);
            _reqPushChannel         = SystemChannel.CreateBounded <PushReqPacket>(_setting.PushReqCapacity);
            _pushMessageHandlerDict = new ConcurrentDictionary <short, IPushMessageHandler>();
            _responseFutureDict     = new ConcurrentDictionary <int, ResponseFuture>();
        }
Ejemplo n.º 9
0
    public Task EchoServerStreaming_Interface_Canceled()
    {
        return(this.EchoTest_Interface(async client =>
        {
            MultiStringMessage message = new MultiStringMessage
            {
                Value = Enumerable.Range(0, 100).Select(x => Guid.NewGuid().ToString()).ToList()
            };

            var channel = SChannel.CreateUnbounded <StringMessage>();
            CancellationTokenSource cts = new();

            var task = client.EchoServerStreaming(message, channel, cts.Token);

            int i = 0;
            while (await channel.Reader.WaitToReadAsync(cts.Token))
            {
                Assert.True(channel.Reader.TryRead(out var item));
                Assert.Equal(message.Value[i++], item.Value);

                if (i == 50)
                {
                    cts.Cancel();

                    await this.AssertCanceled(
                        async() => await channel.Reader.WaitToReadAsync(cts.Token));

                    break;
                }
            }

            await this.AssertCanceled(() => task);

            Assert.True(channel.Reader.Completion.IsCompleted);
            Assert.False(channel.Reader.Completion.IsCompletedSuccessfully);
        }));
    }