/// <summary> /// 接收方的文件进行续传;如果回复是null;说明不存在 /// </summary> /// <param name="fileLable">文件标签</param> /// <param name="stateOne">StateBase</param> /// <returns>byte[]</returns> internal byte[] FileContinue(int fileLable, StateBase stateOne) { byte[] haveDate = null; FileState state = FileLabelToState(fileLable); if (state != null && state.StateFile == 2) { haveDate = EncryptionDecryptFile.ReceiveContingueEncryption(PasswordCode._fileContinue, state); ExpandBuffer(state, stateOne); } return(haveDate); }
/// <summary> /// 扩充因要接收文件而增大的缓冲区 /// </summary> /// <param name="state">FileState</param> /// <param name="stateOne">stateOne</param> private void ExpandBuffer(FileState state, StateBase stateOne) { if (state.StateOne != stateOne) { if (stateOne.Buffer.Length != BufferSize) { stateOne.BufferBackup = stateOne.Buffer; stateOne.Buffer = new byte[BufferSize]; } state.StateOne = stateOne; } }
/// <summary> /// 内部用的进度;里面会自动计算 /// </summary> /// <param name="state">FileState</param> internal void FileProgress(FileState state) { if (state.FileLenth == state.FileOkLenth) { FileProgress(state.FileLabel, 100); } else { long haveLong = state.FileOkLenth * 100 / state.FileLenth; int haveInt = (int)haveLong; FileProgress(state.FileLabel, haveInt); } }
/// <summary> /// 得到文件的进度;返回大于0小于100的进度数;如果文件已不存在返回100;不抛异常 /// </summary> /// <param name="FileLabel">文件标签</param> /// <returns></returns> public int FileProgress(int FileLabel) { FileState state = FileLabelToState(FileLabel); if (state == null) { return(100); } long haveLong = state.FileOkLenth * 100 / state.FileLenth; int haveInt = (int)haveLong; return(haveInt); }
/// <summary> /// 发送方对一个文件包头进行加密得到一个byte[] /// </summary> /// <param name="fileSend"></param> /// <returns></returns> internal static byte[] FileHeadEncryption(FileState fileSend) { string fileName = EngineTool.StringRight(fileSend.FileName, "\\"); byte[] fileNameByte = Encoding.UTF8.GetBytes(fileName); byte[] haveDate = new byte[15 + fileNameByte.Length]; haveDate[0] = PasswordCode._fileCode; haveDate[1] = PasswordCode._sendUser; haveDate[2] = PasswordCode._fileHeadCode; ByteToDate.IntToByte(fileSend.FileLabel, 3, haveDate); ByteToDate.IntToByte(fileSend.FileLenth, 7, haveDate); fileNameByte.CopyTo(haveDate, 15); return(haveDate); }
/// <summary> /// 对文件主体进行解密; /// </summary> /// <param name="fileSend"></param> /// <param name="receiveDate"></param> /// <returns></returns> internal static byte[] FileSubjectDecrypt(FileState fileSend, byte[] receiveDate) { byte[] haveDate = null; if (fileSend.StateFile == 2)//说明我这里已经暂停了;要发一个暂停的消息给对方 { return(FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._sendStop, fileSend.FileLabel)); } haveDate = new byte[receiveDate.Length - 7]; Array.Copy(receiveDate, 7, haveDate, 0, haveDate.Length); try { fileSend.Filestream.Write(haveDate, 0, haveDate.Length); }//异常说明这个文件已经被我方取消掉;返回一个取消信息给对方 catch { return(EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileCancel, fileSend.FileLabel)); } fileSend.FileOkLenth = fileSend.FileOkLenth + haveDate.Length; haveDate = FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._dateSuccess, fileSend.FileLabel); return(haveDate); }
/// <summary> /// 暂停发送;文件不存在或在等待状态会抛出异常; /// </summary> /// <param name="FileLabel">文件标签</param> public void FileStop(int FileLabel) { FileState state = FileLabelToState(FileLabel); if (state == null) { throw new Exception("这文件已不存在"); } if (state.StateFile == 2) { throw new Exception("这个文件已处于暂停状态"); } if (state.StateFile == 0) { throw new Exception("这个文件处于等待状态;不能暂停"); } state.StateFile = 2; }
/// <summary> /// 发送文件 /// </summary> /// <param name="fileLable">文件标签</param> /// <param name="fileName">文件地址</param> /// <param name="stateOne">StateBase</param> /// <returns>形成之后的数据</returns> internal byte[] Send(ref int fileLable, string fileName, StateBase stateOne) { int fileLenth = 0; FileStream fs; try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); fileLenth = (int)fs.Length; } catch (Exception Ex) { throw new Exception(Ex.Message); } fileLable = RandomPublic.RandomNumber(16787); FileState fileState = new FileState(fileLable, fileLenth, fileName, fs); fileState.StateOne = stateOne; FS.Add(fileState); byte[] haveByte = EncryptionDecryptFile.FileHeadEncryption(fileState); return(haveByte); }
/// <summary> /// 取消发送;文件不存在会抛出异常 /// </summary> /// <param name="FileLabel">文件标签</param> public void FileCancel(int FileLabel) { try { int offset = FS.FindIndex(delegate(FileState state1) { return(state1.FileLabel == FileLabel); }); FileState state2 = FS[offset]; if (state2.Filestream != null) { state2.Filestream.Close();//移除一个文件之后把所有的一切都处理掉 } FS.RemoveAt(offset); try { int offset1 = FS.FindIndex(delegate(FileState state1) { return(state1.StateOne == state2.StateOne); }); } catch { if (state2.StateOne.Buffer.Length != state2.StateOne.BufferSize) { state2.StateOne.BufferBackup = state2.StateOne.Buffer; state2.StateOne.Buffer = new byte[state2.StateOne.BufferSize]; } } } catch { throw new Exception("文件已不存在"); } }
/// <summary> /// 接收方发过来的数据; /// </summary> /// <param name="receiveToDate">收到的数据</param> /// <param name="stateOne">StateBase</param> /// <returns>回复的数据</returns> internal byte[] ReceiveDateTO(byte[] receiveToDate, StateBase stateOne) { byte[] haveDate = null; int fileLabel = ByteToDate.ByteToInt(3, receiveToDate); byte code = receiveToDate[2]; FileState state = FileLabelToState(fileLabel); if (state == null) { if (code == PasswordCode._fileCancel) { return(null); } else { return(EncryptionDecryptFile.FileSevenEncryption(PasswordCode._sendUser, PasswordCode._fileCancel, fileLabel)); } } else { switch (code) { case PasswordCode._fileOk: //对方同意接收文件 Thread.Sleep(500); state.StateFile = 1; haveDate = EncryptionDecryptFile.FileSubjectEncryption(state, stateOne.BufferSize); SendMust.FileStartOn(fileLabel); //第一次发送用原来尺寸 break; case PasswordCode._fileRefuse: //对方拒绝接收文件 FileRemove(fileLabel); SendMust.FileRefuse(fileLabel); break; case PasswordCode._dateSuccess: //主体部分数据发送成功;准备发送下一批 haveDate = EncryptionDecryptFile.FileSubjectEncryption(state, BufferSize); SendMust.FileProgress(state); if (haveDate == null)//说明这个文件已经发送成功了 { FileRemove(fileLabel); SendMust.SendSuccess(fileLabel); } break; case PasswordCode._fileCancel: //对方已经取消了这个文件; FileRemove(fileLabel); SendMust.FileCancel(fileLabel); break; case PasswordCode._sendStop: //对方暂停发送 FileStopIn(state, SendMust); break; case PasswordCode._fileContinue: //对方发过来一个续传的确认信息;你是否同意; state.StateOne = stateOne; //有可能stateOne会有变化 FileStopIn(state, SendMust); //对方已经暂停了这个文件;我这边肯定也要先暂停掉 bool orStop = SendMust.FileOrNotContingue(fileLabel); //让客户确认;是否续传 if (orStop) { state.StateFile = 1; state.FileOkLenth = ByteToDate.ByteToLong(7, receiveToDate); state.Filestream.Position = state.FileOkLenth; //设置流的当前读位置 haveDate = EncryptionDecryptFile.FileSubjectEncryption(state, stateOne.BufferSize); if (haveDate != null) { haveDate[2] = PasswordCode._fileContinueOk; SendMust.FileContinue(fileLabel); } } else { haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._sendUser, PasswordCode._fileContinueNo, fileLabel); } break; case PasswordCode._fileContinueOk: //对方同意续传 state.FileOkLenth = ByteToDate.ByteToLong(7, receiveToDate); state.StateFile = 1; state.Filestream.Position = state.FileOkLenth; //设置流的当前读位置 haveDate = EncryptionDecryptFile.FileSubjectEncryption(state, stateOne.BufferSize); SendMust.FileContinue(fileLabel); break; case PasswordCode._fileContinueNo: //对方拒绝续传 SendMust.FileNoContinue(fileLabel); FileStopIn(state, SendMust); break; } } return(haveDate); }
/// <summary> /// 发送方发过来的数据; /// </summary> /// <param name="receiveToDate">收到的数据</param> /// <param name="stateOne">StateBase</param> /// <returns>需要回复的数据</returns> internal byte[] ReceiveDateTO(byte[] receiveToDate, StateBase stateOne) { byte[] haveDate = null; int fileLabel = ByteToDate.ByteToInt(3, receiveToDate); byte code = receiveToDate[2]; if (code == PasswordCode._fileHeadCode)//说明发送过来的是文件是否传输的包头信息 { FileStream fs = null; FileState stateHead = EncryptionDecryptFile.FileHeadDecrypt(receiveToDate, fs); string fileName = ReceiveMust.ReceiveOrNo(stateHead.FileLabel, stateHead.FileName, stateHead.FileLenth); if (fileName == "") { haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileRefuse, fileLabel); } else { fs = EngineTool.FileStreamWrite(fileName); if (fs == null) { haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileRefuse, fileLabel); } else { ExpandBuffer(stateHead, stateOne); stateHead.FileName = fileName; stateHead.Filestream = fs; stateHead.StateFile = 1; FS.Add(stateHead); haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileOk, fileLabel); } } return(haveDate); } FileState state = FileLabelToState(fileLabel); if (state == null) { if (code == PasswordCode._fileCancel) { return(null); } else { return(EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileCancel, fileLabel)); } } else { switch (code) { case PasswordCode._fileSubjectCode: //收到的是文件主体代码 haveDate = EncryptionDecryptFile.FileSubjectDecrypt(state, receiveToDate); ReceiveMust.FileProgress(state); //输出文件进度 if (state.FileOkLenth >= state.FileLenth) //说明文件接收完成了 { FileRemove(fileLabel); ReceiveMust.ReceiveSuccess(fileLabel); } break; case PasswordCode._fileCancel: //对方已经取消了这个文件; FileRemove(fileLabel); ReceiveMust.FileCancel(fileLabel); break; case PasswordCode._sendStop: //对方暂停发送 FileStopIn(state, ReceiveMust); break; case PasswordCode._fileContinue: //对方发过来一个续传的确认信息;你是否同意; ExpandBuffer(state, stateOne); //有可能是stateOne换过了; FileStopIn(state, ReceiveMust); //对方已经暂停了这个文件;我这边肯定也要先暂停掉 bool orStop = ReceiveMust.FileOrNotContingue(fileLabel); //让客户确认;是否续传 if (orStop) { haveDate = EncryptionDecryptFile.ReceiveContingueEncryption(PasswordCode._fileContinueOk, state); state.StateFile = 1; ReceiveMust.FileContinue(fileLabel); } else { haveDate = EncryptionDecryptFile.FileSevenEncryption(PasswordCode._receiveUser, PasswordCode._fileContinueNo, fileLabel); } break; case PasswordCode._fileContinueOk: //对方同意续传 state.StateFile = 1; ReceiveMust.FileContinue(fileLabel); haveDate = EncryptionDecryptFile.FileSubjectDecrypt(state, receiveToDate); if (state.FileOkLenth >= state.FileLenth) //说明文件接收完成了 { FileRemove(fileLabel); ReceiveMust.ReceiveSuccess(fileLabel); } break; case PasswordCode._fileContinueNo: //对方拒绝续传 ReceiveMust.FileNoContinue(fileLabel); FileStopIn(state, ReceiveMust); break; } } return(haveDate); }