private static async Task RunClientAsync() { var pipeClient = new PipeClient <IAdder>("mypipe"); pipeClient.SetLogger(message => Console.WriteLine(message)); try { await pipeClient.ConnectAsync().ConfigureAwait(false); WrappedInt result = await pipeClient.InvokeAsync(adder => adder.AddWrappedNumbers(new WrappedInt { Num = 1 }, new WrappedInt { Num = 3 })).ConfigureAwait(false); Console.WriteLine("Server add result: " + result.Num); await pipeClient.WaitForRemotePipeCloseAsync(); Console.WriteLine("Server closed pipe."); } catch (Exception exception) { Console.WriteLine("Exception in pipe processing: " + exception); } }
public void Test_SendReceive_MessagesBlock() { const int messageCount = 10; var sentValues = Enumerable.Range(0, messageCount).Select(x => $"value #{x}").ToList(); using var barrier = new Barrier(2); var serverTask = Task.Run(async() => { using var server = new PipeServer(PipeName); await server.WaitForConnectionAsync(); sentValues.ForEach(x => server.SendMessage(new TestMessage { TestProperty = x })); barrier.SignalAndWait(); }); List <string> receivedValues = new List <string>(); var clientTask = Task.Run(async() => { using var client = new PipeClient(PipeName); await client.ConnectAsync(); barrier.SignalAndWait(); for (int i = 0; i < messageCount; i++) { receivedValues.Add(((TestMessage)client.ReceiveMessage()).TestProperty); } }); var tasksCompleted = Task.WaitAll(new[] { serverTask, clientTask }, TimeSpan.FromSeconds(1)); Assert.That(tasksCompleted, Is.False); Assert.That(receivedValues, Is.EqualTo(new string[0])); }
private static async Task <AutomationResult> TryActionAsync(Expression <Action <IVidCoderAutomation> > action) { try { string betaString = string.Empty; if (CommonUtilities.Beta) { betaString = "Beta"; } using (var client = new PipeClient <IVidCoderAutomation>("VidCoderAutomation" + betaString)) { client.SetLogger(Console.WriteLine); await client.ConnectAsync().ConfigureAwait(false); await client.InvokeAsync(action).ConfigureAwait(false); } return(AutomationResult.Success); } catch (PipeInvokeFailedException exception) { WriteError(exception.ToString()); return(AutomationResult.FailedInVidCoder); } catch (Exception) { return(AutomationResult.ConnectionFailed); } }
public void Test_SendReceive_MessageContent() { var sendMessage = new TestMessage { TestProperty = "testValue" }; var serverTask = Task.Run(async() => { using var server = new PipeServer(PipeName); await server.WaitForConnectionAsync(); server.SendMessage(sendMessage); }); IMessage receivedMessage = null; var clientTask = Task.Run(async() => { using var client = new PipeClient(PipeName); await client.ConnectAsync(); receivedMessage = client.ReceiveMessage(); }); var tasksCompleted = Task.WaitAll(new[] { serverTask, clientTask }, TimeSpan.FromSeconds(1)); Assert.That(tasksCompleted, Is.True); Assert.That(receivedMessage.Id, Is.EqualTo(sendMessage.Id)); Assert.That(receivedMessage, Is.InstanceOf <TestMessage>()); Assert.That(((TestMessage)receivedMessage).TestProperty, Is.EqualTo(sendMessage.TestProperty)); }
public async Task SetUp() { Trace.WriteLine("Setting up test..."); _barrier.Reset(); _exceptions.Clear(); _server = new PipeServer <TestCollection>(PipeName); _client = new PipeClient <TestCollection>(PipeName); _expectedData = null; _expectedHash = 0; _actualData = null; _actualHash = 0; _server.MessageReceived += ServerOnMessageReceived; _server.ExceptionOccurred += (sender, args) => OnExceptionOccurred(args.Exception); _client.ExceptionOccurred += (sender, args) => OnExceptionOccurred(args.Exception); await _server.StartAsync(); await _client.ConnectAsync(); Trace.WriteLine("Client and server started"); Trace.WriteLine("---"); _startTime = DateTime.Now; }
public async Task RunAsync() { var pipeClient = new PipeClient <IAdder>("mypipe"); await pipeClient.ConnectAsync(); int result = await pipeClient.InvokeAsync(adder => adder.AddNumbers(1, 3)); }
public void Run() { var pipeName = "pipe"; _ = Task.Run(async() => { using var pipeServer = new PipeServer(pipeName); await pipeServer.WaitForConnectionAsync(); Console.WriteLine("Start sending..."); pipeServer.SendMessage(new LoadContextMessage("asd.dll")); Console.WriteLine("Done sending"); }); Task.Run(async() => { using var pipeClient = new PipeClient(pipeName); await pipeClient.ConnectAsync(); Console.WriteLine("Start receiving..."); var command = pipeClient.ReceiveMessage(); Console.WriteLine("Done receiving"); switch (command) { case LoadContextMessage loadCommand: Console.WriteLine(loadCommand.TargetAssemblyPath); break; } }).Wait(); }
public static async Task RunAsync(string pipeName) { try { using var source = new CancellationTokenSource(); Console.WriteLine($"Running in CLIENT mode. PipeName: {pipeName}"); Console.WriteLine("Enter 'q' to exit"); await using var client = new PipeClient <MyMessage>(pipeName); client.MessageReceived += (o, args) => Console.WriteLine("MessageReceived: " + args.Message); client.Disconnected += (o, args) => Console.WriteLine("Disconnected from server"); client.Connected += (o, args) => Console.WriteLine("Connected to server"); client.ExceptionOccurred += (o, args) => OnExceptionOccurred(args.Exception); // Dispose is not required var _ = Task.Run(async() => { while (true) { try { var message = await Console.In.ReadLineAsync().ConfigureAwait(false); if (message == "q") { source.Cancel(); break; } await client.WriteAsync(new MyMessage { Id = new Random().Next(), Text = message, }, source.Token).ConfigureAwait(false); } catch (Exception exception) { OnExceptionOccurred(exception); } } }, source.Token); Console.WriteLine("Client connecting..."); await client.ConnectAsync(source.Token).ConfigureAwait(false); await Task.Delay(Timeout.InfiniteTimeSpan, source.Token).ConfigureAwait(false); } catch (TaskCanceledException) { } catch (OperationCanceledException) { } catch (Exception exception) { OnExceptionOccurred(exception); } }
private async void OnLoad(object sender, EventArgs eventArgs) { Client = new PipeClient <string>(PipeName); Client.MessageReceived += (o, args) => AddLine("MessageReceived: " + args.Message); Client.Disconnected += (o, args) => AddLine("Disconnected from server"); Client.Connected += (o, args) => AddLine("Connected to server"); Client.ExceptionOccurred += (o, args) => OnExceptionOccurred(args.Exception); try { AddLine("Client connecting..."); await Client.ConnectAsync().ConfigureAwait(false); } catch (Exception exception) { OnExceptionOccurred(exception); } }
public async Task Run() { using var pipeClient = new PipeClient(Constants.PipeName); await pipeClient.ConnectAsync(); var messageHandler = new MessageHandler(); var isDone = false; while (!isDone) { var message = pipeClient.ReceiveMessage(); var result = messageHandler.Handle(message); if (result.Response != null) { pipeClient.SendMessage(result.Response); } isDone = result.IsDone; } }
public async Task ConnectTest() { using var cancellationTokenSource = new CancellationTokenSource(TimeSpan.FromSeconds(1)); await using var client = new PipeClient <string>("this_pipe_100%_is_not_exists"); await Assert.ThrowsExceptionAsync <OperationCanceledException>(async() => await client.ConnectAsync(cancellationTokenSource.Token)); }
/// <summary> /// /// </summary> /// <param name="cancellationToken"></param> /// <returns></returns> public override Task InitializeAsync(CancellationToken cancellationToken = default) { return(InitializeAsync(() => PipeClient.ConnectAsync(cancellationToken), cancellationToken)); }