/// <summary> /// 获取 server 文件列表 /// </summary> /// <returns>请求是否成功 bool 值</returns> private Task <bool> ListFiles() { return(Task.Run(() => { try { Logger.Log("Requesting directory : " + RemoteDirectory, LogLevel.Info); SocketClient client = SocketFactory.GenerateConnectedSocketClient(); client.SendBytes(SocketPacketFlag.DirectoryRequest, RemoteDirectory); client.ReceiveBytesWithHeaderFlag(SocketPacketFlag.DirectoryResponse, out byte[] recv_bytes); this.fileClasses = SocketFileInfo.BytesToList(recv_bytes); client.Close(); this.Dispatcher.Invoke(() => { this.ListViewFile.ItemsSource = this.fileClasses; this.TextRemoteDirectory.Text = RemoteDirectory; }); return true; } catch (Exception ex) { string msg = "Requesting remote directory \"" + RemoteDirectory + "\" failure : " + ex.Message; Logger.Log(msg, LogLevel.Warn); System.Windows.Forms.MessageBox.Show(msg); return false; } })); }
/// <summary> /// 响应对方的 Directory 列表查询, 文件夹不存在或权限异常返回message字符串处理 /// 参数 bytes 为接收byte流的内容信息 /// client : SocketPacketFlag.DirectoryRequest + path bytes(UTF-8) /// server : SocketPacketFlag.DirectoryResponse + List<SocketFileInfo> -> bytes /// or : SocketPacketFlag.DirectoryException + err_msg /// </summary> /// <param name="client"></param> /// <param name="bytes"></param> private void ResponseDirectory(Socket client, byte[] bytes) { List <SocketFileInfo> fileClasses = new List <SocketFileInfo>(); string err_msg = ""; /// get SocketFileInfo[] try { if ((GetIdentity(client) & SocketIdentity.ReadFile) == 0) { throw new Exception("Socket not authenticated."); } string path = Encoding.UTF8.GetString(bytes); fileClasses = GetDirectoryAndFiles(path); } catch (Exception ex) { err_msg = "Directory response exception from server: " + ex.Message; } /// Send bytes if (string.IsNullOrEmpty(err_msg)) { SendBytes(client, SocketPacketFlag.DirectoryResponse, SocketFileInfo.ListToBytes(fileClasses)); } else { SendBytes(client, SocketPacketFlag.DirectoryException, err_msg); } }
private void unselectChoice() { if (selectedFile != null) { selectedFile = null; } if (dg_main.SelectedItem != null) { dg_main.UnselectAll(); } btn_download.IsEnabled = false; btn_deselect.IsEnabled = false; }
private void DownloadSingleTaskDirectory(FileTask task) { /// 创建本地 directory if (!Directory.Exists(task.LocalPath)) { DirectoryInfo dirInfo = new DirectoryInfo(task.LocalPath); dirInfo.Create(); } /// 获取 server 端文件列表 List <SocketFileInfo> files; try { SocketClient client = SocketFactory.GenerateConnectedSocketClient(task, 1); client.SendBytes(SocketPacketFlag.DirectoryRequest, task.RemotePath); client.ReceiveBytesWithHeaderFlag(SocketPacketFlag.DirectoryResponse, out byte[] recv_bytes); files = SocketFileInfo.BytesToList(recv_bytes); client.Close(); } catch (Exception) { task.Status = FileTaskStatus.Denied; return; } /// 更新 TaskList, 重新定位当前任务 UpdateTasklist(this, new UpdateUIInvokeEventArgs(new Action(() => { this.Record.RemoveTaskAt(Record.CurrentTaskIndex); int bias = Record.CurrentTaskIndex; for (int i = 0; i < files.Count; ++i) { SocketFileInfo f = files[i]; FileTask task_add = new FileTask { Route = task.Route.Copy(), IsDirectory = f.IsDirectory, Type = TransferType.Download, RemotePath = Path.Combine(task.RemotePath, f.Name), LocalPath = task.LocalPath + "\\" + f.Name, Length = f.Length, }; UpdateTaskLength(task_add); this.Record.InsertTask(bias + i, task_add); } }))); }
void client_Revice(object sender, ReviceBuffer e) { SocketFileInfo fileInfo = SocketFileInfo.DeSerialize(e.ReviceByte); using (FileStream fs = new FileStream("Socket" + fileInfo.FileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None)) { fs.Position = fileInfo.CurrentOffset; fs.Write(fileInfo.CurrentByte, 0, fileInfo.CurrentLen); fs.Flush(); fs.Close(); fs.Dispose(); } long downSize = fileInfo.CurrentOffset + fileInfo.CurrentLen; int offset = (int)(downSize * 100 / fileInfo.FileLength); if (this.IsDisposed) { return; } this.Invoke((MethodInvoker) delegate() { this.pbSocketDown.Value = offset; this.lbSocketDownPos.Text = offset + "%"; if (downSize > 1024 * 1024 * 1024) { float posLen = downSize / (1024f * 1024f * 1024f); this.lbSocketDown.Text = posLen.ToString("f1") + "G"; } else if (downSize > 1024 * 1024) { float posLen = downSize / (1024f * 1024f); this.lbSocketDown.Text = posLen.ToString("f1") + "M"; } else if (downSize > 1024) { float posLen = downSize / 1024f; this.lbSocketDown.Text = posLen.ToString("f1") + "K"; } else { this.lbSocketDown.Text = downSize.ToString("f1") + "B"; } }); }
private void dg_main_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (cbb_fileserver.Items.Count > 0 && dg_main.Items.Count > 0) { int selectedRow = dg_main.SelectedIndex; if (selectedRow < 0) { return; } if (msl.socketFileManagers[cbb_fileserver.SelectedIndex].FileList != null) { if (msl.socketFileManagers[cbb_fileserver.SelectedIndex].FileList[selectedRow] != null) { SocketFileInfo tmp = msl.socketFileManagers[cbb_fileserver.SelectedIndex].FileList[selectedRow]; selectedFile = new SocketFileInfo(tmp.ID, tmp.Name, tmp.Path, tmp.Type, tmp.Size, tmp.MD5); } } } btn_download.IsEnabled = true; btn_deselect.IsEnabled = true; }