2using System.Collections.Generic;
6using System.Threading.Tasks;
26 private const int bufferSize = 65536;
28 private readonly StringBuilder fragment =
new StringBuilder();
29 private readonly
string pipeName;
30 private readonly
byte[] inputBuffer =
new byte[bufferSize];
31 private readonly
bool logIncoming;
33 private NamedPipeServerStream pipe =
null;
34 private bool disposed =
false;
35 private int inputState = 0;
36 private int inputDepth = 0;
43 : this(PipeName, true, DefaultFactory)
53 : this(PipeName, LogIncomingEvents, DefaultFactory)
65 if (StreamFactory is
null)
66 throw new ArgumentNullException(nameof(StreamFactory),
"Pipe stream factory cannot be null.");
68 this.pipeStreamFactory = StreamFactory;
69 this.pipeName = PipeName;
70 this.logIncoming = LogIncomingEvents;
71 this.pipe = StreamFactory(this.pipeName);
72 this.pipe.BeginWaitForConnection(this.PipeClientConnected,
null);
75 private static NamedPipeServerStream DefaultFactory(
string Name)
77 return new NamedPipeServerStream(Name, PipeDirection.In, 1, PipeTransmissionMode.Message,
78 PipeOptions.Asynchronous, 65536, 65536);
84 public NamedPipeServerStream
Pipe => this.pipe;
99 private void RestartPipe()
101 if (this.pipe is
null || this.disposed)
106 this.pipe?.Disconnect();
112 this.pipe?.Dispose();
115 this.pipe = this.pipeStreamFactory(this.pipeName);
120 this.fragment.Clear();
122 this.pipe.BeginWaitForConnection(this.PipeClientConnected,
null);
125 private void PipeClientConnected(IAsyncResult ar)
129 if (this.pipe is
null)
132 this.pipe.EndWaitForConnection(ar);
133 this.pipe.BeginRead(this.inputBuffer, 0, bufferSize, this.PipeReadComplete,
null);
142 private async
void PipeReadComplete(IAsyncResult ar)
146 if (this.pipe is
null)
149 int NrRead = this.pipe.EndRead(ar);
151 this.pipe.BeginWaitForConnection(this.PipeClientConnected,
null);
154 string s = Encoding.UTF8.GetString(this.inputBuffer, 0, NrRead);
159 Continue = await this.ParseIncoming(s);
168 this.pipe.BeginRead(this.inputBuffer, 0, bufferSize, this.PipeReadComplete,
null);
173 Log.
Error(
"Pipe-connection down.", this.pipeName);
183 private async Task<bool> ParseIncoming(
string s)
187 foreach (
char ch
in s)
189 switch (this.inputState)
194 this.fragment.Append(ch);
197 else if (this.inputDepth > 0)
198 this.fragment.Append(ch);
207 this.fragment.Append(ch);
211 this.inputState += 2;
215 this.fragment.Append(ch);
219 if (this.inputDepth < 0)
226 if (this.inputDepth == 0)
228 if (!await this.ProcessFragment(this.fragment.ToString()))
231 this.fragment.Clear();
234 if (this.inputState > 0)
241 this.fragment.Append(ch);
252 this.fragment.Append(ch);
255 if (this.inputDepth == 0)
257 if (!await this.ProcessFragment(this.fragment.ToString()))
260 this.fragment.Clear();
263 if (this.inputState != 0)
278 private async Task<bool> ProcessFragment(
string Xml)
282 XmlDocument Doc =
new XmlDocument();
288 if (this.logIncoming)
Class representing an event.
static bool TryParse(string Xml, out Event[] Parsed)
Tries to parse an event object (or collection of event objects) from an XML document.
Static class managing the application event log. Applications and services log events on this static ...
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
static void Error(string Message, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, string StackTrace, params KeyValuePair< string, object >[] Tags)
Logs an error event.
static async void Event(Event Event)
Logs an event. It will be distributed to registered event sinks.
Receives events from an operating system pipe
EventHandlerAsync< CustomFragmentEventArgs > CustomFragmentReceived
Event raised when a custom XML fragment has been received.
EventHandlerAsync< EventEventArgs > EventReceived
Event raised when an event has been received.
PipeEventRecipient(string PipeName, bool LogIncomingEvents, NamedPipeServerStreamFactory StreamFactory)
Receives events from an operating system pipe
NamedPipeServerStream Pipe
Pipe object.
void Dispose()
IDisposable.Dispose
PipeEventRecipient(string PipeName, bool LogIncomingEvents)
Receives events from an operating system pipe
PipeEventRecipient(string PipeName)
Receives events from an operating system pipe
delegate NamedPipeServerStream NamedPipeServerStreamFactory(string Name)
Delegate for methods that create object instances of NamedPipeServerStream.