public void Close() { Debuger.Log(LOG_TAG_MAIN, "Close()"); m_IsRunning = false; if (m_Game != null) { m_Game.Dispose(); m_Game = null; } if (m_Room != null) { m_Room.Dispose(); m_Room = null; m_RoomRPC = null; } if (m_GameSocket != null) { m_GameSocket.Dispose(); m_GameSocket = null; } if (m_ThreadMain != null) { m_ThreadMain.Interrupt(); m_ThreadMain = null; } DelAllSession(); }
public void Close() { MyLogger.Log(LOG_TAG_MAIN, "Close()"); isRunning = false; if (mGame != null) { mGame.Dispose(); mGame = null; } if (mRoom != null) { mRoom.Dispose(); mRoom = null; mRoomRPC = null; } if (mGameSocket != null) { mGameSocket.Dispose(); mGameSocket = null; } if (mThreadMain != null) { mThreadMain.Interrupt(); mThreadMain = null; } DelAllSession(); }
//------------------------------------------------------------ #region 启动 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_LogicLastTicks = DateTime.Now.Ticks; m_RealTicksAtStart = m_LogicLastTicks; //创建Game Socket m_GameSocket = new KCPSocket(0, 1); m_GameSocket.AddReceiveListener(OnReceive); m_IsRunning = true; //一个简单通用的房间模块 m_Room = new FSPRoom(); m_Room.Create(); m_RoomRPC = m_Room; //创建线程 Debuger.Log(LOG_TAG_MAIN, "Start() 创建服务器线程"); m_ThreadMain = new Thread(Thread_Main) { IsBackground = true }; m_ThreadMain.Start(); } catch (Exception e) { Debuger.LogError(LOG_TAG_MAIN, "Start() " + e.Message); Close(); return(false); } //当用户直接用UnityEditor上的停止按钮退出游戏时,会来不及走完整的析构流程。 //这里做一个监听保护 #if UNITY_EDITOR UnityEditor.EditorApplication.playmodeStateChanged -= OnEditorPlayModeChanged; UnityEditor.EditorApplication.playmodeStateChanged += OnEditorPlayModeChanged; #endif return(true); }
//------------------------------------------------------------ #region start server public bool Start(int port) { if (isRunning) { MyLogger.LogWarning(LOG_TAG_MAIN, "Start()", "cannot start duplicated Server!"); return(false); } MyLogger.Log(LOG_TAG_MAIN, "Start() port = {0}", port.ToString()); DelAllSession(); try { mLogicLastTicks = DateTime.Now.Ticks; mRealTicksAtStart = mLogicLastTicks; //create Game Socket mGameSocket = new KCPSocket(0, 1); mGameSocket.AddReceiveListener(OnReceive); isRunning = true; //create game room mRoom = new FSPRoom(); mRoom.Create(); mRoomRPC = mRoom; //create thread MyLogger.Log(LOG_TAG_MAIN, "Start() create server thead"); mThreadMain = new Thread(Thread_Main) { IsBackground = true }; mThreadMain.Start(); } catch (Exception e) { MyLogger.LogError(LOG_TAG_MAIN, "Start() ", e.Message); Close(); return(false); } //when user exit the game using stop button in UnityEditor, cannot release all resource in time //add listener here #if UNITY_EDITOR UnityEditor.EditorApplication.playmodeStateChanged -= OnEditorPlayModeChanged; UnityEditor.EditorApplication.playmodeStateChanged += OnEditorPlayModeChanged; #endif return(true); }