public void Send(byte[] data, EndPoint target, UInt16 proxyId, Action <UdpPackage, bool> callback)
        {
            UdpPackage p = null;

            lock (packageCache)
            {
                p = packageCache.Push(target, data, frameLength, proxyId);
            }
            p.Callback = callback;
            int c = 0;

            lock (sendList)
            {
                sendList.AddRange(p.Select(x => new SendItem()
                {
                    Frame = x, Time = DateTime.Now, RetryCount = 0, Target = target
                }));
                c = sendList.Count;
            }

            lock (processerList)
            {
                processerList.FirstOrDefault(x => x.Notify());
            }
        }
        protected virtual void OnSendPackageSuccess(UdpPackage package, EndPoint target, UInt16 proxyId)
        {
            OnSendPackageCallback h = SendPackageSuccess;

            if (h != null)
            {
                h(package, target, proxyId);
            }
        }
        public void ReceivedFeedback(UdpFrame frame)
        {
            DateTime now       = DateTime.Now;
            UdpFrame dataFrame = null;

            lock (packageCache)
            {
                dataFrame = packageCache.ChangeFrameStatus(frame.PackageSeq, frame.Seq);
            }
            //EndPoint ep = null;
            //从正在发送的列表中移除
            //lock (sendingList)
            //{
            //    var si = sendingList.FirstOrDefault(x => x.Frame.PackageSeq == frame.PackageSeq && x.Frame.Seq == frame.Seq);
            //    if (si != null)
            //    {
            //        sendingList.Remove(si);
            //        ep = si.Target;
            //    }
            //}

            lock (packageCache)
            {
                UdpPackage up = packageCache.PackageList.FirstOrDefault(x => x.PackageID == frame.PackageSeq);

                if (up != null)
                {
                    long   cost = (long)(now - dataFrame.LastSendTime).TotalMilliseconds;
                    string key  = up.Target.ToString();
                    lock (avgSendCost)
                    {
                        if (avgSendCost.ContainsKey(key))
                        {
                            avgSendCost[key] = (avgSendCost[key] + cost) / 2;
                        }
                        else
                        {
                            avgSendCost.Add(key, cost);
                        }
                    }
                }

                if (up != null && up.Count(x => !x.IsSended) == 0)
                {
                    //    Logger.Trace("发送数据包成功:{0} {1}", up.PackageID, up.Data.Length);
                    //包发送完成
                    //packageCache.PackageList.Remove(up);
                    RemovePackage(up.PackageID, false);
                    OnSendPackageSuccess(up, up.Target, up[0].ProxyID);
                    if (up.Callback != null)
                    {
                        up.Callback(up, true);
                    }
                }
            }
        }
예제 #4
0
        protected override void OnSendPackageError(UdpPackage package, EndPoint target, UInt16 proxyId)
        {
            UdpServerSession session = getSession(target, proxyId);

            if (session != null)
            {
                lock (session)
                {
                    session.ErrorBytes += (package.Data == null ? 0 : package.Data.Length);
                }
            }
            base.OnSendPackageError(package, target, proxyId);
        }
예제 #5
0
        protected override void OnSendedPackage(UdpPackage package, EndPoint target, UInt16 proxyId)
        {
            UdpServerSession session = getSession(target, proxyId);

            if (session != null)
            {
                lock (session)
                {
                    session.SendedBytes += (package.Data == null ? 0 : package.Data.Length);
                    session.LastSendTime = DateTime.Now;
                    session.ActiveTime   = DateTime.Now;
                }
            }
            base.OnSendedPackage(package, target, proxyId);
        }
 public UdpPackage Push(EndPoint target, byte[] data, int frameLength, UInt16 proxyId)
 {
     lock (PackageList)
     {
         UdpPackage package = new UdpPackage(data, packageSeq, frameLength, proxyId, target);
         PackageList.Add(package);
         if (packageSeq >= (int.MaxValue - 1))
         {
             packageSeq = 0;
         }
         //  logger.Trace("包ID: {0}", packageSeq);
         packageSeq++;
         return(package);
     }
 }
        public UdpFrame ChangeFrameStatus(int packageID, UInt16 seq)
        {
            UdpPackage p = null;
            UdpFrame   f = null;

            lock (PackageList)
            {
                p = PackageList.FirstOrDefault(x => x.PackageID == packageID);

                if (p != null)
                {
                    f = p.FirstOrDefault(x => x.Seq == seq);

                    f.IsSended = true;
                }
            }

            return(f);
        }
 public UdpPackage PushText(EndPoint target, string text, int frameLength, UInt16 proxyId)
 {
     byte[] data = new byte[0];
     if (!String.IsNullOrEmpty(text))
     {
         data = Encoding.UTF8.GetBytes(text);
     }
     lock (PackageList)
     {
         UdpPackage package = new UdpPackage(data, packageSeq, frameLength, proxyId, target, true);
         PackageList.Add(package);
         if (packageSeq >= (int.MaxValue - 1))
         {
             packageSeq = 0;
         }
         packageSeq++;
         return(package);
     }
 }
        public void RemovePackage(int packageSeq, bool isError)
        {
            UdpPackage up = null;

            lock (packageCache)
            {
                up = packageCache.PackageList.FirstOrDefault(x => x.PackageID == packageSeq);
                if (up != null)
                {
                    packageCache.PackageList.Remove(up);
                    up.ForEach(f =>
                    {
                        f.IsRemoved = true;
                    });
                }
            }
            lock (sendList)
            {
                sendList.Where(si => si.Frame.PackageSeq == packageSeq).ToList().ForEach(f =>
                {
                    sendList.Remove(f);
                });
            }

            List <SendThread> list = null;

            lock (processerList)
            {
                list = processerList.ToList();
            }
            list.ForEach(p =>
            {
                p.RemovePackageFrames(packageSeq);
            });

            if (isError && up != null)
            {
                OnSendPackageError(up, up.Target, up[0].ProxyID);
            }
        }
예제 #10
0
 protected virtual void OnSendedPackage(UdpPackage package, EndPoint target, UInt16 proxyId)
 {
 }