/// <summary> /// 取消发送文件事件方法 /// </summary> /// <param name="md5"></param> /// <param name="remoteIP"></param> private void OnRequestCancelSendFile(string md5, IPEndPoint remoteIP) { ReceiveFileManager receiveFileManager; if (ReceiveFileManagerList.TryGetValue( md5, out receiveFileManager)) { OnFileReceiveCancel( new FileReceiveEventArgs(receiveFileManager)); lock (_receivesyncLock) { ReceiveFileManagerList.Remove(md5); receiveFileManager.Dispose(); receiveFileManager = null; } } else { FileReceiveEventArgs fe = new FileReceiveEventArgs(); fe.Tag = md5; OnFileReceiveCancel(fe); } Send( (int)Command.ResponeCancelSendFile, "OK", remoteIP); }
/// <summary> /// 接收文件事件方法 /// </summary> /// <param name="traFransfersFile"></param> /// <param name="remoteEp"></param> private void OnRecieveBuffer( TraFransfersFile traFransfersFile, IPEndPoint remoteEp) { ReceiveFileManager receiveFileManager; if (!ReceiveFileManagerList.TryGetValue( traFransfersFile.MD5, out receiveFileManager)) { return; } if (receiveFileManager != null) { ResponeTraFransfersFile responeTraFransfersFile; int size = receiveFileManager.ReceiveBuffer( traFransfersFile.Index, traFransfersFile.Buffer); if (receiveFileManager.Completed) { responeTraFransfersFile = new ResponeTraFransfersFile( traFransfersFile.MD5, size, -2); Send( (int)Command.ResponeSendFilePack, responeTraFransfersFile, remoteEp); } else { responeTraFransfersFile = new ResponeTraFransfersFile( traFransfersFile.MD5, size, receiveFileManager.GetNextReceiveIndex()); Send( (int)Command.ResponeSendFilePack, responeTraFransfersFile, remoteEp); } OnFileReceiveBuffer( new FileReceiveBufferEventArgs( receiveFileManager, traFransfersFile.Buffer.Length)); } }
/// <summary> /// 允许接收 /// </summary> /// <param name="e"></param> public void AcceptReceive(RequestSendFileEventArgs e) { TraFransfersFileStart traFransfersFileStart = e.TraFransfersFileStart; IPEndPoint remoteIP = e.RemoteIP; ResponeTraFransfersFile responeTraFransfersFile; if (e.Cancel) { responeTraFransfersFile = new ResponeTraFransfersFile( traFransfersFileStart.MD5, 0, -1); Send((int)Command.ResponeSendFile, responeTraFransfersFile, remoteIP); } else { ReceiveFileManager receiveFileManager; if (!ReceiveFileManagerList.TryGetValue( traFransfersFileStart.MD5, out receiveFileManager)) { receiveFileManager = new ReceiveFileManager( traFransfersFileStart.MD5, e.Path, traFransfersFileStart.FileName, traFransfersFileStart.PartCount, traFransfersFileStart.PartSize, traFransfersFileStart.Length, remoteIP); receiveFileManager.ReceiveFileComplete += new FileReceiveCompleteEventHandler( ReceiveFileManagerReceiveFileComplete); receiveFileManager.ReceiveFileTimeout += new EventHandler(ReceiveFileManagerReceiveFileTimeout); ReceiveFileManagerList.Add( traFransfersFileStart.MD5, receiveFileManager); receiveFileManager.Start(); } responeTraFransfersFile = new ResponeTraFransfersFile( traFransfersFileStart.MD5, 0, 0); Send((int)Command.ResponeSendFile, responeTraFransfersFile, remoteIP); } }
/// <summary> /// 取消接收 /// </summary> /// <param name="md5"></param> /// <param name="remoteIP"></param> public void CancelReceive(string md5, IPEndPoint remoteIP) { ReceiveFileManager receiveFileManager; if (ReceiveFileManagerList.TryGetValue( md5, out receiveFileManager)) { Send( (int)Command.RequestCancelReceiveFile, md5, remoteIP); lock (_receivesyncLock) { ReceiveFileManagerList.Remove(md5); receiveFileManager.Dispose(); receiveFileManager = null; } } }