public static void Write(Pipe pipe, string message) { try { Exception lastException = null; for (int i = 0; i < 10; i++) { try { lastException = null; string header = message.Length.ToString().PadRight(8); var stringBuilder = new StringBuilder(); stringBuilder.Append(header); stringBuilder.Append(message); byte[] bytes = PipeEncoding.GetBytes(stringBuilder.ToString()); pipe.Write(bytes); return; } catch (Exception exception) { lastException = exception; } System.Threading.Thread.Sleep(150); } throw lastException; } catch (Exception exception) { Log.Error(exception); } }
public static string Read(Pipe pipe) { try { Exception lastException = null; for (int i = 0; i < 10; i++) { try { lastException = null; int length = 8; byte[] bytes = pipe.Read(length * PipeEncodingSize); if (bytes == null) { throw new Exception("No data was read."); } string header = PipeEncoding.GetString(bytes); if (!int.TryParse(header.Trim(), out length) || length < 0) { throw new Exception("Data read contained an unexpected header."); } if (length == 0) { return ""; } bytes = pipe.Read(length * PipeEncodingSize); if (bytes == null) { throw new Exception("No messsage following header was read."); } string message = PipeEncoding.GetString(bytes); if (message == null) { throw new Exception("Received null value from Pipe read."); } return message; } catch (Exception exception) { lastException = exception; } System.Threading.Thread.Sleep(150); } throw lastException; } catch (Exception exception) { Log.Error(exception); return null; } }