/* Sog 游戏基础库 2016 by zouwei */ using System; using System.Collections.Generic; using System.Threading; using Sog; using SimpleHttpServer; using Sog.Service; using Sog.Log; namespace HttpProxyPay { // 服务器入口 public class HttpProxyPayServer : IScript { private Guid m_guid; private ServerApp m_app; private HttpProxyPayMsgHandler m_messageHandler; private long m_tickSecondMs; public HttpProxyPayServer() { m_guid = Guid.NewGuid(); } public IScriptHotfixCheck GetScriptHotfixCheck() { return new ServerScriptHotfixCheck(); } private void CommInitOnCreateReload(ServerApp app) { m_app = app; RegisterAllService(); DBServerSelect.Init(app); } private void StartSimpleHttpServer() { var route_config = new List() { new Route {Name = "HeroPay", UrlRegex = @"^/heropay_sea/$", Method = "POST", Callable = HeroPayHttpHandler.HeroPayCallback_SEA}, new Route {Name = "HeroPay", UrlRegex = @"^/heropay_jp/$", Method = "POST", Callable = HeroPayHttpHandler.HeroPayCallback_JP}, new Route {Name = "HeroPay", UrlRegex = @"^/heropay_kr/$", Method = "POST", Callable = HeroPayHttpHandler.HeroPayCallback_KR}, new Route {Name = "HeroPay", UrlRegex = @"^/heropay_zh/$", Method = "POST", Callable = HeroPayHttpHandler.HeroPayCallback_ZH}, new Route {Name = "HeroPay", UrlRegex = @"^/heropay_jp2/$", Method = "POST", Callable = HeroPayHttpHandler.HeroPayCallback_JP2}, new Route {Name = "HeroPay", UrlRegex = @"^/heropay_en/$", Method = "POST", Callable = HeroPayHttpHandler.HeroPayCallback_EN}, new Route {Name = "Hero AD Playback", UrlRegex = @"^/AdPlayback/",Method = "GET", Callable = HeroPayHttpHandler.HomeAdCallBack}, new Route {Name = "Mini pay", UrlRegex = @"/MiniPayback",Method = "POST", Callable = HeroPayHttpHandler.HomeMiniCallBack}, new Route {Name = "Mini pay", UrlRegex = @"/MiniPayback",Method = "GET", Callable = HeroPayHttpHandler.HomeMiniCallBack}, new Route {Name = "Mini pay", UrlRegex = @"/MiniJSAPIPayback",Method = "POST", Callable = WXHandler.HomeMiniJSAPICallBack}, new Route {Name = "Mini pay", UrlRegex = @"/MiniJSAPICall",Method = "GET", Callable = WXHandler.MiniJSAPICall} }; HttpServer httpServer = new HttpServer(31006, route_config); //等待5秒钟再关系线程,防止线程结束网络对象被垃圾回收 httpServer.ProcessorThreadEndWaitTimeMs = 5000; Thread thread = new Thread(httpServer.Listen); //设置成后台线程,如果不是后台线程,线程不是主动关闭,则会一直运行,那怕进程主线程退出也没用 thread.IsBackground = true; thread.Start(); TraceLog.Debug("HttpProxyPayServer.StartSimpleHttpServer listen on 31006"); } public virtual void OnCreate(ServerApp app) { TraceLog.Debug("HttpProxyPayServer.OnCreate at {0}, guid {1}", DateTime.Now, m_guid); app.GetCluster().NeedLogMsgStat = true; //多线程特殊需求 app.GetCluster().EnableMultiThreadSendSafe(); m_app = app; RegisterAllDataObj(); CommInitOnCreateReload(app); // 注意httpsvr目前不支持reload StartSimpleHttpServer(); } public virtual void OnHotfix(ServerApp app) { TraceLog.Debug("HttpProxyPayServer.OnHotfix at {0}, guid {1}", DateTime.Now, m_guid); m_app = app; ReadServerConfig(); CommInitOnCreateReload(app); } public virtual void OnReloadConfig(string excelConfigFile) { TraceLog.Debug("HttpProxyPayServer.OnReloadConfig at {0}", DateTime.Now); ReadServerConfig(); } private void TickSecond(long nowMs, long nowSec) { if (nowMs < m_tickSecondMs + 1000) { return; } m_tickSecondMs = nowMs; HeroUSDKSvc.Tick(nowMs); HttpContextCache.TickTimeoutClient(nowSec); } public virtual void OnTick(long nowMs) { ServerStat.Instance.Tick(nowMs); long nowSec = m_app.GetTimeSecond(); TickSecond(nowMs, nowSec); } public virtual void OnMessage(uint remoteAppID, MessageData message) { m_messageHandler.HandlerMessage(remoteAppID, message); } public virtual void OnStop() { //很多服务器停服可以直接退出 m_app.SetStopSuccess(); //关闭所有消息处理任务 MessageTaskDistributor.Instance.CloseAllTask(); } //所有引用对象置空 public void Dispose() { TraceLog.Debug("HttpProxyPayServer.Dispose"); m_app = null; m_messageHandler = null; //关闭所有消息处理任务 MessageTaskDistributor.Instance.CloseAllTask(); } private void ReadServerConfig() { //读取配置文件 string strLogicServerConfig = m_app.GetCluster().GetAppConfigPath() + "/" + m_app.AppParam.ServerConfig.configfile; HttpProxyPayServerConfig serverConfig = ServerConfigMgr.Instance.ReloadServerConfig(m_app,strLogicServerConfig); var svrData = HttpProxyPayServerUtils.GetHttpProxyServerData(); svrData.selfPayId = serverConfig.selfPayId; svrData.SetPayUrlConfig(serverConfig.payUrls); } private void RegisterAllDataObj() { HttpProxyPayServerData serverData = new HttpProxyPayServerData(m_app); ServerDataObjMgr.Instance.RegisterDataObj(serverData); PlayerTable playerTable = new PlayerTable(); ServerDataObjMgr.Instance.RegisterDataObj(playerTable); ReadServerConfig(); GameConfigMgr.Instance.ReadAllConfig(); AppTime.UpdateGameResetHour(CommParamDescMgr.Instance.CrossDaysTime.int_val); } private void RegisterAllService() { TraceLog.Debug("HttpProxyPayServer.RegisterAllService"); InitMessageTask(); m_messageHandler = new HttpProxyPayMsgHandler(m_app); ServiceMgr.Instance.RegisterService(m_messageHandler); GmCmdSvc gmCmdSvc = new GmCmdSvc(); ServiceMgr.Instance.RegisterService(gmCmdSvc); PlayerTableOp playerTableOp = new PlayerTableOp(HttpProxyPayServerUtils.GetPlayerTable()); ServiceMgr.Instance.RegisterService(playerTableOp); HttpProxyPayServerConfig config = HttpProxyPayServerUtils.GetServerConfig(); // 英雄usdk HeroUSDKSvc.Init(config); // 英雄支付回调的key HeroPayHttpHandler.Init(config); // 华为支付 HuaweiPaySvc.Init(config.huaweiClientId, config.huaweiClientSecret , config.huaweiPayCheckUrl, config.huaweiGetTokenUrl); // 获取一次Token HuaweiPaySvc.GetAppAccessToken(); } /// /// 初始化消息处理任务 /// private void InitMessageTask() { uint iTotalTaskCount = HttpProxyPayServerUtils.HttpWorkThreadCount; TraceLog.Debug("HttpProxyPayServer.InitMessageTask messageTaskCount is {0}", iTotalTaskCount); MessageTaskDistributor.Instance.InitTask(iTotalTaskCount); MessageTaskDistributor.Instance.StartAllTask(MessageTaskHandler.HandlerPacket); } } }