You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

230 lines
7.8 KiB

/*
Sog 游戏基础库
2016 by zouwei
*/
using System;
using System.Collections.Generic;
using System.Threading;
using Sog;
using SimpleHttpServer;
using Sog.Service;
using Sog.Log;
namespace HttpProxy
{
// 服务器入口
public class HttpProxyServer : IScript
{
private Guid m_guid;
private ServerApp m_app;
private HttpProxyMsgHandler m_messageHandler;
public HttpProxyServer()
{
m_guid = Guid.NewGuid();
}
public IScriptHotfixCheck GetScriptHotfixCheck()
{
return new ServerScriptHotfixCheck();
}
private void CommInitOnCreateReload(ServerApp app)
{
m_app = app;
RegisterAllService();
}
private void StartSimpleHttpServer()
{
var route_config = new List<Route>() {
new Route {
Name = "Hello Handler",
UrlRegex = @"^/$",
Method = "GET",
Callable = (HttpContext httpContext) => {
httpContext.httpResponse = new HttpResponse
{
ContentAsUTF8 = "Hello from SimpleHttpServer",
ReasonPhrase = "OK",
StatusCode = "200"
};
HttpServer.SendResponse(httpContext);
}
},
new Route {
Name = "Hero AD Playback",
UrlRegex = @"^/AdPlayback/$",
Method = "POST",
Callable = (HttpContext httpContext) => {
TraceLog.Error("ADADADADADADADADADAADAADADAADAADAD:"+httpContext.httpRequest.Content);
httpContext.httpResponse = new HttpResponse
{
ContentAsUTF8 = "AD Play Finish",
ReasonPhrase = "OK",
StatusCode = "200"
};
HttpServer.SendResponse(httpContext);
}
},
//new Route {
// Name = "FileSystem Static Handler",
// UrlRegex = @"^/Static/(.*)$",
// Method = "GET",
// Callable = new FileSystemRouteHandler() { BasePath = @"C:\Tmp", ShowDirectories=true }.Handle,
//},
new Route {Name = "Mini Msg callback", UrlRegex = @"/MiniMsg",Method = "POST",
Callable = WXMsgHandler.HomeMiniMsgBack},
new Route {Name = "Mini Msg callbackGET", UrlRegex = @"/MiniMsg",Method = "GET",
Callable = WXMsgHandler.HomeMiniMsgBack},
};
//http://43.139.10.237:8181/AdPlayback/
HttpServer httpServer = new HttpServer(31005, route_config);
httpServer.ProcessorThreadEndWaitTimeMs = 5000;
Thread thread = new Thread(httpServer.Listen);
//设置成后台线程,如果不是后台线程,线程不是主动关闭,则会一直运行,那怕进程主线程退出也没用
thread.IsBackground = true;
thread.Start();
TraceLog.Debug("SimpleHttpServer listen on 31005");
}
public virtual void OnCreate(ServerApp app)
{
//打印消息统计日志
app.GetCluster().NeedLogMsgStat = true;
//多线程特殊需求
app.GetCluster().EnableMultiThreadSendSafe();
m_app = app;
RegisterAllDataObj();
CommInitOnCreateReload(app);
StartSimpleHttpServer();
TraceLog.Debug("HttpProxyServer start at {0}, guid {1}", DateTime.Now, m_guid);
}
public virtual void OnHotfix(ServerApp app)
{
m_app = app;
ReadServerConfig();
CommInitOnCreateReload(app);
TraceLog.Debug("HttpProxyServer hotfix at {0}, guid {1}", DateTime.Now, m_guid);
}
public virtual void OnReloadConfig(string excelConfigFile)
{
TraceLog.Debug("Server OnReloadConfig at {0}", DateTime.Now);
ReadServerConfig();
}
public virtual void OnTick(long nowMs)
{
ServerStat.Instance.Tick(nowMs);
long nowSec = m_app.GetTimeSecond();
HuaweiAuth.Update(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("HttpProxyServer be disposed");
m_app = null;
m_messageHandler = null;
//关闭所有消息处理任务
MessageTaskDistributor.Instance.CloseAllTask();
}
private void ReadServerConfig()
{
//读取配置文件
string strLogicServerConfig = m_app.GetCluster().GetAppConfigPath() + "/" + m_app.AppParam.ServerConfig.configfile;
HttpProxyServerConfig serverConfig = ServerConfigMgr.Instance.ReloadServerConfig<HttpProxyServerConfig>(m_app,strLogicServerConfig);
}
private void RegisterAllDataObj()
{
HttpProxyServerData serverData = new HttpProxyServerData(m_app);
ServerDataObjMgr.Instance.RegisterDataObj(serverData);
GameConfigMgr.Instance.ReadAllConfig();
AppTime.UpdateGameResetHour(CommParamDescMgr.Instance.CrossDaysTime.int_val);
ReadServerConfig();
}
private void RegisterAllService()
{
TraceLog.Debug("HttpProxyServer RegisterAllService");
InitMessageTask();
m_messageHandler = new HttpProxyMsgHandler(m_app);
ServiceMgr.Instance.RegisterService(m_messageHandler);
HttpProxyServerConfig config = HttpProxyServerUtils.GetServerConfig();
//放到配置文件里
//建新申请的测试app:token:542562932612342|ZT3Ey6eGvrJTOzqnYHzFmmHdP6g id:542562932612342" seckey:f551145fa89a1fb95cc197a2f002797b
//sg 1063277453733016 b2e1ee0b6dbc7f33f506af345481ba7d
FacebookAuth.Init(config.facebook_appid, config.facebook_seckey);
HeroAuth.Init("366", "2d2fa08727b54f5f96c19d2e1b863e8d");
HeroUSDKAuth.Init(config);
// 华为HMSCore
HuaweiAuth.Init(config.huaweiClientId, config.huaweiClientSecret, config.huaweiGetAccessTokenUrl, config.huaweiGetIDTokenUrl);
// 华为AGC
HuaweiAuth.InitAGC(config.huaweiAGC_ClientId, config.huaweiAGC_ClientSecret
, config.huaweiAGC_GetTokenUrl, config.huaweiAGC_VerifyTokenUrl, config.huaweiAGC_ProductId);
GmCmdSvc gmCmdSvc = new GmCmdSvc();
ServiceMgr.Instance.RegisterService(gmCmdSvc);
}
/// <summary>
/// 初始化消息处理任务
/// </summary>
private void InitMessageTask()
{
//分配消息任务,写死10个正常的,1个推送的
uint iTotalTaskCount = (uint)HttpProxyServerUtils.HttpProxyLoginWorkThreadCount;
TraceLog.Debug("HttpProxyServer.InitMessageTask messageTaskCount is {0}", iTotalTaskCount);
// 避免http请求阻塞时影响同尾数uid的玩家登录
MessageTaskDistributor.Instance.InitTask(iTotalTaskCount, null);
MessageTaskDistributor.Instance.StartAllTask(MessageTaskHandler.HandlerPacket);
}
}
}