Parent <-> child communication design. #1025
-
Hi, My case: launcher application that is able to spawn 2 child apps. I want my launcher and child apps be able to talk to each other via json rpc, meaning sending multiple messages back and forth when needed. Based on od cond sampes I implemented it this way:
while (true)
{
var stream = new NamedPipeServerStream("StreamJsonRpcSamplePipe", PipeDirection.InOut, NamedPipeServerStream.MaxAllowedServerInstances, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);
await stream.WaitForConnectionAsync();
var jsonRpc = JsonRpc.Attach(stream, this);
await jsonRpc.Completion;
} is that the way to go for this case? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Named pipes are bidirectional, so you don't need each process to create both a server and a client pipe. The parent process should create a named pipe and all child processes can connect to it as clients. Now each client has a bidirectional stream with the parent, over which JSON-RPC can be done. Once the connection is established, you can and should use it for all your JSON-RPC messages. Don't recreate the stream after each processed message. Here is a sample named pipe server that can accept multiple clients: |
Beta Was this translation helpful? Give feedback.
Named pipes are bidirectional, so you don't need each process to create both a server and a client pipe. The parent process should create a named pipe and all child processes can connect to it as clients. Now each client has a bidirectional stream with the parent, over which JSON-RPC can be done.
The server would indeed have a loop that would repeatedly listen on the named pipe for incoming connections and as each connection is made, it should connect a JsonRpc object to it using Attach as you've done. But it should not await jsonRpc.Completion or else it'll hang there until the client hangs up before listening for the next incoming connection.
Once the connection is established, you can …