SessionService.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. using DeepCore;
  2. using DeepCore.IO;
  3. using DeepCore.Log;
  4. using DeepCore.Threading;
  5. using DeepCrystal;
  6. using DeepCrystal.ORM.Generic;
  7. using DeepCrystal.ORM.Query;
  8. using DeepCrystal.RPC;
  9. using DeepMMO.Data;
  10. using DeepMMO.Protocol;
  11. using DeepMMO.Protocol.Client;
  12. using DeepMMO.Server.Area;
  13. using DeepMMO.Server.SystemMessage;
  14. using System;
  15. using System.Threading.Tasks;
  16. namespace DeepMMO.Server.Connect
  17. {
  18. /// <summary>
  19. /// 单个链接服务
  20. /// </summary>
  21. public partial class SessionService : IService
  22. {
  23. public readonly Logger log;
  24. public readonly string accountID;
  25. public readonly TypeCodec client_battle_action_codec;
  26. public readonly TypeCodec session_battle_action_codec;
  27. protected ConnectServer.ViewSession session { get; private set; }
  28. protected ClientEnterServerRequest enter { get; private set; }
  29. protected IRemoteService remote_logic_service { get; private set; }
  30. protected IRemoteService remote_area_service { get; private set; }
  31. protected IRemoteService remote_log_service { get; private set; }
  32. protected ClientEnterGameRequest enter_game { get; private set; }
  33. public string Channel { get; private set; }
  34. protected IDisposable heartbeat_timer;
  35. protected DateTime last_heartbeat = DateTime.Now;
  36. protected string sessionToken;
  37. protected MappingReference<AccountData> accountSave;
  38. protected QueryMappingReference<RoleSnap> queryRoleSnap;
  39. protected MappingReference<AccountRoleSnap> accountRoleSnapSave;
  40. protected QueryMappingReference<RoleDataStatusSnap> queryRoleDataStatusSnap;
  41. protected bool mDisconnected = true;
  42. public override ServiceProperties Properties
  43. {
  44. get
  45. {
  46. var ret = base.Properties;
  47. ret.IgnoreRequestError = true;
  48. ret.IgnoreResponseError = true;
  49. return ret;
  50. }
  51. }
  52. public SessionService(ServiceStartInfo start) : base(start)
  53. {
  54. this.log = LoggerFactory.GetLogger(start.Address.ServiceName);
  55. this.accountID = start.Config["accountID"].ToString();
  56. this.client_battle_action_codec = ConnectServer.ClientCodec.Factory.GetCodec(typeof(ClientBattleAction));
  57. this.session_battle_action_codec = base.ServerCodec.Factory.GetCodec(typeof(SessionBattleAction));
  58. this.Channel = start.Config["channel"]?.ToString();
  59. }
  60. protected override void OnDisposed()
  61. {
  62. this.accountSave.Dispose();
  63. this.accountRoleSnapSave.Dispose();
  64. this.queryRoleSnap.Dispose();
  65. this.queryRoleDataStatusSnap.Dispose();
  66. this.session = null;
  67. this.enter = null;
  68. this.remote_logic_service = null;
  69. this.remote_area_service = null;
  70. this.enter_game = null;
  71. this.heartbeat_timer = null;
  72. this.sessionToken = null;
  73. this.accountSave = null;
  74. this.queryRoleSnap = null;
  75. this.queryRoleDataStatusSnap = null;
  76. this.accountRoleSnapSave = null;
  77. }
  78. protected override async Task OnStartAsync()
  79. {
  80. this.accountSave =
  81. new MappingReference<AccountData>(RPGServerPersistenceManager.TYPE_ACCOUNT_DATA, accountID, this);
  82. this.queryRoleSnap =
  83. new QueryMappingReference<RoleSnap>(RPGServerPersistenceManager.TYPE_ROLE_SNAP_DATA, this);
  84. this.accountRoleSnapSave =
  85. new MappingReference<AccountRoleSnap>(RPGServerPersistenceManager.TYPE_ACCOUNT_ROLE_SNAP_DATA,
  86. accountID, this);
  87. this.queryRoleDataStatusSnap =
  88. new QueryMappingReference<RoleDataStatusSnap>(
  89. RPGServerPersistenceManager.TYPE_ROLE_DATA_STATUS_SNAP_DATA, this);
  90. this.Provider.AutoDispose(accountSave);
  91. this.Provider.AutoDispose(queryRoleSnap);
  92. this.Provider.AutoDispose(accountRoleSnapSave);
  93. this.Provider.AutoDispose(queryRoleDataStatusSnap);
  94. this.heartbeat_timer = base.Provider.CreateTimer(CheckHeartbeat, this,
  95. TimeSpan.FromSeconds(TimerConfig.timer_sec_SessionKeepTimeout / 2),
  96. TimeSpan.FromSeconds(TimerConfig.timer_sec_SessionKeepTimeout / 2));
  97. var data = await this.accountSave.LoadDataAsync();
  98. var roleSnap = await this.accountRoleSnapSave.LoadDataAsync();
  99. remote_log_service = await this.Provider.GetAsync(ServerNames.LogService);
  100. }
  101. protected override async Task OnStopAsync(ServiceStopInfo reason)
  102. {
  103. this.heartbeat_timer.Dispose();
  104. if (session != null)
  105. {
  106. this.session.socket.Disconnect(reason.Reason);
  107. }
  108. await ShutdownLogicServiceAsync("session destroy");
  109. await this.accountSave.FlushAsync();
  110. }
  111. protected void CheckHeartbeat(object state)
  112. {
  113. if (DateTime.Now - last_heartbeat > TimeSpan.FromSeconds(TimerConfig.timer_sec_SessionKeepTimeout))
  114. {
  115. if (session == null || session.socket.IsConnected == false)
  116. {
  117. this.ShutdownSelf("timeout");
  118. }
  119. }
  120. }
  121. [RpcHandler(typeof(SystemShutdownNotify))]
  122. public virtual void system_rpc_Handle(SystemShutdownNotify shutdown)
  123. {
  124. var logic = remote_logic_service;
  125. if (logic != null)
  126. {
  127. logic.Invoke(new SessionDisconnectNotify() {sessionName = SelfAddress.ServiceName,});
  128. }
  129. this.ShutdownSelf(shutdown.reason);
  130. }
  131. [RpcHandler(typeof(KickPlayerNotify))]
  132. public virtual void rpc_Handle(KickPlayerNotify notify)
  133. {
  134. if (session != null)
  135. {
  136. this.ShutdownSelf(notify.reason);
  137. }
  138. }
  139. //--------------------------------------------------------------------------------------------------------------------------------------------
  140. /// <summary>
  141. /// 首次连接或者重新连接
  142. /// </summary>
  143. /// <param name="bind"></param>
  144. /// <param name="cb"></param>
  145. [RpcHandler(typeof(LocalBindSessionRequest), typeof(LocalBindSessionResponse), ServerNames.ConnectServerType)]
  146. public virtual async Task<LocalBindSessionResponse> connect_rpc_Handle(LocalBindSessionRequest bind)
  147. {
  148. if (!string.IsNullOrEmpty(bind.enter.c2s_session_token) && !string.IsNullOrEmpty(this.sessionToken) &&
  149. bind.enter.c2s_session_token != this.sessionToken)
  150. {
  151. this.sessionToken = null;
  152. return (new LocalBindSessionResponse() {s2c_code = Response.CODE_ERROR});
  153. }
  154. var savedLoginToken = await accountSave.LoadFieldAsync<string>(nameof(AccountData.lastLoginToken));
  155. var savedServerGroup = await accountSave.LoadFieldAsync<string>(nameof(AccountData.lastLoginServerGroupID));
  156. if (savedLoginToken != bind.enter.c2s_login_token)
  157. {
  158. this.sessionToken = null;
  159. return (new LocalBindSessionResponse() {s2c_code = Response.CODE_ERROR});
  160. }
  161. var old_session = this.session;
  162. if (old_session != null)
  163. {
  164. var disconnect = new SessionDisconnectNotify()
  165. {
  166. socketID = old_session.socket.ID,
  167. sessionName = SelfAddress.ServiceName,
  168. };
  169. if (enter_game != null)
  170. {
  171. disconnect.roleID = enter_game.c2s_roleUUID;
  172. }
  173. //老Session暂停发包//
  174. var logic = this.remote_logic_service;
  175. if (logic != null)
  176. {
  177. logic.Invoke(disconnect);
  178. }
  179. //log.Log("Reconnect");
  180. //老Session踢下线//
  181. old_session.socket.Disconnect("New Session Reconnect");
  182. }
  183. else
  184. {
  185. //log.Log("Connect");
  186. }
  187. this.session = bind.session;
  188. this.enter = bind.enter;
  189. //登录成功后,产生新的Token用于断线重连//
  190. this.sessionToken = Guid.NewGuid().ToString();
  191. this.last_heartbeat = DateTime.Now;
  192. return (new LocalBindSessionResponse()
  193. {
  194. session = this,
  195. sessionToken = sessionToken,
  196. serverGroupID = savedServerGroup,
  197. });
  198. }
  199. /// <summary>
  200. /// 用户断线
  201. /// </summary>
  202. /// <param name="disconnect"></param>
  203. [RpcHandler(typeof(SessionDisconnectNotify), ServerNames.ConnectServerType)]
  204. public virtual void connect_rpc_Handle(SessionDisconnectNotify disconnect)
  205. {
  206. //log.Log("Disconnect");
  207. last_heartbeat = DateTime.Now;
  208. disconnect.sessionName = SelfAddress.ServiceName;
  209. if (enter_game != null)
  210. {
  211. disconnect.roleID = enter_game.c2s_roleUUID;
  212. }
  213. // var area = remote_area_service;
  214. // if (area != null)
  215. // {
  216. // area.Invoke(disconnect);
  217. // }
  218. //排除老Session踢下线导致的Disconnect//
  219. if (this.session == null || disconnect.socketID == this.session.socket.ID)
  220. {
  221. this.session = null;
  222. var logic = this.remote_logic_service;
  223. if (logic != null)
  224. {
  225. mDisconnected = true;
  226. logic.Invoke(disconnect);
  227. }
  228. }
  229. }
  230. /// <summary>
  231. /// 从网络线程接受协议
  232. /// </summary>
  233. /// <param name="session"></param>
  234. /// <param name="route_codec"></param>
  235. /// <param name="binary"></param>
  236. /// <param name="cb"></param>
  237. public void connect_OnReceivedBinaryImmediately(TypeCodec route_codec, BinaryMessage binary,
  238. OnRpcBinaryReturn cb = null)
  239. {
  240. last_heartbeat = DateTime.Now;
  241. if (client_battle_action_codec.MessageID == route_codec.MessageID)
  242. {
  243. SendToArea(route_codec, binary);
  244. }
  245. else
  246. {
  247. this.Provider.Execute(new Action(do_async_OnReceivedBinaryImmediately));
  248. void do_async_OnReceivedBinaryImmediately()
  249. {
  250. SendToLogic(route_codec, binary, cb);
  251. }
  252. }
  253. }
  254. /// <summary>
  255. /// 玩家进入游戏
  256. /// </summary>
  257. /// <param name="enter"></param>
  258. /// <param name="cb"></param>
  259. [RpcHandler(typeof(ClientEnterGameRequest), typeof(ClientEnterGameResponse), ServerNames.ConnectServerType)]
  260. public virtual async Task<ClientEnterGameResponse> client_rpc_Handle(ClientEnterGameRequest enter)
  261. {
  262. //验证此角色UID是否在此账号列表中
  263. var roleIDMap = accountRoleSnapSave.Data.roleIDMap;
  264. if (!roleIDMap.ContainsKey(enter.c2s_roleUUID))
  265. {
  266. return new ClientEnterGameResponse()
  267. {
  268. s2c_code = ClientEnterGameResponse.CODE_ROLEID_INVAILD,
  269. };
  270. }
  271. //第三方/一号通验证//
  272. var serverPassportResult = await RPGServerManager.Instance.Passport.VerifyEnterGameAsync(this.enter, enter);
  273. if (!serverPassportResult.Verified)
  274. {
  275. return new ClientEnterGameResponse()
  276. {
  277. s2c_code = ClientEnterGameResponse.CODE_ROLE_SUSPEND,
  278. s2c_msg = serverPassportResult.Message
  279. };
  280. }
  281. //log.Log("ClientEnterGameRequest");
  282. #region 账号封停验证.
  283. var statusSnap = await queryRoleDataStatusSnap.LoadDataAsync(enter.c2s_roleUUID);
  284. if (statusSnap != null)
  285. {
  286. //没过期代表已封停.
  287. if (!((DateTime.UtcNow - statusSnap.SuspendDate).TotalMilliseconds > 0))
  288. {
  289. return (new ClientEnterGameResponse()
  290. {
  291. s2c_code = ClientEnterGameResponse.CODE_ROLE_SUSPEND,
  292. s2c_suspendTime = statusSnap.SuspendDate
  293. });
  294. }
  295. }
  296. #endregion
  297. last_heartbeat = DateTime.Now;
  298. this.enter_game = enter;
  299. bool reconnect = false;
  300. var rec = new SessionReconnectNotify();
  301. rec.sessionName = SelfAddress.ServiceName;
  302. if (enter_game != null)
  303. {
  304. rec.roleID = enter_game.c2s_roleUUID;
  305. }
  306. var logic = remote_logic_service;
  307. if (logic != null)
  308. {
  309. var oldRoleID = logic.Config["roleID"].ToString();
  310. if (oldRoleID != enter.c2s_roleUUID)
  311. {
  312. log.WarnFormat(string.Format("Role Already Login : Acc={0} : Role={1} -> {2}", accountID, oldRoleID,
  313. enter.c2s_roleUUID));
  314. //cb(new ClientEnterGameResponse() { s2c_code = ClientEnterGameResponse.CODE_LOGIC_ALREADY_LOGIN });
  315. await logic.ShutdownAsync("switch role");
  316. logic = await this.CreateLogicServiceAsync(enter);
  317. //return;
  318. }
  319. else
  320. {
  321. //if (DeepCore.Log.Logger.SHOW_LOG)
  322. {
  323. log.InfoFormat(string.Format("Role Reconnect : Acc={0} : Role={1}", accountID, enter.c2s_roleUUID));
  324. }
  325. reconnect = true;
  326. rec.config = await InitConfig();
  327. }
  328. }
  329. else
  330. {
  331. //if (DeepCore.Log.Logger.SHOW_LOG)
  332. {
  333. log.InfoFormat(string.Format("Role Connect : Acc={0} : Role={1}", accountID, enter.c2s_roleUUID));
  334. }
  335. logic = await this.CreateLogicServiceAsync(enter);
  336. }
  337. if (logic != null)
  338. {
  339. accountSave.SetField(nameof(AccountData.lastLoginRoleID), enter.c2s_roleUUID);
  340. await accountSave.FlushAsync();
  341. try
  342. {
  343. mDisconnected = false;
  344. var ret = await logic.CallAsync<ClientEnterGameResponse>(enter);
  345. //log.Log("ClientEnterGameResponse: " + ret.IsSuccess);
  346. return ret;
  347. }
  348. finally
  349. {
  350. if (reconnect)
  351. {
  352. logic.Invoke(rec);
  353. }
  354. }
  355. }
  356. else
  357. {
  358. return (new ClientEnterGameResponse() {s2c_code = ClientEnterGameResponse.CODE_LOGIC_NOT_FOUND,});
  359. }
  360. }
  361. /// <summary>
  362. /// 玩家离开游戏
  363. /// </summary>
  364. /// <param name="enter"></param>
  365. /// <param name="cb"></param>
  366. [RpcHandler(typeof(ClientExitGameRequest), typeof(ClientExitGameResponse), ServerNames.ConnectServerType)]
  367. public virtual async Task<ClientExitGameResponse> client_rpc_Handle(ClientExitGameRequest exit)
  368. {
  369. //log.Log("ClientExitGameRequest");
  370. last_heartbeat = DateTime.Now;
  371. await ShutdownLogicServiceAsync("player exit");
  372. this.remote_area_service = null;
  373. return new ClientExitGameResponse();
  374. //return Task.FromResult(new ClientExitGameResponse());
  375. }
  376. //--------------------------------------------------------------------------------------------------------------------------------------------
  377. [RpcHandler(typeof(SessionBindAreaNotify), ServerNames.AreaServiceType)]
  378. public virtual void area_rpc_Handle(SessionBindAreaNotify bind)
  379. {
  380. this.Provider.GetAsync(new RemoteAddress(bind.areaName, bind.areaNode)).ContinueWith(t =>
  381. {
  382. remote_area_service = t.GetResultAs();
  383. });
  384. }
  385. [RpcHandler(typeof(SessionUnbindAreaNotify), ServerNames.AreaServiceType)]
  386. public virtual void area_rpc_Handle(SessionUnbindAreaNotify msg)
  387. {
  388. remote_area_service = null;
  389. }
  390. // from service: TODO
  391. [RpcHandler(true)]
  392. public virtual void rpc_Handle(BinaryMessage msg)
  393. {
  394. if (mDisconnected) return;
  395. var session = this.session;
  396. if (session != null)
  397. {
  398. session.SocketSend(msg);
  399. }
  400. }
  401. public override void OnWormholeTransported(RemoteAddress from, object message)
  402. {
  403. var session = this.session;
  404. if (session != null)
  405. {
  406. if (message is BinaryMessage bin)
  407. {
  408. session.socket.Send(bin);
  409. }
  410. else if (message is ISerializable ser)
  411. {
  412. session.socket.Send(ser);
  413. }
  414. }
  415. }
  416. //--------------------------------------------------------------------------------------------------------------------------------------------
  417. //--------------------------------------------------------------------------------------------------------------------------------------------
  418. /// <summary>
  419. /// 战斗协议 ClientBattleAction 直接发往AreaService
  420. /// </summary>
  421. /// <param name="action"></param>
  422. public virtual void SendToArea(TypeCodec route_codec, BinaryMessage action)
  423. {
  424. try
  425. {
  426. var area = remote_area_service;
  427. var enter = enter_game;
  428. if (area != null && enter != null)
  429. {
  430. using (var output = IOStreamObjectPool.AllocOutputAutoRelease(ConnectServer.ClientCodec.Factory))
  431. {
  432. output.PutUTF(enter.c2s_roleUUID);
  433. output.PutBytes(action.Buffer, action.BufferOffset, action.BufferLength);
  434. var to_area = BinaryMessage.FromBuffer(session_battle_action_codec.MessageID, output.Buffer);
  435. area.WormholeTransport(to_area);
  436. }
  437. }
  438. }
  439. catch (Exception err)
  440. {
  441. log.Error(err);
  442. }
  443. }
  444. /// <summary>
  445. /// 逻辑协议发往LogicService
  446. /// </summary>
  447. /// <param name="msg"></param>
  448. /// <param name="callback"></param>
  449. public virtual void SendToLogic(TypeCodec route_codec, BinaryMessage msg, OnRpcBinaryReturn callback = null)
  450. {
  451. var logic = remote_logic_service;
  452. if (logic != null)
  453. {
  454. if (callback != null)
  455. logic.Call(msg, callback);
  456. else
  457. logic.Invoke(msg);
  458. }
  459. else
  460. {
  461. log.Warn("SendToLogic Error : Logic Service Not Init : " + route_codec);
  462. if (callback != null) callback(BinaryMessage.NULL);
  463. }
  464. }
  465. protected virtual async Task<IRemoteService> CreateLogicServiceAsync(ClientEnterGameRequest enter_game)
  466. {
  467. var cfg = await InitConfig();
  468. var ret = await this.Provider.CreateAsync(
  469. ServerNames.GetLogicServiceAddress(enter_game.c2s_roleUUID, this.SelfAddress.ServiceNode), cfg);
  470. this.remote_logic_service = ret;
  471. return ret;
  472. }
  473. protected virtual async Task ShutdownLogicServiceAsync(string reason)
  474. {
  475. var logic = remote_logic_service;
  476. remote_logic_service = null;
  477. if (logic != null)
  478. {
  479. try
  480. {
  481. await logic.CallAsync<SessionBeginLeaveResponse>(new SessionBeginLeaveRequest()
  482. {
  483. sessionName = SelfAddress.ServiceName,
  484. roleID = enter_game.c2s_roleUUID,
  485. });
  486. }
  487. catch (Exception err)
  488. {
  489. log.Error(err.Message, err);
  490. }
  491. try
  492. {
  493. var result = await logic.ShutdownAsync(reason);
  494. log.Info("ShutdownAsync Complete : " + result);
  495. }
  496. catch (Exception err)
  497. {
  498. log.Error("ShutdownAsync Error : " + err.Message, err);
  499. }
  500. }
  501. }
  502. private async Task<HashMap<string, string>> InitConfig()
  503. {
  504. HashMap<string, string> cfg = new HashMap<string, string>();
  505. var serverID = await accountSave.LoadFieldAsync<string>(nameof(AccountData.lastLoginServerID));
  506. var serverGroupID = await accountSave.LoadFieldAsync<string>(nameof(AccountData.lastLoginServerGroupID));
  507. //var privilege = await accountSave.LoadFieldAsync<RolePrivilege>(nameof(AccountData.privilege));
  508. cfg["sessionNode"] = this.SelfAddress.ServiceNode;
  509. cfg["sessionName"] = this.SelfAddress.ServiceName;
  510. cfg["accountID"] = enter.c2s_account;
  511. //cfg["privilege"] = privilege.ToString();
  512. cfg["roleID"] = enter_game.c2s_roleUUID;
  513. cfg["serverID"] = serverID;
  514. cfg["serverGroupID"] = serverGroupID;
  515. cfg["channel"] = enter.c2s_clientInfo.channel;
  516. cfg["passport"] = enter.c2s_clientInfo.sdkName;
  517. cfg["clientVersion"] = enter.c2s_clientInfo.clientVersion;
  518. cfg["deviceId"] = enter.c2s_clientInfo.deviceId;
  519. cfg["deviceModel"] = enter.c2s_clientInfo.deviceModel;
  520. cfg["deviceType"] = enter.c2s_clientInfo.deviceType;
  521. cfg["network"] = enter.c2s_clientInfo.network;
  522. cfg["region"] = enter.c2s_clientInfo.region;
  523. cfg["sdkName"] = enter.c2s_clientInfo.sdkName;
  524. cfg["sdkVersion"] = enter.c2s_clientInfo.sdkVersion;
  525. cfg["subChannel"] = enter.c2s_clientInfo.subChannel;
  526. cfg["userAgent"] = enter.c2s_clientInfo.userAgent;
  527. cfg["userSource1"] = enter.c2s_clientInfo.userSource1;
  528. cfg["userSource2"] = enter.c2s_clientInfo.userSource2;
  529. var ip = (this.session.socket.RemoteAddress as System.Net.IPEndPoint)?.Address?.ToString();
  530. cfg["clientIp"] = ip;
  531. cfg["platformAccount"] = enter.c2s_clientInfo.platformAcount;
  532. return cfg;
  533. }
  534. }
  535. /// <summary>
  536. /// Connect 进程内,通知SessionService绑定ViewSession
  537. /// </summary>
  538. public class LocalBindSessionRequest : Request, IRpcNoneSerializable
  539. {
  540. public ConnectServer.ViewSession session;
  541. public ClientEnterServerRequest enter;
  542. }
  543. public class LocalBindSessionResponse : Response, IRpcNoneSerializable
  544. {
  545. public SessionService session;
  546. public string sessionToken;
  547. public string serverGroupID;
  548. }
  549. }