示例#1
0
 private void SendFuc_Direct(byte[] msg)
 {
     foreach (var point in _RemoteAddr.Values)
     {
         while (true)
         {
             try
             {
                 _UDPClient.Send(msg, msg.Length, point);
                 break;
             }
             catch (SocketException e)
             {
                 if (e.SocketErrorCode == SocketError.WouldBlock)
                 {
                     SleepHelper.Delay();
                 }
                 else
                 {
                     throw e;
                 }
             }
         }
     }
 }
示例#2
0
        public void test_network_udp_send()
        {
            var sender = new UdpClient();

            sender.Client.Blocking       = false;
            sender.Client.SendBufferSize = 50 * 1024 * 1024;
            var remote = new IPEndPoint(IPAddress.Parse("192.168.0.4"), 19208);
            //sender.Connect(remote);

            Stopwatch watch = new Stopwatch();

            watch.Start();

            byte[] bytes         = new byte[4096];
            var    count         = 0;
            var    count_invalid = 0;

            while (count < 600_000)
            {
                //sender.Client.Send(bytes);
                //sender.SendAsync(bytes, bytes.Length, remote);

                while (true)
                {
                    try
                    {
                        sender.Send(bytes, bytes.Length, remote);
                        break;
                    }
                    catch (SocketException e)
                    {
                        if (e.SocketErrorCode == SocketError.WouldBlock)
                        {
                            SleepHelper.Delay();
                        }
                        else
                        {
                            count_invalid++;
                            throw e;
                        }
                    }
                }


                //sender.Send(bytes, bytes.Length);
                count++;

                //if (count % 100 == 0)
                //{
                //    Thread.Sleep(1);
                //}
            }

            watch.Stop();

            System.Console.WriteLine(watch.ElapsedMilliseconds + "===" + watch.ElapsedMilliseconds / 600_000);

            Assert.AreEqual(0, count_invalid);
        }
示例#3
0
        private void InfoThread()
        {
            Task.Run(() =>
            {
                while (true)
                {
                    if (_infos.TryDequeue(out ReplayInfo info))
                    {
                        _infoHandler?.Invoke(info);
                        continue;
                    }

                    SleepHelper.Delay();
                }
            });
        }
示例#4
0
 private void SendThread()
 {
     Task.Run(() =>
     {
         while (true)
         {
             if (_msgForSend.TryDequeue(out SendInfo msg))
             {
                 _sendHandler?.Invoke(msg);
             }
             else
             {
                 SleepHelper.Delay();
             }
         }
     });
 }
示例#5
0
        //public void Send(byte[] msg, IPandPort ipAndPort)
        //{
        //    Send(msg, new IPEndPoint(IPAddress.Parse(ipAndPort.IP), ipAndPort.Port));
        //}

        public void Send(byte[] msg, IPEndPoint point)
        {
            while (true)
            {
                try
                {
                    _UDPClient.Send(msg, msg.Length, point);
                    break;
                }
                catch (SocketException e)
                {
                    if (e.SocketErrorCode == SocketError.WouldBlock)
                    {
                        SleepHelper.Delay();
                    }
                    else
                    {
                        throw e;
                    }
                }
            }
        }
示例#6
0
        public void Clip(long startIndex, long endIndex, double[] segmentPara, string path, string name, string notes = null)
        {
            _reader.Set(startIndex);

            var info = _reader.GetFilesInfo();

            if (notes == null)
            {
                notes = info.notes;
            }

            _writer = new Writer(segmentPara, path, name, notes,
                                 new List <System.Net.IPEndPoint>(info.points),
                                 info.timeInterval, info.time + startIndex * info.timeInterval);

            int i = 0;

            while (i <= endIndex - startIndex)
            {
                var pkg = _reader.Get();

                if (pkg == null)
                {
                    SleepHelper.Delay();
                    continue;
                }

                _writer.AppendCoded(pkg);

                _reader.Return(ref pkg);

                i++;
            }

            _writer.FlushAndClose();
        }
