コード例 #1
0
 // Дропнуть соединение
 public static void DropConnection()
 {
     serialPort.DataReceived -= new SerialDataReceivedEventHandler(serialPort_DataReceived);
     if (DataLink.FileRecieving || DataLink.FileSending)
     {
         MessageBox.Show("Ошибка передачи!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     PhysLayer.ShutDown();
     serialPort.Close();
 }
コード例 #2
0
ファイル: Form1.cs プロジェクト: unqx/KursNetworks
        private void TransmittingWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                if (!DataLink.FileSending && !DataLink.FileRecieving)
                {
                    if (!DataLink.SendQueue.IsEmpty)
                    {
                        File F;
                        if (DataLink.SendQueue.TryDequeue(out F))
                        {
                            DataLink.FileSending = true;
                            DataLink.StartSendingFile(F);

                            /****** установка элементов формы ******/

                            DownloadButton.Invoke((MethodInvoker) delegate
                            {
                                DownloadButton.Enabled = false;
                            });

                            progressBar1.Invoke((MethodInvoker) delegate
                            {
                                progressBar1.Maximum = (int)(F.Size / 1024);
                            });

                            ActionLabel.Invoke((MethodInvoker) delegate
                            {
                                ActionLabel.Text = "Идет передача файла...";
                            });

                            /**************************************/

                            FileStream Stream = new FileStream(F.Name, FileMode.Open, FileAccess.Read);
                            byte       R;
                            byte[]     buffer = new byte[1024];

                            int counter = 0; // счетчик ошибок

                            while (DataLink.FileSending)
                            {
                                if (!PhysLayer.DsrSignal())
                                {
                                    Stream.Close();
                                    MessageBox.Show("Ошибка передачи!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    PhysLayer.ShutDown();
                                }

                                if (PhysLayer.Responses.TryDequeue(out R))
                                {
                                    if (R == Convert.ToByte('A'))
                                    {
                                        counter = 0;
                                        try
                                        {
                                            int BytesRead = Stream.Read(buffer, 0, buffer.Length);
                                            if (BytesRead > 0)
                                            {
                                                byte[] clean = new byte[BytesRead];
                                                for (int i = 0; i < BytesRead; i++)
                                                {
                                                    clean[i] = buffer[i];
                                                }

                                                int step = clean.Length;

                                                clean = DataLink.pack('I', clean);
                                                clean = DataLink.EncodeFrame(clean);
                                                PhysLayer.Write(clean);

                                                progressBar1.Invoke((MethodInvoker) delegate
                                                {
                                                    progressBar1.Step = step / 1024;
                                                    progressBar1.PerformStep();
                                                });
                                            }

                                            else
                                            {
                                                Stream.Close();
                                                DataLink.EOF();
                                                DataLink.FileSending = false;

                                                progressBar1.Invoke((MethodInvoker) delegate
                                                {
                                                    progressBar1.Value = 0;
                                                });

                                                ActionLabel.Invoke((MethodInvoker) delegate
                                                {
                                                    ActionLabel.Text = "";
                                                });

                                                MessageBox.Show("Передача файла завершена!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.None);
                                            }
                                        }

                                        catch (ArgumentException)
                                        {
                                            MessageBox.Show("ISKLUCHENIE");
                                        }
                                    }

                                    if (R == Convert.ToByte('N'))
                                    {
                                        if (counter < 5)
                                        {
                                            counter++;
                                            PhysLayer.Write(buffer);
                                        }

                                        else
                                        {
                                            MessageBox.Show("Ошибка передачи!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                                            PhysLayer.ShutDown();
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                if (DataLink.FileRecieving)
                {
                    DownloadButton.Invoke((MethodInvoker) delegate
                    {
                        DownloadButton.Enabled = false;
                    });

                    string desktop  = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
                    string fullPath = desktop + "\\(NEW)" + DataLink.FileRecievingName;

                    FileStream Stream = new FileStream(fullPath, FileMode.Create, FileAccess.Write);

                    while (DataLink.FileRecieving)
                    {
                        if (!PhysLayer.DsrSignal())
                        {
                            Stream.Close();
                            System.IO.File.Delete(fullPath);
                            MessageBox.Show("Ошибка передачи!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            PhysLayer.ShutDown();
                        }

                        byte[] result;

                        if (PhysLayer.FramesRecieved.TryDequeue(out result))
                        {
                            if (Encoding.Default.GetString(result) == "EOF")
                            {
                                Stream.Close();
                                DataLink.FileRecieving = false;

                                MessageBox.Show("Прием файла завершен!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.None);

                                progressBar1.Invoke((MethodInvoker) delegate
                                {
                                    progressBar1.Value = 0;
                                });

                                break;
                            }

                            if (Encoding.Default.GetString(result) == "FNF")
                            {
                                Stream.Close();
                                System.IO.File.Delete(fullPath);

                                progressBar1.Invoke((MethodInvoker) delegate
                                {
                                    progressBar1.Value = 0;
                                });

                                PhysLayer.ShutDown();
                                MessageBox.Show("Файл не найден.\r\nВыберите другой файл.", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                                break;
                            }

                            if (Encoding.Default.GetString(result) == "SIZE")
                            {
                                progressBar1.Invoke((MethodInvoker) delegate
                                {
                                    progressBar1.Maximum = DataLink.FileRecievingSize / 1024;
                                });
                            }

                            else
                            {
                                try
                                {
                                    Stream.Write(result, 0, result.Length);

                                    progressBar1.Invoke((MethodInvoker) delegate
                                    {
                                        progressBar1.Step = result.Length / 1024;
                                        progressBar1.PerformStep();
                                    });
                                }

                                catch (IOException)
                                {
                                    MessageBox.Show("Ошибка передачи!", "Ошибка!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    PhysLayer.ShutDown();

                                    progressBar1.Invoke((MethodInvoker) delegate
                                    {
                                        progressBar1.Value = 0;
                                    });
                                }
                            }
                        }
                    }

                    Stream.Close();
                    PhysLayer.ShutDown();
                }

                DownloadButton.Invoke((MethodInvoker) delegate
                {
                    if (listBox1.Text != "")
                    {
                        DownloadButton.Enabled = true;
                    }
                });

                Thread.Sleep(1000);
            }
        }