Пример #1
0
        public async Task Test_Bad_Service(CharGenClient_Rfc_864.ProtocolType protocol)
        {
            var host          = new HostName("localhost");
            var serverOptions = new CharGenServer_Rfc_864.ServerOptions()
            {
                LoggingLevel = CharGenServer_Rfc_864.ServerOptions.Verbosity.None
            };
            var clientOptions = new CharGenClient_Rfc_864.ClientOptions()
            {
                LoggingLevel = CharGenClient_Rfc_864.ClientOptions.Verbosity.None
            };

            using (var server = new CharGenServer_Rfc_864(serverOptions))
            {
                bool serverOk = await server.StartAsync();

                if (!serverOk)
                {
                    Infrastructure.Error($"unable to start server");
                    return;
                }

                var client = new CharGenClient_Rfc_864(clientOptions);
                var result = await client.WriteAsync(host, "79", protocol, "WontCauseAnyTraffic");

                Infrastructure.IfTrueError(result.Succeeded != CharGenClient_Rfc_864.CharGenResult.State.Failed, "result.Succeeded ");
                Infrastructure.IfTrueError(!String.IsNullOrEmpty(result.Value), "result.Value is not null");
                // ConnectionRefused is for TCP
                // ConnectionResetByPeer is for UDP
                Infrastructure.IfTrueError(result.Error != SocketErrorStatus.ConnectionRefused && result.Error != SocketErrorStatus.ConnectionResetByPeer, $"result.Error is wrong ({result.Error})");
            }
        }
Пример #2
0
        /// <summary>
        /// Demonstrates the simplest calling pattern.
        /// </summary>
        /// <param name="patternType">Normally only set when testing that the code works (setting the pattern to the 'Fixed' pattern)</param>
        /// <returns></returns>
        public static async Task <CharGenClient_Rfc_864.CharGenResult> RunTcpAsync(CharGenServer_Rfc_864.ServerOptions.PatternType patternType = CharGenServer_Rfc_864.ServerOptions.PatternType.Classic72)
        {
            // Hint: the using here means that the server is automatically closed
            // after the code is done. If you don't do that, the server stays open.
            using (var server = new CharGenServer_Rfc_864())
            {
                server.Options.OutputPattern = patternType;
                await server.StartAsync();

                var client = new CharGenClient_Rfc_864();
                // You only get the CharGen'd result from TCP with the final CloseAsync()
                // But for UDP  you get the returned packet.
                var partialResult = await client.WriteAsync(
                    new Windows.Networking.HostName("localhost"),
                    server.Options.Service,
                    CharGenClient_Rfc_864.ProtocolType.Tcp);

                // TODO: remove magic value?
                // Wait for the CharGen to have worked. The server is right here, so the
                // CharGen should be almost instantaneous.
                await Task.Delay(100);

                var completeResult = await client.CloseAsync(); // Close is essential for TCP.

                return(completeResult);
            }
        }
Пример #3
0
        public async Task Test_CharGen_Good_Path_Udp()
        {
            using (var server = new CharGenServer_Rfc_864())
            {
                server.Options.OutputPattern = CharGenServer_Rfc_864.ServerOptions.PatternType.Fixed;
                var client = new CharGenClient_Rfc_864();
                await server.StartAsync();

                var result1 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, CharGenClient_Rfc_864.ProtocolType.Udp, "ABC");

                var expected = CharGenServer_Rfc_864.GetSampleReturn();
                Infrastructure.IfTrueError(result1.Succeeded != CharGenClient_Rfc_864.CharGenResult.State.Succeeded, "Close TCP status should be succeeded");
                Infrastructure.IfTrueError(result1.Value != expected, $"Should have gotten back {expected}, not {result1.Value}");

                var result2 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, CharGenClient_Rfc_864.ProtocolType.Udp, "123");

                Infrastructure.IfTrueError(result2.Succeeded != CharGenClient_Rfc_864.CharGenResult.State.Succeeded, "Close TCP status should be succeeded");
                Infrastructure.IfTrueError(result2.Value != expected, $"Should have gotten back {expected}, not {result2.Value}");
            }
        }
