public bool Send(int dest_handle, object msg)
        {
            byte[] data;
            bool   ret = Build(msg, out data);

            if (ret)
            {
                if (null != data)
                {
                    if (world_id_ == 0)
                    {
                        ret = CenterClientApi.SendByHandle(dest_handle, data, data.Length);
                    }
                    else
                    {
                        ret = CenterHubApi.SendByHandle(world_id_, dest_handle, data, data.Length);
                    }
                }
                else
                {
                    ret = false;
                }
            }

            return(ret);
        }
        private void OnCommand(int worldId, int src, int dest, string command)
        {
            const string c_QuitLobby    = "QuitLobby";
            const string c_ReloadConfig = "ReloadConfig";

            try {
                if (worldId == UserServerConfig.WorldId)
                {
                    if (0 == command.CompareTo(c_QuitLobby))
                    {
                        LogSys.Log(LOG_TYPE.MONITOR, "receive {0} command, save data and then quitting ...", command);
                        if (!m_WaitQuit)
                        {
                            //收到关闭服务器指令,退出前保存数据
                            m_UserProcessScheduler.DefaultUserThread.QueueAction(m_UserProcessScheduler.DoLastSaveUserData);
                            m_GlobalProcessThread.QueueAction(m_GlobalProcessThread.DoLastSaveGlobalData);
                            m_LastWaitQuitTime = TimeUtility.GetLocalMilliseconds();
                            m_WaitQuit         = true;
                        }
                    }
                    else if (0 == command.CompareTo(c_ReloadConfig))
                    {
                        CenterHubApi.ReloadConfigScript();
                        UserServerConfig.Init();
                        LogSys.Log(LOG_TYPE.WARN, "receive {0} command.", command);
                    }
                }
                else
                {
                }
            } catch (Exception ex) {
                LogSys.Log(LOG_TYPE.ERROR, "Exception {0}\n{1}", ex.Message, ex.StackTrace);
            }
        }
        public bool Send(string dest_name, object msg)
        {
            bool ret = false;

            if (null != dest_name)
            {
                if (world_id_ == 0)
                {
                    int handle = CenterClientApi.TargetHandle(dest_name);
                    if (handle != 0)
                    {
                        ret = Send(handle, msg);
                    }
                }
                else
                {
                    int handle = CenterHubApi.TargetHandle(world_id_, dest_name);
                    if (handle != 0)
                    {
                        ret = Send(handle, msg);
                    }
                }
            }
            return(ret);
        }
Beispiel #4
0
        static void Main(string[] args)
        {
#if TEST_CENTER_CLIENT
            CenterClientApi.HandleNameHandleChangedCallback cb1 = HandleNameHandleChanged;
            CenterClientApi.HandleMessageCallback           cb2 = HandleMessage;
            CenterClientApi.HandleMessageResultCallback     cb3 = HandleMessageResult;
            CenterClientApi.HandleCommandCallback           cb4 = HandleCommand;
            byte[] bytes = Encoding.ASCII.GetBytes("test test test");
            CenterClientApi.Init("csharpclient", args.Length, args, cb1, cb2, cb3, cb4);

            CenterClientApi.ReloadConfigScript();
            CenterClientApi.ReloadConfigScript();

            for (; ;)
            {
                CenterClientApi.Tick();
                Thread.Sleep(10);

                int handle = CenterClientApi.TargetHandle("ServerCenter");
                if (handle > 0)
                {
                    bool ret = CenterClientApi.SendByHandle(handle, bytes, (ushort)bytes.Length);
                    CenterClientApi.SendCommandByHandle(handle, "output('test test test');");
                }
            }
            //CenterClientApi.Release();
#else
            CenterHubApi.HandleNameHandleChangedCallback cb1 = HandleNameHandleChanged2;
            CenterHubApi.HandleMessageCallback           cb2 = HandleMessage2;
            CenterHubApi.HandleMessageResultCallback     cb3 = HandleMessageResult2;
            CenterHubApi.HandleCommandCallback           cb4 = HandleCommand2;
            byte[] bytes = Encoding.ASCII.GetBytes("test test test");
            CenterHubApi.Init("centerhub", args.Length, args, cb1, cb2, cb3, cb4);

            CenterHubApi.ReloadConfigScript();
            CenterHubApi.ReloadConfigScript();

            for (; ;)
            {
                CenterHubApi.Tick();
                Thread.Sleep(10);

                int handle = CenterHubApi.TargetHandle(0, "hub2world0");
                if (handle > 0)
                {
                    bool ret = CenterHubApi.SendByHandle(0, handle, bytes, (ushort)bytes.Length);
                    CenterHubApi.SendCommandByHandle(0, handle, "output('test test test');");
                }
                handle = CenterHubApi.TargetHandle(1, "hub2world1");
                if (handle > 0)
                {
                    bool ret = CenterHubApi.SendByHandle(1, handle, bytes, (ushort)bytes.Length);
                    CenterHubApi.SendCommandByHandle(1, handle, "output('test test test');");
                }
            }
            //CenterHubApi.Release();
#endif
        }
 internal static void ForwardMessage(string name, byte[] data)
 {
     if (s_Inited)
     {
         if (null != name && null != data)
         {
             CenterHubApi.SendByName(s_WorldId, name, data, data.Length);
         }
     }
 }
 internal static void ForwardMessage(int handle, byte[] data)
 {
     if (s_Inited)
     {
         if (null != data)
         {
             CenterHubApi.SendByHandle(s_WorldId, handle, data, data.Length);
         }
     }
 }
 internal static void SendNodeMessage(string name, JsonMessage msg)
 {
     if (s_Inited)
     {
         byte[] data = BuildNodeMessage(msg);
         if (null != name && null != data)
         {
             CenterHubApi.SendByName(s_WorldId, name, data, data.Length);
         }
     }
 }
 internal static void SendNodeMessage(int handle, JsonMessage msg)
 {
     if (s_Inited)
     {
         byte[] data = BuildNodeMessage(msg);
         if (null != data)
         {
             CenterHubApi.SendByHandle(s_WorldId, handle, data, data.Length);
         }
     }
 }
        public bool Forward(string dest_name, byte[] data)
        {
            bool ret = false;

            if (null != data)
            {
                if (world_id_ == 0)
                {
                    ret = CenterClientApi.SendByName(dest_name, data, data.Length);
                }
                else
                {
                    ret = CenterHubApi.SendByName(world_id_, dest_name, data, data.Length);
                }
            }
            return(ret);
        }
