|
29 | 29 | // Official Documentation: https://gameframex.doc.alianblank.com/ |
30 | 30 | // ========================================================================================== |
31 | 31 |
|
| 32 | +using System.Net; |
32 | 33 | using GameFrameX.AppHost.ServiceDefaults; |
33 | 34 | using GameFrameX.Foundation.Logger; |
34 | 35 | using GameFrameX.Foundation.Localization.Core; |
35 | 36 | using GameFrameX.NetWork.Abstractions; |
36 | 37 | using GameFrameX.NetWork.HTTP; |
| 38 | +using GameFrameX.NetWork.Kcp; |
37 | 39 | using GameFrameX.NetWork.Message; |
38 | 40 | using GameFrameX.SuperSocket.Connection; |
39 | 41 | using GameFrameX.SuperSocket.Primitives; |
@@ -171,6 +173,41 @@ protected virtual ValueTask OnDisconnected(IAppSession appSession, CloseEventArg |
171 | 173 | return ValueTask.CompletedTask; |
172 | 174 | } |
173 | 175 |
|
| 176 | + /// <summary> |
| 177 | + /// KCP客户端连接成功处理 |
| 178 | + /// </summary> |
| 179 | + /// <param name="remoteEndPoint">远程端点</param> |
| 180 | + /// <returns></returns> |
| 181 | + protected virtual ValueTask OnKcpConnected(EndPoint remoteEndPoint) |
| 182 | + { |
| 183 | + LogHelper.Info(LocalizationService.GetString(Localization.Keys.StartUp.KcpServer.NewClientConnection, remoteEndPoint)); |
| 184 | + return ValueTask.CompletedTask; |
| 185 | + } |
| 186 | + |
| 187 | + /// <summary> |
| 188 | + /// KCP消息处理方法 |
| 189 | + /// </summary> |
| 190 | + /// <param name="session">游戏应用会话</param> |
| 191 | + /// <param name="message">接收到的消息</param> |
| 192 | + protected virtual void KcpPackageHandler(IGameAppSession session, IMessage message) |
| 193 | + { |
| 194 | + if (Setting.IsDebug && Setting.IsDebugReceive) |
| 195 | + { |
| 196 | + LogHelper.Debug(LocalizationService.GetString(Localization.Keys.StartUp.TcpServer.MessageReceived, ServerType, message.ToFormatMessageString())); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + /// <summary> |
| 201 | + /// KCP客户端断开连接处理 |
| 202 | + /// </summary> |
| 203 | + /// <param name="remoteEndPoint">远程端点</param> |
| 204 | + /// <returns></returns> |
| 205 | + protected virtual ValueTask OnKcpDisconnected(EndPoint remoteEndPoint) |
| 206 | + { |
| 207 | + LogHelper.Info(LocalizationService.GetString(Localization.Keys.StartUp.KcpServer.ClientDisconnected, remoteEndPoint)); |
| 208 | + return ValueTask.CompletedTask; |
| 209 | + } |
| 210 | + |
174 | 211 | /// <summary> |
175 | 212 | /// 客户端连接成功时的处理方法。 |
176 | 213 | /// </summary> |
@@ -358,6 +395,34 @@ private async Task StartServer(List<BaseHttpHandler> baseHandler, Func<string, B |
358 | 395 | LogHelper.Info(LocalizationService.GetString(Localization.Keys.StartUp.WebSocketServer.ServiceNotEnabled, ServerType, Setting.WsPort)); |
359 | 396 | } |
360 | 397 |
|
| 398 | + // 启动KCP服务器 |
| 399 | + if (Setting.IsEnableKcp) |
| 400 | + { |
| 401 | + var kcpPort = Setting.KcpPort > 0 ? Setting.KcpPort : Setting.InnerPort; |
| 402 | + if (kcpPort > 0 && NetHelper.PortIsAvailable(kcpPort)) |
| 403 | + { |
| 404 | + LogHelper.Info(LocalizationService.GetString(Localization.Keys.StartUp.KcpServer.StartingServer, ServerType, Setting.InnerHost, kcpPort)); |
| 405 | + var kcpServer = new KcpServer( |
| 406 | + kcpPort, |
| 407 | + new KcpOptions { Enable = true }, |
| 408 | + Setting, |
| 409 | + KcpPackageHandler, |
| 410 | + OnKcpConnected, |
| 411 | + OnKcpDisconnected |
| 412 | + ); |
| 413 | + _ = kcpServer.StartAsync(); |
| 414 | + LogHelper.Info(LocalizationService.GetString(Localization.Keys.StartUp.KcpServer.StartupComplete, ServerType, Setting.InnerHost, kcpPort)); |
| 415 | + } |
| 416 | + else |
| 417 | + { |
| 418 | + LogHelper.Warning(LocalizationService.GetString(Localization.Keys.StartUp.KcpServer.StartupFailed, ServerType, Setting.InnerHost, kcpPort)); |
| 419 | + } |
| 420 | + } |
| 421 | + else |
| 422 | + { |
| 423 | + LogHelper.Info(LocalizationService.GetString(Localization.Keys.StartUp.KcpServer.ServerDisabled, ServerType, Setting.InnerHost, Setting.KcpPort)); |
| 424 | + } |
| 425 | + |
361 | 426 | // await StartHttpServerAsync(hostBuilder,baseHandler, httpFactory, aopHandlerTypes, minimumLevelLogLevel); |
362 | 427 | await StartHttpServer(baseHandler, httpFactory, aopHandlerTypes, minimumLevelLogLevel); |
363 | 428 |
|
|
0 commit comments