/// <summary> /// 解析数据 /// </summary> /// <param name="source"></param> private void AnalysisRequest(SrvDataSource source) { byte[] req = source.Message as byte[]; //ID,数据,AES if (req[0] == 1) { //登陆请求 byte[] bytes = new byte[req.Length - 1]; Array.Copy(req, 1, bytes, 0, bytes.Length); ClientLoginRequest request = StructManager.BytesToStruct <ClientLoginRequest>(bytes); var rsp = CryptoServer.Singleton.ResponseLogin(request, ""); var result = SerializerFactory <CommonSerializer> .Serializer(rsp); source.Rsponse(result); } else { byte[] bytes = new byte[req.Length - 1]; Array.Copy(req, 1, bytes, 0, bytes.Length); //解析客户端数据 //获取解密后的数据已经AES秘钥 var creq = CryptoServer.Singleton.ProcessRequest(bytes); source.Message = creq.data; creq.data = null; ProcessClient(source, creq); } }
/// <summary> /// 登录重置 /// </summary> /// <param name="source"></param> /// <param name="taskID"></param> /// <param name="aesKey"></param> private void LoginOut(SrvDataSource source, long taskID, string aesKey) { RequestResult result = new RequestResult(); result.Error = ErrorCode.Exception; result.ReslutMsg = "请登录"; result.Result = null; result.ID = Interlocked.Increment(ref rspID); result.RequestID = taskID; byte[] buffer = SerializerFactory <CommonSerializer> .Serializer(result); buffer = CryptoServer.Singleton.EnCrypto(buffer, aesKey);//AES 加密回传 source.Rsponse(buffer); }
/// <summary> /// 准备数据回传 /// </summary> /// <param name="channel"></param> /// <param name="result"></param> /// <param name="model"></param> /// <param name="client"></param> private void ResponseResult(SrvDataSource source, RequestResult result, DBTransfer model, ClientRequest client) { result.RequestID = client.TaskID; result.ID = client.SessionID; if (model.IsJson && result.Error == ErrorCode.Sucess && model.IsQuery) { CommonSerializer common = new CommonSerializer(); result.Result = common.JSONObjectToString(result.Result); } byte[] buffer = null; try { if (result.Result is DataTable) { DataTable dt = result.Result as DataTable; DataSetModel dataSet = new DataSetModel() { Content = dt.DataSet.GetXml(), Schema = dt.DataSet.GetXmlSchema() }; result.Result = dataSet; } buffer = SerializerFactory <CommonSerializer> .Serializer(result); } catch (Exception ex) { result.Error = ErrorCode.Exception; result.ReslutMsg = "序列化失败," + ex.Message; result.Result = null; buffer = SerializerFactory <CommonSerializer> .Serializer(result); } buffer = CryptoServer.Singleton.EnCrypto(buffer, client.AESKey);//AES 加密回传 //source.Context.Send(buffer) source.Rsponse(buffer); Console.WriteLine("回传:" + buffer.Length); }