Skip to content

Commit

Permalink
v2.4.2023.1001 开启可空特性
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 1, 2023
1 parent f864cdf commit 65bbb72
Show file tree
Hide file tree
Showing 8 changed files with 548 additions and 555 deletions.
144 changes: 70 additions & 74 deletions NewLife.Remoting/ApiAction.cs
Original file line number Diff line number Diff line change
@@ -1,101 +1,97 @@
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Reflection;
using NewLife.Data;
using NewLife.Log;
using NewLife.Reflection;

namespace NewLife.Remoting
namespace NewLife.Remoting;

/// <summary>Api动作</summary>
public class ApiAction
{
/// <summary>Api动作</summary>
public class ApiAction
{
/// <summary>动作名称</summary>
public String Name { get; }
/// <summary>动作名称</summary>
public String Name { get; }

/// <summary>动作所在类型</summary>
public Type Type { get; }
/// <summary>动作所在类型</summary>
public Type Type { get; }

/// <summary>方法</summary>
public MethodInfo Method { get; }
/// <summary>方法</summary>
public MethodInfo Method { get; }

/// <summary>控制器对象</summary>
/// <remarks>如果指定控制器对象,则每次调用前不再实例化对象</remarks>
public Object Controller { get; set; }
/// <summary>控制器对象</summary>
/// <remarks>如果指定控制器对象,则每次调用前不再实例化对象</remarks>
public Object? Controller { get; set; }

/// <summary>是否二进制参数</summary>
public Boolean IsPacketParameter { get; }
/// <summary>是否二进制参数</summary>
public Boolean IsPacketParameter { get; }

/// <summary>是否二进制返回</summary>
public Boolean IsPacketReturn { get; }
/// <summary>是否二进制返回</summary>
public Boolean IsPacketReturn { get; }

/// <summary>处理统计</summary>
public ICounter StatProcess { get; set; } = new PerfCounter();
/// <summary>处理统计</summary>
public ICounter StatProcess { get; set; } = new PerfCounter();

/// <summary>最后会话</summary>
public String LastSession { get; set; }
/// <summary>最后会话</summary>
public String? LastSession { get; set; }

/// <summary>实例化</summary>
public ApiAction(MethodInfo method, Type type)
{
if (type == null) type = method.DeclaringType;
Name = GetName(type, method);
/// <summary>实例化</summary>
public ApiAction(MethodInfo method, Type type)
{
if (type == null) type = method.DeclaringType;
Name = GetName(type, method);

// 必须同时记录类型和方法,因为有些方法位于继承的不同层次,那样会导致实例化的对象不一致
Type = type;
Method = method;
// 必须同时记录类型和方法,因为有些方法位于继承的不同层次,那样会导致实例化的对象不一致
Type = type;
Method = method;

var ps = method.GetParameters();
if (ps != null && ps.Length == 1 && ps[0].ParameterType == typeof(Packet)) IsPacketParameter = true;
var ps = method.GetParameters();
if (ps != null && ps.Length == 1 && ps[0].ParameterType == typeof(Packet)) IsPacketParameter = true;

if (method.ReturnType == typeof(Packet)) IsPacketReturn = true;
}
if (method.ReturnType == typeof(Packet)) IsPacketReturn = true;
}

/// <summary>获取名称</summary>
/// <param name="type"></param>
/// <param name="method"></param>
/// <returns></returns>
public static String GetName(Type type, MethodInfo method)
{
if (type == null) type = method.DeclaringType;
if (type == null) return null;
/// <summary>获取名称</summary>
/// <param name="type"></param>
/// <param name="method"></param>
/// <returns></returns>
public static String GetName(Type? type, MethodInfo method)
{
if (type == null) type = method.DeclaringType;
//if (type == null) return null;

var typeName = type.Name.TrimEnd("Controller", "Service");
var att = type.GetCustomAttribute<ApiAttribute>(true);
if (att != null) typeName = att.Name;
var typeName = type.Name.TrimEnd("Controller", "Service");
var att = type.GetCustomAttribute<ApiAttribute>(true);
if (att != null) typeName = att.Name;

var miName = method.Name;
att = method.GetCustomAttribute<ApiAttribute>();
if (att != null) miName = att.Name;
var miName = method.Name;
att = method.GetCustomAttribute<ApiAttribute>();
if (att != null) miName = att.Name;

if (typeName.IsNullOrEmpty() || miName.Contains('/'))
return miName;
else
return $"{typeName}/{miName}";
}
if (typeName.IsNullOrEmpty() || miName.Contains('/'))
return miName;
else
return $"{typeName}/{miName}";
}

/// <summary>已重载。</summary>
/// <returns></returns>
public override String ToString()
{
var mi = Method;
/// <summary>已重载。</summary>
/// <returns></returns>
public override String ToString()
{
var mi = Method;

var type = mi.ReturnType;
var rtype = type.Name;
if (type.As<Task>())
var type = mi.ReturnType;
var rtype = type.Name;
if (type.As<Task>())
{
if (!type.IsGenericType)
rtype = "void";
else
{
if (!type.IsGenericType)
rtype = "void";
else
{
type = type.GetGenericArguments()[0];
rtype = type.Name;
}
type = type.GetGenericArguments()[0];
rtype = type.Name;
}

var ps = mi.GetParameters().Select(pi => $"{pi.ParameterType.Name} {pi.Name}").Join(", ");
return $"{rtype} {mi.Name}({ps})";
}

var ps = mi.GetParameters().Select(pi => $"{pi.ParameterType.Name} {pi.Name}").Join(", ");
return $"{rtype} {mi.Name}({ps})";
}
}
16 changes: 8 additions & 8 deletions NewLife.Remoting/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class ApiClient : ApiHost, IApiClient
public Boolean UsePool { get; set; }

/// <summary>令牌。每次请求携带</summary>
public String Token { get; set; }
public String? Token { get; set; }

/// <summary>最后活跃时间</summary>
public DateTime LastActive { get; set; }
Expand All @@ -36,7 +36,7 @@ public class ApiClient : ApiHost, IApiClient
public ICounter StatInvoke { get; set; }

/// <summary>性能跟踪器</summary>
public ITracer Tracer { get; set; } = DefaultTracer.Instance;
public ITracer? Tracer { get; set; } = DefaultTracer.Instance;
#endregion

#region 构造
Expand Down Expand Up @@ -137,7 +137,7 @@ protected virtual ICluster<String, ISocketClient> InitCluster()
/// <param name="args">参数</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public virtual async Task<TResult> InvokeAsync<TResult>(String action, Object args = null, CancellationToken cancellationToken = default)
public virtual async Task<TResult?> InvokeAsync<TResult>(String action, Object? args = null, CancellationToken cancellationToken = default)
{
// 让上层异步到这直接返回,后续代码在另一个线程执行
//!!! Task.Yield会导致强制捕获上下文,虽然会在另一个线程执行,但在UI线程中可能无法抢占上下文导致死锁
Expand Down Expand Up @@ -174,14 +174,14 @@ public virtual async Task<TResult> InvokeAsync<TResult>(String action, Object ar
/// <param name="action">服务操作</param>
/// <param name="args">参数</param>
/// <returns></returns>
public virtual TResult Invoke<TResult>(String action, Object args = null) => Task.Run(() => InvokeAsync<TResult>(action, args)).Result;
public virtual TResult Invoke<TResult>(String action, Object? args = null) => Task.Run(() => InvokeAsync<TResult>(action, args)).Result;

/// <summary>单向发送。同步调用,不等待返回</summary>
/// <param name="action">服务操作</param>
/// <param name="args">参数</param>
/// <param name="flag">标识</param>
/// <returns></returns>
public virtual Int32 InvokeOneWay(String action, Object args = null, Byte flag = 0)
public virtual Int32 InvokeOneWay(String action, Object? args = null, Byte flag = 0)
{
if (!Open()) return -1;

Expand All @@ -204,7 +204,7 @@ public virtual Int32 InvokeOneWay(String action, Object args = null, Byte flag =
/// <param name="flag">标识</param>
/// <param name="cancellationToken">取消通知</param>
/// <returns></returns>
public virtual async Task<TResult> InvokeWithClientAsync<TResult>(ISocketClient client, String action, Object args = null, Byte flag = 0, CancellationToken cancellationToken = default)
public virtual async Task<TResult> InvokeWithClientAsync<TResult>(ISocketClient? client, String action, Object? args = null, Byte flag = 0, CancellationToken cancellationToken = default)
{
// 性能计数器,次数、TPS、平均耗时
var st = StatInvoke;
Expand All @@ -221,7 +221,7 @@ public virtual async Task<TResult> InvokeWithClientAsync<TResult>(ISocketClient
}

var span = Tracer?.NewSpan("rpc:" + action, args);
args = span.Attach(args);
if (args != null) args = span.Attach(args);

// 编码请求,构造消息
var enc = Encoder;
Expand Down Expand Up @@ -408,7 +408,7 @@ protected virtual ISocketClient OnCreate(String svr)
/// <summary>显示统计信息的周期。默认600秒,0表示不显示统计信息</summary>
public Int32 StatPeriod { get; set; } = 600;

private void DoWork(Object state)
private void DoWork(Object? state)
{
var sb = Pool.StringBuilder.Get();
var pf1 = StatInvoke;
Expand Down
6 changes: 3 additions & 3 deletions NewLife.Remoting/ApiHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class ApiHost : DisposeBase, IApiHost, IExtend, ILogFeature
public String Name { get; set; }

/// <summary>编码器</summary>
public IEncoder Encoder { get; set; }
public IEncoder? Encoder { get; set; }

Check warning on line 17 in NewLife.Remoting/ApiHost.cs

View workflow job for this annotation

GitHub Actions / build-publish

Nullability of reference types in return type of 'IEncoder? ApiHost.Encoder.get' doesn't match implicitly implemented member 'IEncoder IApiHost.Encoder.get' (possibly because of nullability attributes).

Check warning on line 17 in NewLife.Remoting/ApiHost.cs

View workflow job for this annotation

GitHub Actions / test

Nullability of reference types in return type of 'IEncoder? ApiHost.Encoder.get' doesn't match implicitly implemented member 'IEncoder IApiHost.Encoder.get' (possibly because of nullability attributes).

Check warning on line 17 in NewLife.Remoting/ApiHost.cs

View workflow job for this annotation

GitHub Actions / test

Nullability of reference types in return type of 'IEncoder? ApiHost.Encoder.get' doesn't match implicitly implemented member 'IEncoder IApiHost.Encoder.get' (possibly because of nullability attributes).

Check warning on line 17 in NewLife.Remoting/ApiHost.cs

View workflow job for this annotation

GitHub Actions / build-publish

Nullability of reference types in return type of 'IEncoder? ApiHost.Encoder.get' doesn't match implicitly implemented member 'IEncoder IApiHost.Encoder.get' (possibly because of nullability attributes).

/// <summary>调用超时时间。请求发出后,等待响应的最大时间,默认15_000ms</summary>
public Int32 Timeout { get; set; } = 15_000;
Expand All @@ -23,12 +23,12 @@ public abstract class ApiHost : DisposeBase, IApiHost, IExtend, ILogFeature
public Int32 SlowTrace { get; set; } = 5_000;

/// <summary>用户会话数据</summary>
public IDictionary<String, Object> Items { get; set; } = new NullableDictionary<String, Object>();
public IDictionary<String, Object?> Items { get; set; } = new NullableDictionary<String, Object?>();

/// <summary>获取/设置 用户会话数据</summary>
/// <param name="key"></param>
/// <returns></returns>
public virtual Object this[String key] { get => Items[key]; set => Items[key] = value; }
public virtual Object? this[String key] { get => Items[key]; set => Items[key] = value; }

/// <summary>启动时间</summary>
public DateTime StartTime { get; set; } = DateTime.Now;
Expand Down
Loading

0 comments on commit 65bbb72

Please sign in to comment.