Exemplo n.º 1
0
        private bool Send(FastPacket pack)
        {
            lock (this.clientSession)
                this.clientSession.WriteAndFlushAsync(pack);

            return(true);
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        /// <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));
        }
Exemplo n.º 4
0
 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);
 }
Exemplo n.º 5
0
 /// <summary>
 /// 发送数据包
 /// </summary>
 /// <param name="package">数据包</param>
 /// <returns></returns>
 private bool TrySendPackage(FastPacket package)
 {
     try
     {
         return(this.Send(package));
     }
     catch (Exception)
     {
         connected = false;
         return(false);
     }
 }
Exemplo n.º 6
0
        /// <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);
            }
        }
Exemplo n.º 7
0
        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);
        }
Exemplo n.º 8
0
        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);
        }
Exemplo n.º 9
0
        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));
        }
Exemplo n.º 10
0
 /// <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;
 }
Exemplo n.º 11
0
 /// <summary>
 ///  当操作中遇到处理异常时,将触发此方法
 /// </summary>
 /// <param name="packet">数据包对象</param>
 /// <param name="exception">异常对象</param>
 protected virtual void OnException(FastPacket packet, Exception exception)
 {
     connected = false;
 }
Exemplo n.º 12
0
 /// <summary>
 /// 在执行服务方法前触发
 /// 如果返回false,将不执行服务方法
 /// </summary>
 /// <param name="client">客户端</param>
 /// <param name="packet">数据包</param>
 /// <returns></returns>
 public abstract bool OnExecuting(SocketAsync <FastPacket> client, FastPacket packet);
Exemplo n.º 13
0
 /// <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);
 }