/// <summary> /// 收到fast请求 /// </summary> /// <param name="context">上下文</param> /// <returns></returns> private Task OnFastRequest(IContenxt context) { var fastPacket = default(FastPacket); if (FastPacket.Parse(context.Buffer, out fastPacket) == false) { return(this.Next.Invoke(context)); } if (fastPacket == null) { return(new Task(() => { })); } if (context.Session.Protocol == null) { var wrapper = new FastSession(context.Session, this); context.Session.SetProtocolWrapper("fast", wrapper); } var fastSession = (FastSession)context.Session.Wrapper; var fastPackets = this.GenerateFastPackets(context, fastPacket); return(new Task(() => { foreach (var packet in fastPackets) { var requestContext = new RequestContext(fastSession, packet, context.AllSessions); this.OnRecvFastPacket(requestContext); } })); }
/// <summary> /// 收到fast请求 /// </summary> /// <param name="context">上下文</param> /// <returns></returns> private Task OnFastRequestAsync(IContenxt context) { var fastPacket = default(FastPacket); if (FastPacket.Parse(context.InputStream, out fastPacket) == false) { return(this.Next.Invoke(context)); } // 数据未完整 if (fastPacket == null) { return(TaskExtend.CompletedTask); } if (context.Session.Protocol == Protocol.None) { var wrapper = new FastSession(context.Session, this); context.Session.SetProtocolWrapper(Protocol.Fast, wrapper); } var fastSession = (FastSession)context.Session.Wrapper; var fastPackets = this.GenerateFastPackets(context, fastPacket); foreach (var packet in fastPackets) { var requestContext = new RequestContext(fastSession, packet, context.AllSessions); this.OnRecvFastPacketAsync(requestContext); } return(TaskExtend.CompletedTask); }
/// <summary> /// 调用服务端实现的Api /// </summary> /// <param name="api">Api行为的api</param> /// <param name="parameters">参数列表</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> public void InvokeApi(string api, params object[] parameters) { var packet = new FastPacket(api, this.packetIdProvider.GetId(), true); packet.SetBodyParameters(this.Serializer, parameters); this.Send(packet.ToByteRange()); }
/// <summary> /// 调用服务端实现的Api /// 并返回结果数据任务 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="api">Api行为的api</param> /// <param name="parameters">参数</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> /// <returns>远程数据任务</returns> public Task <T> InvokeApi <T>(string api, params object[] parameters) { var id = this.packetIdProvider.NewId(); var packet = new FastPacket(api, id, true); packet.SetBodyParameters(this.Serializer, parameters); return(Common.InvokeApi <T>(this.UnWrap(), this.taskSetterTable, this.Serializer, packet, this.TimeOut)); }
/// <summary> /// 调用远程端实现的Api /// </summary> /// <param name="api">数据包Api名</param> /// <param name="parameters">参数列表</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> /// <exception cref="ProtocolException"></exception> public void InvokeApi(string api, params object[] parameters) { var id = this.Server.PacketIdProvider.NewId(); var packet = new FastPacket(api, id, false); packet.SetBodyParameters(this.Server.Serializer, parameters); this.Send(packet.ToByteRange()); }
/// <summary> /// 调用远程端实现的Api /// 并返回结果数据任务 /// </summary> /// <typeparam name="T">返回值类型</typeparam> /// <param name="api">数据包Api名</param> /// <param name="parameters">参数</param> /// <exception cref="SocketException"></exception> /// <exception cref="SerializerException"></exception> /// <exception cref="ProtocolException"></exception> /// <returns>远程数据任务</returns> public Task <T> InvokeApi <T>(string api, params object[] parameters) { var id = this.Server.PacketIdProvider.NewId(); var packet = new FastPacket(api, id, false); packet.SetBodyParameters(this.Server.Serializer, parameters); return(FastTcpCommon.InvokeApi <T>(this, this.Server.TaskSetActionTable, this.Server.Serializer, packet)); }
/// <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.middleware.PacketIdProvider.NewId(); var packet = new FastPacket(api, id, false); packet.SetBodyParameters(this.middleware.Serializer, parameters); return(Common.InvokeApi <T>(this.session, this.middleware.TaskSetterTable, this.middleware.Serializer, packet, this.middleware.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.middleware.PacketIdProvider.NewId(); var packet = new FastPacket(api, id, false); packet.SetBodyParameters(this.middleware.Serializer, parameters); this.session.Send(packet.ToArraySegment()); }
/// <summary> /// 解析一个数据包 /// 不足一个封包时返回null /// </summary> /// <param name="streamReader">数据读取器</param> /// <param name="packet">数据包</param> /// <returns></returns> public static bool Parse(ISessionStreamReader streamReader, out FastPacket packet) { packet = null; const int packetMinSize = 16; if (streamReader.Length < packetMinSize || streamReader[0] != FastPacket.Mark) { return(false); } streamReader.Position = 1; var totalBytes = streamReader.ReadInt32(); if (totalBytes < packetMinSize) { return(false); } // 数据包未接收完整 if (streamReader.Length < totalBytes) { return(true); } // api名称数据长度 var apiNameLength = streamReader.ReadByte(); if (totalBytes < apiNameLength + packetMinSize) { return(false); } // api名称数据 var apiNameBytes = streamReader.ReadArray(apiNameLength); // 标识符 var id = streamReader.ReadInt64(); // 是否为客户端封包 var isFromClient = streamReader.ReadBoolean(); // 是否异常 var isException = streamReader.ReadBoolean(); // 实体数据 var body = streamReader.ReadArray(totalBytes - streamReader.Position); // 清空本条数据 streamReader.Clear(totalBytes); var apiName = Encoding.UTF8.GetString(apiNameBytes); packet = new FastPacket(apiName, id, isFromClient) { TotalBytes = totalBytes, ApiNameLength = apiNameLength, IsException = isException, Body = body }; return(true); }
/// <summary> /// 获取数据包 /// </summary> /// <param name="buffer">接收到的历史数据</param> /// <returns></returns> private IEnumerable <FastPacket> GetPacketsFromBuffer(ReceiveBuffer buffer) { FastPacket packet; while ((packet = FastPacket.From(buffer)) != null) { yield return(packet); } }
/// <summary> /// 发送数据包 /// </summary> /// <param name="package">数据包</param> /// <returns></returns> private bool TrySendPackage(FastPacket package) { try { return(this.Send(package.ToArraySegment()) > 0); } catch (Exception) { return(false); } }
/// <summary> /// 接收到会话对象的数据包 /// </summary> /// <param name="session">会话对象</param> /// <param name="packet">数据包</param> private void OnRecvPacket(FastSession session, FastPacket packet) { var requestContext = new RequestContext(session, packet, this.AllSessions); if (packet.IsException) { this.ProcessRemoteException(requestContext); } else { this.ProcessRequest(requestContext); } }
/// <summary> /// 接收到服务发来的数据包 /// </summary> /// <param name="packet">数据包</param> private void OnRecvPacket(FastPacket packet) { var requestContext = new RequestContext(null, packet, null); if (packet.IsException == false) { this.ProcessRequest(requestContext); } else { this.ProcessRemoteException(requestContext); } }
/// <summary> /// 当接收到远程端的数据时,将触发此方法 /// </summary> /// <param name="buffer">接收到的历史数据</param> protected override void OnReceive(ReceiveBuffer buffer) { while (true) { var packet = FastPacket.From(buffer); if (packet == null) { break; } // 新线程处理业务内容 Task.Factory.StartNew(() => this.OnRecvPacket(packet)); } }
/// <summary> /// 接收到服务发来的数据包 /// </summary> /// <param name="packet">数据包</param> private void OnReceivePacket(FastPacket packet) { var requestContext = new RequestContext(null, packet, null); if (packet.IsException == true) { Common.SetApiActionTaskException(this.taskSetActionTable, requestContext); } else { this.ProcessRequest(requestContext); } }
/// <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="packet">数据包</param> private async void ProcessPacketAsync(FastPacket packet) { var requestContext = new RequestContext(null, packet, null); if (packet.IsException == true) { Common.SetApiActionTaskException(this.taskSetterTable, requestContext); } else if (packet.IsFromClient == true) { Common.SetApiActionTaskResult(requestContext, this.taskSetterTable, this.Serializer); } else { await TryProcessRequestPackageAsync(requestContext); } }
/// <summary> /// 当接收到远程端的数据时,将触发此方法 /// </summary> /// <param name="stream">接收到的历史数据</param> protected sealed override void OnReceive(INsStream stream) { while (true) { var packet = default(FastPacket); if (FastPacket.Parse(stream, out packet) == false) { stream.Clear(); } if (packet == null) { break; } // 新线程处理业务内容 Task.Factory.StartNew(() => this.OnReceivePacket(packet)); } }
/// <summary> /// 生成数据包 /// </summary> /// <param name="streamReader">数据流</param> /// <returns></returns> private IList <FastPacket> GenerateFastPackets(ISessionStreamReader streamReader) { var list = new List <FastPacket>(); while (true) { if (FastPacket.Parse(streamReader, out FastPacket packet) == false) { return(list); } if (packet == null) { return(list); } list.Add(packet); } }
/// <summary> /// 生成数据包 /// </summary> /// <param name="inputStream">数据流</param> /// <returns></returns> private IList <FastPacket> GenerateFastPackets(IStreamReader inputStream) { var list = new List <FastPacket>(); while (true) { var packet = default(FastPacket); if (FastPacket.Parse(inputStream, out packet) == false) { return(list); } if (packet == null) { return(list); } list.Add(packet); } }
/// <summary> /// 生成数据包 /// </summary> /// <param name="context">上下文</param> /// <param name="fastPacket">数据包</param> /// <returns></returns> private IEnumerable <FastPacket> GenerateFastPackets(IContenxt context, FastPacket fastPacket) { yield return(fastPacket); while (true) { var packet = default(FastPacket); if (FastPacket.Parse(context.Buffer, out packet) == false) { yield break; } if (packet == null) { yield break; } yield return(packet); } }
/// <summary> /// 生成数据包 /// </summary> /// <param name="context">上下文</param> /// <param name="fastPacket">数据包</param> /// <returns></returns> private IList <FastPacket> GenerateFastPackets(IContenxt context, FastPacket fastPacket) { var list = new List <FastPacket> { fastPacket }; while (true) { if (FastPacket.Parse(context.StreamReader, out FastPacket packet) == false) { return(list); } if (packet == null) { return(list); } list.Add(packet); } }
/// <summary> /// 生成数据包 /// </summary> /// <param name="context">上下文</param> /// <param name="fastPacket">数据包</param> /// <returns></returns> private IList <FastPacket> GenerateFastPackets(IContenxt context, FastPacket fastPacket) { var list = new List <FastPacket> { fastPacket }; while (true) { var packet = default(FastPacket); if (FastPacket.Parse(context.InputStream, out packet) == false) { return(list); } if (packet == null) { return(list); } list.Add(packet); } }
/// <summary> /// 当接收到会话对象的数据时,将触发此方法 /// </summary> /// <param name="session">会话对象</param> /// <param name="buffer">接收到的历史数据</param> /// <returns></returns> protected override void OnReceive(FastSession session, ReceiveBuffer buffer) { while (true) { FastPacket packet = null; try { packet = FastPacket.From(buffer); } catch (Exception ex) { buffer.Clear(); this.OnException(session, ex); } if (packet == null) { break; } // 新线程处理业务内容 Task.Factory.StartNew(() => this.OnRecvPacket(session, packet)); } }
/// <summary> /// 解析一个数据包 /// 不足一个封包时返回null /// </summary> /// <param name="buffer">接收到的历史数据</param> /// <exception cref="ProtocolException"></exception> /// <returns></returns> public static FastPacket From(ReceiveBuffer buffer) { if (buffer.Length < 4) { return null; } buffer.Position = 0; var totalBytes = buffer.ReadInt32(); const int packegMaxSize = 10 * 1204 * 1024; // 10M if (totalBytes > packegMaxSize) { throw new ProtocolException(); } // 少于15字节是异常数据,清除收到的所有数据 const int packetMinSize = 15; if (totalBytes < packetMinSize) { throw new ProtocolException(); } // 数据包未接收完整 if (buffer.Length < totalBytes) { return null; } // api名称数据长度 var apiNameLength = buffer.ReadByte(); if (totalBytes < apiNameLength + packetMinSize) { throw new ProtocolException(); } // api名称数据 var apiNameBytes = buffer.ReadArray(apiNameLength); // 标识符 var id = buffer.ReadInt64(); // 是否为客户端封包 var isFromClient = buffer.ReadBoolean(); // 是否异常 var isException = buffer.ReadBoolean(); // 实体数据 var body = buffer.ReadArray(totalBytes - buffer.Position); // 清空本条数据 buffer.Clear(totalBytes); var apiName = Encoding.UTF8.GetString(apiNameBytes); var packet = new FastPacket(apiName, id, isFromClient) { TotalBytes = totalBytes, ApiNameLength = apiNameLength, IsException = isException, Body = body }; return packet; }
/// <summary> /// 解析一个数据包 /// 不足一个封包时返回null /// </summary> /// <param name="buffer">接收到的历史数据</param> /// <param name="packet">数据包</param> /// <returns></returns> public static bool Parse(IReceiveBuffer buffer, out FastPacket packet) { if (buffer.Length < 1 || buffer[0] != FastPacket.Mark) { packet = null; return false; } if (buffer.Length < 5) { packet = null; return true; } buffer.Position = 1; const int packetMinSize = 16; var totalBytes = buffer.ReadInt32(); if (totalBytes < packetMinSize) { packet = null; return false; } // 数据包未接收完整 if (buffer.Length < totalBytes) { packet = null; return true; } // api名称数据长度 var apiNameLength = buffer.ReadByte(); if (totalBytes < apiNameLength + packetMinSize) { packet = null; return false; } // api名称数据 var apiNameBytes = buffer.ReadArray(apiNameLength); // 标识符 var id = buffer.ReadInt64(); // 是否为客户端封包 var isFromClient = buffer.ReadBoolean(); // 是否异常 var isException = buffer.ReadBoolean(); // 实体数据 var body = buffer.ReadArray(totalBytes - buffer.Position); // 清空本条数据 buffer.Clear(totalBytes); var apiName = Encoding.UTF8.GetString(apiNameBytes); packet = new FastPacket(apiName, id, isFromClient) { TotalBytes = totalBytes, ApiNameLength = apiNameLength, IsException = isException, Body = body }; return true; }
/// <summary> /// 当操作中遇到处理异常时,将触发此方法 /// </summary> /// <param name="packet">数据包对象</param> /// <param name="pxception">异常对象</param> /// <param name="exceptionHandled">异常是否已处理</param> protected virtual void OnException(FastPacket packet, Exception pxception, out bool exceptionHandled) { exceptionHandled = false; }
/// <summary> /// 请求上下文 /// </summary> /// <param name="session">当前会话对象</param> /// <param name="packet">数据包对象</param> /// <param name="allSessions">所有会话对象</param> internal RequestContext(FastSession session, FastPacket packet, IEnumerable <FastSession> allSessions) { this.Session = session; this.Packet = packet; this.AllSessions = allSessions; }
/// <summary> /// 当操作中遇到处理异常时,将触发此方法 /// </summary> /// <param name="packet">数据包对象</param> /// <param name="exception">异常对象</param> protected virtual void OnException(FastPacket packet, Exception exception) { }
/// <summary> /// 解析一个数据包 /// 不足一个封包时返回null /// </summary> /// <param name="buffer">接收到的历史数据</param> /// <exception cref="ProtocolException"></exception> /// <returns></returns> public static FastPacket From(ReceiveStream buffer) { if (buffer.Length < 4) { return(null); } buffer.Position = 0; var totalBytes = buffer.ReadInt32(); const int packegMaxSize = 10 * 1204 * 1024; // 10M if (totalBytes > packegMaxSize) { throw new ProtocolException(); } // 少于15字节是异常数据,清除收到的所有数据 const int packetMinSize = 15; if (totalBytes < packetMinSize) { throw new ProtocolException(); } // 数据包未接收完整 if (buffer.Length < totalBytes) { return(null); } // api名称数据长度 var apiNameLength = buffer.ReadByte(); if (totalBytes < apiNameLength + packetMinSize) { throw new ProtocolException(); } // api名称数据 var apiNameBytes = buffer.ReadArray(apiNameLength); // 标识符 var id = buffer.ReadInt64(); // 是否为客户端封包 var isFromClient = buffer.ReadBoolean(); // 是否异常 var isException = buffer.ReadBoolean(); // 实体数据 var body = buffer.ReadArray(totalBytes - buffer.Position); // 清空本条数据 buffer.Clear(totalBytes); var apiName = Encoding.UTF8.GetString(apiNameBytes); var packet = new FastPacket(apiName, id, isFromClient) { TotalBytes = totalBytes, ApiNameLength = apiNameLength, IsException = isException, Body = body }; return(packet); }
/// <summary> /// 生成数据包 /// </summary> /// <param name="context">上下文</param> /// <param name="fastPacket">数据包</param> /// <returns></returns> private IList<FastPacket> GenerateFastPackets(IContenxt context, FastPacket fastPacket) { var list = new List<FastPacket> { fastPacket }; while (true) { var packet = default(FastPacket); if (FastPacket.Parse(context.Buffer, out packet) == false) { return list; } if (packet == null) { return list; } list.Add(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>(ISession session, TaskSetterTable <long> taskSetActionTable, ISerializer serializer, FastPacket packet, TimeSpan timeout) { var taskSetter = taskSetActionTable.Create <T>(packet.Id, timeout); session.Send(packet.ToArraySegment()); return(new ApiResult <T>(taskSetter)); }
/// <summary> /// 请求上下文 /// </summary> /// <param name="session">当前会话对象</param> /// <param name="packet">数据包对象</param> /// <param name="allSessions">所有会话对象</param> internal RequestContext(FastSession session, FastPacket packet, IEnumerable<FastSession> allSessions) { this.Session = session; this.Packet = packet; this.AllSessions = allSessions; }
/// <summary> /// 请求上下文 /// </summary> /// <param name="session">当前会话对象</param> /// <param name="packet">数据包对象</param> /// <param name="allSessions">所有会话对象</param> internal RequestContext(FastSession session, FastPacket packet, ISessionProvider allSessions) { this.Session = session; this.Packet = packet; this.AllSessions = allSessions; }