/// <summary> /// 发送文件,会阻塞线程,发完返回TRUE,没发完返回FALSE /// </summary> /// <param name="file">需要发送的文件的文件描述对象</param> public void SendFile(FileDescription file) { Sending = true; try{ FileStream f = new FileStream(file.getFullPath(), FileMode.Open, FileAccess.Read, FileShare.ReadWrite); byte[] data = GetDataHead('d', f.Length); MaxSize = data.Length + f.Length; SendSize = 0; MemoryStream tmp = new MemoryStream(data); byte[] tmpsend = new byte[15]; tmp.Read(tmpsend, 0, 15); client.GetStream().Write(tmpsend, 0, tmpsend.Length); SendSize = SendSize + tmpsend.Length; tmp.Dispose(); tmp.Close(); while (SendSize < MaxSize) { byte[] send = new byte[MaxSize - SendSize < SendSizeBag ? (MaxSize - SendSize) : SendSizeBag]; f.Read(send, 0, send.Length); client.GetStream().Write(send, 0, send.Length); SendSize = SendSize + send.Length; ChangeState("正在发送文件.." + File.getName() + "..." + SendSize + "/" + MaxSize); onFileTransferMissionAcc?.Invoke(this, 100.0 * (Convert.ToDouble(SendSize) / MaxSize)); } ChangeState("文件发送完成"); MaxSize = -1; SendSize = 0; } catch { Stop(); } Sending = false; }
/// <summary> /// 从文件描述字符串集合中获取所有文件描述的数组 /// </summary> /// <param name="fstring"></param> /// <returns></returns> public List <FileDescription> GetFileDescriptionFromString(string fstring, string windowspath) { return(FileDescription.getFileDescriptionFromString(fstring, windowspath)); }
/// <summary> /// 接收文本型消息的处理函数 /// </summary> private void GetStringMessage(string msg) { if (msg.IndexOf('@') != -1) { string[] rmsg = msg.Split('@'); if (rmsg[0] == "GETDIRLIST") { ChangeState("正在获取目录列表"); string send = Form1.GetDirList();//只有服务端有 if (send == "") { ChangeState("获取目录列表失败"); SendData("DIRLIST@FALSE"); } else { ChangeState("获取目录列表成功,正在发送"); SendData("DIRLIST@" + send); } } if (rmsg[0] == "GETDIR") { ChangeState("正在获取" + rmsg[1] + "的文件结构"); string send = Form1.GetDirFilesFromDirName(rmsg[1]);//只有服务端有 if (send == "NULL") { ChangeState("获取" + rmsg[1] + "的文件失败"); SendData("DIR@" + rmsg[1] + "@FALSE"); } else { ChangeState("获取" + rmsg[1] + "的文件成功"); SendData("DIR@" + rmsg[1] + "@" + send); FileDes = GetFileDescriptionFromString(send, downloadPath + "\\" + rmsg[1]); //ChangeState("目录[" + rmsg[1] + "]的文件结构已发送,共" + FileDes.Count().ToString() + "个文件"); } } if (rmsg[0] == "GETFILE") { ChangeState("正在获取文件:" + rmsg[1]); File = null; foreach (FileDescription i in FileDes) { if (i.getFullName() == rmsg[1]) { File = i; break; } } if (File == null) { ChangeState("获取文件:" + rmsg[1] + "失败,该文件不存在"); SendData("FILE@" + rmsg[1] + "@FALSE"); } else { ChangeState("获取文件:" + rmsg[1] + "成功,准备发送"); SendFile(File); } } if (rmsg[0] == "DIR") { ChangeState("收到关于[" + rmsg[1] + "]的请求回复"); if (rmsg[2] == "FALSE") { ChangeState("请求失败"); } else { ChangeState(rmsg[2]); FileDes = GetFileDescriptionFromString(rmsg[2], downloadPath); ChangeState("文件数量:" + FileDes.Count().ToString()); onDirFile?.Invoke(this); } } if (rmsg[0] == "FILE") { ChangeState("收到关于文件[" + rmsg[1] + "]的请求回复"); File = null; if (rmsg[2] == "FALSE") { ChangeState("请求失败"); } else { ChangeState("回复内容:" + rmsg[2]); } } } onDataString?.Invoke(this, msg); }