Beispiel #10
0
        //------------------------------------------------------------------------------------------------------
        private void HandleAccountLogin(NodeMessage msg, int handle, uint seq)
        {
            StringBuilder stringBuilder = new StringBuilder(1024);
            int           size          = stringBuilder.Capacity;

            CenterHubApi.TargetName(UserServerConfig.WorldId, handle, stringBuilder, size);
            string node_name = stringBuilder.ToString();

            GameFrameworkMessage.NodeMessageWithAccount loginMsg = msg.m_NodeHeader as GameFrameworkMessage.NodeMessageWithAccount;
            if (null != loginMsg)
            {
                GameFrameworkMessage.AccountLogin protoData = msg.m_ProtoData as GameFrameworkMessage.AccountLogin;
                if (null != protoData)
                {
                    m_UserProcessScheduler.DefaultUserThread.QueueAction(m_UserProcessScheduler.DoAccountLogin, loginMsg.m_Account, protoData.m_Password, protoData.m_ClientInfo, node_name);
                }
            }
        }
 private void OnMessage(int worldId, uint seq, int source_handle, int dest_handle,
                        IntPtr data, int len)
 {
     try {
         if (worldId == UserServerConfig.WorldId)
         {
             if (IsUnknownServer(source_handle))
             {
                 StringBuilder sb = new StringBuilder(256);
                 if (CenterHubApi.TargetName(worldId, source_handle, sb, 256))
                 {
                     string name = sb.ToString();
                     if (name.StartsWith("NodeJs"))
                     {
                         m_NodeHandles.Add(source_handle);
                     }
                 }
             }
             byte[] bytes = new byte[len];
             Marshal.Copy(data, bytes, 0, len);
             if (IsNode(source_handle))
             {
                 if (!m_WaitQuit)
                 {
                     m_UserProcessScheduler.DispatchJsonMessage(false, seq, source_handle, dest_handle, bytes);
                 }
             }
             else if (IsDataCache(source_handle))
             {
                 m_DataCacheThread.DispatchAction(m_DataCacheChannel.Dispatch, source_handle, seq, bytes);
             }
         }
         else
         {
             if (IsUnknownServerOnBigworld(source_handle))
             {
                 StringBuilder sb = new StringBuilder(256);
                 if (CenterHubApi.TargetName(worldId, source_handle, sb, 256))
                 {
                     string name = sb.ToString();
                     if (name.StartsWith("NodeJs"))
                     {
                         m_NodeHandlesOnBigworld.Add(source_handle);
                     }
                 }
             }
             byte[] bytes = new byte[len];
             Marshal.Copy(data, bytes, 0, len);
             if (IsNodeOnBigworld(source_handle))
             {
                 if (!m_WaitQuit)
                 {
                     m_UserProcessScheduler.DispatchJsonMessage(true, seq, source_handle, dest_handle, bytes);
                 }
             }
             else if (IsLobbyOnBigworld(source_handle))
             {
                 m_GlobalProcessThread.QueueAction(m_BigworldChannel.Dispatch, source_handle, seq, bytes);
             }
         }
     } catch (Exception ex) {
         LogSys.Log(LOG_TYPE.ERROR, "Exception {0}\n{1}", ex.Message, ex.StackTrace);
     }
 }
 private void Release()
 {
     Stop();
     CenterHubApi.Release();
     LogSys.Release();
 }
        private void Loop()
        {
            try {
                while (CenterHubApi.IsRun())
                {
                    long curTime = TimeUtility.GetLocalMilliseconds();
                    if (m_LastTickTime != 0)
                    {
                        long elapsedTickTime = curTime - m_LastTickTime;
                        if (elapsedTickTime > c_WarningTickTime)
                        {
                            LogSys.Log(LOG_TYPE.MONITOR, "GameFramework Network Tick:{0}", curTime - m_LastTickTime);
                        }
                    }
                    m_LastTickTime = curTime;

                    CenterHubApi.Tick();
                    Thread.Sleep(10);
                    if (m_WaitQuit)
                    {
                        if (m_GlobalProcessThread.LastSaveFinished && m_UserProcessScheduler.LastSaveFinished && m_QuitFinish == false)
                        {
                            //全局数据和玩家数据存储完毕
                            int saveReqCount = m_DataCacheThread.CalcSaveRequestCount();
                            LogSys.Log(LOG_TYPE.MONITOR, "QuitStep_1. GlobalData and UserData last save done. SaveRequestCount:{0}", saveReqCount);
                            if (saveReqCount > 0)
                            {
                                //等待5s
                                long startTime = TimeUtility.GetLocalMilliseconds();
                                while (startTime + 5000 > TimeUtility.GetLocalMilliseconds())
                                {
                                    CenterClientApi.Tick();
                                    Thread.Sleep(10);
                                }
                            }
                            else
                            {
                                //通知关闭DataCache
                                m_QuitFinish = true;
                                LogSys.Log(LOG_TYPE.MONITOR, "QuitStep_2. Notice DataCache to quit.");
                                CenterClientApi.SendCommandByName("DataCache", "QuitDataStore");
                                //等待10s
                                long startTime = TimeUtility.GetLocalMilliseconds();
                                while (startTime + 10000 > TimeUtility.GetLocalMilliseconds())
                                {
                                    CenterClientApi.Tick();
                                    Thread.Sleep(10);
                                }
                                //关闭GameFramework
                                LogSys.Log(LOG_TYPE.MONITOR, "QuitStep_3. LastSaveDone. GameFramework quit...");
                                CenterClientApi.Quit();
                            }
                        }
                        else
                        {
                            if (curTime - m_LastWaitQuitTime > c_WaitQuitTimeInterval)
                            {
                                m_WaitQuit = false;
                                LogSys.Log(LOG_TYPE.MONITOR, "QuitStep_-1. Reset m_WaitQuit to false.");
                            }
                        }
                    }
                }
            } catch (Exception ex) {
                LogSys.Log(LOG_TYPE.ERROR, "GameFramework.Loop throw exception:{0}\n{1}", ex.Message, ex.StackTrace);
            }
        }
        private void Init(string[] args)
        {
            m_NameHandleCallback = this.OnNameHandleChanged;
            m_MsgCallback        = this.OnMessage;
            m_MsgResultCallback  = this.OnMessageResultCallback;
            m_CmdCallback        = this.OnCommand;
            m_LogHandler         = this.OnCenterLog;
            CenterHubApi.SetCenterLogHandler(m_LogHandler);
            CenterHubApi.Init("userserver", args.Length, args, m_NameHandleCallback, m_MsgCallback, m_MsgResultCallback, m_CmdCallback);

            LogSys.Init("./config/logconfig.xml");
            UserServerConfig.Init();

            GlobalVariables.Instance.IsClient = false;

            string key = "防君子不防小人";

            byte[] xor = Encoding.UTF8.GetBytes(key);

            ResourceReadProxy.OnReadAsArray = ((string filePath) => {
                byte[] buffer = null;
                try {
                    buffer = File.ReadAllBytes(filePath);
                } catch (Exception e) {
                    LogSys.Log(LOG_TYPE.ERROR, "Exception:{0}\n{1}", e.Message, e.StackTrace);
                    return(null);
                }
                return(buffer);
            });
            LogSystem.OnOutput += (Log_Type type, string msg) => {
                switch (type)
                {
                case Log_Type.LT_Debug:
                    LogSys.Log(LOG_TYPE.DEBUG, msg);
                    break;

                case Log_Type.LT_Info:
                    LogSys.Log(LOG_TYPE.INFO, msg);
                    break;

                case Log_Type.LT_Warn:
                    LogSys.Log(LOG_TYPE.WARN, msg);
                    break;

                case Log_Type.LT_Error:
                case Log_Type.LT_Assert:
                    LogSys.Log(LOG_TYPE.ERROR, msg);
                    break;
                }
            };

            LoadData();
            LogSys.Log(LOG_TYPE.INFO, "Init Config ...");
            s_Instance = this;
            InstallMessageHandlers();
            LogSys.Log(LOG_TYPE.INFO, "Init Messenger ...");
            m_DataCacheThread.Init(m_DataCacheChannel);
            LogSys.Log(LOG_TYPE.INFO, "Init DataCache ...");
            Start();
            LogSys.Log(LOG_TYPE.INFO, "Start Threads ...");
        }
