How to implement a multitask named pipe listener that runs asynchronously?

I cannot find a good example of how to create a multitask named pipe listener that runs asynchronously. I can do a second listen:

NamedPipeServerStream pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut); while (true) { pipeServer.WaitForConnection(); StreamReader reader = new StreamReader(pipeServer); MessageBox.Show(reader.ReadLine()); pipeServer.Disconnect(); } 

and I can make an asynchronous listener:

 NamedPipeServerStream pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); pipeServer.BeginWaitForConnection((a) => { pipeServer.EndWaitForConnection(a); StreamReader reader = new StreamReader(pipeServer); MessageBox.Show(reader.ReadLine()); }, null); 

But I don't seem to go. Is there a good example for this? I am also concerned about partially sent messages, as I believe this is a problem with asynchronous messages like this.

Update : I'm a little closer.

 pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); pipeServer.BeginWaitForConnection((a) => { pipeServer.EndWaitForConnection(a); StreamReader reader = new StreamReader(pipeServer); while (running) { String text = reader.ReadLine(); if (String.IsNullOrEmpty(text) == false) { MessageBox.Show(text); } } MessageBox.Show("Done!"); }, null); 

This will be read successfully once and continue the loop when ReadLine returns an empty blank line after the initial successful read. Thus, it clearly does not block and tries to read again. The problem is that if I send the same message a second time, it will not be picked up, and my trumpeter says that he is getting error 2316 (although I cannot understand what this means). I think I just need to do something similar to this, when the pipe will be cleaned every time, like the first code sample that I listed, but I have not received this to work.

+6
asynchronous named-pipes reusability
source share
2 answers

I think I did it:

 pipeServer = new NamedPipeServerStream("MyPipe", PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous); Boolean connectedOrWaiting = false; Byte[] buffer = new Byte[65535]; while (running) { if (!connectedOrWaiting) { pipeServer.BeginWaitForConnection((a) => { pipeServer.EndWaitForConnection(a); }, null); connectedOrWaiting = true; } if (pipeServer.IsConnected) { Int32 count = pipeServer.Read(buffer, 0, 65535); if (count > 0) { UTF8Encoding encoding = new UTF8Encoding(); String message = encoding.GetString(buffer, 0, count); MessageBox.Show(message); } pipeServer.Disconnect(); connectedOrWaiting = false; } } 

This will cause several messages to appear as they arrive and turn off as soon as the launch is set to false (in another thread, obviously). This seems to be what I need. Can someone verify that I'm not doing anything stupid?

+6
source share

I am also concerned about partially sent messages

They are not a problem with NamedPipes using their own (Win32) APIs, so I doubt very much that this is a problem using .NET. However, in the native documentation, he says:

Data is written to the channel as a message stream. The pipe processes the bytes recorded during each write operation as a message unit. The GetLastError function returns ERROR_MORE_DATA when the message is not fully read. This mode can be used with PIPE_READMODE_MESSAGE or PIPE_READMODE_BYTE.

(Note ERROR_MORE_DATA is 234.)

The documentation also states that for the FILE_FLAG_OVERLAPPED flag (native equivalent of PipeOptions.Asynchronous ):

Overlapped mode is on. If this mode is enabled, functions that perform read, write, and connection operations, which can take a considerable amount of time, can be immediately returned.

I always used asynchronous I / O with asynchronous named Stream.BeginRead (i.e. Stream.BeginRead ), but that means losing the functionality of TextReader , but then PipeTransmissionMode.Message is defined in terms of transferring byte groups anyway.

+2
source share

All Articles