Exemplo n.º 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Server"/> class.
 /// </summary>
 /// <param name="pipeStream">The named pipe stream for the incoming RPC messages.</param>
 /// <param name="broadcastClientNotify">And interface used to broadcast notifications to the clients.</param>
 public Server(Stream pipeStream, IBroadcastClientNotify broadcastClientNotify)
 {
     this.broadcastClientNotify        = broadcastClientNotify;
     this.mainRpcChannel               = new JsonRpc(pipeStream, pipeStream, this);
     this.mainRpcChannel.Disconnected += this.OnRpcChannelDisconnected;
     this.mainRpcChannel.StartListening();
 }
Exemplo n.º 2
0
        /// <summary>
        /// The server entry point.
        /// </summary>
        /// <remarks>
        /// The server can only handle two command line options.
        /// The first is the local pipe name (--pipe) used for the JSON-RPC messages.D:\git\ShortStack\src\ShortStackServer\Program.cs
        /// The second can be used to break into the server on launch (--debugOnStart).
        /// </remarks>
        /// <param name="startParameters">Startup parameters.</param>
        /// <returns>A task for the server.</returns>
        public static Task KickOff(ServerStartParameters startParameters)
        {
            return(Tasks.PerformLongRunningOperation(
                       () =>
            {
                broadcastClientNotify = new BroadClassClientNotify(rpcServersLock, rpcServers);

                // The server will attempt to connect to an already running instance unless explicitly asked by the client to not do so.
                if (startParameters.ForceNewInstance)
                {
                    using (var threadsDone = new EventWaitHandle(false, EventResetMode.ManualReset))
                    {
                        CreateRPCServerThread(threadsDone, startParameters);

                        threadsDone.WaitOne();
                    }
                }
                else
                {
                    // This section of code handles making sure there is only a single instance of this server.

                    // The client has asked us to attempt to use a single instance. So set up the
                    // names used for the mutex, semaphore and memory mapped file.
                    SetupSingleInstanceNames();

                    // Create the named mutex that ensures that the process only runs once.

                    // If the mutex was already created, this signifies that there is already an instance
                    // of the application running.
                    alreadyRunningMutex = new Mutex(true, singleInstanceMutexName, out var createdNew);

                    if (createdNew)
                    {
                        StartupPrimaryInstance(startParameters);
                    }
                    else
                    {
                        HandleSecondaryInstance(startParameters);
                    }
                }
            }, CancellationToken.None));
        }