Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting Anonymous Pipe across processes gives Invalid Handle Error, I'm using System.Io.Pipes

Tags:

c#

ipc

I'm trying to put together a class to handle Ipc between processes using anonymous pipes provided by System.Io.Pipes.

The problem I'm having is that when I test the class using a single process the pipes set up correctly and I can send data between client and server without a problem. However, when I split the client and server into separate processes ( on the same machine ), the client is unable to connect to the end of the server pipe.

The error System.Io.Exception Invalid pipe handle is raised when call

_outboundPipeServerStream = new AnonymousPipeClientStream(PipeDirection.Out, serverHandle);

The full code of the class is pasted below.

Essentially its work like this;

  1. Server Process. Create anonymous pipe set for inbound data - call this Pipe A
  2. Server Process. Starts Client process and passes PipeHandle via command argument
  3. Client Process. Connects to end of Pipe A
  4. Client Process. Create anonymous pipe set for inbound data (Pipe B) 5 Client process. Passes pipe handle back to Server using Pipe A
  5. Server Process. Connects to end of Pipe B

So now we have two anonymous pipes, pointing in opposite directions between Server and Client.

Here is the full code of my IPC class

    public class MessageReceivedEventArgs : EventArgs
{
    public string Message { get; set; }
}

public class IpcChannel : IDisposable
{
    private AnonymousPipeServerStream _inboundPipeServerStream;
    private StreamReader _inboundMessageReader;
    private string _inboundPipeHandle;

    private AnonymousPipeClientStream _outboundPipeServerStream;
    private StreamWriter _outboundMessageWriter;

    public delegate void MessageReceivedHandler(object sender, MessageReceivedEventArgs e);
    public event MessageReceivedHandler MessageReceived;

    private Thread _clientListenerThread;
    private bool _disposing = false;

    public IpcChannel()
    {
        SetupServerChannel();
    }

    public IpcChannel(string serverHandle)
    {
        SetupServerChannel();
        // this is the client end of the connection

        // create an outbound connection to the server
        System.Diagnostics.Trace.TraceInformation("Connecting client stream to server : {0}", serverHandle);
        SetupClientChannel(serverHandle);

        IntroduceToServer();
    }

    private void SetupClientChannel(string serverHandle)
    {
        _outboundPipeServerStream = new AnonymousPipeClientStream(PipeDirection.Out, serverHandle);
        _outboundMessageWriter = new StreamWriter(_outboundPipeServerStream)
        {
            AutoFlush = true
        };
    }

    private void SetupServerChannel()
    {
        _inboundPipeServerStream = new AnonymousPipeServerStream(PipeDirection.In);
        _inboundMessageReader = new StreamReader(_inboundPipeServerStream);
        _inboundPipeHandle = _inboundPipeServerStream.GetClientHandleAsString();
        _inboundPipeServerStream.DisposeLocalCopyOfClientHandle();

        System.Diagnostics.Trace.TraceInformation("Created server stream " + _inboundPipeServerStream.GetClientHandleAsString());

        _clientListenerThread = new Thread(ClientListener)
        {
            IsBackground = true
        };

        _clientListenerThread.Start();

    }

    public void SendMessage(string message)
    {
        System.Diagnostics.Trace.TraceInformation("Sending message {0} chars", message.Length);

        _outboundMessageWriter.WriteLine("M" + message);
    }

    private void IntroduceToServer()
    {
        System.Diagnostics.Trace.TraceInformation("Telling server callback channel is : " + _inboundPipeServerStream.GetClientHandleAsString());

        _outboundMessageWriter.WriteLine("CI" + _inboundPipeServerStream.GetClientHandleAsString());
    }

    public string ServerHandle
    {
        get
        {
            return _inboundPipeHandle;
        }
    }

    private void ProcessControlMessage(string message)
    {
        if (message.StartsWith("CI"))
        {
            ConnectResponseChannel(message.Substring(2));
        }
    }

    private void ConnectResponseChannel(string channelHandle)
    {
        System.Diagnostics.Trace.TraceInformation("Connecting response (OUT) channel to : {0}", channelHandle);

        _outboundPipeServerStream = new AnonymousPipeClientStream(PipeDirection.Out, channelHandle);
        _outboundMessageWriter = new StreamWriter(_outboundPipeServerStream);
        _outboundMessageWriter.AutoFlush = true;
    }

    private void ClientListener()
    {
        System.Diagnostics.Trace.TraceInformation("ClientListener started on thread {0}", Thread.CurrentThread.ManagedThreadId);

        try
        {
            while (!_disposing)
            {
                var message = _inboundMessageReader.ReadLine();
                if (message != null)
                {
                    if (message.StartsWith("C"))
                    {
                        ProcessControlMessage(message);
                    }
                    else if (MessageReceived != null)
                        MessageReceived(this, new MessageReceivedEventArgs()
                        {
                            Message = message.Substring(1)
                        });
                }
            }
        }
        catch (ThreadAbortException)
        {
        }
        finally
        {

        }
    }

    public void Dispose()
    {
        _disposing = true;

        _clientListenerThread.Abort();

        _outboundMessageWriter.Flush();
        _outboundMessageWriter.Close();
        _outboundPipeServerStream.Close();
        _outboundPipeServerStream.Dispose();

        _inboundMessageReader.Close();
        _inboundMessageReader.Dispose();

        _inboundPipeServerStream.DisposeLocalCopyOfClientHandle();
        _inboundPipeServerStream.Close();
        _inboundPipeServerStream.Dispose();
    }
}

In a single process, it can be used like this;

class Program
{
    private static IpcChannel _server;
    private static IpcChannel _client;

    static void Main(string[] args)
    {
        _server = new IpcChannel();
        _server.MessageReceived += (s, e) => Console.WriteLine("Server Received : " + e.Message);

        _client = new IpcChannel(_server.ServerHandle);
        _client.MessageReceived += (s, e) => Console.WriteLine("Client Received : " + e.Message);


        Console.ReadLine();

        _server.SendMessage("This is the server sending to the client");

        Console.ReadLine();

        _client.SendMessage("This is the client sending to the server");

        Console.ReadLine();

        _client.Dispose();
        _server.Dispose();
    }

Thanks in advance for any suggestions.

like image 261
Andy Baker Avatar asked Feb 17 '12 17:02

Andy Baker


1 Answers

You didn't post the server code, but anyway. In the server:

  • You need to specify that the client's pipe handle is inheritable when you create it.
  • When you launch the client you need to specify that inheritable handles will be inherited.

If you miss either of these steps then the pipe handle will be invalid in the client process.

Also, your step 4 won't work. If you create a pipe handle in the client it won't mean anything to the server when you pass it back. You can make this work using the DuplicateHandle function, but it's much easier to create all the handles in the server and inherit them in the client.

The key point is that handles are per-process, not system-wide.

like image 174
arx Avatar answered Sep 17 '22 23:09

arx