public static void checkForPipes(bool chrome, bool ff) { registreChromeNativeMessagingHost(false); registreffNativeMessagingHost(false); if (chromepipe == null && chrome) { chromepipe = new NamedPipeClientAsync <NativeMessagingMessage>(PIPE_NAME + "_chrome"); chromepipe.ServerMessage += Client_OnReceivedMessage; chromepipe.Disconnected += () => { onDisconnected?.Invoke("chrome"); }; chromepipe.Connected += () => { Connected?.Invoke("chrome"); Task.Run(() => reloadtabs()); }; chromepipe.Error += (e) => { Log.Debug(e.ToString()); }; chromepipe.Start(); //chromepipe.OnReceivedMessage += Client_OnReceivedMessage; //chromepipe.onDisconnected += () => { onDisconnected?.Invoke(); }; //chromepipe.onConnected += (pipe) => { FoundBrowser?.Invoke("chrome"); }; } if (ffpipe == null && ff) { ffpipe = new NamedPipeClientAsync <NativeMessagingMessage>(PIPE_NAME + "_ff"); ffpipe.ServerMessage += Client_OnReceivedMessage; ffpipe.Disconnected += () => { onDisconnected?.Invoke("ff"); }; ffpipe.Connected += () => { Connected?.Invoke("ff"); Task.Run(() => reloadtabs()); }; ffpipe.Error += (e) => { Log.Debug(e.ToString()); }; ffpipe.Start(); //ffpipe = new rpa.pipe.NamedPipeClient<NativeMessagingMessage>(PIPE_NAME + "_ff", true); //ffpipe.OnReceivedMessage += Client_OnReceivedMessage; //ffpipe.onDisconnected += () => { onDisconnected?.Invoke(); }; //ffpipe.onConnected += (pipe) => { FoundBrowser?.Invoke("ff"); }; } }
public static void checkForPipes(bool chrome, bool ff) { //registreChromeNativeMessagingHost(false); //registreffNativeMessagingHost(false); if (chromepipe == null && chrome) { var SessionId = System.Diagnostics.Process.GetCurrentProcess().SessionId; chromepipe = new NamedPipeClientAsync <NativeMessagingMessage>(SessionId + "_" + PIPE_NAME + "_chrome"); chromepipe.ServerMessage += Client_OnReceivedMessage; chromepipe.Disconnected += () => { onDisconnected?.Invoke("chrome"); }; chromepipe.Connected += () => { Connected?.Invoke("chrome"); Task.Run(() => enumwindowandtabs()); }; chromepipe.Error += (e) => { Log.Debug(e.ToString()); }; chromepipe.Start(); } if (ffpipe == null && ff) { var SessionId = System.Diagnostics.Process.GetCurrentProcess().SessionId; ffpipe = new NamedPipeClientAsync <NativeMessagingMessage>(SessionId + "_" + PIPE_NAME + "_ff"); ffpipe.ServerMessage += Client_OnReceivedMessage; ffpipe.Disconnected += () => { onDisconnected?.Invoke("ff"); }; ffpipe.Connected += () => { Connected?.Invoke("ff"); Task.Run(() => enumwindowandtabs()); }; ffpipe.Error += (e) => { Log.Debug(e.ToString()); }; ffpipe.Start(); } }
public static async Task RunAsync(string listenToAppServerPipeName, string sendToAppServerPipeName) { ConsoleOutput.ConsoleWriteLine("DataServer"); NamedPipeServerAsync listenToAppServer = new NamedPipeServerAsync(listenToAppServerPipeName); TaskCompletionSource <object> taskCompletionSource = new TaskCompletionSource <object>(); listenToAppServer.TokenReceivedEventAsync += async(t) => { UpdateStatus(t, "R"); if (!t.End) { await Task.Delay(500).ConfigureAwait(false); } UpdateStatus(t, "D"); await NamedPipeClientAsync.SendAsync(sendToAppServerPipeName, t).ConfigureAwait(false); UpdateStatus(t, "F"); }; await listenToAppServer.StartAsync().ConfigureAwait(false); }
public async Task SendReceiveMultiple_TaskCompletionSource() { Token[] token = new Token[] { new Token(1), new Token(2) }; List <Token> result = new List <Token>(); TaskCompletionSource <object> taskCompletionSource = new TaskCompletionSource <object>(); namedPipeServer.TokenReceivedEventAsync += (t) => { result.Add(t); if (result.Count >= 2) { taskCompletionSource.SetResult(true); } return(Task.CompletedTask); }; var task = Task.Run(() => namedPipeServer.StartAsync()); await NamedPipeClientAsync.SendAsync(pipeName, token[0]); await NamedPipeClientAsync.SendAsync(pipeName, token[1]); await taskCompletionSource.Task; Assert.AreEqual(2, result.Count); Assert.AreEqual(token[0].ID, result[0].ID); Assert.AreEqual(token[1].ID, result[1].ID); namedPipeServer.Dispose(); await task; }
public async Task SendReceiveMultiple() { AutoResetEvent autoResetEvent = new AutoResetEvent(false); Token[] token = new Token[] { new Token(1), new Token(2) }; List <Token> result = new List <Token>(); namedPipeServer.TokenReceivedEventAsync += (t) => { result.Add(t); if (result.Count >= 2) { autoResetEvent.Set(); } return(Task.CompletedTask); }; var task = Task.Run(() => namedPipeServer.StartAsync()); await NamedPipeClientAsync.SendAsync(pipeName, token[0]).ConfigureAwait(false); await NamedPipeClientAsync.SendAsync(pipeName, token[1]).ConfigureAwait(false); autoResetEvent.WaitOne(1000); Assert.AreEqual(2, result.Count); Assert.AreEqual(token[0].ID, result[0].ID); Assert.AreEqual(token[1].ID, result[1].ID); namedPipeServer.Dispose(); await task.ConfigureAwait(false); }
public async Task SendReceive() { AutoResetEvent autoResetEvent = new AutoResetEvent(false); Token token = new Token(1); Token result = null; namedPipeServer.TokenReceivedEventAsync += (t) => { result = t; autoResetEvent.Set(); return(Task.CompletedTask); }; var task = Task.Run(() => namedPipeServer.StartAsync()); await NamedPipeClientAsync.SendAsync(pipeName, token); autoResetEvent.WaitOne(1000); Assert.IsNotNull(result); Assert.AreEqual(token.ID, result.ID); namedPipeServer.Dispose(); await task; // wait for the thread to finished. }
public async Task ReceiveOneMessage() { // Receive one message than stop AutoResetEvent autoResetEvent = new AutoResetEvent(false); Token token = new Token(1); Token result = null; bool listenThreadCompleted = false; namedPipeServer.TokenReceivedEventAsync += (t) => { result = t; return(Task.CompletedTask); }; var task = Task.Run(async() => { await namedPipeServer.StartAsync(true); listenThreadCompleted = true; autoResetEvent.Set(); }); await NamedPipeClientAsync.SendAsync(pipeName, token); autoResetEvent.WaitOne(1000); Assert.IsTrue(listenThreadCompleted); namedPipeServer.Dispose(); await task; }
public static async Task RunAsync(int clientNumber, string clientListenPipeName, string clientSendPipeName, string dataserverListenPipeName, string dataserverSendPipeName) { Console.WriteLine($"AppServer Async Start {clientNumber}"); using (NamedPipeServerAsync listenToClient = new NamedPipeServerAsync(clientListenPipeName)) { using (NamedPipeServerAsync listenToDataServer = new NamedPipeServerAsync(dataserverListenPipeName)) { object lockId = new object(); listenToClient.TokenReceivedEventAsync += async(t) => { var token = t; // Start the time when the first value comes in from the first client if (!Program.Start.HasValue) { Program.Start = ConsoleOutput.StartTime = DateTime.Now; } lock (lockId) { t.AppServerID = ID; ID++; } ConsoleOutput.UpdateStatus("AppServer Async: ", token, "R"); // Key: Using Async allows the thread to go off and do other work // until the DataServer responds await NamedPipeClientAsync.SendAsync(dataserverSendPipeName, token).ConfigureAwait(false); ConsoleOutput.UpdateStatus("AppServer Async: ", token, "D"); }; listenToDataServer.TokenReceivedEventAsync += async(t) => { ConsoleOutput.UpdateStatus("AppServer Async: ", t, "C"); await NamedPipeClientAsync.SendAsync(clientSendPipeName, t).ConfigureAwait(false); ConsoleOutput.UpdateStatus("AppServer Async: ", t, "F"); }; Task[] listenTasks = new Task[2]; listenTasks[0] = listenToClient.StartAsync(); listenTasks[1] = listenToDataServer.StartAsync(); await Task.WhenAll(listenTasks).ConfigureAwait(false); } } //Console.WriteLine($"AppServer Async End {clientNumber}"); }
public static async Task RunAsync(int numToSend, string sendToAppServerPipeName, string listenToAppServerPipeName) { object receivedLock = new object(); int received = 0; Console.WriteLine($"Client Start {sendToAppServerPipeName}"); status = new string[numToSend]; for (var i = 0; i < status.Length; i++) { status[i] = "_"; } List <Task> sendTasks = new List <Task>(); using (var listen = new NamedPipeServerAsync(listenToAppServerPipeName)) { List <int> tokens = new List <int>(); TaskCompletionSource <object> taskCompletionSource = new TaskCompletionSource <object>(); listen.TokenReceivedEventAsync += (t) => { UpdateStatus(t, "R"); if (!t.End) { lock (receivedLock) { received++; tokens.Remove(t.ID); if (tokens.Count == 0) { taskCompletionSource.SetResult(null); } } } return(Task.CompletedTask); }; var listenTask = listen.StartAsync(); for (var i = 0; i < numToSend; i++) { var token = new Token(i); tokens.Add(token.ID); UpdateStatus(token, "S"); sendTasks.Add(NamedPipeClientAsync.SendAsync(sendToAppServerPipeName, token)); } await Task.WhenAll(sendTasks.ToArray()); await taskCompletionSource.Task; Console.WriteLine($"Client Send End Token {sendToAppServerPipeName}"); var endToken = new Token(true); await NamedPipeClientAsync.SendAsync(sendToAppServerPipeName, endToken).ConfigureAwait(false); await listenTask; // Wait for the End Token to be received and exit the pipe infinit loop } // Send Token.End = true token to shut down the AppServer (but not the DataServer in case there are multiple clients) if (received != numToSend) { throw new Exception($"Failure: Number sent {numToSend} Number received {received}"); } Console.WriteLine($"Client End {sendToAppServerPipeName}"); }