Beispiel #1
0
        /// <summary>
        /// 单独的Uv线程
        /// </summary>
        /// <param name="obj"></param>
        private void InitLoopThread(object obj)
        {
            //申请内存 调用uv_loop_init
            _loopHandle.Init(_libuv);

            //先绑定asyncHandle 监视器 后绑定Tcp监视器
            //AsyncHandle 注册 当执行async_send的时候 唤醒loopHandle 执行callback方法
            _asyncHand1.Init(_loopHandle, UserPostActionExcute);
            _asyncHand2.Init(_loopHandle, UserPostActionExcute);
            TaskCompletionSource <int> taskCompletionSource = (TaskCompletionSource <int>)obj;

            taskCompletionSource.SetResult(0);//结束task 的 wait

            while (true)
            {
                bool flag = false;
                try
                {
                    //因为上面已有async的监视器 有要监听的事件 所以会阻塞
                    //调用uv_run
                    _loopHandle.Start();
                }
                catch (Exception ex)
                {
                    Console.WriteLine();
                    Console.WriteLine("***** libuv.run error: " + ex.Message);
                    if (!(ex is NullReferenceException))
                    {
                        throw ex;
                    }
                    flag = true;
                }
                if (!flag)
                {
                    break;
                }
                Console.WriteLine("===== Try to start again ===");
            }

            //如果已经终止了 就直接退出
            if (Interlocked.CompareExchange(ref _disposeFlag, 0, -1) == 1)
            {
                return;
            }
            _asyncHand1.UvRef();
            _asyncHand1.dispose();
            _asyncHand2.UvRef();
            _asyncHand2.dispose();
            //遍历循环中的handle //所有非内部的handle,调用回调 进行关闭
            _libuv.Walk(_loopHandle, new LibUv.Walk_Callback(MainThreadWalkCallBack), IntPtr.Zero);
            _loopHandle.Start();
            _loopHandle.Dispose();
        }
Beispiel #2
0
        private void InitLoopThread(object obj)
        {
            _loopHandle.Init(_libuv);
            _asyncHand1.Init(_loopHandle, new Action(UserPostActionExcute));
            _asyncHand2.Init(_loopHandle, new Action(UserPostActionExcute));
            TaskCompletionSource <int> taskCompletionSource = (TaskCompletionSource <int>)obj;

            taskCompletionSource.SetResult(0);
            while (true)
            {
                bool flag = false;
                try
                {
                    _loopHandle.Start();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    Console.WriteLine("***** libuv.run error: " + ex.Message);
                    if (!(ex is NullReferenceException))
                    {
                        throw ex;
                    }
                    flag = true;
                }
                if (!flag)
                {
                    break;
                }
                Console.WriteLine("===== Try to start again ===");
            }
            if (Interlocked.CompareExchange(ref _disposeFlag, 0, -1) == 1)
            {
                return;
            }
            _asyncHand1.UvRef();
            _asyncHand2.UvRef();
            _libuv.Walk(_loopHandle, new LibUv.Walk_Callback(MainThreadWalkCallBack), IntPtr.Zero);
            _loopHandle.Start();
            _loopHandle.Dispose();
        }