コード例 #1
0
 private void Window_Closed(object sender, EventArgs e)
 {
     if (DownMan != null)
     {
         DownMan.SaveToDataBase();
     }
     Disconnect();
 }
コード例 #2
0
        void CheckDBFile(DBDownItem item)
        {
            // Connecting
            client.Connect(_hostName, _port);
            PathOnServer = item.PathOnServer;
            string name = Environment.MachineName + PathOnServer;

            byte[] bytes = Encoding.UTF8.GetBytes(name);
            client.GetStream().Write(bytes, 0, bytes.Length);
            bytes = new byte[2048];
            client.GetStream().Read(bytes, 0, bytes.Length);
            string mess = Encoding.UTF8.GetString(bytes);

            // Connecting

            //Check file exist on server
            mess  = $"/FileExist {item.PathOnServer}";
            bytes = Encoding.UTF8.GetBytes(mess);
            client.GetStream().Write(bytes, 0, bytes.Length);
            bytes = new byte[2048];
            client.GetStream().Read(bytes, 0, bytes.Length);
            mess = Encoding.UTF8.GetString(bytes);
            mess = mess.Replace("\0", "");
            //Check file exist on server
            if (mess == "true")
            {
                if (File.Exists(item.PathOnClient))
                {
                    currSize      = new FileInfo(item.PathOnClient).Length;
                    IsPause       = false;
                    pauseStopIcon = "play.png";

                    //Take info about file

                    mess  = $"/GetFile {PathOnServer}";
                    bytes = Encoding.UTF8.GetBytes(mess);
                    client.GetStream().Write(bytes, 0, bytes.Length);
                    bytes = new byte[500000];

                    Task.Run(() => ServerTimeOut());
                    client.GetStream().Read(bytes, 0, bytes.Length);
                    IsTimeOut = false;

                    Emigration.EmigrationObject takedinfo;
                    takedinfo = ByteArrayToObject(bytes) as Emigration.EmigrationObject;
                    if (takedinfo.type == "file")
                    {
                        Emigration.EmigrationFileInfo efi = (Emigration.EmigrationFileInfo)takedinfo.message;
                        name     = efi.FileName;
                        FullSize = efi.Size;
                        if (DriveInfo.GetDrives().Where(a => a.Name == FolderOnClient).ToList().Count > 0)
                        {
                            PathOnClient = FolderOnClient + name;
                        }
                        else
                        {
                            PathOnClient = FolderOnClient + @"\" + name;
                        }
                        bytes = new byte[currSize];
                        client.GetStream().Read(bytes, 0, bytes.Length);
                        try
                        {
                            using (fs = new FileStream(PathOnClient, FileMode.OpenOrCreate))
                            {
                                fs.Position         = fs.Length;
                                ProgressBarProccess = CalculateValueProgressBar();
                                while (currSize < FullSize)
                                {
                                    //if (ct.IsCancellationRequested)
                                    //{
                                    //    break;
                                    //}
                                    if (IsPause)
                                    {
                                        bytes = new byte[1];
                                        client.GetStream().Read(bytes, 0, bytes.Length);

                                        fs.WriteByte(bytes[0]);

                                        currSize++;
                                        if (currSize % 1024 == 0)
                                        {
                                            ProgressBarProccess = CalculateValueProgressBar();
                                        }
                                    }
                                    else
                                    {
                                        Thread.Sleep(1000);
                                    }
                                }
                                ProgressBarProccess = 100;
                                IsDownComplete      = true;
                                pauseStopIcon       = "CheckMark.png";
                                parrent.SaveToDataBase();
                                client.Close();
                                client = null;
                            }
                        }
                        catch (Exception ex)
                        {
                            parrent.Message($"Can't open file {PathOnClient}, probably file busy.", "Client: ERROR");
                            Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() => DeleteDownload()));
                        }
                    }
                }
                else
                {
                    PathOnServer        = item.PathOnServer;
                    IsPause             = false;
                    pauseStopIcon       = "play.png";
                    IsDownComplete      = false;
                    ProgressBarProccess = 0;
                    client.Close();
                    client = new TcpClient();
                    ProcessOfDownload();
                }
            }
            else
            {
                DBInjector dbi = new DBInjector();
                dbi.DBDownItems.Remove(dbi.DBDownItems.FirstOrDefault(a => a.PathOnServer == item.PathOnServer && a.ServerItemId == item.ServerItemId));
                dbi.SaveChanges();
                parrent.Message($"File {PathOnServer} is not exist on server...", "Server: Error");
                DeleteDownload();
            }
        }