using System; using WingServerCommon.Service; using WingInterfaceLibrary.OpLog; using System.Threading.Tasks; using System.Collections.Generic; using WingInterfaceLibrary.Interface.DBInterface; using System.Linq; using WingServerCommon.Log; using CSScriptLib; using System.Reflection; using WingInterfaceLibrary.LiveConsultation; namespace WingCloudServer.InteractionCenter { public partial class MasterInteractionCenterService : InteractionCenterService, IMasterInteractionCenterService { private Dictionary _serverUrlMap; public override void Load(JsonRpcClientPool jsonRpcClientPool) { base.Load(jsonRpcClientPool); _opLogDBService = GetProxy(); _distributedServerInfoDBService = GetProxy(); _liveRoomDBService = GetProxy(); _liveConsultationService = GetProxy(); _serverUrlMap = new Dictionary(); } /// /// Get op logs from master server /// /// /// The op log list public async Task> GetOpLogsFromMasterAsync(GetOpLogsFormMasterRequest request) { return await _opLogDBService.GetOpLogAsync(request); } /// /// Get op logs from master server /// /// /// The op log list public async Task> GetOpLogsByCodesFromMasterAsync(GetOpLogsByCodesFormMasterRequest request) { return await _opLogDBService.GetOpLogsByCodesAsync(request); } /// /// Synchronize op log to master server /// /// /// public async Task SyncOpLogToMasterAsync(SyncOpLogToMasterRequest request) { try { if (!_serverUrlMap.Any(x => x.Key == request.SourceUrl)) { DynamicAddRemoteService(request.ServerID, request.SourceUrl); _serverUrlMap.TryAdd(request.SourceUrl, request.ServerID); } } catch (Exception ex) { Logger.WriteLineWarn("TryAdd-AddRemoteService:" + ex.ToString()); } return await _opLogDBService.SyncOpLogAsync(request); } public void DynamicAddRemoteService(string serverID, string sourceUrl) { var eval = CSScript.Evaluator.ReferenceDomainAssemblies(DomainAssemblies.AllStaticNonGAC); var str = @$"using System; using System.Threading.Tasks; using WingServerCommon.Service; using WingInterfaceLibrary.OpLog; using System.Collections.Generic; using JsonRpcLite.Rpc; using WingInterfaceLibrary.Interface.DBInterface; using WingCloudServer; using WingCloudServer.InteractionCenter; public interface IDynamicSlaveService{serverID}:IDynamicSlaveService {{ Task DynamicSlaveAsync(SyncReceiveServiceDataRequest request); }} public class DynamicAddRemoteServiceClass {{ public void DynamicAddRemoteMethod() {{ WingServer.AddRemoteService(""IDynamicSlaveService{serverID}"",""{sourceUrl}""); }} }}"; Assembly compilecode = eval.CompileCode(str); var ps = compilecode.GetType("css_root+DynamicAddRemoteServiceClass"); var obj = compilecode.CreateInstance("css_root+DynamicAddRemoteServiceClass"); var mes = ps.GetMethod("DynamicAddRemoteMethod"); mes.Invoke(obj, new object[] { }); } } public class SlaveInteractionCenterService : InteractionCenterService, ISlaveInteractionCenterService { public override void Load(JsonRpcClientPool jsonRpcClientPool) { base.Load(jsonRpcClientPool); _opLogDBService = GetProxy(); _liveConsultationService = GetProxy(); } /// Sychronize Receive ServiceData for slave public async Task SyncReceiveMasterServiceDataAsync(SyncReceiveServiceDataRequest request) { Logger.WriteLineInfo("SyncReceiveSlaveServiceDataAsync:" + request.ServiceDataJson + "," + request.ServerID); //执行oplogs if (request.Oplogs != null && request.Oplogs.Count > 0) { var syncCompleteOpLogsRequest = new SyncCompleteOpLogsRequest { Oplogs = request.Oplogs }; await _opLogDBService.SyncCompleteOpLogsAsync(syncCompleteOpLogsRequest); } await _liveConsultationService.SyncServerMessageAsync(request); return true; } } }