Ejemplo n.º 1
0
        private void PrimitiveReqData(TransferReqDataSchduleInfoManager reqDataSchduleInfoManager, TransferReqDataSchduleInfo currentReqDataSchduleInfo)
        {
            var endPoint = reqDataSchduleInfoManager.NotifyMessage.SrcEndPoint;
            int repeatCount = 0;
            int mtu, rto;

            while (repeatCount++ < this._config.MtuRepeatMaxCount)
            {
                mtu = TransferParaManager.GetMtu(endPoint);
                rto = TransferParaManager.GetRto(endPoint);
                var ep = currentReqDataSchduleInfo.EndPostion - currentReqDataSchduleInfo.Postion;
                if (mtu > ep)
                {
                    mtu = (int)ep;
                }

                if (mtu <= 0)
                {
                    break;
                }

                var reqMessage      = new ResourceRequestMessage(reqDataSchduleInfoManager.NotifyMessage.Header.Rid, currentReqDataSchduleInfo.Postion, mtu);
                var buffer          = reqMessage.GenerateBuffer();
                var eventHandleId   = reqMessage.ContextId;
                var eventHandleInfo = AutoEventWaitHandleManager.CreateEventWaitHandle(eventHandleId, reqDataSchduleInfoManager.NotifyMessage.Timeout);
                try
                {
                    //_ht[$"{reqMessage.Header.Rid}_{reqMessage.ContextId}"] = _watch.ElapsedMilliseconds;
                    this._net.Send(buffer, endPoint);
                    if (eventHandleInfo.EventWaitHandle.WaitOne(rto))
                    {
                        currentReqDataSchduleInfo.UpdatePostion(mtu);
                        if (currentReqDataSchduleInfo.IsTransferCompleted())
                        {
                            //Loger.Warn($"线程[{System.Threading.Thread.CurrentThread.Name}]传输[Rid:{reqDataSchduleInfoManager.NotifyMessage.Header.Rid}],[{currentReqDataSchduleInfo.BeginPostion}-{currentReqDataSchduleInfo.EndPostion}]完成");
                        }
                    }
                    else
                    {
                        TransferParaManager.RecordMtuAndRto(endPoint, mtu, rto, true);
                        mtu = TransferParaManager.AdjustDownMtu(endPoint);
                        rto = TransferParaManager.AdjustUpRto(endPoint);
                    }
                }
                finally
                {
                    AutoEventWaitHandleManager.RemoveEventWaitHandle(eventHandleId);
                    eventHandleInfo.EventWaitHandle.Dispose();
                }
            }
        }
