Example #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);
        }
Example #2
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Tabs.Visibility = Visibility.Hidden;
            string ip   = ConnectionIP.Text;
            string port = ConnectionPort.Text;

            if (client != null)
            {
                client.Close();
            }
            client = null;
            stream = null;
            FolderTreeView.Items.Clear();
            LabelPathFolder.Content = "";
            if (DownMan != null)
            {
                for (int i = 0; i < DownMan.items.Count; i++)
                {
                    DownMan.items[i].Disconnect();
                }
                DownMan.items.Clear();
                DownMan = null;
            }
            Task.Run(() => Connection(ip, port));
        }
        //CancellationTokenSource ts = new CancellationTokenSource();
        //CancellationToken ct;

        public DownloadItem(string pathOnServer, string hostname, int port, string pathFolderOnClient, DownloadManager par)
        {
            //ct = ts.Token;
            _hostName = hostname;
            _port     = port;
            parrent   = par;

            FolderOnClient      = pathFolderOnClient;
            PathOnServer        = pathOnServer;
            IsPause             = true;
            IsDownComplete      = false;
            ProgressBarProccess = 0;

            Task.Run(() => ProcessOfDownload());
        }
        public DownloadItem(DBDownItem item, string hostname, int port, string pathFolderOnClient, DownloadManager par)
        {
            //ct = ts.Token;
            _hostName = hostname;
            _port     = port;
            parrent   = par;

            if (item.FolderOnClient == null)
            {
                FolderOnClient = pathFolderOnClient;
            }
            else
            {
                FolderOnClient = item.FolderOnClient;
            }

            if (item.IsDownComplete)
            {
                if (File.Exists(item.PathOnClient))
                {
                    PathOnClient        = item.PathOnClient;
                    PathOnServer        = item.PathOnServer;
                    Name                = new FileInfo(item.PathOnClient).Name;
                    FullSize            = new FileInfo(item.PathOnClient).Length;
                    currSize            = FullSize;
                    IsDownComplete      = true;
                    IsPause             = false;
                    ProgressBarProccess = 100;
                    pauseStopIcon       = "CheckMark.png";
                    return;
                }
            }

            Task.Run(() => CheckDBFile(item));
        }