コード例 #1
0
        /*
         *
         * Pipe con il TCP_Client
         *
         */
        //----------------------------------------------------------
        private void polling_client_pipe()
        {
            namedPipe.Connect();

            byte[] buffer = new byte[1024];
            string buff_string;
            string tmp;

            string[] substrings;
            int      max        = 0;
            string   maxStr     = "0";
            string   maxStrPrec = "0";

            //-----------------------------------------------------
            //OK da TCP_Listener e Hello
            //-----------------------------------------------------
            namedPipe.Read(buffer, 0, 3);
            //-------------------
            Array.Clear(buffer, 0, buffer.Length);
            //-------------------
            buff_string = "Hello\0";
            buffer      = Encoding.UTF8.GetBytes(buff_string);
            namedPipe.Write(buffer, 0, buffer.Length);
            //-----------------------------------------------------
            //-----------------------------------------------------

            Array.Clear(buffer, 0, buffer.Length);
            buff_string = "";

            //-----------------------------------------------------
            //OK da TCP_Server e ACK-OK
            //-----------------------------------------------------
            namedPipe.Read(buffer, 0, 3);
            buff_string = System.Text.Encoding.UTF8.GetString(buffer);
            if (buff_string.Contains("X"))
            {
                //Connessione rifiutata, far apparire il popup
                try
                {
                    pbd.BeginInvoke((Action) delegate
                    {
                        cr = new ConnectionRejected();
                        cr.Show();
                    });
                }catch (InvalidOperationException e)
                {
                }


                namedPipe.Close();
                return;
            }
            else
            {
                /*pbd.BeginInvoke((Action)delegate
                 * {
                 *  pbd.Show();
                 * });*/
            }
            //-------------------
            Array.Clear(buffer, 0, buffer.Length);
            //-------------------
            buff_string = "OK\0";
            buffer      = Encoding.UTF8.GetBytes(buff_string);
            namedPipe.Write(buffer, 0, buffer.Length);
            //-----------------------------------------------------
            //-----------------------------------------------------

            while (true)
            {
                //-----------------------------------------------------
                // ricezione % di trasferimento e ACK-A
                //-----------------------------------------------------
                byte[] dataBuffer = new byte[5];

                namedPipe.Read(dataBuffer, 0, 5);
                buff_string = System.Text.Encoding.UTF8.GetString(dataBuffer);
                tmp         = buff_string.Replace("\0", string.Empty);
                substrings  = tmp.Split(new string[] { "|" }, StringSplitOptions.None);
                foreach (string i in substrings)
                {
                    if (i != "")
                    {
                        int x = Int32.Parse(i);
                        if (x > max)
                        {
                            max    = x;
                            maxStr = i;
                        }
                    }
                }
                //Scrittura sulla progress bar l'avanzamento del trasferimento
                if (maxStr != maxStrPrec)
                {
                    maxStrPrec = maxStr;
                    try
                    {
                        pbd.Invoke(barraDel, new object[] { maxStr });
                    }catch (Exception generic)
                    {
                    }
                }
                //-------------------

                //Array.Clear(buffer, 0, buffer.Length);
                //buff_string = "";

                //-------------------
                mtx.WaitOne();

                if (pbd.is_pipe_closed)
                {
                    buffer = Encoding.UTF8.GetBytes("X\0");
                    try
                    {
                        namedPipe.Write(buffer, 0, buffer.Length);
                    }
                    catch (System.IO.IOException)
                    {
                        namedPipe.Connect();
                        namedPipe.Write(buffer, 0, buffer.Length);
                    }
                    namedPipe.Close();
                    pbd.is_pipe_closed = false;
                    break;
                }
                else
                {
                    buffer = Encoding.UTF8.GetBytes("K\0");
                    try
                    {
                        namedPipe.Write(buffer, 0, buffer.Length);
                    }
                    catch (System.IO.IOException)
                    {
                        namedPipe.Connect();
                        namedPipe.Write(buffer, 0, buffer.Length);
                    }
                }
                mtx.ReleaseMutex();
                //-----------------------------------------------------
                //-----------------------------------------------------

                Array.Clear(buffer, 0, buffer.Length);
                buff_string = "";

                //-----------------------------------------------------
                //ACK-B da TCP_Server e ACK-ACK-B
                //-----------------------------------------------------
                namedPipe.Read(buffer, 0, buffer.Length);
                buff_string = System.Text.Encoding.UTF8.GetString(buffer);
                if (buff_string.Contains("X"))
                {
                    //visual messaggio di comm killata
                    pbd.BeginInvoke((Action) delegate
                    {
                        cr = new ConnectionRejected();
                        cr.Show();
                    });

                    break;
                }

                //-------------------
                buff_string = "OK\0";
                buffer      = Encoding.UTF8.GetBytes(buff_string);
                namedPipe.Write(buffer, 0, buffer.Length);
                //-----------------------------------------------------
                //-----------------------------------------------------


                //Condizione di terminazione di trasferimento del file
                if (maxStr == "100")
                {
                    break;
                }
            }

            namedPipe.Close();
            return;
        }
