public string ExecuteTaskString(string strServer)
        {
            // __________Execute Tasks__________

            enMainState mainState     = enMainState.GetTask;
            enTask      curTask       = enTask.None;
            string      strPathSource = "";
            string      strPathTarget = "";

            try
            {
                //Console.WriteLine("Receives {0} bytes; Message: \"{1}\"",
                //    cbBytesRead, strMessage);

                string[] listReceived = strServer.Split('\n');

                foreach (string line in listReceived)
                {
                    if (line.Length > 0)
                    {
                        string strline = line.TrimEnd('\0');
                        strline = strline.TrimStart('\0');

                        Console.WriteLine("[CLIENT] Echo: " + strline);


                        // Process Message
                        switch (mainState)
                        {
                        case enMainState.GetTask:
                            curTask   = GetTask(strline);
                            mainState = enMainState.GetPathSource;
                            break;

                        case enMainState.GetPathSource:
                            strPathSource = strline;
                            mainState     = enMainState.GetPathTarget;
                            break;

                        case enMainState.GetPathTarget:
                            strPathTarget = strline;
                            mainState     = enMainState.ExecuteTask;
                            break;

                        default:
                            mainState = enMainState.GetTask;
                            break;
                        }


                        if (mainState == enMainState.ExecuteTask)
                        {
                            // Send one message to the pipe.

                            string strAnswer = ExecuteTask(curTask, strPathSource, strPathTarget);


                            return(strAnswer);
                        }
                    }
                }
            }


            catch (Exception ex)
            {
                Console.WriteLine("The client throws the error: {0}", ex.Message);
            }


            return(null);
        }
Exemple #2
0
        static void BCLSystemIOPipeClient()
        {
            /////////////////////////////////////////////////////////////////////
            // Try to open a named pipe.
            //

            // Prepare the pipe name
            String strServerName = ".";
            String strPipeName   = "RoMoRDuP";

            NamedPipeClientStream pipeClient = null;

            try
            {
                pipeClient = new NamedPipeClientStream(
                    strServerName,              // The server name
                    strPipeName,                // The unique pipe name
                    PipeDirection.InOut,        // The pipe is bi-directional
                    PipeOptions.None,           // No additional parameters

                    //The server process cannot obtain identification information about
                    //the client, and it cannot impersonate the client.
                    TokenImpersonationLevel.Anonymous);

                pipeClient.Connect(60000); // set TimeOut for connection
                pipeClient.ReadMode = PipeTransmissionMode.Message;

                Console.WriteLine(@"The named pipe, \\{0}\{1}, is connected.",
                                  strServerName, strPipeName);



                /////////////////////////////////////////////////////////////////
                // Send a message to the pipe server and receive its response.
                //

                // A byte buffer of BUFFER_SIZE bytes. The buffer should be big
                // enough for ONE request to the client

                string strServer = "";
                byte[] bClient   = new byte[4096];       // Client -> Server
                int    cbRequestBytes;
                byte[] bServer = new byte[BUFFER_SIZE];  // Server -> Client
                int    cbBytesRead, cbReplyBytes;



                // __________Execute Tasks__________

                enMainState mainState     = enMainState.GetTask;
                enTask      curTask       = enTask.None;
                string      strPathSource = "";
                string      strPathTarget = "";


                while (pipeClient.IsConnected)
                {
                    // Receive one message from the pipe.
                    cbReplyBytes = BUFFER_SIZE;
                    int offset = 0;
                    int index  = -1;
                    strServer = "";
                    bool bSuccess = false;

                    do
                    {
                        cbBytesRead = pipeClient.Read(bServer, offset, 50);
                        offset     += cbBytesRead;

                        // Unicode-encode the byte array and trim all the '\0' chars
                        // at the end.
                        strServer = Encoding.Unicode.GetString(bServer, 0, offset); //.TrimEnd('\0'); // Trim End is insufficient
                        //strServer = strServer.Remove(offset); // !!!!!!!!!!!!Problem Unicode

                        if (strServer.Contains("*"))
                        {
                            index    = strServer.IndexOf('*');
                            bSuccess = true;
                        }
                        else
                        {
                            index = strServer.IndexOf('\0');
                        }

                        if (index >= 0)
                        {
                            strServer = strServer.Remove(index);
                        }
                    } while((offset < 4096) && (bSuccess == false));


                    if (bSuccess)
                    {
                        //Console.WriteLine("Receives {0} bytes; Message: \"{1}\"",
                        //    cbBytesRead, strMessage);

                        string[] listReceived = strServer.Split('\n');

                        foreach (string line in listReceived)
                        {
                            if (line.Length > 0)
                            {
                                string strline = line.TrimEnd('\0');
                                strline = strline.TrimStart('\0');

                                Console.WriteLine("[CLIENT] Echo: " + strline);


                                // Process Message
                                switch (mainState)
                                {
                                case enMainState.GetTask:
                                    curTask   = GetTask(strline);
                                    mainState = enMainState.GetPathSource;
                                    break;

                                case enMainState.GetPathSource:
                                    strPathSource = strline;
                                    mainState     = enMainState.GetPathTarget;
                                    break;

                                case enMainState.GetPathTarget:
                                    strPathTarget = strline;
                                    mainState     = enMainState.ExecuteTask;
                                    break;

                                default:
                                    mainState = enMainState.GetTask;
                                    break;
                                }


                                if (mainState == enMainState.ExecuteTask)
                                {
                                    // Send one message to the pipe.

                                    string strAnswer = ExecuteTask(curTask, strPathSource, strPathTarget);

                                    cbRequestBytes = Encoding.Unicode.GetBytes(strAnswer + "\0", 0, strAnswer.Length + 1, bClient, 0);


                                    if (pipeClient.CanWrite)
                                    {
                                        pipeClient.Write(bClient, 0, cbRequestBytes);
                                    }
                                    pipeClient.Flush();

                                    //Console.WriteLine("Sends {0} bytes; Message: \"{1}\"",
                                    //    cbRequestBytes, strMessage.TrimEnd('\0'));

                                    mainState = enMainState.GetTask;
                                }
                            }
                        }
                    }
                }


                bClient        = Encoding.Unicode.GetBytes("EXIT");
                cbRequestBytes = bClient.Length;
                if (pipeClient.CanWrite)
                {
                    pipeClient.Write(bClient, 0, cbRequestBytes);
                }
                pipeClient.Flush();
            }
            catch (TimeoutException ex)
            {
                Console.WriteLine("Unable to open named pipe {0}\\{1}",
                                  strServerName, strPipeName);
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine("The client throws the error: {0}", ex.Message);
            }
            finally
            {
                /////////////////////////////////////////////////////////////////
                // Close the pipe.
                //

                if (pipeClient != null)
                {
                    pipeClient.Close();
                }
            }
        }
