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:
- Start the server. It blocks in pipe.WaitForConnection.
- Start the client. Connection succeeds and the code blocks at Serializer.SerializeWithLengthPrefix.
- As a consequence of 2, server resumes and calls Serialize.DeserializeWithLengthPrefix, which does not return.
Now both processes are deadlocked.
- Kill the client.
- 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?