コード例 #1
0
        public async Task TestConsoleProtocol()
        {
            var hostConfigurator = new RegularHostConfigurator();

            using (var server = CreateSocketServerBuilder <TextPackageInfo, LinePipelineFilter>(hostConfigurator)
                                .UsePackageHandler(async(IAppSession s, TextPackageInfo p) =>
            {
                await s.SendAsync(Utf8Encoding.GetBytes("Hello World\r\n"));
            }).BuildAsServer() as IServer)
            {
                Assert.True(await server.StartAsync());
                Assert.Equal(0, server.SessionCount);

                var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await client.ConnectAsync(hostConfigurator.GetServerEndPoint());

                using (var stream = await hostConfigurator.GetClientStream(client))
                    using (var streamReader = new StreamReader(stream, Utf8Encoding, true))
                        using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4))
                        {
                            await streamWriter.WriteAsync("Hello World\r\n");

                            await streamWriter.FlushAsync();

                            var line = await streamReader.ReadLineAsync();

                            Assert.Equal("Hello World", line);
                        }

                await server.StopAsync();
            }
        }
コード例 #2
0
ファイル: MainTest.cs プロジェクト: ypeng-ming/SuperSocket
        public void TestCustomConfigOptions()
        {
            var hostConfigurator = new RegularHostConfigurator();
            var propName         = "testPropName";
            var propValue        = "testPropValue";

            using (var server = CreateSocketServerBuilder <TextPackageInfo, LinePipelineFilter>(hostConfigurator)
                                .ConfigureAppConfiguration((HostBuilder, configBuilder) =>
            {
                configBuilder.AddInMemoryCollection(new Dictionary <string, string>
                {
                    { $"serverOptions:values:{propName}", propValue }
                });
            }).BuildAsServer())
            {
                Assert.Equal(propValue, server.Options.Values[propName]);
            }
        }
コード例 #3
0
        public async Task TestInProcSessionContainer()
        {
            var hostConfigurator = new RegularHostConfigurator();

            using (var server = CreateSocketServerBuilder <StringPackageInfo, MyPipelineFilter>(hostConfigurator)
                                .UseCommand <string, StringPackageInfo>(commandOptions =>
            {
                commandOptions.AddCommand <SESS>();
            })
                                .UseInProcSessionContainer()
                                .ConfigureServices((ctx, services) =>
                                                   services.AddSingleton <SessionContainerDependentService>()
                                                   )
                                .BuildAsServer())
            {
                Assert.Equal("TestServer", server.Name);

                var sessionContainer = server.GetSessionContainer();
                Assert.NotNull(sessionContainer);

                var sessionContainerDependentService = server.ServiceProvider.GetService <SessionContainerDependentService>();
                Assert.NotNull(sessionContainerDependentService);
                Assert.NotNull(sessionContainerDependentService.SessionContainer);
                var asyncSessionContainer = sessionContainerDependentService.AsyncSessionContainer as SyncToAsyncSessionContainerWrapper;
                Assert.NotNull(asyncSessionContainer);
                Assert.Same(sessionContainerDependentService.SessionContainer, asyncSessionContainer.SessionContainer);
                Assert.Same(sessionContainer, sessionContainerDependentService.SessionContainer);

                Assert.True(await server.StartAsync());
                OutputHelper.WriteLine("Server started.");

                var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await client.ConnectAsync(hostConfigurator.GetServerEndPoint());

                OutputHelper.WriteLine("Connected.");

                Thread.Sleep(1000);

                Assert.Equal(1, sessionContainer.GetSessionCount());

                var sessionID = string.Empty;

                var closed = false;

                using (var stream = await hostConfigurator.GetClientStream(client))
                    using (var streamReader = new StreamReader(stream, Utf8Encoding, true))
                        using (var streamWriter = new StreamWriter(stream, Utf8Encoding, 1024 * 1024 * 4))
                        {
                            await streamWriter.WriteAsync("SESS\r\n");

                            await streamWriter.FlushAsync();

                            sessionID = await streamReader.ReadLineAsync();

                            Assert.False(string.IsNullOrEmpty(sessionID));

                            var session = sessionContainer.GetSessionByID(sessionID);

                            Assert.NotNull(session);
                            Assert.Equal(sessionID, session.SessionID);

                            session.Closed += (s, e) =>
                            {
                                closed = true;
                                return(new ValueTask());
                            };

                            await session.Channel.CloseAsync(CloseReason.LocalClosing);
                        }

                await Task.Delay(1000);

                Assert.Equal(0, sessionContainer.GetSessionCount());
                Assert.Null(sessionContainer.GetSessionByID(sessionID));
                Assert.True(closed);

                await server.StopAsync();
            }
        }
