Exemple #1
0
        public void Close()
        {
            Debuger.Log(LOG_TAG_MAIN, "Close()");

            m_IsRunning = false;

            if (m_Game != null)
            {
                m_Game.Stop();
                m_Game = null;
            }

            if (m_Room != null)
            {
                m_Room.Destroy();
                m_Room = null;
            }

            DelAllSession();

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

            if (m_ThreadMain != null)
            {
                // m_ThreadMain.Interrupt();
                m_ThreadMain.Abort();
                m_ThreadMain = null;
            }
        }
Exemple #2
0
        public bool Start(int port)
        {
            if (m_IsRunning)
            {
                Debuger.LogWarning(LOG_TAG_MAIN, "Start() 不能重复创建启动Server!");
                return false;
            }
            Debuger.Log(LOG_TAG_MAIN, "Start()  port = {0}", port);

            DelAllSession();

            try
            {
                // 初始化参数
                m_RealTicksAtStart = DateTime.Now.Ticks;
                m_LogicLastTicks = m_RealTicksAtStart;  //也从这个时候开始算

                //创建Game Socket
                m_GameSocket = new KCPSocket(0, 1);
                m_GameSocket.AddReceiveListener(OnReceive);

                m_IsRunning = true;

                // 创建房间先
                m_Room = new FSPRoom();
                m_Room.Start();

                // 创建线程
                Debuger.Log(LOG_TAG_MAIN, "Start()  创建服务器线程");
                m_ThreadMain = new Thread(Thread_Main) { IsBackground = true };
                m_ThreadMain.Start();
            }
            catch(System.Exception e)
            {
                Debuger.LogError(LOG_TAG_MAIN, "Start failed: " + e.Message);
                Close();
                return false;
            }

            //当用户直接用UnityEditor上的停止按钮退出游戏时,会来不及走完整的析构流程。
            //这里做一个监听保护
            #if UNITY_EDITOR
            UnityEditor.EditorApplication.playmodeStateChanged -= OnEditorPlayModeChanged;
            UnityEditor.EditorApplication.playmodeStateChanged += OnEditorPlayModeChanged;
            #endif
            return true;
        }