private bool Send(FastPacket pack) { lock (this.clientSession) this.clientSession.WriteAndFlushAsync(pack); return(true); }
/// <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.NewId(), true); packet.SetBodyParameters(this.Serializer, parameters); this.Send(packet); }
/// <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 ApiResult <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.clientSession, this.taskSetterTable, this.Serializer, packet, this.TimeOut)); }
public static bool Parse(byte[] buffer, out FastPacket packet) { packet = new FastPacket(); if (buffer.Length != Marshal.SizeOf(typeof(FastPacket))) { return(false); } packet = Tool.ByteToStruct <FastPacket>(buffer, 0, buffer.Length); return(true); }
/// <summary> /// 发送数据包 /// </summary> /// <param name="package">数据包</param> /// <returns></returns> private bool TrySendPackage(FastPacket package) { try { return(this.Send(package)); } catch (Exception) { connected = false; return(false); } }
/// <summary> /// 处理接收到服务发来的数据包 /// </summary> /// <param name="packet">数据包</param> internal async void ProcessPacketAsync(FastPacket packet) { var requestContext = new RequestContext(null, packet); 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); } }
internal async void ProcessPacketAsync(IChannel channel, FastPacket packet) { FastSession fs = null; if (allSessions.ContainsKey(channel.Id.AsLongText())) { fs = allSessions[channel.Id.AsLongText()]; } else { fs = new FastSession(channel, this); allSessions.TryAdd(channel.Id.AsLongText(), fs); } var requestContext = new RequestContext(fs, packet); this.OnRecvFastPacketAsync(requestContext); }
protected override Object Decode(IChannelHandlerContext ctx, IByteBuffer input) { // var x = base.Decode(ctx, input); IByteBuffer bbin = input; if (bbin == null) { return(null); } bbin.MarkReaderIndex(); FastPacket customMsg = null; if (!FastPacket.Parse(bbin, out customMsg)) { bbin.ResetReaderIndex(); return(null); } return(customMsg); }
public static ApiResult <object> InvokeApi(Type returnType, ISession session, TaskSetterTable <long> taskSetActionTable, ISerializer serializer, FastPacket packet, TimeSpan timeout) { var taskSetter = taskSetActionTable.Create(returnType, packet.Id, timeout); session.Send(packet.ToArraySegment()); return(new ApiResult <object>(taskSetter)); }
/// <summary> /// 请求上下文 /// </summary> /// <param name="session">当前会话对象</param> /// <param name="packet">数据包对象</param> /// <param name="allSessions">所有会话对象</param> internal RequestContext(FastSession session, FastPacket packet, ISessionManager 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) { connected = false; }
/// <summary> /// 在执行服务方法前触发 /// 如果返回false,将不执行服务方法 /// </summary> /// <param name="client">客户端</param> /// <param name="packet">数据包</param> /// <returns></returns> public abstract bool OnExecuting(SocketAsync <FastPacket> client, FastPacket packet);
/// <summary> /// 异常时触发此方法 /// </summary> /// <param name="client">客户端</param> /// <param name="exception">异常</param> /// <param name="FastPacket">封包</param> /// <param name="needReturn">是否需要返回数据</param> protected override void OnException(SocketAsync <FastPacket> client, Exception exception, FastPacket FastPacket) { base.OnException(client, exception, FastPacket); }