示例#7
0
        private void ProcessStart()
        {
            Task.Run(() =>
            {
                double indexTime = _startTimeStamp;

                Package pkg = new Package()
                {
                    time = indexTime,
                };

                while (true)
                {
                    if (_queue.Count == 0)
                    {
                        var t = DateTime.UtcNow.TotalSeconds();

                        // 向下取整
                        if (t - indexTime > _intervalTime)
                        {
                            do
                            {
                                if (_flagStop)
                                {
                                    return(null);
                                }
                                _writer.Append(pkg);

                                // Core 中不应该有直接输出
                                //Logger.Info.WriteLine("Pkg_Count: " + pkg.MsgCount
                                //                    + " Compress_Rate: " + (pkg.codedLength * 100.0 / (pkg.originLength == 0 ? -pkg.codedLength : pkg.originLength)).ToString("f2") + "%"
                                //                    + " Pkg_Time: " + pkg.time);
                                _infos.Enqueue(new ReplayInfo()
                                {
                                    time         = DateTime.UtcNow,
                                    count        = pkg.MsgCount,
                                    codedLength  = pkg.codedLength,
                                    originLength = pkg.originLength,
                                    pkgTime      = pkg.time
                                });

                                indexTime += _intervalTime;

                                //pkg = new Package()
                                //{
                                //    time = indexTime,
                                //};
                                pkg.time = indexTime;
                                pkg.Clear();
                            }while (indexTime + _intervalTime < t);
                        }

                        SleepHelper.Delay(1);

                        continue;
                    }

                    _queue.TryDequeue(out Message item);

                    // 向下取整
                    if (item.header.time - indexTime > _intervalTime)
                    {
                        do
                        {
                            if (_flagStop)
                            {
                                return(null);
                            }
                            _writer.Append(pkg);

                            // Core 中不应该有直接输出
                            //Logger.Info.WriteLine("Pkg_Count: " + pkg.MsgCount
                            //                    + " Compress_Rate: " + (pkg.codedLength * 100.0 / (pkg.originLength == 0 ? -pkg.codedLength : pkg.originLength)).ToString("f2") + "%"
                            //                    + " Pkg_Time: " + pkg.time);
                            _infos.Enqueue(new ReplayInfo()
                            {
                                time         = DateTime.UtcNow,
                                count        = pkg.MsgCount,
                                codedLength  = pkg.codedLength,
                                originLength = pkg.originLength,
                                pkgTime      = pkg.time
                            });

                            indexTime += _intervalTime;

                            //pkg = new Package()
                            //{
                            //    time = indexTime,
                            //};
                            pkg.time = indexTime;
                            pkg.Clear();
                        }while (indexTime + _intervalTime < item.header.time);
                    }

                    pkg.Add(item);
                }
            });
        }
示例#8
0
        private void RePlayThread()
        {
            Task.Run(() =>
            {
                double sendDelay = 0.000005;
                _signalJump      = false;
                _signalEnd       = false;

                if (_watch.IsRunning)
                {
                    _watch.Restart();
                }
                else
                {
                    _watch.Reset();
                }

                while (true)
                {
                    var pkg = _reader.Get();

                    if (pkg == null)
                    {
                        if (_signalEnd || _signalJump)
                        {
                            if (_watch.IsRunning)
                            {
                                _watch.Restart();
                            }
                            else
                            {
                                _watch.Reset();
                            }

                            _signalJump = false;
                        }
                        SleepHelper.Delay();
                        continue;
                    }

                    //Logger.Debug.WriteLine(pkg.index);

                    //int c = 0, d = 0;

                    foreach (var msg in pkg.GetMessages())
                    {
                        var sendTiming = (msg.header.time - pkg.time) / SpeedRate;

                        while (_watch.Elapsed.TotalSeconds + _sleepDelay + sendDelay < sendTiming)
                        {
                            if (_signalJump)
                            {
                                RePlayThread();
                                return;
                            }


                            //if (_watch.Elapsed.TotalSeconds + _sleepDelay + sendDelay < sendTiming)
                            //{
                            //c++;
                            //var a = _watch.Elapsed.TotalSeconds + _sleepDelay + sendDelay - sendTiming;
                            SleepHelper.Delay();
                            //if (_watch.Elapsed.TotalSeconds + _sleepDelay + sendDelay < sendTiming)
                            //{
                            //    var aa = _watch.Elapsed.TotalSeconds + _sleepDelay + sendDelay - sendTiming;
                            //    var aaa = a - aa;
                            //}
                            //}
                            //else
                            //{
                            //    d++;
                            //}
                        }

                        _map.TryGetValue(ConverToIP64(msg.header.ip, msg.header.port), out var point);
                        if (point == null)
                        {
                            continue;
                        }

                        _msgForSend.Enqueue(new SendInfo()
                        {
                            bytes = msg.bytes, point = point
                        });

                        //_sendHandler?.Invoke(msg.bytes.Span, point);
                    }

                    // 发送下一个 pkg 之前的延时
                    while (_watch.Elapsed.TotalSeconds + _sleepDelay < _reader.Interval / SpeedRate)
                    {
                        if (_signalJump)
                        {
                            RePlayThread();
                            return;
                        }

                        //if (_watch.Elapsed.TotalSeconds + _sleepDelay < _reader.Interval / SpeedRate)
                        //{
                        SleepHelper.Delay();
                        //}
                    }


                    // Core 中不应该有直接输出
                    //Logger.Debug.WriteLine(pkg.index + " " + _watch.Elapsed.TotalSeconds);
                    _infos.Enqueue(new ReplayInfo()
                    {
                        time        = new DateTime((long)(pkg.time * 1e7)).AddYears(1970 - 1).AddDays(-1),
                        index       = pkg.index,
                        pkgCostTime = _watch.Elapsed.TotalSeconds
                                      //pkgCostTime = (pkg.GetMessages().First().header.time - pkg.time) / SpeedRate
                    });


                    // 播放完毕
                    if (pkg.index >= _reader.Count - 1)
                    {
                        _signalEnd = true;
                    }
                    else
                    {
                        _signalEnd = false;
                    }

                    // 需要在所有使用 pkg 的代码完成后再归还
                    // 缓解内存压力
                    _reader.Return(ref pkg);

                    _watch.Restart();
                }
            });
        }