2025-11-16 19:16:44 +08:00

184 lines
5.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using CGamesServer.Api;
using CGamesServer.Api.ServerLogic;
using CGamesServer.Api.WebServers;
using CGamesServer.Business.Extensions;
using CGamesServer.DataModel;
using CrasyStudio.Core.Common;
using CrasyStudio.Core.Common.Eitities;
using CrasyStudio.Core.Common.Status;
using CrasyStudio.Core.Common.Tools;
using CrasyStudio.Core.Common.Tools.Extensions;
using Microsoft.AspNetCore.Server.Kestrel.Core;
//using CGamesServer.Api.WebServers.Plugins;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
//我的集群服务器标签
Global.MyServerClusterTag = builder.Configuration.GetSection("Cluster:MyServer").Get<string>()!;
string certPath = builder.Configuration.GetSection("Cert:Path").Get<string>()!;
string certPassword = builder.Configuration.GetSection("Cert:Passsword").Get<string>()!;
if (!string.IsNullOrWhiteSpace(certPath) && !string.IsNullOrWhiteSpace(certPassword))
{
builder.WebHost.ConfigureKestrel((context, kesetrelOptions) =>
{
kesetrelOptions.ConfigureEndpointDefaults((ListenOptions listenOptions) =>
{
#if !DEBUG
listenOptions.UseHttps(certPath, certPassword);
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
#endif
//listenOptions.Protocols = HttpProtocols.Http1AndHttp2AndHttp3;
});
});
}
new AppSettings(AppDomain.CurrentDomain.BaseDirectory);
List<string> urls = new List<string>();// builder.Configuration.GetSection("Urls").Get<string[]>()!;
if (args.Length > 1)
{
urls.Add(args[1]);
}
ushort listenPort = 5200;
#if DEBUG
builder.WebHost.UseUrls($"http://*:{listenPort}");
#else
if (urls != null && urls.Count > 0)
{
listenPort = ushort.Parse(urls[0].Split(':')[2]);
builder.WebHost.UseUrls(urls[0]);
}
#endif
//SqlSugar ORM 配置
builder.Services.AddSqlSugar(builder.Configuration);
//注入仓储
builder.Services.AddScoped(typeof(SqlSugarRepository<>));
//注入各Service
builder.Services.AddServices(builder.Configuration);
// 跨域配置
builder.Services.AddCors(builder.Configuration);
builder.Services.AddControllers();
//builder.Services.AddLogicApis(typeof(IBaseApi));
//添加websocket配置
//await builder.Services.AddWebSocketServer(builder.Configuration, listenPort, new List<Type>() { typeof(GameWebSocketPlugin) }, new List<Type>() { });
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var redisOptions = builder.Configuration.GetSection("RedisOptions").Get<RedisOptions>();
Global.ServerToken = builder.Configuration.GetSection("ServerToken").Get<string>()!;
Global.FlightTime = builder.Configuration.GetSection("FlightTime").Get<int>();
Global.FlightCollDownTipTime = builder.Configuration.GetSection("FlightCollDownTipTime").Get<int>();
//if (args.Length > 3)
//{
// Global.ServerToken += "_" + args[3];
//}
//服务器的关键标识加上前缀
Global.RoomListKey = PrefixTool.GetPatchID(Global.RoomListKey);
Global.RedisLockKey = PrefixTool.GetPatchID(Global.RedisLockKey);
Global.RedisTimerKey = PrefixTool.GetPatchID(Global.RedisTimerKey);
Global.RedisTimerToken = PrefixTool.GetPatchID(Global.RedisTimerToken);
Console.WriteLine("ServerToken:" + Global.ServerToken + Environment.NewLine);
////var transportServer = new WebTransportServer();
////.AddJsonProtocol()
//builder.Services.AddSignalR((hubOptions) =>
//{
// hubOptions.SupportedProtocols!.Remove("json");
// //hubOptions.ClientTimeoutInterval = TimeSpan.FromSeconds(2);
// //hubOptions.KeepAliveInterval = TimeSpan.FromSeconds(1.5);
//}).AddMessagePackProtocol().AddStackExchangeRedis((options) =>
//{
// options.Configuration = new ConfigurationOptions
// {
// EndPoints = { { IPAddress.Parse(redisOptions!.Host!), int.Parse(redisOptions!.Port!) } },
// Password = redisOptions!.Password
// //AsyncTimeout = 5000,
// //SyncTimeout = 5000
// };
//});
//注入HttpContextAccessor
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
//注入Redis服务
var redisHelper = (await builder.Services.AddRedis(builder.Configuration, Global.MyServerClusterTag))!;
GameRoomServer gameRoomServer = new GameRoomServer(redisHelper);
//实例化一些状态,事件相关类
ClusterOnlineUsers onlineUsers = new ClusterOnlineUsers(redisHelper, Global.MyServerClusterTag);
var netEventRegister = new NetEventRegister(onlineUsers);
//注册网络包业务逻辑事件
netEventRegister.InitCallbackEvents();
//注入一些业务逻辑必须用到的相关类
builder.Services.AddSingleton(redisHelper);
builder.Services.AddSingleton(onlineUsers);
builder.Services.AddSingleton(netEventRegister);
//注入游戏房间服务,单例
var servers = builder.Services.AddSingleton(gameRoomServer);
////注入WebTransportServer服务单例
//builder.Services.AddSingleton<WebTransportServer>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
//app.UseHttpsRedirection();
app.UseCors("Policy");
app.UseAuthentication();
app.UseAuthorization();
////WebTransport Handle
//app.Use(async (context, next) =>
//{
// //bool isHandled = await transportServer.HandleHttpContext(context);
// //if (!isHandled)
// //{
// // await next();
// //}
//});
app.MapControllers();
//var webSocketOptions = new WebSocketOptions
//{
// //KeepAliveInterval = TimeSpan.FromMinutes(2)
//};
//app.UseWebSockets(webSocketOptions);
//await gameRoomServer.Init();
var webApiTask = app.RunAsync();
////实例化WebSocket游戏服务并启动
//new GameServer(onlineUsers, netEventRegister).Start(listenPort + 2);
// 等待所有服务结束
await Task.WhenAll(webApiTask);