Beispiel #15
0
    internal static void Init()
    {
        StringBuilder sb = new StringBuilder(256);

        if (CenterHubApi.GetConfig("DataStoreFlag", sb, 256))
        {
            string dsflag = sb.ToString();
            s_Instance.m_DataStoreFlag = (int.Parse(dsflag) != 0 ? true : false);
        }

        if (CenterHubApi.GetConfig("GMServerFlag", sb, 256))
        {
            string gsflag = sb.ToString();
            s_Instance.m_GMServerFlag = (int.Parse(gsflag) != 0 ? true : false);
        }

        if (CenterHubApi.GetConfig("Debug", sb, 256))
        {
            string debug = sb.ToString();
            s_Instance.m_Debug = (int.Parse(debug) != 0 ? true : false);

            if (s_Instance.m_Debug)
            {
                GameFramework.GlobalVariables.Instance.IsDebug = true;
            }
        }

        if (CenterHubApi.GetConfig("EnableDirectLogin", sb, 256))
        {
            string val = sb.ToString();
            s_Instance.m_EnableDirectLogin = (int.Parse(val) != 0 ? true : false);
        }

        if (CenterHubApi.GetConfig("UserSaveInterval", sb, 256))
        {
            string saveinterval = sb.ToString();
            int    val          = int.Parse(saveinterval);
            if (s_Instance.m_UserSaveInterval != val)
            {
                s_Instance.m_UserSaveInterval = val;
            }
        }

        if (CenterHubApi.GetConfig("ActivateCodeAvailable", sb, 256))
        {
            string activatecode = sb.ToString();
            s_Instance.m_ActivateCodeAvailable = (int.Parse(activatecode) != 0 ? true : false);
        }

        if (CenterHubApi.GetConfig("StartServerTime", sb, 256))
        {
            string time = sb.ToString();
            s_Instance.m_StartServerTime = time;
        }

        if (CenterHubApi.GetConfig("worldid", sb, 256))
        {
            string worldid = sb.ToString();
            int    val     = int.Parse(worldid);
            if (s_Instance.m_WorldId != val)
            {
                s_Instance.m_WorldId = val;
            }
        }

        if (CenterHubApi.GetConfig("centernum", sb, 256))
        {
            string worldidnum = sb.ToString();
            int    val        = int.Parse(worldidnum);
            if (s_Instance.m_WorldIdNum != val)
            {
                s_Instance.m_WorldIdNum = val;
            }
        }

        if (CenterHubApi.GetConfig("worldid0", sb, 256))
        {
            string worldid = sb.ToString();
            int    val     = int.Parse(worldid);
            if (s_Instance.m_WorldId0 != val)
            {
                s_Instance.m_WorldId0 = val;
            }
        }

        if (CenterHubApi.GetConfig("worldid1", sb, 256))
        {
            string worldid = sb.ToString();
            int    val     = int.Parse(worldid);
            if (s_Instance.m_WorldId1 != val)
            {
                s_Instance.m_WorldId1 = val;
            }
        }

        if (CenterHubApi.GetConfig("DSRequestTimeout", sb, 256))
        {
            s_Instance.m_DSRequestTimeout = uint.Parse(sb.ToString());
        }
    }