/// <summary> /// 文件发送/接收过程中被对方终止 /// </summary> private void fileAborted() { sendTimeOutTimer.Stop(); if (filePacker != null) { filePacker.Close(); filePacker = null; } if (fileWriter != null) { fileWriter.Close(); fileWriter = null; } safeFileNameBase64 = null; }
/// <summary> /// 完成文件收发 /// </summary> private void finishSendingOrReceivingFile() { // 关定时器 sendTimeOutTimer.Stop(); // 关文件 if (filePacker != null) { filePacker.Close(); filePacker = null; } if (fileWriter != null) { fileWriter.Close(); fileWriter = null; } ProtocalHandler protocalHandler = new ProtocalHandler(); owner.BeginInvoke(owner.stopSendingOrReceivingFileDelegate, String.Format("文件{0}的收发已完成。", protocalHandler.Base64stringToString(safeFileNameBase64))); safeFileNameBase64 = null; }
/// <summary> /// 发起方: /// 申请发送文件 /// </summary> /// <param name="fileName">文件名(含路径)</param> public void RequestToSendFile(string fileName) { // 打开文件 filePacker = new FilePacker(owner.fileName); maxSeq = filePacker.GetPackNumbers(); owner.BeginInvoke(owner.setProgressDelegate, (int)maxSeq); // 取得文件名,并将文件名编码以便使用 safeFileNameBase64 = owner.GetSafeFileName(fileName); ProtocalHandler protocalHandler = new ProtocalHandler(owner.targetName); safeFileNameBase64 = protocalHandler.StringToBase64string(safeFileNameBase64); // 封装协议文本 string protocalText = protocalHandler.Pack("file_requesttosend"); protocalText = protocalHandler.Append(protocalText, "file", safeFileNameBase64); protocalText = protocalHandler.Append(protocalText, "maxseq", maxSeq.ToString()); try { // 发送 byte[] sendbuf = Encoding.ASCII.GetBytes(protocalText); SocketAsyncEventArgs saEA = new SocketAsyncEventArgs(); saEA.Completed += new EventHandler <SocketAsyncEventArgs>( SendCompletedDoNothing); saEA.SetBuffer(sendbuf, 0, sendbuf.Length); socket.SendAsync(saEA); } catch (Exception ex) { Trace.WriteLine("异常位置:RequestToSendFile"); Trace.WriteLine(ex.Message); } }