MasterInteractionCenterService.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using WingServerCommon.Service;
  3. using WingInterfaceLibrary.OpLog;
  4. using System.Threading.Tasks;
  5. using System.Collections.Generic;
  6. using WingInterfaceLibrary.Interface.DBInterface;
  7. using System.Linq;
  8. using WingServerCommon.Log;
  9. using CSScriptLib;
  10. using System.Reflection;
  11. using WingInterfaceLibrary.LiveConsultation;
  12. namespace WingCloudServer.InteractionCenter
  13. {
  14. public partial class MasterInteractionCenterService : InteractionCenterService, IMasterInteractionCenterService
  15. {
  16. private Dictionary<string, string> _serverUrlMap;
  17. public override void Load(JsonRpcClientPool jsonRpcClientPool)
  18. {
  19. base.Load(jsonRpcClientPool);
  20. _opLogDBService = GetProxy<IOpLogDBService>();
  21. _distributedServerInfoDBService = GetProxy<IDistributedServerInfoDBService>();
  22. _liveRoomDBService = GetProxy<ILiveRoomDBService>();
  23. _liveConsultationService = GetProxy<ILiveConsultationService>();
  24. _serverUrlMap = new Dictionary<string, string>();
  25. }
  26. /// <summary>
  27. /// Get op logs from master server
  28. /// </summary>
  29. /// <param name="request"></param>
  30. /// <returns>The op log list</returns>
  31. public async Task<List<OperationLogDTO>> GetOpLogsFromMasterAsync(GetOpLogsFormMasterRequest request)
  32. {
  33. return await _opLogDBService.GetOpLogAsync(request);
  34. }
  35. /// <summary>
  36. /// Get op logs from master server
  37. /// </summary>
  38. /// <param name="request"></param>
  39. /// <returns>The op log list</returns>
  40. public async Task<List<OperationLogDTO>> GetOpLogsByCodesFromMasterAsync(GetOpLogsByCodesFormMasterRequest request)
  41. {
  42. return await _opLogDBService.GetOpLogsByCodesAsync(request);
  43. }
  44. /// <summary>
  45. /// Synchronize op log to master server
  46. /// </summary>
  47. /// <param name="request"></param>
  48. /// <returns></returns>
  49. public async Task<bool> SyncOpLogToMasterAsync(SyncOpLogToMasterRequest request)
  50. {
  51. try
  52. {
  53. if (!_serverUrlMap.Any(x => x.Key == request.SourceUrl))
  54. {
  55. DynamicAddRemoteService(request.ServerID, request.SourceUrl);
  56. _serverUrlMap.TryAdd(request.SourceUrl, request.ServerID);
  57. }
  58. }
  59. catch (Exception ex)
  60. {
  61. Logger.WriteLineWarn("TryAdd-AddRemoteService:" + ex.ToString());
  62. }
  63. return await _opLogDBService.SyncOpLogAsync(request);
  64. }
  65. public void DynamicAddRemoteService(string serverID, string sourceUrl)
  66. {
  67. var eval = CSScript.Evaluator.ReferenceDomainAssemblies(DomainAssemblies.AllStaticNonGAC);
  68. var str = @$"using System;
  69. using System.Threading.Tasks;
  70. using WingServerCommon.Service;
  71. using WingInterfaceLibrary.OpLog;
  72. using System.Collections.Generic;
  73. using JsonRpcLite.Rpc;
  74. using WingInterfaceLibrary.Interface.DBInterface;
  75. using WingCloudServer;
  76. using WingCloudServer.InteractionCenter;
  77. public interface IDynamicSlaveService{serverID}:IDynamicSlaveService
  78. {{
  79. Task<bool> DynamicSlaveAsync(SyncReceiveServiceDataRequest request);
  80. }}
  81. public class DynamicAddRemoteServiceClass
  82. {{
  83. public void DynamicAddRemoteMethod()
  84. {{
  85. WingServer.AddRemoteService<IDynamicSlaveService{serverID}>(""IDynamicSlaveService{serverID}"",""{sourceUrl}"");
  86. }}
  87. }}";
  88. Assembly compilecode = eval.CompileCode(str);
  89. var ps = compilecode.GetType("css_root+DynamicAddRemoteServiceClass");
  90. var obj = compilecode.CreateInstance("css_root+DynamicAddRemoteServiceClass");
  91. var mes = ps.GetMethod("DynamicAddRemoteMethod");
  92. mes.Invoke(obj, null);
  93. }
  94. }
  95. public class SlaveInteractionCenterService : InteractionCenterService, ISlaveInteractionCenterService
  96. {
  97. public override void Load(JsonRpcClientPool jsonRpcClientPool)
  98. {
  99. base.Load(jsonRpcClientPool);
  100. _opLogDBService = GetProxy<IOpLogDBService>();
  101. _liveConsultationService = GetProxy<ILiveConsultationService>();
  102. }
  103. /// Sychronize Receive ServiceData for slave
  104. public async Task<bool> SyncReceiveMasterServiceDataAsync(SyncReceiveServiceDataRequest request)
  105. {
  106. Logger.WriteLineInfo("SyncReceiveSlaveServiceDataAsync:" + request.ServiceDataJson + "," + request.ServerID);
  107. //执行oplogs
  108. if (request.Oplogs != null && request.Oplogs.Count > 0)
  109. {
  110. var syncCompleteOpLogsRequest = new SyncCompleteOpLogsRequest { Oplogs = request.Oplogs };
  111. await _opLogDBService.SyncCompleteOpLogsAsync(syncCompleteOpLogsRequest);
  112. }
  113. await _liveConsultationService.SyncServerMessageAsync(request);
  114. return true;
  115. }
  116. }
  117. }