Exemple #3
0
        // In separate Thread!!
        private void Execute()
        {
            if (userInterface.mainWindow != null)
            {
                userInterface.mainWindow.Dispatcher.Invoke(
                    (Action)(() =>
                {
                    // Reset Output
                    userInterface.strTaskExecution = "";

                    // Reset Current Progress
                    userInterface.ProcessCurrentSizeTaskExecution = "0";
                }
                             ), null);
            }



            try
            {
                enMainState mainState = enMainState.GetTask;


                List <string> listTasks = new List <string>();
                // Add Playlist Tasks
                listTasks = ProcessPlaylistUpdates(playlistUpdatesModel);
                // Add SourceList Tasks

                List <List <TaskPlanner.FileListEntry> > ListfileLists = new List <List <TaskPlanner.FileListEntry> >();
                ListfileLists.Add(fileLists.SourceFileListBefore);

                if (userInterface is UserInterface.UserOptionsMirror)
                {
                    ListfileLists.Add(fileLists.TargetFileListBefore);
                }

                List <string> listTasksAdd = ProcessFileLists(ListfileLists);

                foreach (string strAdd in listTasksAdd)
                {
                    listTasks.Add(strAdd);
                }


                if (userInterface.mainWindow != null)
                {
                    userInterface.mainWindow.Dispatcher.Invoke(
                        (Action)(() =>
                    {
                        userInterface.ProcessTargetSizeTaskExecution = listTasks.Count.ToString();
                    }
                                 ), null);
                }


                int iProcessCurrentSize = 0; // Current Progress

                TaskExecutionClient.ExecutionClient executionClinet = new TaskExecutionClient.ExecutionClient();

                string strLine = "";

                foreach (string line in listTasks)
                {
                    strLine += line + "\n";

                    mainState++;

                    if (mainState == enMainState.ExecuteTask)
                    {
                        mainState = enMainState.GetTask;

                        string strInfo = executionClinet.ExecuteTaskString(strLine);
                        strLine = "";

                        if (strInfo.Length > 0)
                        {
                            if (strInfo != "EXIT")
                            {
                                if (userInterface.mainWindow != null)
                                {
                                    userInterface.mainWindow.Dispatcher.Invoke(
                                        (Action)(() =>
                                    {
                                        userInterface.strTaskExecution += strInfo + "\n";
                                    }
                                                 ), null);
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }


                    // Increment Current Progress
                    if (userInterface.mainWindow != null)
                    {
                        userInterface.mainWindow.Dispatcher.Invoke(
                            (Action)(() =>
                        {
                            iProcessCurrentSize++;
                            userInterface.ProcessCurrentSizeTaskExecution = iProcessCurrentSize.ToString();
                        }
                                     ), null);
                    }
                } // foreach taskline


                if (userInterface.mainWindow != null)
                {
                    userInterface.mainWindow.Dispatcher.Invoke(
                        (Action)(() =>
                    {
                        userInterface.strTaskExecution += "Finished!" + "\n";
                    }
                                 ), null);
                }
            }
            catch (Exception ex)
            {
                //Console.WriteLine("The server throws the error: {0}", ex.Message);

                System.Windows.MessageBox.Show("Error: " + ex.Message);
            }
        } // Execute