Beispiel #1
0
        void Connection(string Host, string Port)
        {
            host = Host;
            if (!int.TryParse(Port, out port))
            {
                LoadingAnim.Set(() => this.ShowMessageAsync("Error", "Connectriong port incorrect. Try again."));
                Tabs.Set(() => Tabs.Visibility = Visibility.Visible);
                return;
            }
            client = new TcpClient();
            try
            {
                client.Connect(host, port);  //подключение клиента
                stream = client.GetStream(); // получаем поток

                string message = userName;
                byte[] data    = Encoding.UTF8.GetBytes(message);
                stream.Write(data, 0, data.Length);


                int bytes = 0;
                data    = new byte[256];
                bytes   = stream.Read(data, 0, data.Length);
                message = Encoding.UTF8.GetString(data, 0, bytes);
                //MessageBox.Show(message, "Infomation", MessageBoxButton.OK, MessageBoxImage.Asterisk);
                LoadingAnim.Set(() => this.ShowMessageAsync("Infomation", message));


                DBInjector dbi = new DBInjector();
                if (dbi.Servers.ToList().Where(a => a.host == host && a.port == port).ToList().Count <= 0)
                {
                    dbi.Servers.Add(new ServerItem()
                    {
                        host = Host, port = port, DBDownItems = new List <DBDownItem>()
                    });
                    dbi.SaveChanges();
                }

                Button_Click_1(null, null);

                Thread receiveThread = new Thread(new ThreadStart(ReceiveMessage));
                receiveThread.Start();
                Tabs.Set(() => {
                    Tabs.Visibility = Visibility.Visible;
                    Title           = "Client: Connect.";
                });
                DownMan = new DownloadManager.DownloadManager(host, port, this);
                DMList.Set(() => {
                    DMList.ItemsSource            = DownMan.items;
                    SaveFolder.Text               = DownMan.DownloadFolder;
                    BtnChangeSaveFolder.IsEnabled = true;
                });
            }
            catch (Exception ex)
            {
                LoadingAnim.Set(() => this.ShowMessageAsync("Error", ex.Message));
                Tabs.Set(() => Tabs.Visibility = Visibility.Visible);
            }
            Tabs.Set(() => Tabs.SelectedIndex = 1);
        }
        public DownloadManager(string host, int port, MainWindow _parrent)
        {
            parrent = _parrent;
            _host   = host;
            _port   = port;
            //<- тут должна быть загрузка из бд
            DBInjector dbi = new DBInjector();

            if (dbi.Servers.ToList().Where(a => a.host == _host && a.port == _port).ToList().Count <= 0)
            {
                dbi.Servers.Add(new ServerItem()
                {
                    host = _host, port = _port, DBDownItems = new List <DBDownItem>()
                });
                dbi.SaveChanges();
            }
            else
            {
                int idServer = dbi.Servers.ToList().Where(a => a.host == _host && a.port == _port).FirstOrDefault().ServerItemId;
                foreach (DBDownItem item in dbi.DBDownItems.Where(a => a.ServerItemId == idServer))
                {
                    items.Add(new DownloadItem(item, _host, _port, DownloadFolder, this));
                }
            }
        }
        public void DeleteDownload()
        {
            DBInjector dbi      = new DBInjector();
            int        idServer = dbi.Servers.ToList().Where(a => a.host == _hostName && a.port == _port).FirstOrDefault().ServerItemId;

            dbi.DBDownItems.Remove(dbi.DBDownItems.FirstOrDefault(a => a.PathOnServer == PathOnServer && a.ServerItemId == idServer));
            dbi.SaveChanges();
            parrent.RemoveItem(this);
        }
 public void SaveToDataBase()
 {
     foreach (DownloadItem item in items)
     {
         DBDownItem obj = new DBDownItem()
         {
             FolderOnClient = item.FolderOnClient,
             PathOnClient   = item.PathOnClient,
             PathOnServer   = item.PathOnServer,
             FullSize       = item.FullSize,
             Name           = item.Name,
             IsDownComplete = item.IsDownComplete
         };
         DBInjector dbi = new DBInjector();
         if (dbi.Servers.ToList().Where(a => a.host == _host && a.port == _port).ToList().Count <= 0)
         {
             dbi.Servers.Add(new ServerItem()
             {
                 host = _host, port = _port, DBDownItems = new List <DBDownItem>()
             });
             dbi.SaveChanges();
         }
         int idServer = dbi.Servers.ToList().Where(a => a.host == _host && a.port == _port).FirstOrDefault().ServerItemId;
         if (dbi.DBDownItems.ToList().Where(a => a.ServerItemId == idServer && a.PathOnClient == item.PathOnClient || a.PathOnServer == item.PathOnServer).ToList().Count <= 0)
         {
             dbi.DBDownItems.Add(obj);
             dbi.SaveChanges();
             dbi.Servers.FirstOrDefault(a => a.host == _host && a.port == _port).DBDownItems.Add(obj);
             dbi.SaveChanges();
         }
         else
         {
             DBDownItem it = dbi.DBDownItems.FirstOrDefault(a => a.ServerItemId == idServer && a.PathOnClient == item.PathOnClient || a.PathOnServer == item.PathOnServer);
             it.Name           = obj.Name;
             it.FolderOnClient = obj.FolderOnClient;
             it.FullSize       = obj.FullSize;
             it.IsDownComplete = obj.IsDownComplete;
             it.PathOnClient   = obj.PathOnClient;
             it.PathOnServer   = obj.PathOnServer;
             dbi.SaveChanges();
         }
     }
 }
        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();
            }
        }