Esempio n. 1
0
        /*
         *  METHOD        : RecieveFromAllClients
         *  DESCRIPTION   : This method is used to receive messages from the clients,
         *   and save them to the data repository where they can be accessed by the sending thread
         *  PARAMETERS    : object data : Generic object type argument which us
         *  cast to a pipe, and used to receive incoming messages
         *  RETURNS       : void : The methods has no return
         */
        private static void RecieveFromAllClients(object data, ref Dictionary <int, string> messageList, ref int messageCounter, ref Dictionary <NamedPipeServerStream, NamedPipeServerStream> PairPipes)
        {
            //Cast the object as a pipe
            NamedPipeServerStream Client_IN = null;

            Client_IN = (NamedPipeServerStream)data;


            //Keep cycling an receiving new messages until the clients indicate they are shutting down
            bool clientDisconnectCommand = false;

            while (clientDisconnectCommand == false)
            {
                //Open an stream to the pipe taking incoming messages, and write the message to the string
                StreamReader readFromPipe    = new StreamReader(Client_IN);
                string       incomingMessage = readFromPipe.ReadLine();

                // If a null comes in, that means the client disconnected.
                if (incomingMessage == null)
                {
                    clientDisconnectCommand = true;
                }
                if (clientDisconnectCommand == false)
                {
                    messageCounter++;
                    messageList.Add(messageCounter, incomingMessage);
                }

                Thread.Sleep(100);
            }

            // We should close the matching pipe for that client
            if (PairPipes.TryGetValue(Client_IN, out NamedPipeServerStream Client_OUT))
            {
                Client_OUT.Close();
            }
            Client_IN.Close();

//// ///////////////////////////////////
//            // DEBUG
//            foreach (KeyValuePair<NamedPipeServerStream, NamedPipeServerStream> kv in PairPipes)
//                Console.WriteLine(kv);
//// ///////////////////////////////////
        }//RecieveFromAllClients
Esempio n. 2
0
        /*
         *  METHOD        : RecieveFromAllClients
         *  DESCRIPTION   : This method is used to receive messages from the clients,
         *   and save them to the data repository where they can be accessed by the sending thread
         *  PARAMETERS    :
         *       object data :
         *                       Generic object type argument which us
         *                       cast to a pipe, and used to receive incoming messages
         *       ref Dictionary<int, string> messageList:
         *                       Message repository
         *       ref int messageCounter :
         *                       How many messages are currently sent from the client
         *       ref Dictionary<NamedPipeServerStream, NamedPipeServerStream> PairPipesIn:
         *                       Used to keep track of which two pipes are from the client
         *       ref bool Pause:
         *                       If the server should or shouldnt be paused
         *  RETURNS       : void : The methods has no return
         */
        private static void RecieveFromAllClients(object data, ref Dictionary <int, string> messageList, ref int messageCounter, ref Dictionary <NamedPipeServerStream, NamedPipeServerStream> PairPipesIn, ref bool Pause)
        {
            Thread.Sleep(100);

            // Store the NamedPipeServerStream "data" into "Client_IN"
            NamedPipeServerStream Client_IN = null;

            Client_IN = (NamedPipeServerStream)data;


            //Keep cycling an receiving new messages until the clients indicate they are shutting down
            bool clientDisconnectCommand = false;

            while (clientDisconnectCommand == false)
            {
                string incomingMessage = string.Empty;
                //Open an stream to the pipe taking incoming messages, and write the message to the string
                StreamReader readFromPipe = new StreamReader(Client_IN);
                do
                {
                    incomingMessage = readFromPipe.ReadLine();
                } while (Pause == true);

                // If a null comes in, that means the client disconnected.
                if (incomingMessage == null)
                {
                    clientDisconnectCommand = true;
                }
                if (clientDisconnectCommand == false)
                {
                    messageCounter++;
                    messageList.Add(messageCounter, incomingMessage);
                }

                Thread.Sleep(100);
            }

            // We should close the matching pipe for that client
            if (PairPipesIn.TryGetValue(Client_IN, out NamedPipeServerStream Client_OUT))
            {
                Client_OUT.Close();
                Client_IN.Close();
            }
        }//RecieveFromAllClients