Skip to content

Instantly share code, notes, and snippets.

@Suplanus
Last active August 28, 2019 06:53
Show Gist options
  • Select an option

  • Save Suplanus/f652a15a55bff64b6ccb33836041f8bb to your computer and use it in GitHub Desktop.

Select an option

Save Suplanus/f652a15a55bff64b6ccb33836041f8bb to your computer and use it in GitHub Desktop.

Revisions

  1. Suplanus revised this gist Aug 28, 2019. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions PipeBidirectional.cs
    Original 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;
  2. Suplanus revised this gist Aug 28, 2019. 3 changed files with 22 additions and 20 deletions.
    10 changes: 0 additions & 10 deletions Client.cs
    Original file line number Diff line number Diff line change
    @@ -1,10 +0,0 @@
    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
    }
    22 changes: 22 additions & 0 deletions PipeBidirectional.cs
    Original 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;
    10 changes: 0 additions & 10 deletions Server.cs
    Original file line number Diff line number Diff line change
    @@ -1,10 +0,0 @@
    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
    }
  3. Suplanus revised this gist Aug 28, 2019. No changes.
  4. Suplanus revised this gist Aug 28, 2019. No changes.
  5. Suplanus revised this gist Aug 28, 2019. No changes.
  6. Suplanus revised this gist Aug 28, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Server.cs
    Original 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", @"C:\MyPipeClient.exe");
    pipeServer.Start("MyPipe");
    pipeServer.ListenAsync();
    pipeServer.WriteAsync("Hello from Server");

  7. Suplanus revised this gist Aug 28, 2019. 2 changed files with 10 additions and 10 deletions.
    10 changes: 5 additions & 5 deletions Client.cs
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    PipeClient = new PipeBidirectional(PipeType.Client);
    PipeClient.PipeMessage += PipeServerOnPipeMessage;
    PipeClient.Start("MyPipe");
    PipeClient.ListenAsync();
    PipeClient.WriteAsync("Hello from Client");
    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)
    {
    10 changes: 5 additions & 5 deletions Server.cs
    Original file line number Diff line number Diff line change
    @@ -1,8 +1,8 @@
    PipeServer = new PipeBidirectional(PipeType.Server);
    PipeServer.PipeMessage += PipeServerOnPipeMessage;
    PipeServer.Start("MyPipe", @"C:\MyPipeClient.exe");
    PipeServer.ListenAsync();
    PipeServer.WriteAsync("Hello from Server");
    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)
    {
  8. Suplanus revised this gist Aug 28, 2019. 2 changed files with 2 additions and 0 deletions.
    1 change: 1 addition & 0 deletions Client.cs
    Original 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)
    {
    1 change: 1 addition & 0 deletions Server.cs
    Original 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)
    {
  9. Suplanus revised this gist Aug 28, 2019. 3 changed files with 1 addition and 1 deletion.
    File renamed without changes.
    2 changes: 1 addition & 1 deletion PipeBidirectional.cs
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    using System.IO.Pipes;
    using System.Linq;

    namespace ibKastl.MD3.Objects.Model
    namespace Suplanus.Pipes
    {
    public class PipeBidirectional : IDisposable
    {
    File renamed without changes.
  10. Suplanus created this gist Aug 28, 2019.
    9 changes: 9 additions & 0 deletions Client
    Original 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
    }
    117 changes: 117 additions & 0 deletions PipeBidirectional.cs
    Original 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);
    }
    9 changes: 9 additions & 0 deletions Server
    Original 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
    }