Last active
August 28, 2019 06:53
-
-
Save Suplanus/f652a15a55bff64b6ccb33836041f8bb to your computer and use it in GitHub Desktop.
Revisions
-
Suplanus revised this gist
Aug 28, 2019 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,4 @@ // Client var pipeClient = new PipeBidirectional(PipeType.Client); pipeClient.PipeMessage += PipeServerOnPipeMessage; pipeClient.Start("MyPipe"); @@ -9,6 +10,7 @@ private static void PipeServerOnPipeMessage(string args) // Do stuff } // Server var pipeServer = new PipeBidirectional(PipeType.Server); pipeServer.PipeMessage += PipeServerOnPipeMessage; pipeServer.Start("MyPipe"); @@ -20,6 +22,7 @@ private void PipeServerOnPipeMessage(string args) // Do stuff } // Model using System; using System.Diagnostics; using System.IO; -
Suplanus revised this gist
Aug 28, 2019 . 3 changed files with 22 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,25 @@ var pipeClient = new PipeBidirectional(PipeType.Client); pipeClient.PipeMessage += PipeServerOnPipeMessage; pipeClient.Start("MyPipe"); pipeClient.ListenAsync(); pipeClient.WriteAsync("Hello from Client"); private static void PipeServerOnPipeMessage(string args) { // Do stuff } var pipeServer = new PipeBidirectional(PipeType.Server); pipeServer.PipeMessage += PipeServerOnPipeMessage; pipeServer.Start("MyPipe"); pipeServer.ListenAsync(); pipeServer.WriteAsync("Hello from Server"); private void PipeServerOnPipeMessage(string args) { // Do stuff } using System; using System.Diagnostics; using System.IO; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +0,0 @@ -
Suplanus revised this gist
Aug 28, 2019 . No changes.There are no files selected for viewing
-
Suplanus revised this gist
Aug 28, 2019 . No changes.There are no files selected for viewing
-
Suplanus revised this gist
Aug 28, 2019 . No changes.There are no files selected for viewing
-
Suplanus revised this gist
Aug 28, 2019 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ var pipeServer = new PipeBidirectional(PipeType.Server); pipeServer.PipeMessage += PipeServerOnPipeMessage; pipeServer.Start("MyPipe"); pipeServer.ListenAsync(); pipeServer.WriteAsync("Hello from Server"); -
Suplanus revised this gist
Aug 28, 2019 . 2 changed files with 10 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,8 @@ var pipeClient = new PipeBidirectional(PipeType.Client); pipeClient.PipeMessage += PipeServerOnPipeMessage; pipeClient.Start("MyPipe"); pipeClient.ListenAsync(); pipeClient.WriteAsync("Hello from Client"); private static void PipeServerOnPipeMessage(string args) { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,8 +1,8 @@ var pipeServer = new PipeBidirectional(PipeType.Server); pipeServer.PipeMessage += PipeServerOnPipeMessage; pipeServer.Start("MyPipe", @"C:\MyPipeClient.exe"); pipeServer.ListenAsync(); pipeServer.WriteAsync("Hello from Server"); private void PipeServerOnPipeMessage(string args) { -
Suplanus revised this gist
Aug 28, 2019 . 2 changed files with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,7 @@ PipeClient.PipeMessage += PipeServerOnPipeMessage; PipeClient.Start("MyPipe"); PipeClient.ListenAsync(); PipeClient.WriteAsync("Hello from Client"); private static void PipeServerOnPipeMessage(string args) { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,6 +2,7 @@ PipeServer.PipeMessage += PipeServerOnPipeMessage; PipeServer.Start("MyPipe", @"C:\MyPipeClient.exe"); PipeServer.ListenAsync(); PipeServer.WriteAsync("Hello from Server"); private void PipeServerOnPipeMessage(string args) { -
Suplanus revised this gist
Aug 28, 2019 . 3 changed files with 1 addition and 1 deletion.There are no files selected for viewing
File renamed without changes.This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ using System.IO.Pipes; using System.Linq; namespace Suplanus.Pipes { public class PipeBidirectional : IDisposable { File renamed without changes. -
Suplanus created this gist
Aug 28, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ PipeClient = new PipeBidirectional(PipeType.Client); PipeClient.PipeMessage += PipeServerOnPipeMessage; PipeClient.Start("MyPipe"); PipeClient.ListenAsync(); private static void PipeServerOnPipeMessage(string args) { // Do stuff } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,117 @@ using System; using System.Diagnostics; using System.IO; using System.IO.Pipes; using System.Linq; namespace ibKastl.MD3.Objects.Model { public class PipeBidirectional : IDisposable { public int MaxNumberOfServerInstances { get; set; } = 1; public int InBufferSize { get; set; } = 4096; public int OutBufferSize { get; set; } = 4096; public event DelegateMessage PipeMessage; private readonly PipeType _pipeType; private NamedPipeServerStream _serverPipe; private NamedPipeClientStream _clientPipe; private Process _clientProcess; private StreamWriter _streamWriter; private StreamReader _streamReader; public PipeBidirectional(PipeType pipeType) { _pipeType = pipeType; } public void Start(string pipeName, string clientProcessPath = null) { switch (_pipeType) { case PipeType.Server: _serverPipe = new NamedPipeServerStream(pipeName, PipeDirection.InOut, MaxNumberOfServerInstances, PipeTransmissionMode.Message, PipeOptions.Asynchronous, InBufferSize, OutBufferSize); // Start client process if (!string.IsNullOrEmpty(clientProcessPath)) { _clientProcess = Process.GetProcesses() .FirstOrDefault(obj => clientProcessPath.Contains(obj.ProcessName) && obj.MainModule != null && obj.MainModule.FileName.Equals(clientProcessPath)); if (_clientProcess == null) { _clientProcess = Process.Start(new ProcessStartInfo { CreateNoWindow = true, FileName = clientProcessPath, UseShellExecute = false, }); } } // Start server _serverPipe.WaitForConnection(); _streamWriter = new StreamWriter(_serverPipe); _streamWriter.AutoFlush = true; _streamReader = new StreamReader(_serverPipe); break; // Start client case PipeType.Client: _clientPipe = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous); _clientPipe.Connect(); _streamWriter = new StreamWriter(_clientPipe); _streamWriter.AutoFlush = true; _streamReader = new StreamReader(_clientPipe); break; } } public async void ListenAsync() { do { if (_streamReader != null) { string line = await _streamReader.ReadLineAsync(); if (!string.IsNullOrEmpty(line)) { PipeMessage?.Invoke(line); } } } while (true); // ReSharper disable once FunctionNeverReturns } public async void WriteAsync(string message) { await _streamWriter.WriteLineAsync(message); } public void Dispose() { _streamWriter?.Dispose(); _streamReader?.Dispose(); if (_serverPipe != null && _serverPipe.IsConnected) { _serverPipe?.Disconnect(); } _serverPipe?.Dispose(); _clientPipe?.Dispose(); _clientProcess?.Kill(); _clientProcess?.Dispose(); } } public delegate void DelegateMessage(string args); } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,9 @@ PipeServer = new PipeBidirectional(PipeType.Server); PipeServer.PipeMessage += PipeServerOnPipeMessage; PipeServer.Start("MyPipe", @"C:\MyPipeClient.exe"); PipeServer.ListenAsync(); private void PipeServerOnPipeMessage(string args) { // Do stuff }