Exemple #1
0
 internal static extern int select([In] int ignoredParam, [In][Out] IntPtr[] readfds, [In][Out] IntPtr[] writefds, [In][Out] IntPtr[] exceptfds, [In] ref timeval timeout);
Exemple #2
0
        /// <summary>
        /// 循环监视已注册的Socket,等待这些Socket发生已注册的事件
        /// </summary>
        /// <param name="interval">微秒</param>
        public static void Run(long interval)
        {
            _stopped   = false;
            _last_time = Environment.TickCount;
            timeval tv = new timeval(interval);

            while (true)
            {
                if (_stopped)
                {
                    RecvList.Clear();
                    SendList.Clear();
                    ErrorList.Clear();
                    EventCallback.Clear();
                    CheckCallback.Clear();
                    _last_time = Environment.TickCount;
                    return;
                }
                IntPtr[] rlist = SocketSetToIntPtrArray(RecvList);
                IntPtr[] slist = SocketSetToIntPtrArray(SendList);
                IntPtr[] elist = SocketSetToIntPtrArray(ErrorList);
                int      res;
                if (interval > 0)
                {
                    res = select(0, rlist, slist, elist, ref tv);
                }
                else
                {
                    res = select(0, rlist, slist, elist, IntPtr.Zero);
                }
                if (res > 0)
                {   //rlist、wlist、xlist第一个元素表示文件描述符数量
                    bool _first = true;
                    foreach (IntPtr ptr in rlist)
                    {
                        if (!_first)
                        {
                            EventCallback[(Socket)RecvList[ptr]]((Socket)RecvList[ptr], IOMode.recv);
                        }
                        else
                        {
                            _first = false;
                        }
                    }
                    _first = true;
                    foreach (IntPtr ptr in slist)
                    {
                        if (!_first)
                        {
                            EventCallback[(Socket)SendList[ptr]]((Socket)SendList[ptr], IOMode.send);
                        }
                        else
                        {
                            _first = false;
                        }
                    }
                    _first = true;
                    foreach (IntPtr ptr in elist)
                    {
                        if (!_first)
                        {
                            EventCallback[(Socket)ErrorList[ptr]]((Socket)ErrorList[ptr], IOMode.error);
                        }
                        else
                        {
                            _first = false;
                        }
                    }
                }
                else if (res == 0)
                {
                    //Logging.Error("SELECT调用超时");
                }
                else
                {
                    //Logging.Error($"SELECT调用错误:{GetErrorMsg(GetLastError())}");
                    continue;
                }
                //定时触发需要定期执行的方法
                if (Environment.TickCount - _last_time >= _timeout_interval)
                {
                    foreach (Action func in CheckCallback)
                    {
                        func();
                    }
                    _last_time = Environment.TickCount;
                }
            }
        }