/// <summary> 接收到的普通数据处中心 </summary> internal void CommonCodeManage(StateBase stateOne, StateCode stateCode) { if (stateCode == null || stateOne == null) { return; } switch (stateCode.State) { case PasswordCode._textCode: //文本信息 Send(stateOne, stateCode.ReplyDate); OnAcceptString(stateOne.IpEndPoint, stateCode.Datestring); //触发文本收到的事件 break; case PasswordCode._photographCode: //图片信息 Send(stateOne, stateCode.ReplyDate); OnAcceptByte(stateOne.IpEndPoint, stateCode.DateByte); //触发图片收到的事件 break; case PasswordCode._dateSuccess: //数据发送成功 stateOne.SendDate = null; OndateSuccess(stateOne.IpEndPoint); //对方收到触发事件 break; case 0: //说明这个数据只要直接回复给对方就可以了 Send(stateOne, stateCode.ReplyDate); break; } }
/// <summary> /// 接收到信息的回调函数 /// </summary> /// <param name="iar"></param> private void EndReceiveFromCallback(IAsyncResult iar) { int byteRead = 0; try { //完成接收 byteRead = state.WorkSocket.EndReceiveFrom(iar, ref state.RemoteEP); } catch {} if (byteRead > 0) { state.IpEndPoint = (IPEndPoint)state.RemoteEP; byte[] haveDate = ReceiveDateOne.DateOneManage(state, byteRead); //接收完成之后对数组进行重置 BeginReceiveFrom(); int havePort = UdpPortSetGet.GetPort(ref haveDate); if (havePort != 0) { state.IpEndPoint.Port = havePort; } StateCode statecode = ReceiveDateDistribution.Distribution(haveDate); codeManage(state, statecode); } else { BeginReceiveFrom(); } }
/// <summary> 数据第二层分配中心;把数据归类 </summary> internal void codeManage(StateBase stateOne, StateCode statecode) { if (statecode == null || stateOne == null) { return; } StateCode stateCode = null; switch (statecode.State) { case PasswordCode._commonCode: //普通数据信息;抛给普通Code去处理 stateCode = EncryptionDecrypt.deciphering(statecode.DateByte, stateOne); CommonCodeManage(stateOne, stateCode); break; case PasswordCode._bigDateCode: //抛给分包Code去处理 stateCode = EncryptionDecryptSeparateDate.FileDecrypt(statecode.DateByte, stateOne); CommonCodeManage(stateOne, stateCode); break; case PasswordCode._fileCode: //抛给文件处理器去处理;如果返回null就不用发送了 byte[] haveDate = FileStart.ReceiveDateTO(statecode.DateByte, stateOne); if (haveDate != null) { Send(stateOne, haveDate); } break; } }
/// <summary> /// 当Tcp收到数据全部在这里处理;也是数据的第一次处理 /// </summary> /// <param name="stateOne">TcpState</param> /// <param name="reciverByte">数据</param> internal void TcpDateOne(TcpState stateOne, byte[] reciverByte) { stateOne.HeartTime = DateTime.Now; List <byte[]> listDate = StickPackage.DecryptPackage(reciverByte, ref stateOne.Residualpackage); foreach (byte[] date in listDate) { StateCode statecode = ReceiveDateDistribution.Distribution(date); TcpCodeManage(stateOne, statecode); } }
/// <summary> 服务器向客户端发送文本数据 </summary> internal void sendMessage(StateBase stateOne, string data) { if (stateOne == null) { return; } StateCode stateCode = new StateCode(PasswordCode._textCode, data); byte[] sendDate = EncryptionDecrypt.encryption(stateCode, stateOne); stateOne.SendDate = sendDate; Send(stateOne, sendDate); }
/// <summary> /// TCP协议使用的数据第二层分配中心;把数据归类; /// </summary> /// <param name="stateOne"></param> /// <param name="statecode"></param> internal void TcpCodeManage(TcpState stateOne, StateCode statecode) { if (statecode == null || stateOne == null) { return; } if (statecode.State == PasswordCode._verificationCode)//说明是暗号;抛给暗号处理中心 { byte haveDate = EncryptionDecryptVerification.DecryptVerification(statecode.DateByte); VerificationCodeManage(stateOne, haveDate); } else { codeManage(stateOne, statecode); } }
/// <summary> /// 一个分配函数;返回是NULL说明不是本系统的数据;违法数据等等 /// </summary> /// <param name="date">数据</param> /// <returns>StateCode</returns> internal static StateCode Distribution(byte[] date) { StateCode statecode = null; if (date.Length < 2) { return(statecode); } byte headcode = date[0]; if (headcode == PasswordCode._fileCode || headcode == PasswordCode._bigDateCode || headcode == PasswordCode._commonCode || headcode == PasswordCode._verificationCode) { statecode = new StateCode(headcode, date); } return(statecode); }
/// <summary> /// 对文本和图片数据进行加密;如果长度超过限制,直接抛给文件处理中心 /// </summary> /// <param name="stateCode">StateCode</param> /// <param name="state">StateBase</param> /// <returns>要发送的数据</returns> internal static byte[] encryption(StateCode stateCode, StateBase state) { byte[] returnByte = null; if (stateCode.State == PasswordCode._textCode) { byte textCode = PasswordCode._textCode; byte[] date = Encoding.UTF8.GetBytes(stateCode.Datestring); returnByte = encryptionTemporary(date, textCode, state); } else if (stateCode.State == PasswordCode._photographCode) { byte photographCode = stateCode.State; returnByte = encryptionTemporary(stateCode.DateByte, photographCode, state); } return(returnByte); }
/// <summary> /// 对文本和图片数据进行解密; /// </summary> /// <param name="date">接收到的数据</param> /// <param name="state">StateBase</param> /// <returns>StateCode</returns> internal static StateCode deciphering(byte[] date, StateBase state) { StateCode stateCode = null; if (date.Length < 6) { return(stateCode);//收到的数据不正确 } byte headDate = date[1]; if (headDate == PasswordCode._textCode || headDate == PasswordCode._photographCode)//解密到的是文本数据 { int SendDateLabel = 0; byte[] dateAll = ByteToDate.OffsetDecrypt(date, out SendDateLabel, 2); byte[] ReplyDate = ByteToDate.CombinationTwo(PasswordCode._commonCode, PasswordCode._dateSuccess, SendDateLabel);//直接回复发送成功 if (headDate == PasswordCode._textCode) { string str = Encoding.UTF8.GetString(dateAll); stateCode = new StateCode(PasswordCode._textCode, str, ReplyDate); //解析出来是文本数据 } else { stateCode = new StateCode(PasswordCode._photographCode, dateAll, ReplyDate); //解释出来是图片数据 } } else if (headDate == PasswordCode._dateSuccess) //数据成功或重发 { int SendDateLabel = ByteToDate.ByteToInt(2, date); //找出已发数据的标签 if (headDate == PasswordCode._dateSuccess) { stateCode = new StateCode(headDate); if (SendDateLabel == state.SendDateLabel) { state.SendDate = null; } //已经成功对已发数据进行删除 } } return(stateCode); }
/// <summary> /// 当收到是分组数据代码的到这里来统一处理 /// </summary> /// <param name="date">数据</param> /// <param name="state">StateBase</param> /// <returns>StateCode</returns> internal static StateCode FileDecrypt(byte[] date, StateBase state) { StateCode stateCode = null; if (date.Length < 6) { return(stateCode); } byte headDate = date[1]; if (headDate == PasswordCode._fileAgreeReceive) {//对方同意接收文件;我应该怎么处理 int FileLabel = ByteToDate.ByteToInt(2, date); if (state.SendFile != null && state.SendFile.FileLabel == FileLabel) { byte[] SendSubjectDate = FileGetSendDate(state); if (SendSubjectDate == null) { stateCode = new StateCode(PasswordCode._dateSuccess); } else { stateCode = new StateCode(SendSubjectDate);//直接发送 } } } else if (headDate == PasswordCode._dateSuccess) {//对方已经接收到数据 int FileLabel = ByteToDate.ByteToInt(2, date); if (state.SendFile != null && state.SendFile.FileLabel == FileLabel) { byte[] SendSubjectDate = FileGetSendDate(state); if (SendSubjectDate == null) { stateCode = new StateCode(PasswordCode._dateSuccess); } else { stateCode = new StateCode(SendSubjectDate);//直接发送 } } } //上面是发送方接收要做的;下面是接收方发送要做的事情 else if (headDate == PasswordCode._fileHeadCode) {//收到的是文件包头部分 byte whatCode = date[2]; int fileLabel = ByteToDate.ByteToInt(3, date); int fileLenth = ByteToDate.ByteToInt(7, date); state.ReceiveFile = new FileBase(whatCode, fileLabel, fileLenth); byte[] dateAll = new byte[6]; dateAll[0] = PasswordCode._bigDateCode; dateAll[1] = PasswordCode._fileAgreeReceive; ByteToDate.IntToByte(fileLabel, 2, dateAll); stateCode = new StateCode(dateAll); } else if (headDate == PasswordCode._fileSubjectCode) {//收到的是文件主体部分 int SendDateLabel = 0; byte[] dateAll = ByteToDate.OffsetDecrypt(date, out SendDateLabel, 2); byte[] ReplyDate = ByteToDate.CombinationTwo(PasswordCode._bigDateCode, PasswordCode._dateSuccess, state.ReceiveFile.FileLabel); if (state.ReceiveFile.FileDateAll == null) { state.ReceiveFile.FileDateAll = dateAll;//是第一次接收到主体数据 stateCode = new StateCode(ReplyDate); } else { byte[] FileDateAll = new byte[state.ReceiveFile.FileDateAll.Length + dateAll.Length]; state.ReceiveFile.FileDateAll.CopyTo(FileDateAll, 0); dateAll.CopyTo(FileDateAll, state.ReceiveFile.FileDateAll.Length); state.ReceiveFile.FileDateAll = FileDateAll; if (FileDateAll.Length == state.ReceiveFile.FileLenth) { if (state.ReceiveFile.FileClassification == PasswordCode._textCode) { string str = Encoding.UTF8.GetString(FileDateAll); stateCode = new StateCode(PasswordCode._textCode, str, ReplyDate); } else { stateCode = new StateCode(PasswordCode._photographCode, FileDateAll, ReplyDate); } state.ReceiveFile = null;//文件接收完成;释放接收器 } else { stateCode = new StateCode(ReplyDate); } } } return(stateCode); }