UDP发送频率控制

场景一:

  用户需要在界面上看到消息的发送与回复,UDP得发送1->接收回复1->发送2->接收回复2

场景二:

  无关用户体验,但是需要大量的数据发送,可能导致接收方处理不过来而端口堵塞消息丢弃

方案:

  使用EventWaitHandle的WaitOne来暂停发送,当收到消息回复或者有消息超时时Set继续发送,代码(包含伪代码,不可编译)如下:

  ConcurrentDictionary<int, DateTime> _batchMsgID = new ConcurrentDictionary<int, DateTime>();//等待中的消息<消息唯一标识,开始等待时间>

  EventWaitHandle _waitHandler = new AutoResetEvent(false);//执行下发等待信号

  SendFunc()

  {

    for()

    {

      _batchMsgID.TryAdd();//加入等待

      UdpSendOne();//发送一条数据       

      if(_batchMsgID.Count>=10) _waitHandler.WaitOne(); //场景一,不用判断直接wait;场景二,等待数超过10条暂停发送

    }    

  }

  ReceiveFunc()

  {

    while()

    {      

      UdpReceiveOne();//收到一条数据

       if (_batchMsgID.ContainsKey(msgID))
                       {
                              DateTime time;
                               _batchMsgID.TryRemove(msgID, out time);
                               if (_batchMsgID.Count <= 10) _waitHandler.Set(); //场景一,不用判断直接set;
                       }

    }

  }

  timer_tick()

  {//消息超时处理

        try
            {
                List<int> keys = new List<int>();
                foreach(var msg in _batchMsgID)
                {
                    if ((DateTime.Now - msg.Value).TotalSeconds >= 3) keys.Add(msg.Key);
                }
                DateTime time;
                keys.ForEach(x => _batchMsgID.TryRemove(x, out time));

     if (_batchMsgID.Count <= 10) _waitHandler.Set(); //场景一,不用判断直接set;
            }
            catch (Exception ex) { LogHelper.logger.Error(ex); }

  }

猜你喜欢

转载自www.cnblogs.com/chixiner/p/12792994.html