Implement C# bindings for pipe io client

This commit is contained in:
Kristóf Tóth 2019-04-10 18:14:40 +02:00
parent 5ff2f6c27d
commit 74e08fd052
2 changed files with 97 additions and 0 deletions

83
clients/csharp/PipeIO.cs Normal file
View File

@ -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();
}
}
}

14
clients/csharp/Test.cs Normal file
View File

@ -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();
}
}
}