Ejemplo n.º 1
0
        /// <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);
            }
        }
Ejemplo n.º 2
0
        /// <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);
        }
Ejemplo n.º 3
0
 /// <summary>
 /// 处理数据
 /// </summary>
 /// <param name="source"></param>
 private void ProcessClient(SrvDataSource source, ClientRequest client)
 {
     if (!CipherReply.Singleton.Session.ContainsKey(client.SessionID))
     {
         //已经离线处理
         LoginOut(source, client.TaskID, client.AESKey);
     }
     else
     {
         //开启线程处理,不影响接收
         Task.Factory.StartNew(() =>
         {
             SubmitTask(source, client);
         });
     }
 }
Ejemplo n.º 4
0
        /// <summary>
        /// 处理请求任务
        /// </summary>
        /// <param name="source"></param>
        /// <param name="client"></param>
        private void SubmitTask(SrvDataSource source, ClientRequest client)
        {
            Console.WriteLine("Submint:" + Thread.CurrentThread.ManagedThreadId);
            var data = source.Message as byte[];
            //获取传递的请求
            DBTransfer model = SerializerFactory <CommonSerializer> .Deserialize <DBTransfer>(data);

            if (model.TimeOut == 0)
            {
                //不超时
                var result = DBAcessSrv.Singleton.Execete(Interlocked.Increment(ref rspID), model);
                ResponseResult(source, result, model, client);
            }
            else
            {
                //超时请求
                var result = ExecuteTimeOut(source, client, model);
                ResponseResult(source, result, model, client);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 监视超时处理
        /// </summary>
        /// <param name="source"></param>
        /// <param name="client"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        private RequestResult ExecuteTimeOut(SrvDataSource source, ClientRequest client, DBTransfer model)
        {
            int timeOut = model.TimeOut < 0 ? TimeOut : model.TimeOut * 1000; //转换成毫秒
            ManualResetEventSlim slim   = new ManualResetEventSlim(false);
            RequestResult        result = null;

            ThreadPool.QueueUserWorkItem((item) =>
            {
                //提交线程池
                result = DBAcessSrv.Singleton.Execete(Interlocked.Increment(ref rspID), model);
                slim.Set();  //设置
            });
            if (!slim.Wait(timeOut))
            {
                //等待超时时间
                result           = new RequestResult();
                result.Error     = ErrorCode.TimeOut;
                result.RequestID = client.TaskID;
            }
            return(result);
        }
Ejemplo n.º 6
0
        /// <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);
        }