/// <summary> /// 生成客户端C#代码文件 /// 另外此方法名不宜调整 /// </summary> /// <param name="folder"></param> /// <param name="spaceName"></param> public static void Generate(string folder, string spaceName) { RPCMapping.RegistAll(); GenerateProxy(spaceName); var filePath = Path.Combine(folder, "RPCServiceProxy.cs"); StringBuilder sb = new StringBuilder(); sb.AppendLine(_proxyStr); if (_serviceStrs != null && _serviceStrs.Count > 0) { foreach (var serviceStr in _serviceStrs) { sb.AppendLine(serviceStr); } } if (_modelStrs != null && _modelStrs.Count > 0) { foreach (var entry in _modelStrs) { sb.AppendLine(entry.Value); } } if (File.Exists(filePath)) { File.Delete(filePath); } File.WriteAllText(filePath, sb.ToString(), Encoding.UTF8); }
/// <summary> /// 生成代理代码 /// </summary> /// <param name="spaceName"></param> internal static void GenerateProxy(string spaceName) { StringBuilder csStr = new StringBuilder(); csStr.AppendLine(Header("using System.Collections.Generic;", "using SAEA.Common;", "using SAEA.RPC.Consumer;", $"using {spaceName}.Consumer.Model;", $"using {spaceName}.Consumer.Service;")); csStr.AppendLine($"namespace {spaceName}.Consumer"); csStr.AppendLine("{"); csStr.AppendLine($"{GetSpace(1)}public class RPCServiceProxy"); csStr.AppendLine(GetSpace(1) + "{"); csStr.AppendLine(GetSpace(2) + "public event ExceptionCollector.OnErrHander OnErr;"); csStr.AppendLine(GetSpace(2) + "ServiceConsumer _serviceConsumer;"); csStr.AppendLine(GetSpace(2) + "public RPCServiceProxy(string uri = \"rpc://127.0.0.1:39654\") : this(uri, 4, 5, 10 * 1000) { }"); csStr.AppendLine(GetSpace(2) + "public RPCServiceProxy(string uri, int links = 4, int retry = 5, int timeOut = 10 * 1000)"); csStr.AppendLine(GetSpace(2) + "{"); csStr.AppendLine(GetSpace(3) + "ExceptionCollector.OnErr += ExceptionCollector_OnErr;"); csStr.AppendLine(GetSpace(3) + "_serviceConsumer = new ServiceConsumer(new Uri(uri), links, retry, timeOut);"); var names = RPCMapping.GetServiceNames(); if (names != null) { foreach (var name in names) { csStr.AppendLine(GetSpace(3) + GetSuffixStr(name) + $" = new {name}(_serviceConsumer);"); } } csStr.AppendLine(GetSpace(2) + "}"); csStr.AppendLine(GetSpace(2) + "private void ExceptionCollector_OnErr(string name, Exception ex)"); csStr.AppendLine(GetSpace(2) + "{"); csStr.AppendLine(GetSpace(3) + "OnErr(name, ex);"); csStr.AppendLine(GetSpace(2) + "}"); if (names != null) { foreach (var name in names) { var suffixStr = GetSuffixStr(name); csStr.AppendLine(GetSpace(2) + $"{name} {suffixStr};"); csStr.AppendLine(GetSpace(2) + $"public {name} {name}"); csStr.AppendLine(GetSpace(2) + "{"); csStr.AppendLine($"{GetSpace(3)} get{{ return {suffixStr}; }}"); csStr.AppendLine(GetSpace(2) + "}"); var list = RPCMapping.GetAll(name); if (list != null) { GenerateService(spaceName, name, list); } } } csStr.AppendLine(GetSpace(1) + "}"); csStr.AppendLine("}"); _proxyStr = csStr.ToString(); }
/// <summary> /// RPC服务提供者 /// </summary> /// <param name="serviceTypes"></param> /// <param name="port"></param> public ServiceProvider(Type[] serviceTypes, int port = 39654) { _serviceTypes = serviceTypes; _port = 39654; RPCMapping.Regists(_serviceTypes); _RServer = new RServer(); _RServer.OnMsg += _RServer_OnMsg; _RServer.OnError += _RServer_OnError; }
/// <summary> /// 启动服务 /// </summary> public void Start() { if (!_started) { _RServer.Start(); RPCMapping.Regists(_serviceTypes); _started = true; } }
/// <summary> /// 生成代理代码 /// </summary> /// <param name="spaceName"></param> internal static void GenerateProxy(string spaceName) { StringBuilder csStr = new StringBuilder(); csStr.AppendLine(Header("using System.Collections.Generic;", "using SAEA.RPC.Consumer;", $"using {spaceName}.Consumer.Model;", $"using {spaceName}.Consumer.Service;")); csStr.AppendLine($"namespace {spaceName}.Consumer"); csStr.AppendLine("{"); csStr.AppendLine($"{GetSpace(1)}public class RPCServiceProxy"); csStr.AppendLine(GetSpace(1) + "{"); csStr.AppendLine(GetSpace(2) + "ServiceConsumer _serviceConsumer;"); csStr.AppendLine(GetSpace(2) + "public RPCServiceProxy(string uri = \"rpc://127.0.0.1:39654\") : this(new Uri(uri)){}"); csStr.AppendLine(GetSpace(2) + "public RPCServiceProxy(Uri uri)"); csStr.AppendLine(GetSpace(2) + "{"); csStr.AppendLine(GetSpace(3) + "_serviceConsumer = new ServiceConsumer(uri);"); var names = RPCMapping.GetServiceNames(); if (names != null) { foreach (var name in names) { csStr.AppendLine(GetSpace(3) + GetSuffixStr(name) + $" = new {name}(_serviceConsumer);"); } } csStr.AppendLine(GetSpace(2) + "}"); if (names != null) { foreach (var name in names) { var suffixStr = GetSuffixStr(name); csStr.AppendLine(GetSpace(2) + $"{name} {suffixStr};"); csStr.AppendLine(GetSpace(2) + $"public {name} {name}"); csStr.AppendLine(GetSpace(2) + "{"); csStr.AppendLine($"{GetSpace(3)} get{{ return {suffixStr}; }}"); csStr.AppendLine(GetSpace(2) + "}"); var list = RPCMapping.GetAll(name); if (list != null) { GenerateService(spaceName, name, list); } } } csStr.AppendLine(GetSpace(1) + "}"); csStr.AppendLine("}"); _proxyStr = csStr.ToString(); }