コード例 #1
0
ファイル: DNServer.cs プロジェクト: KOLLSOFT/DNET
        /// <summary>
        /// 启动服务器,会开启工作线程然后释放一个DoStart信号量。
        /// </summary>
        /// <param name="port">端口号</param>
        /// <param name="threadCount">服务器使用的处理线程数量</param>
        /// <param name="hostName">服务器的主机IP,一般使用Any表示所有的可能IP</param>
        public void Start(int port, int threadCount = 1, string hostName = "Any")
        {
            try
            {
                DxDebug.LogConsole("DNServer.Start():服务器工作线程数 " + threadCount);
                if (disposed)
                {
                    TokenManager.GetInst().Clear();
                    _msgQueue     = new DQueue <NetWorkMsg>(MSG_QUEUE_CAPACITY);
                    _msgSemaphore = new Semaphore(0, MSG_QUEUE_CAPACITY);

                    _workThread = new Thread[threadCount];
                    for (int i = 0; i < threadCount; i++)
                    {
                        _workThread[i] = new Thread(DoWork);
                        _workThread[i].IsBackground = true;
                        //工作线程的优先级(影响不大)
                        _workThread[i].Priority = ThreadPriority.Highest;
                        _workThread[i].Name     = "SeverThread " + i;
                        _workThread[i].Start(); //启动线程
                    }

                    disposed = false;
                }

                _port = port;
                NetWorkMsg msg = new NetWorkMsg(NetWorkMsg.Tpye.S_Start, null);
                msg.text1 = hostName;
                AddMessage(msg);
            }
            catch (Exception e)
            {
                DxDebug.LogError("DNServer.Start():异常 " + e.Message);
            }
        }
コード例 #2
0
        private void DoClose()
        {
            try
            {
                IsInited = false;
                DxDebug.LogConsole("DNClient.DoClose():开始释放资源 ");
                _disposed = true;

                // 清理托管资源
                _msgQueue.Clear();
                _msgPool.Clear();

                _msgQueue = null;
                _msgPool  = null;

                // 清理非托管资源
                _msgSemaphore.Close();
                _msgSemaphore = null;
                Interlocked.Exchange(ref _curSemCount, 0);

                if (_socketClient != null)
                {
                    _socketClient.Dispose();
                    _socketClient = null;
                }

                IsConnecting = false;
            }
            catch (Exception e)
            {
                DxDebug.LogWarning("DNClient.DoClose():异常: " + e.Message);
            }
        }
コード例 #3
0
        private void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            IsInited = false;

            try
            {   //最先去把线程关了
                if (_workThread != null && _workThread.IsAlive)
                {
                    DxDebug.LogConsole("DNClient.Dispose():_workThread.Abort()线程中断!");
                    _workThread.Abort();
                }
            }
            catch (Exception e)
            {
                DxDebug.LogWarning("DNClient.Dispose(): _workThread.Abort()异常" + e.Message);
            }
            finally
            {
                _workThread = null;
            }

            try
            {
                if (disposing)
                {
                    // 清理托管资源
                    _msgQueue.Clear();
                    _msgPool.Clear();

                    _msgQueue = null;
                    _msgPool  = null;
                }
                // 清理非托管资源
                _packet2.Clear();
                _msgSemaphore.Close();
                _msgSemaphore = null;
                if (_socketClient != null)
                {
                    _socketClient.Dispose();
                    _socketClient = null;
                }
            }
            catch (Exception e)
            {
                DxDebug.LogWarning("DNClient.Dispose():释放异常" + e.Message);
            }
            //让类型知道自己已经被释放
            _disposed = true;

            IsConnecting = false;
        }
コード例 #4
0
        /// <summary>
        /// 工作线程启动
        /// </summary>
        public void Start()
        {
            try
            {
                //标记线程可以运行
                _isRun = true;

                if (_disposed)
                {
                    //标记自己已经有了资源申请
                    _disposed = false;
                    try
                    {
                        DxDebug.LogConsole("WorkThread.Start():这个类对象经被释放或刚刚构造,重新初始化");
                        _msgQueue     = new DQueue <IWorkMsg>(MSG_QUEUE_CAPACITY, _initMsgQueueSize);
                        _msgSemaphore = new Semaphore(0, 64);//由于AddMessage的改动,这里只需要是随便一个数既可

                        _isWorking    = new bool[_threadCount];
                        _lastWorkTime = new long[_threadCount];
                        _workThreadID = new int[_threadCount];

                        _workThread = new Thread[_threadCount];
                        for (int i = 0; i < _threadCount; i++)
                        {
                            _workThread[i] = new Thread(DoWork);
                            _workThread[i].IsBackground = true;
                            //工作线程的优先级(影响不大)
                            _workThread[i].Priority = ThreadPriority.Highest;
                            _workThread[i].Name     = name + i;
                            //记录线程ID
                            _workThreadID[i] = _workThread[i].ManagedThreadId;
                            _workThread[i].Start(); //启动线程
                        }
                    }
                    catch (Exception e)
                    {
                        DxDebug.LogError("WorkThread.Start():构造失败!异常:" + e.Message);
                        Dispose();
                    }
                }
                else
                {
                    Dispose();
                    Start();
                }
            }
            catch (Exception e)
            {
                DxDebug.LogError("WorkThread.Start():异常:" + e.Message);
            }
        }
コード例 #5
0
        /// <summary>
        /// 初始化这个对象,会创建工作线程
        /// </summary>
        private void Init()
        {
            try
            {
                //if (!_disposed)//强制释放一遍
                //{
                //    DxDebug.LogConsole("DNClient.Init():释放资源");
                //    Dispose();
                //}
                if (_msgQueue == null)
                {
                    _msgQueue = new DQueue <NetWorkMsg>(int.MaxValue, 256);
                }
                if (_msgPool == null)
                {
                    _msgPool = new DQueue <NetWorkMsg>(int.MaxValue, 256);
                }

                if (_msgSemaphore == null)
                {
                    _msgSemaphore = new Semaphore(0, 4);
                    Interlocked.Exchange(ref _curSemCount, 0);
                }

                if (_workThread == null)
                {
                    _workThread = new Thread(DoWork);
                    _workThread.IsBackground = true;

                    _workThread.Start(); //启动线程
                }

                _disposed = false;
                IsInited  = true;
            }
            catch (Exception e)
            {
                Dispose();
                DxDebug.LogError("DNClient.Init():异常:" + e.Message);
            }
        }