/// <summary> /// 文件数据分析处理中心;如果是发送方发过来的数据让接收方去处理 /// </summary> /// <param name="fileDate">文件数据</param> /// <param name="stateOne">StateBase</param> /// <returns>处理以后的数据</returns> internal static byte[] ReceiveDateTO(byte[] fileDate, TransmitData stateOne) { if (fileDate.Length < 7) { return(null); } byte[] haveDate = null; byte code = fileDate[1]; if (code == CipherCode._receiveUser) { if (fileSend == null)//这里没有开启;直接让对方取消 { haveDate = EncDecFile.FileSevenEncryption(CipherCode._sendUser, CipherCode._fileCancel, ByteToDate.ByteToInt(3, fileDate)); } else { haveDate = fileSend.ReceiveDateTO(fileDate, stateOne); } } else if (code == CipherCode._sendUser) { if (fileReceive == null)//这里没有开启;直接让对方取消 { haveDate = EncDecFile.FileSevenEncryption(CipherCode._receiveUser, CipherCode._fileCancel, ByteToDate.ByteToInt(3, fileDate)); } else { haveDate = fileReceive.ReceiveDateTO(fileDate, stateOne); } } return(haveDate); }
/// <summary> /// 发送方的文件进行续传;如果回复是null;说明不存在 /// </summary> /// <param name="fileLable">文件标签</param> /// <param name="stateOne">StateBase</param> /// <returns>byte[]</returns> internal byte[] FileContinue(int fileLable, TransmitData stateOne) { byte[] haveDate = null; FileState state = FileLabelToState(fileLable); if (state != null && state.StateFile == 2) { haveDate = EncDecFile.FileSevenEncryption(CipherCode._sendUser, CipherCode._fileContinue, fileLable); state.StateOne = stateOne;//可以续传;用新的stateOne代替旧的 } return(haveDate); }
/// <summary> /// 发送方发过来的数据; /// </summary> /// <param name="receiveToDate">收到的数据</param> /// <param name="stateOne">StateBase</param> /// <returns>需要回复的数据</returns> internal byte[] ReceiveDateTO(byte[] receiveToDate, TransmitData stateOne) { byte[] haveDate = null; int fileLabel = ByteToDate.ByteToInt(3, receiveToDate); byte code = receiveToDate[2]; if (code == CipherCode._fileHeadCode)//说明发送过来的是文件是否传输的包头信息 { FileStream fs = null; FileState stateHead = EncDecFile.FileHeadDecrypt(receiveToDate, fs); string fileName = ReceiveMust.ReceiveOrNo(stateHead.FileLabel, stateHead.FileName, stateHead.FileLenth); if (fileName == "") { haveDate = EncDecFile.FileSevenEncryption(CipherCode._receiveUser, CipherCode._fileRefuse, fileLabel); } else { fs = CommonMethod.FileStreamWrite(fileName); if (fs == null) { haveDate = EncDecFile.FileSevenEncryption(CipherCode._receiveUser, CipherCode._fileRefuse, fileLabel); } else { ExpandBuffer(stateHead, stateOne); stateHead.FileName = fileName; stateHead.Filestream = fs; stateHead.StateFile = 1; FS.Add(stateHead); haveDate = EncDecFile.FileSevenEncryption(CipherCode._receiveUser, CipherCode._fileOk, fileLabel); } } return(haveDate); } FileState state = FileLabelToState(fileLabel); if (state == null) { if (code == CipherCode._fileCancel) { return(null); } else { return(EncDecFile.FileSevenEncryption(CipherCode._receiveUser, CipherCode._fileCancel, fileLabel)); } } else { switch (code) { case CipherCode._fileSubjectCode: //收到的是文件主体代码 haveDate = EncDecFile.FileSubjectDecrypt(state, receiveToDate); ReceiveMust.FileProgress(state); //输出文件进度 if (state.FileOkLenth >= state.FileLenth) //说明文件接收完成了 { FileRemove(fileLabel); ReceiveMust.ReceiveSuccess(fileLabel); } break; case CipherCode._fileCancel: //对方已经取消了这个文件; FileRemove(fileLabel); ReceiveMust.FileCancel(fileLabel); break; case CipherCode._sendStop: //对方暂停发送 FileStopIn(state, ReceiveMust); break; case CipherCode._fileContinue: //对方发过来一个续传的确认信息;你是否同意; ExpandBuffer(state, stateOne); //有可能是stateOne换过了; FileStopIn(state, ReceiveMust); //对方已经暂停了这个文件;我这边肯定也要先暂停掉 bool orStop = ReceiveMust.FileOrNotContingue(fileLabel); //让客户确认;是否续传 if (orStop) { haveDate = EncDecFile.ReceiveContingueEncryption(CipherCode._fileContinueOk, state); state.StateFile = 1; ReceiveMust.FileContinue(fileLabel); } else { haveDate = EncDecFile.FileSevenEncryption(CipherCode._receiveUser, CipherCode._fileContinueNo, fileLabel); } break; case CipherCode._fileContinueOk: //对方同意续传 state.StateFile = 1; ReceiveMust.FileContinue(fileLabel); haveDate = EncDecFile.FileSubjectDecrypt(state, receiveToDate); if (state.FileOkLenth >= state.FileLenth) //说明文件接收完成了 { FileRemove(fileLabel); ReceiveMust.ReceiveSuccess(fileLabel); } break; case CipherCode._fileContinueNo: //对方拒绝续传 ReceiveMust.FileNoContinue(fileLabel); FileStopIn(state, ReceiveMust); break; } } return(haveDate); }
/// <summary> /// 接收方发过来的数据; /// </summary> /// <param name="receiveToDate">收到的数据</param> /// <param name="stateOne">StateBase</param> /// <returns>回复的数据</returns> internal byte[] ReceiveDateTO(byte[] receiveToDate, TransmitData stateOne) { byte[] haveDate = null; int fileLabel = ByteToDate.ByteToInt(3, receiveToDate); byte code = receiveToDate[2]; FileState state = FileLabelToState(fileLabel); if (state == null) { if (code == CipherCode._fileCancel) { return(null); } else { return(EncDecFile.FileSevenEncryption(CipherCode._sendUser, CipherCode._fileCancel, fileLabel)); } } else { switch (code) { case CipherCode._fileOk: //对方同意接收文件 Thread.Sleep(500); state.StateFile = 1; haveDate = EncDecFile.FileSubjectEncryption(state, stateOne.BufferSize); SendMust.FileStartOn(fileLabel); //第一次发送用原来尺寸 break; case CipherCode._fileRefuse: //对方拒绝接收文件 FileRemove(fileLabel); SendMust.FileRefuse(fileLabel); break; case CipherCode._dateSuccess: //主体部分数据发送成功;准备发送下一批 haveDate = EncDecFile.FileSubjectEncryption(state, BufferSize); SendMust.FileProgress(state); if (haveDate == null) //说明这个文件已经发送成功了 { FileRemove(fileLabel); SendMust.SendSuccess(fileLabel); } break; case CipherCode._fileCancel: //对方已经取消了这个文件; FileRemove(fileLabel); SendMust.FileCancel(fileLabel); break; case CipherCode._sendStop: //对方暂停发送 FileStopIn(state, SendMust); break; case CipherCode._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 = EncDecFile.FileSubjectEncryption(state, stateOne.BufferSize); if (haveDate != null) { haveDate[2] = CipherCode._fileContinueOk; SendMust.FileContinue(fileLabel); } } else { haveDate = EncDecFile.FileSevenEncryption(CipherCode._sendUser, CipherCode._fileContinueNo, fileLabel); } break; case CipherCode._fileContinueOk: //对方同意续传 state.FileOkLenth = ByteToDate.ByteToLong(7, receiveToDate); state.StateFile = 1; state.Filestream.Position = state.FileOkLenth; //设置流的当前读位置 haveDate = EncDecFile.FileSubjectEncryption(state, stateOne.BufferSize); SendMust.FileContinue(fileLabel); break; case CipherCode._fileContinueNo: //对方拒绝续传 SendMust.FileNoContinue(fileLabel); FileStopIn(state, SendMust); break; } } return(haveDate); }