Пример #4
0
        public async Task Test_Bad_Host(CharGenClient_Rfc_864.ProtocolType protocol)
        {
            var host          = new HostName("invalid.host.doesnt.exist.example.com");
            var clientOptions = new CharGenClient_Rfc_864.ClientOptions()
            {
                LoggingLevel = CharGenClient_Rfc_864.ClientOptions.Verbosity.None
            };

            var client = new CharGenClient_Rfc_864(clientOptions);
            var result = await client.WriteAsync(host, "10013", protocol, "ABCDEF");

            Infrastructure.IfTrueError(result.Succeeded != CharGenClient_Rfc_864.CharGenResult.State.Failed, "result.Succeeded ");
            Infrastructure.IfTrueError(!String.IsNullOrEmpty(result.Value), "result.Value is not null");
            Infrastructure.IfTrueError(result.Error != SocketErrorStatus.HostNotFound, $"result.Error is wrong ({result.Error})");
        }
Пример #5
0
        /// <summary>
        /// Demonstrates the simplest calling pattern
        /// </summary>
        /// <param name="patternType">Normally only set when testing that the code works (setting the pattern to the 'Fixed' pattern)</param>
        /// <returns></returns>
        public static async Task <CharGenClient_Rfc_864.CharGenResult> RunUdpAsync(CharGenServer_Rfc_864.ServerOptions.PatternType patternType = CharGenServer_Rfc_864.ServerOptions.PatternType.Classic72)
        {
            // Hint: the using here means that the server is automatically closed
            // after the code is done. If you don't do that, the server stays open.
            using (var server = new CharGenServer_Rfc_864())
            {
                server.Options.OutputPattern = patternType;
                await server.StartAsync();

                var client = new CharGenClient_Rfc_864();
                var result = await client.WriteAsync(
                    new Windows.Networking.HostName("localhost"),
                    server.Options.Service,
                    CharGenClient_Rfc_864.ProtocolType.Udp);

                await client.CloseAsync(); // Close is really needed for TCP.

                return(result);
            }
        }
Пример #6
0
        public async Task Test_CharGen_Good_Path_Tcp()
        {
            using (var server = new CharGenServer_Rfc_864())
            {
                server.Options.OutputPattern = CharGenServer_Rfc_864.ServerOptions.PatternType.Fixed;
                var client = new CharGenClient_Rfc_864();
                await server.StartAsync();

                var result1 = await client.WriteAsync(new HostName("localhost"), server.Options.Service, CharGenClient_Rfc_864.ProtocolType.Tcp);

                Infrastructure.IfTrueError(result1.Succeeded != CharGenClient_Rfc_864.CharGenResult.State.InProgress, "TCP status should be in progress");

                await Task.Delay(100); //TODO: artificial delay to get the results.

                var expected = CharGenServer_Rfc_864.GetSampleReturn();
                var result3  = await client.CloseAsync();

                Infrastructure.IfTrueError(result3.Succeeded != CharGenClient_Rfc_864.CharGenResult.State.Succeeded, "Close TCP status should be succeeded");
                Infrastructure.IfTrueError(result3.Value != expected, $"Should have gotten back {expected}, not {result3.Value}");
            }
        }
        private async void OnSend(object sender, RoutedEventArgs e)
        {
            try
            {
                var host    = new HostName(uiAddress.Text);
                var service = uiService.Text;
                var data    = uiData.Text;
                var ptype   = uiProtocolType.IsOn ? CharGenClient_Rfc_864.ProtocolType.Udp : CharGenClient_Rfc_864.ProtocolType.Tcp; // double-checked; off is TCP.

                if (client == null)
                {
                    client           = new CharGenClient_Rfc_864();
                    client.LogEvent += Client_LogEvent;
                }
                await client.WriteAsync(host, service, ptype, data);
            }
            catch (Exception ex)
            {
                Client_LogEvent(this, $"ERROR: Client: Write exception {ex.Message} for host {uiAddress.Text}");
            }
        }