Implement C# bindings for pipe io client
This commit is contained in:
		
							
								
								
									
										83
									
								
								clients/csharp/PipeIO.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										83
									
								
								clients/csharp/PipeIO.cs
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										14
									
								
								clients/csharp/Test.cs
									
									
									
									
									
										Normal 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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user