2019-04-10 16:14:40 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
2019-04-11 16:30:25 +00:00
|
|
|
|
public PipeReader Reader;
|
|
|
|
|
public PipeWriter Writer;
|
2019-04-10 16:14:40 +00:00
|
|
|
|
|
|
|
|
|
public PipeIO(String in_pipe_path, String out_pipe_path)
|
|
|
|
|
{
|
2019-04-11 16:30:25 +00:00
|
|
|
|
this.Reader = new PipeReader(in_pipe_path);
|
|
|
|
|
this.Writer = new PipeWriter(out_pipe_path);
|
2019-04-10 16:14:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2019-04-11 16:30:25 +00:00
|
|
|
|
this.Reader.Dispose();
|
|
|
|
|
this.Writer.Dispose();
|
2019-04-10 16:14:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|