Unable to establish two way communications using named pipes and Protobuf in .NET

67 Views Asked by At

Starting from https://johnkoerner.com/csharp/IPC-in-net-core-using-protobuf/, I am trying to establish a two-way communication. I have also seen this SO post on the subject, but still I cannot make it work. Below the server and client code (in F#)

// Server
open System.IO.Pipes
open ProtoBuf

    let pipe = new NamedPipeServerStream("MyPipe", PipeDirection.InOut)        
    pipe.WaitForConnection()
    let request = Serializer.DeserializeWithLengthPrefix<string>(pipe, PrefixStyle.Fixed32)
    Serializer.SerializeWithLengthPrefix(pipe, "response", PrefixStyle.Fixed32)
    pipe.Flush()
    pipe.WaitForPipeDrain()
    pipe.Dispose()
//Client
open System.IO.Pipes
open ProtoBuf

    let pipe = new NamedPipeClientStream(".", "MyPipe", PipeDirection.InOut)
    pipe.Connect()
    Serializer.SerializeWithLengthPrefix(pipe, "request", PrefixStyle.Fixed32)
    pipe.Flush()
    pipe.WaitForPipeDrain()
    let response = DeserializeWithLengthPrefix<string>(pipe, PrefixStyle.Fixed32)
    pipe.Dispose()

This is what happens:

  1. Start the server. It blocks in pipe.WaitForConnection.
  2. Start the client. Connection succeeds and the code blocks at Serializer.SerializeWithLengthPrefix.
  3. As a consequence of 2, server resumes and calls Serialize.DeserializeWithLengthPrefix, which does not return.

Now both processes are deadlocked.

  1. Kill the client.
  2. The server resumes and the DeserializeWithLengthPrefix returns null in request.

What can I do to avoid the deadlock and make the workflow run as expected?

0

There are 0 best solutions below

Related Questions in F#