Ejemplo n.º 2
0
        private void ReqDataThreadMethod(ThreadExPara para)
        {
            const int reqDataThreadsEventHandleTimeout = 2000;
            int       currentThreadId           = Thread.CurrentThread.ManagedThreadId;
            var       reqDataThreadsEventHandle = (AutoResetEvent)para.Para;
            TransferReqDataSchduleInfoManager reqDataSchduleInfoManager = null;
            TransferReqDataSchduleInfo        currentReqDataSchduleInfo;
            bool hasSendTransferCompletedMessageLock;

            while (!para.Token.IsCancellationRequested)
            {
                try
                {
                    //var watch = System.Diagnostics.Stopwatch.StartNew();
                    hasSendTransferCompletedMessageLock = this.GetReqDataSchduleInfo(currentThreadId, out reqDataSchduleInfoManager, out currentReqDataSchduleInfo);
                    //watch.Stop();
                    //if (reqDataSchduleInfoManager != null)
                    //{
                    //    Loger.Warn($"RID[{reqDataSchduleInfoManager.NotifyMessage.Header.Rid}]GetReqDataSchduleInfo耗时{watch.Elapsed.Milliseconds}毫秒");
                    //}

                    if (hasSendTransferCompletedMessageLock)
                    {
                        this.SendTransferCompletedMessage(reqDataSchduleInfoManager.NotifyMessage);
                    }
                    else
                    {
                        if (reqDataSchduleInfoManager == null || currentReqDataSchduleInfo == null)
                        {
                            try
                            {
                                reqDataThreadsEventHandle.WaitOne(reqDataThreadsEventHandleTimeout);
                            }
                            catch (ObjectDisposedException)
                            {
                                break;
                            }
                        }
                        else
                        {
                            this.PrimitiveReqData(reqDataSchduleInfoManager, currentReqDataSchduleInfo);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Loger.Error(ex, "请求数据异常");
                }
            }
        }
Ejemplo n.º 3
0
 public bool AddReqDataSchduleInfoManager(TransferReqDataSchduleInfoManager reqDataSchduleInfoManager)
 {
     return(this._reqDataSchduleInfoManagerDic.TryAdd(reqDataSchduleInfoManager.NotifyMessage.Header.Rid, reqDataSchduleInfoManager));
 }
Ejemplo n.º 4
0
        private bool ProResourceMode(SendDataNotifyMessage message)
        {
            lock (this._revItemDicLock)
            {
                var             rid = message.Header.Rid;
                ReceiveDataItem receiveDataItem;
                if (this._revItemDic.TryGetValue(rid, out receiveDataItem))
                {
                    receiveDataItem.UpdateLastAccessTimestamp();
                    //Loger.Warn($"[RID:{rid}]的接收项已存在,忽略");
                    return(false);
                }

                if (MemoryCacheEx.Get(CacheKeyGenerator.GenerateRevTimeoutKey(rid)) != null)
                {
                    //Loger.Warn($"[RID:{rid}]接收已超时,忽略");
                    return(false);
                }

                //Loger.Warn($"处理RID[{rid}]发送通知");
                receiveDataItem = new ReceiveDataItem(message, this._config.LocalFileDirectory, this._config.TransferThreadCount);
                if (!this._revItemDic.TryAdd(rid, receiveDataItem))
                {
                    receiveDataItem.Close(true);
                    Loger.Error("this._revItemDic.TryAdd失败,原因未知");
                    return(false);
                }

                int millisecondsTimeout;
                var revItemCount = this._revItemDic.Count;
                if (revItemCount < 1)
                {
                    millisecondsTimeout = message.Timeout / this._config.TimeoutHeartMul;
                }
                else
                {
                    millisecondsTimeout = message.Timeout / (revItemCount * this._config.TimeoutHeartMul);
                }

                var reqDataSchduleInfoManager = new TransferReqDataSchduleInfoManager(message, this._reqDataThreads, millisecondsTimeout);
                TransferResourceManager resourceTransferManager;
                var priority = message.Priority;
                if (this._priorityResourceTransferManagerSortedList.ContainsKey(priority))
                {
                    resourceTransferManager = this._priorityResourceTransferManagerSortedList[priority];
                }
                else
                {
                    lock (this._priorityResourceTransferManagerSortedListLock)
                    {
                        if (this._priorityResourceTransferManagerSortedList.ContainsKey(priority))
                        {
                            resourceTransferManager = this._priorityResourceTransferManagerSortedList[priority];
                        }
                        else
                        {
                            resourceTransferManager = new TransferResourceManager();
                            this._priorityResourceTransferManagerSortedList.Add(priority, resourceTransferManager);
                        }
                    }
                }

                if (resourceTransferManager.AddReqDataSchduleInfoManager(reqDataSchduleInfoManager))
                {
                    this.SendReqDataThreadsEventHandleNotify();
                    return(true);
                }
                else
                {
                    Loger.Error("AddReqDataSchduleInfoManager失败,原因未知");
                    if (!this._revItemDic.TryRemove(rid, out receiveDataItem))
                    {
                        receiveDataItem.Close(true);
                    }

                    return(false);
                }
            }
        }
Ejemplo n.º 5
0
        private bool GetReqDataSchduleInfo(int currentThreadId, out TransferReqDataSchduleInfoManager reqDataSchduleInfoManager, out TransferReqDataSchduleInfo currentReqDataSchduleInfo)
        {
            bool sendHeartFlag = false;

            TransferResourceManager[] resourceTransferManagers;
            lock (this._priorityResourceTransferManagerSortedListLock)
            {
                resourceTransferManagers = this._priorityResourceTransferManagerSortedList.Values.ToArray();
                if (!this._priorityResourceTransferManagerSortedListHeartFlag)
                {
                    this._priorityResourceTransferManagerSortedListHeartFlag = true;
                    sendHeartFlag = true;
                }
            }

            if (sendHeartFlag)
            {
                //发送心跳数据包
                this.SendHeartData(resourceTransferManagers);

                //重置标识
                this._priorityResourceTransferManagerSortedListHeartFlag = false;
            }

            //查找本次传输项
            foreach (var resourceTransferManager in resourceTransferManagers)
            {
                reqDataSchduleInfoManager = resourceTransferManager.GetReqDataSchduleInfoManager();
                if (reqDataSchduleInfoManager == null)
                {
                    continue;
                }

                if (reqDataSchduleInfoManager.IsTransferCompleted())
                {
                    if (reqDataSchduleInfoManager.GetSendTransferCompletedMessageLock())
                    {
                        currentReqDataSchduleInfo = null;
                        return(true);
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                {
                    currentReqDataSchduleInfo = reqDataSchduleInfoManager.GetReqDataSchduleInfo(currentThreadId);
                    if (currentReqDataSchduleInfo.IsTransferCompleted())
                    {
                        continue;
                    }
                    else
                    {
                        return(false);
                    }
                }
            }

            reqDataSchduleInfoManager = null;
            currentReqDataSchduleInfo = null;
            return(false);
        }