/// <summary> /// 调用远程端实现的Api /// 并返回结果数据任务 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="api">数据包Api名</param> /// <param name="parameters">参数</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> /// <returns>远程数据任务</returns> public ApiResult <T> InvokeApi <T>(string api, params object[] parameters) { var id = this.rpcServer.PacketIdProvider.NewId(); var packet = new FastPacket(api, id, false); packet.SetBodyParameters(this.rpcServer.Serializer, parameters); return(Common.InvokeApi <T>(this.channel, this.rpcServer.TaskSetterTable, this.rpcServer.Serializer, packet, this.rpcServer.TimeOut)); }
/// <summary> /// 调用远程端实现的Api /// </summary> /// <param name="api">数据包Api名</param> /// <param name="parameters">参数列表</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> public void InvokeApi(string api, params object[] parameters) { var id = this.rpcServer.PacketIdProvider.NewId(); var packet = new FastPacket(api, id, false); packet.SetBodyParameters(this.rpcServer.Serializer, parameters); lock (this.channel) this.channel.WriteAndFlushAsync(packet); }
/// <summary> /// 请求上下文 /// </summary> /// <param name="session">当前会话对象</param> /// <param name="packet">数据包对象</param> /// <param name="allSessions">所有会话对象</param> internal RequestContext(FastSession session, FastPacket packet) { this.Session = session; this.Packet = packet; }
/// <summary> /// 调用远程端的Api /// 并返回结果数据任务 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="session">会话对象</param> /// <param name="taskSetActionTable">任务行为表</param> /// <param name="serializer">序列化工具</param> /// <param name="packet">封包</param> /// <param name="timeout">超时时间</param> /// <exception cref="SocketException"></exception> /// <returns></returns> public static ApiResult <T> InvokeApi <T>(IChannel session, TaskSetterTable <long> taskSetActionTable, ISerializer serializer, FastPacket packet, TimeSpan timeout) { var taskSetter = taskSetActionTable.Create <T>(packet.Id, timeout); lock (session) session.WriteAndFlushAsync(packet); return(new ApiResult <T>(taskSetter)); }
/// <summary> /// 解析一个数据包 /// 不足一个封包时返回null /// </summary> /// <param name="streamReader">数据读取器</param> /// <param name="packet">数据包</param> /// <returns></returns> public static bool Parse(IByteBuffer streamReader, out FastPacket packet) { packet = null; const int packetMinSize = 16; var allpakagesize = streamReader.ReadableBytes; if (streamReader.ReadableBytes < packetMinSize || streamReader.ReadByte() != FastPacket.Mark) //1 { return(false); } var totalBytes = streamReader.ReadInt();//4 if (totalBytes < packetMinSize) { return(false); } // 数据包未接收完整 if (allpakagesize < totalBytes) { return(false); } // api名称数据长度 var apiNameLength = streamReader.ReadByte();//1 if (totalBytes < apiNameLength + packetMinSize) { return(false); } var headLength = apiNameLength + 16; // api名称数据 byte[] apiNameBytes = new byte[apiNameLength]; streamReader.ReadBytes(apiNameBytes, 0, apiNameLength); // 标识符 var id = streamReader.ReadLong(); //8 // 是否为客户端封包 var isFromClient = streamReader.ReadBoolean(); //1 // 是否异常 var isException = streamReader.ReadBoolean(); //1 // 实体数据 byte[] body = null; if (totalBytes > headLength) { body = new byte[totalBytes - headLength]; streamReader.ReadBytes(body, 0, body.Length); } var apiName = Encoding.UTF8.GetString(apiNameBytes); packet = new FastPacket(apiName, id, isFromClient) { TotalBytes = totalBytes, ApiNameLength = apiNameLength, IsException = isException, Body = body }; return(true); }