Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Asynchronous NamedPipeServerStream understanding

I was trying to find any good and clear example of asynchronous NamedPipeServerStream and couldn't find any suitable for me.

I want to have NamedPipe Server which is asynchronously accept messages from clients. The client is simple and it's fine for me. But I can't find examples of server, or can't understand how it works.

Now as I understand I need to create NamedPipeServerStream object. Let's do this:

namedPipeServerStream = new NamedPipeServerStream(PIPENAME, PipeDirection.In, 1, PipeTransmissionMode.Byte, PipeOptions.Asynchronous, BUFFERSIZE, BUFFERSIZE);

Seems to work. But I don't know, do I have to use PipeSecurity or PipeAccessRule at all? Do I? My server will work as a windows service in a local system.

What next? I'm thinking I need to use BeginWaitForConnection for async connections. Let's see:

namedPipeServerStream.BeginWaitForConnection(WaitForConnectionAsyncCallback, <some strange thing>);

Question 1: What is this "some strange thing"? How to use it?

Question 2: Should I do

while(true)
{
namedPipeServerStream.BeginWaitForConnection(WaitForConnectionAsyncCallback, <some strange thing>);
}

To make my server always wait for connections? Or I need to do it somehow else?

And then... Let's take a look into WaitForConnectionAsyncCallback function:

private void WaitForConnectionAsyncCallback(IAsyncResult result)
        {
            Console.WriteLine("Client connected.");
            byte[] buff = new byte[BUFFERSIZE];
            namedPipeServerStream.Read(buff, 0, namedPipeServerStream.InBufferSize);
            string recStr = General.Iso88591Encoding.GetString(buff, 0, namedPipeServerStream.InBufferSize);
            Console.WriteLine("   " + recStr);
            namedPipeServerStream.EndWaitForConnection(result);
        }

..This doesn't work of course. Because I don't know how exactly to receive string from stream. How? Now it raises an InvalidOperationException:

Pipe hasn't been connected yet.

So how to organize asynchronous work with NamedPipeServerStream?

like image 808
Ksice Avatar asked Dec 26 '22 21:12

Ksice


1 Answers

  • You tinker with the PipeSecurity to restrict access to the pipe, allowing only blessed programs to connect to your service. Put that on the back-burner until you've got this working and have performed a security analysis that shows that this kind of restriction is warranted.

  • The "some strange thing" is simply an arbitrary object that you can pass to the callback method. You don't often need it but it can be helpful if you write your callback so it serves multiple connections. In which case you need to know more about the specific pipe that got connected, the state argument allows you to pass that info. In your callback method, the result.AsyncState property gives you the reference back to that object. Only worry about that later, you'll find a use for it when you need it. Just pass null until then.

  • That's a bug. You must call EndWaitForConnection() first, before doing anything else with the pipe. Simply move it to the top of the method. You typically want to write it inside a try/catch so you can catch ObjectDisposedException, the exception that will be raised when you close the pipe before exiting your program.

like image 174
Hans Passant Avatar answered Jan 10 '23 19:01

Hans Passant