From 74e08fd05271e7542997205063fe6bf0786c32de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krist=C3=B3f=20T=C3=B3th?= Date: Wed, 10 Apr 2019 18:14:40 +0200 Subject: [PATCH] Implement C# bindings for pipe io client --- clients/csharp/PipeIO.cs | 83 ++++++++++++++++++++++++++++++++++++++++ clients/csharp/Test.cs | 14 +++++++ 2 files changed, 97 insertions(+) create mode 100644 clients/csharp/PipeIO.cs create mode 100644 clients/csharp/Test.cs diff --git a/clients/csharp/PipeIO.cs b/clients/csharp/PipeIO.cs new file mode 100644 index 0000000..820a647 --- /dev/null +++ b/clients/csharp/PipeIO.cs @@ -0,0 +1,83 @@ +using System; +using System.IO; + + +namespace Pipe.IO +{ + public class PipeReader : IDisposable + { + public delegate void MessageHandler(String msg); + public event MessageHandler OnMessage; + private StreamReader pipe; + + public PipeReader(String pipe_path) + { + var fileStream = File.OpenRead(pipe_path); + this.pipe = new StreamReader(fileStream); + } + + public void Dispose() + { + this.pipe.Close(); + } + + public void Run() + { + String msg; + while ((msg = this.RecvMessage()) != null) + { + if (this.OnMessage != null) + { + this.OnMessage(msg); + } + } + } + + public String RecvMessage() + { + return this.pipe.ReadLine(); + } + } + + + public class PipeWriter : IDisposable + { + private StreamWriter pipe; + + public PipeWriter(String pipe) + { + var fileStream = File.OpenWrite(pipe); + this.pipe = new StreamWriter(fileStream); + } + + public void Dispose() + { + this.pipe.Close(); + } + + public void SendMessage(String msg) + { + this.pipe.WriteLine(msg); + this.pipe.Flush(); + } + } + + + public class PipeIO : IDisposable + { + public PipeReader reader; + public PipeWriter writer; + + public PipeIO(String in_pipe_path, String out_pipe_path) + { + this.reader = new PipeReader(in_pipe_path); + this.writer = new PipeWriter(out_pipe_path); + } + + public void Dispose() + { + this.reader.Dispose(); + this.writer.Dispose(); + } + } +} diff --git a/clients/csharp/Test.cs b/clients/csharp/Test.cs new file mode 100644 index 0000000..2129ced --- /dev/null +++ b/clients/csharp/Test.cs @@ -0,0 +1,14 @@ +using System; +using Pipe.IO; + +class Program +{ + static void Main(string[] args) + { + using (var t = new PipeIO("in", "out")) + { + t.reader.OnMessage += (String msg) => t.writer.SendMessage(msg); + t.reader.Run(); + } + } +}