コード例 #1
0
        public void RunServer()
        {
            Logger.LogApplicationEvents("Server Starting");
            int messageCounter = 0;
            Dictionary <int, string> messageList = new Dictionary <int, string>();
            var PairPipes = new Dictionary <NamedPipeServerStream, NamedPipeServerStream>();

            FileIO      fileManager      = new FileIO();
            string      incomingPipeName = fileManager.ReadXMLDocument("pipeName-incoming"); //string indicator of the element to search in the XML doc
            string      outgoingPipename = fileManager.ReadXMLDocument("pipeName-outgoing"); //string indicator of the element to search in the XML doc
            ServerPipes openPipes        = new ServerPipes();

            Logger.LogApplicationEvents("Server Started");
            do
            {
                ////Console.WriteLine("Waiting for connections...");
                NamedPipeServerStream pipe_in  = openPipes.OpenInPipe("serverIn");
                NamedPipeServerStream pipe_out = openPipes.OpenOutPipe("serverOut");

                // Start a new thread, Send the pipe_in pipe to the new thread
                Thread RecieveFromClients = new Thread(() => RecieveFromAllClients(pipe_in, ref messageList, ref messageCounter, ref PairPipes));
                RecieveFromClients.Name = "RecieveMessageThread";
                RecieveFromClients.Start();

                //Start a new thread, Send the pipe_out pipe to the new thread
                Thread SendToClients = new Thread(() => SendToAllClients(pipe_out, ref messageList, ref messageCounter));
                SendToClients.Name = "SendMessageThread";
                SendToClients.Start();

                //Lets store both pipes together in a list as a pair. That way when one closes, we can close the other also.
                PairPipes.Add(pipe_in, pipe_out);

                //Increment the client counter so the server knows when to return
                openPipes.ClientCounter++;
                Thread.Sleep(1);

                if (Run == false)
                {
                    RecieveFromClients.Join();
                    SendToClients.Join();
                }
            } while (Run);
        }
コード例 #2
0
        /*
         *  METHOD        : ServerAcceptLoopThread
         *  DESCRIPTION   : This method is used to open the incoming and outgoing pipes to the clients
         *                   before treading off to open another connection
         *  PARAMETERS    : void : The method takes no arguments
         *  RETURNS       : void : The methods has no return
         */

        public static void ServerAcceptLoopThread()
        {
            Logger.LogApplicationEvents("Server Started");
            int messageCounter = 0;
            Dictionary <int, string> messageList = new Dictionary <int, string>();
            var PairPipes = new Dictionary <NamedPipeServerStream, NamedPipeServerStream>();

            FileIO      fileManager      = new FileIO();
            string      incomingPipeName = fileManager.ReadXMLDocument("pipeName-incoming"); //string indicator of the element to search in the XML doc
            string      outgoingPipename = fileManager.ReadXMLDocument("pipeName-outgoing"); //string indicator of the element to search in the XML doc
            ServerPipes openPipes        = new ServerPipes();

            /*  Use the var  initalClientConnection to allow the thread to enter the main connection loop
             *  ONLY one the first cycle before a client has connected. For all other subsequent iterations
             *  of the loop keep cycling until all clients have shutdown, or logged off */
            int initalClientConnection = 1;

            while ((initalClientConnection == 1) || (openPipes.ClientCounter > 0))
            {
                ////Console.WriteLine("Waiting for connections...");
                NamedPipeServerStream pipe_in  = openPipes.OpenInPipe("serverIn");
                NamedPipeServerStream pipe_out = openPipes.OpenOutPipe("serverOut");

                // Start a new thread, Send the pipe_in pipe to the new thread
                Thread RecieveFromClients = new Thread(() => RecieveFromAllClients(pipe_in, ref messageList, ref messageCounter, ref PairPipes));
                RecieveFromClients.Name = "RecieveMessageThread";
                RecieveFromClients.Start();

                //Start a new thread, Send the pipe_out pipe to the new thread
                Thread SendToClients = new Thread(() => SendToAllClients(pipe_out, ref messageList, ref messageCounter));
                SendToClients.Name = "SendMessageThread";
                SendToClients.Start();

                //Lets store both pipes together in a list as a pair. That way when one closes, we can close the other also.
                PairPipes.Add(pipe_in, pipe_out);

                //Increment the client counter so the server knows when to return
                openPipes.ClientCounter++;
                initalClientConnection = 0;
                ////Console.WriteLine("Client {0} connected...", openPipes.ClientCounter);
            }
        }