Esempio n. 1
0
        /// <summary>
        /// 准备数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="parameter"></param>
        private void Begin <T>(T parameter)
        {
            OperatorResult        = new DataResult();
            DataId                = Guid.NewGuid().ToString();
            OperatorResult.DataId = DataId;
            OperatorResult.Action = ActionId;

            var result = ProcessFactory.ProcessToString(parameter);

            DataHolder  = new StringBuilder(result);
            SpliteCount = (int)Math.Ceiling(DataHolder.Length / (double)DataLength);

            var beginDataInfo = new DataInfo {
                ActionId = ActionId, DataId = DataId, SpliteCount = SpliteCount
            };

            LogHelper.Debug(GetType(), "开始请求发送数据", LogInfo.ObjectToMessage(beginDataInfo));
            DataService.Begin(beginDataInfo);
        }
Esempio n. 2
0
        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="parameter"></param>
        /// <returns></returns>
        public DataResult PushData(DataParameter parameter)
        {
            LogHelper.Debug(GetType(), "准备发送数据", LogInfo.ObjectToMessage(parameter));


            PHPRPC_Client    client          = new PHPRPC_Client("http://localhost:4631/PullData.aspx");
            IPushDataService pushDataService = (IPushDataService)client.UseService(typeof(IPushDataService));

            //var pushDataService = AppCtx.Resolve<IPushDataService>("PushService");

            //初始化异常
            if (pushDataService == null)
            {
                LogHelper.Warn(GetType(), "获取接口失败");
                return(null);
            }

            var success       = true;
            var result        = new DataResult();
            var beginDataInfo = new DataInfo();
            var crchash       = new CrcHash();

            result.DataId     = Guid.NewGuid().ToString();
            result.Action     = parameter.Action;
            result.CrcCode    = parameter.CrcCode;
            result.DataString = parameter.DataString;

            var stream    = StreamHeandler.StringToStream(parameter.DataString);
            var zipStream = ZipCompress.Compress(stream);
            var base64    = Base64Convert.ToBase64(zipStream);
            var sb        = new StringBuilder(StreamHeandler.StreamToString(base64));
            var count     = (int)Math.Ceiling(sb.Length / (double)dataLength);

            result.SplitCount = count;

            #region 准备发送数据
            beginDataInfo.ActionId    = parameter.Action;
            beginDataInfo.DataId      = result.DataId;
            beginDataInfo.SpliteCount = count;

            LogHelper.Debug(GetType(), "开始请求发送数据", LogInfo.ObjectToMessage(beginDataInfo));
            pushDataService.Begin(beginDataInfo);
            #endregion

            #region 发送数据
            LogHelper.Debug(GetType(), "开始发送数据");
            for (int i = 0; i < count; i++)
            {
                var          tcs       = new TaskCompletionSource <bool>();
                WaitCallback asyncWork = _ =>
                {
                    var beginPos = i * dataLength;
                    var legth    = i == count - 1 ? sb.Length - beginPos : dataLength;
                    var data     = sb.ToString(beginPos, legth);

                    var dataSegment = new DataSegment();
                    dataSegment.DataId     = result.DataId;
                    dataSegment.Crccode    = crchash.GetHashCode(StreamHeandler.StringToStream(data));
                    dataSegment.DataString = data;
                    dataSegment.Index      = i;
                    LogHelper.Debug(GetType(), string.Format("发送数据,ID:{0},索引:{1}", result.DataId, i), LogInfo.ObjectToMessage(dataSegment));
                    pushDataService.Push(dataSegment);

                    tcs.SetResult(true);
                };
                ThreadPool.QueueUserWorkItem(asyncWork);

                var task       = tcs.Task;
                var taskResult = task.Result;
                if (!taskResult)
                {
                    success = false;
                    break;
                }
            }
            #endregion

            #region 发送完成
            var endResult = pushDataService.End(beginDataInfo.DataId, !success);
            LogHelper.Debug(GetType(), "发送数据完成");

            if (endResult == null)
            {
                success = false;
            }
            #endregion

            return(result);
        }