public async Task RunAsync() { #if DEBUG listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 502)); #else listener = new TcpListener(new IPEndPoint(GetIPAddress(Dns.GetHostName()), 502)); #endif listener.ExclusiveAddressUse = false; listener.Start(); logger?.LogInformation("SCADA client listener started."); while (true) { try { TcpClient tcpClient = await listener.AcceptTcpClientAsync(); tcpClient.LingerState = new LingerOption(true, 0); tcpClient.NoDelay = true; tcpClient.Client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true); CancellationTokenSource cts = new CancellationTokenSource(); logger?.LogDebug("SCADA client connection acquired."); IChannel inputChannel = ChannelFactory.Create(false, tcpClient, 1024, 1024 * 100, cts.Token); logger?.LogDebug("SCADA client created TCP input channel"); IChannel outputChannel = new VirtualRtuChannel(config, logger); logger?.LogDebug("SCADA client created VRTU output channel"); MbapMapper mapper = new MbapMapper(Guid.NewGuid().ToString()); PipelineBuilder builder = new PipelineBuilder(logger); Pipeline pipeline = builder.AddConfig(config) .AddInputChannel(inputChannel) .AddOutputChannel(outputChannel) .AddInputFilter(new InputMapFilter(mapper)) .AddOutputFilter(new OutputMapFilter(mapper)) .Build(); logger?.LogDebug("SCADA client pipeline built."); pipeline.OnPipelineError += Pipeline_OnPipelineError; pipelines.Add(pipeline.Id, pipeline); pipeline.Execute(); logger?.LogDebug("SCADA client pipeline executed."); } catch (Exception ex) { logger?.LogError(ex, "Fault creating pipeline."); } } }
private void BuildPipeline() { DisposePipeline(); input = new ModuleChannel(config, logger); if (output == null) { output = new ModuleTcpChannel(config, logger); } try { PipelineBuilder builder = new PipelineBuilder(logger); pipeline = builder.AddConfig(config) .AddInputChannel(input) .AddOutputChannel(output) .AddOutputFilter(new OutputMapFilter(mapper)) .Build(); //pipeline = builder.AddConfig(config) // .AddInputChannel(input) // .AddOutputChannel(output) // .AddInputFilter(new InputMapFilter(mapper)) // .AddOutputFilter(new OutputMapFilter(mapper)) // .Build(); logger?.LogInformation("Module client pipeline built."); pipeline.OnPipelineError += Pipeline_OnPipelineError; pipeline.Execute(); logger?.LogInformation("Module pipeline running."); } catch (Exception ex) { logger?.LogError(ex, "Fault building module pipeline."); throw ex; } }