Exemplo n.º 1
0
        /// <summary>
        /// 停止UDP转发
        /// </summary>
        public void Stop()
        {
            try
            {
                if (runLocalThread && runRemoteThread)
                {
                    if (timerCheckStatic != null)
                    {
                        timerCheckStatic.Close();
                        timerCheckStatic = null;
                    }

                    if (recviceRemoteThread != null)
                    {
                        runRemoteThread     = false;
                        recviceRemoteThread = null;
                    }

                    if (recviceLocalThread != null)
                    {
                        runLocalThread     = false;
                        recviceLocalThread = null;
                    }

                    if (recviceRemoteThread == null && recviceLocalThread == null)
                    {
                        recviceDelay = 2;
                        State        = RecviceState.Stop;
                    }
                }
            }
            catch { }
        }
Exemplo n.º 2
0
        /// <summary>
        /// 检查断线状态
        /// </summary>
        private void CheckState(object source, ElapsedEventArgs elapsedEventArgs, object sourceArgs)
        {
            missCount++;

            if (missCount > 60)
            {
                Stop();
                timerCheckStatic.Close();
                timerCheckStatic = null;
            }
            else if (missCount > 7)
            {
                State = RecviceState.Waiting;
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 开始UDP转发
        /// </summary>
        public void Start()
        {
            try
            {
                if (!runLocalThread && !runRemoteThread)
                {
                    if (timerCheckStatic == null)
                    {
                        timerCheckStatic = WebsocketCore.CreateTimer(CheckState, null, 1000, true);
                    }

                    if (recviceRemoteThread == null)
                    {
                        recviceRemoteThread = new Thread(RecviceRemote);
                    }

                    if (recviceLocalThread == null)
                    {
                        recviceLocalThread = new Thread(RecviceLocal);
                    }

                    if (recviceRemoteThread != null && recviceLocalThread != null)
                    {
                        recviceDelay = recviceDelay == 2 ? 8 : recviceDelay;
                        if (missCount > 7)
                        {
                            initCount = missCount = 0;
                        }
                        runRemoteThread = runLocalThread = true;
                        recviceRemoteThread.Start();
                        recviceLocalThread.Start();
                        State = RecviceState.Running;
                    }
                }
            }
            catch { }
        }
Exemplo n.º 4
0
        private void Pool_OnReviceData(object sender, long id, byte[] data, RecviceState state)
        {
            //一组接收完成
            if (dicSucess.ContainsKey(id))
            {
                return;
            }
            dicSucess[id] = DateTime.Now;
            RecvicePool pool = null;

            dicPool.TryRemove(id, out pool);
            if (OnDataReceived != null)
            {
                AsyncUdpUserToken token = new AsyncUdpUserToken();
                token.Data   = data;
                token.Remote = remote;
                token.Length = data.Length;
                Task.Factory.StartNew(() =>
                {
                    OnDataReceived(this, token);
                });
                RecvicePool recvice = sender as RecvicePool;
                if (recvice != null)
                {
                    recvice.Clear();
                }
                recvice.OnLossData   -= Pool_OnLossData;
                recvice.OnReviceData -= Pool_OnReviceData;
                //完成发送一次
                LosPackage package = new LosPackage();
                package.packageType = 3;
                package.packageID   = id;
                Pool_OnLossData(this, remote, new LosPackage[] { package });

                Console.WriteLine("sucess:" + id);
            }
        }
Exemplo n.º 5
0
 /// <summary>
 /// 初始化UDP转发
 /// </summary>
 /// <param name="localPoint">本地地址</param>
 /// <param name="remotePoint">远程地址</param>
 public UDPForwarder(IPEndPoint localPoint, IPEndPoint remotePoint)
 {
     State = RecviceState.Stop;
     ChangePoint(localPoint, remotePoint);
 }