/// <summary> /// 调用远程端的Api /// 并返回结果数据任务 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="session">会话对象</param> /// <param name="taskSetActionTable">任务行为表</param> /// <param name="serializer">序列化工具</param> /// <param name="packet">封包</param> /// <exception cref="SocketException"></exception> public static Task <T> InvokeApi <T>(ISession session, TaskSetActionTable taskSetActionTable, ISerializer serializer, FastPacket packet) { var taskSource = new TaskCompletionSource <T>(); var taskSetAction = new TaskSetAction <T>(serializer, taskSource); taskSetActionTable.Add(packet.Id, taskSetAction); session.Send(packet.ToByteRange()); return(taskSource.Task); }
/// <summary> /// 调用远程端的Api /// 并返回结果数据任务 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="session">会话对象</param> /// <param name="taskSetActionTable">任务行为表</param> /// <param name="serializer">序列化工具</param> /// <param name="api">api</param> /// <param name="id">标识符</param> /// <param name="fromClient">是否为客户端封包</param> /// <param name="parameters">参数</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> /// <returns></returns> public static Task <T> InvokeApi <T>(ISession session, TaskSetActionTable taskSetActionTable, ISerializer serializer, string api, long id, bool fromClient, params object[] parameters) { var taskSource = new TaskCompletionSource <T>(); var packet = new FastPacket(api, id, fromClient); packet.SetBodyParameters(serializer, parameters); // 登记TaskSetAction var setAction = FastTcpCommon.NewSetAction <T>(taskSource, serializer); var taskSetAction = new TaskSetAction(setAction); taskSetActionTable.Add(packet.Id, taskSetAction); session.Send(packet.ToByteRange()); return(taskSource.Task); }
/// <summary> /// 添加回调信息记录 /// </summary> /// <param name="key">键值</param> /// <param name="taskSetAction">设置行为</param> /// <returns></returns> public void Add(long key, TaskSetAction taskSetAction) { this.table.TryAdd(key, taskSetAction); }