コード例 #2
0
        /*
         *
         * Pipe con il TCP_Server
         *
         */
        //----------------------------------------------------------
        private void polling_server_pipe()
        {
            namedPipe.Connect();

            byte[] buffer = new byte[1024];
            string buff_string;
            string tmp;

            string[] substrings;
            int      max        = 0;
            string   maxStr     = "0";
            string   maxStrPrec = "0";
            int      ind        = 0;

            //-----------------------------------------------------
            //HELLO Da TCP_Server e OK
            //-----------------------------------------------------
            namedPipe.Read(buffer, 0, 6);
            //-------------------
            if (accepted)
            {
                buff_string = "OK\0";
                buffer      = Encoding.UTF8.GetBytes(buff_string);
                namedPipe.Write(buffer, 0, buffer.Length);
            }
            else
            {
                buff_string = "X\0";
                buffer      = Encoding.UTF8.GetBytes(buff_string);
                namedPipe.Write(buffer, 0, buffer.Length);
                pbdr.Close();
                namedPipe.Close();
                return;
            }

            //-----------------------------------------------------
            //-----------------------------------------------------



            while (true)
            {
                ind = 0;

                //-----------------------------------------------------
                // ricezione % di trasferimento e ACK-%
                //-----------------------------------------------------
                bool found = false;

                byte[] dataBuffer = new byte[5];

                while (!found)
                {
                    namedPipe.Read(dataBuffer, 0, 5);
                    buff_string = System.Text.Encoding.UTF8.GetString(dataBuffer);
                    if (buff_string.Contains("|"))
                    {
                        found = true;
                    }
                    ind++;
                }

                tmp        = buff_string.Replace("\0", string.Empty);
                substrings = tmp.Split(new string[] { "|" }, StringSplitOptions.None);
                foreach (string i in substrings)
                {
                    if (i != "")
                    {
                        int x = Int32.Parse(i);
                        if (x > max)
                        {
                            max    = x;
                            maxStr = i;
                        }
                    }
                }
                //Scrittura sulla progress bar l'avanzamento del trasferimento
                if (maxStr != maxStrPrec)
                {
                    maxStrPrec = maxStr;
                    pbdr.Invoke(barraDelRic, new object[] { maxStr });
                }
                if (maxStr == "100")
                {
                    break;
                }

                buff_string = "OK\0";
                buffer      = Encoding.UTF8.GetBytes(buff_string);
                namedPipe.Write(buffer, 0, buffer.Length);
                //-----------------------------------------------------
                //-----------------------------------------------------


                //-----------------------------------------------------
                //ACK-A da TCP_Client e ACK-B a TCP_client
                //-----------------------------------------------------

                byte[] ackABuffer = new byte[2];
                namedPipe.Read(ackABuffer, 0, 2);
                buff_string = System.Text.Encoding.UTF8.GetString(ackABuffer);
                if (buff_string.Contains("X"))
                {
                    //Visual messaggio di comm killata
                    pbdr.BeginInvoke((Action) delegate
                    {
                        cr = new ConnectionRejected();
                        cr.Show();
                    });
                    break;
                }
                //-------------------

                //-------------------
                mtx.WaitOne();
                if (pbdr.is_pipe_closed)
                {
                    buffer = Encoding.UTF8.GetBytes("X\0");
                    try
                    {
                        namedPipe.Write(buffer, 0, buffer.Length);
                    }
                    catch (System.IO.IOException)
                    {
                        namedPipe.Connect();
                        namedPipe.Write(buffer, 0, buffer.Length);
                    }
                    namedPipe.Close();
                    pbdr.is_pipe_closed = false;
                    break;
                }
                else
                {
                    buffer = Encoding.UTF8.GetBytes("K\0");
                    try
                    {
                        namedPipe.Write(buffer, 0, buffer.Length);
                    }
                    catch (System.IO.IOException)
                    {
                        namedPipe.Connect();
                        namedPipe.Write(buffer, 0, buffer.Length);
                    }
                }
                mtx.ReleaseMutex();
            }
            namedPipe.Close();
            return;
        }