Search code examples
c#ipcinter-process-communicat

A form of IPC that works for my situation


I need to notify 1-many clients to perform a task (reload). The server may or may not be running at any given point in time. (For this reason, I have had some difficulty defining who is the client and who is the server.)

At any given time, the server may start running. When the server closes itself, it will notify all clients to perform their task.

I tried using a NamedPipeServerStream and running multiple instances on the "clients" (remember the relationship is odd so bear with me). Unfortunetly, I can only create one Pipe server for any given server name. So this did not work. I could have the clients continously check for a server, but if I'm going to start polling then I might as well poll the DB directly.

My situation is roughly like an observer pattern. I do not need to dynamically subscribe/unsubscribe. I do want the server to push a notifcation to all running clients to perform a task.

How can I achieve this? Please keep in mind I have to do this with IPC. The server/clients are running under different processes and always on the same machine.


Solution

  • To solve the polling problem you can create a named ManualResetEvent that the client processes will listen to. The clients will spin up a thread then wait on the event, when the server starts it will signal the event and let all of the clients start their listening code which can open a named pipe like you do currently. Look at the EventWaitHandle.GetAccessControl MSDN page to see a example of how to make a named ManualResetEvent.

    For your I can only create one Pipe server for any given server name. issue, if there are multiple servers running how are the clients supposed to know which server to connect to? You said it was a 1 server to * client relationship. If you are going to run multiple servers you will need a way to tell the client which server it should listen to.