/// <summary> /// 对包头文件进行解密 /// </summary> /// <param name="fileDate"></param> /// <param name="fs"></param> /// <returns></returns> internal static FileState FileHeadDecrypt(byte[] fileDate, FileStream fs) { int lable = ByteToDate.ByteToInt(3, fileDate); long fileLenth = ByteToDate.ByteToLong(7, fileDate); byte[] fileNameByte = new byte[fileDate.Length - 15]; Array.Copy(fileDate, 15, fileNameByte, 0, fileNameByte.Length); string fileName = Encoding.UTF8.GetString(fileNameByte); FileState haveState = new FileState(lable, fileLenth, fileName, fs); return(haveState); }
/// <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); }