コード例 #4
0
        public async Task TestSessionEvents()
        {
            var         hostConfigurator = new RegularHostConfigurator();
            var         connected        = false;
            IAppSession session          = null;

            using (var server = CreateSocketServerBuilder <TextPackageInfo, LinePipelineFilter>(hostConfigurator)
                                .UseSessionHandler((s) =>
            {
                connected = true;
                session = s;
                return(new ValueTask());
            })
                                .BuildAsServer())
            {
                Assert.Equal("TestServer", server.Name);

                Assert.True(await server.StartAsync());
                OutputHelper.WriteLine("Started.");

                var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await client.ConnectAsync(hostConfigurator.GetServerEndPoint());

                OutputHelper.WriteLine("Connected.");

                await Task.Delay(1000);

                session.Closed += (s, e) =>
                {
                    connected           = false;
                    session.DataContext = "Hello, my love!";
                    return(new ValueTask());
                };

                var itemKey   = "GirlFriend";
                var itemValue = "Who?";

                session[itemKey] = itemValue;

                Assert.Equal(SessionState.Connected, session.State);
                Assert.True(connected);

                client.Shutdown(SocketShutdown.Both);
                client.Close();

                await Task.Delay(1000);

                Assert.Equal(SessionState.Closed, session.State);
                Assert.False(connected);
                Assert.Equal(itemValue, session[itemKey]);

                Assert.Equal(1, GetEventInvocationCount(session, nameof(session.Closed)));

                session.Reset();

                Assert.Null(session.Channel);
                Assert.Null(session.DataContext);
                Assert.Equal(SessionState.None, session.State);
                Assert.Null(session[itemKey]);
                Assert.Equal(0, GetEventInvocationCount(session, nameof(session.Closed)));

                await server.StopAsync();
            }
        }
コード例 #5
0
        public async Task TestCloseReason()
        {
            var         hostConfigurator = new RegularHostConfigurator();
            IAppSession session          = null;

            using (var server = CreateSocketServerBuilder <TextPackageInfo, LinePipelineFilter>(hostConfigurator)
                                .UseSessionHandler((s) =>
            {
                session = s;
                return(new ValueTask());
            })
                                .BuildAsServer())
            {
                Assert.Equal("TestServer", server.Name);

                Assert.True(await server.StartAsync());
                OutputHelper.WriteLine("Started.");

                CloseReason closeReason = CloseReason.Unknown;

                var client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await client.ConnectAsync(hostConfigurator.GetServerEndPoint());

                OutputHelper.WriteLine("Connected.");

                await Task.Delay(1000);

                session.Closed += (s, e) =>
                {
                    closeReason = e.Reason;
                    return(new ValueTask());
                };

                client.Shutdown(SocketShutdown.Both);
                client.Close();

                await Task.Delay(1000);

                Assert.Equal(SessionState.Closed, session.State);
                Assert.Equal(CloseReason.RemoteClosing, closeReason);

                closeReason = CloseReason.Unknown;

                client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                await client.ConnectAsync(hostConfigurator.GetServerEndPoint());

                OutputHelper.WriteLine("Connected.");

                await Task.Delay(1000);

                session.Closed += (s, e) =>
                {
                    closeReason = e.Reason;
                    return(new ValueTask());
                };

                await session.CloseAsync(CloseReason.LocalClosing);

                await Task.Delay(1000);

                Assert.Equal(SessionState.Closed, session.State);
                Assert.Equal(CloseReason.LocalClosing, closeReason);

                await server.StopAsync();
            }
        }