Exemplo n.º 1
0
        /// <summary>
        /// 调用服务方法
        /// </summary>
        /// <param name="method">方法</param>
        /// <param name="client">客户端对象</param>
        /// <param name="packet">数据</param>
        private void InvokeService(ServiceMethod method, SocketAsync <FastPacket> client, FastPacket packet)
        {
            // 执行Filter特性
            foreach (var filter in method.Filters)
            {
                if (filter.OnExecuting(client, packet) == false)
                {
                    return;
                }
            }

            try
            {
                var parameters  = this.GetParameters(method, client, packet);
                var returnValue = method.Invoke(this, parameters);

                if (method.HasReturn && client.IsConnected)
                {
                    packet.SetBodyBinary(this.Serializer, returnValue);
                    client.Send(packet);
                }
            }
            catch (Exception ex)
            {
                this.OnException(client, ex, packet);
            }
        }
Exemplo n.º 2
0
        ///////////////////////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////////////////////

        /// <summary>
        /// Invokes service method without return parameter.
        /// </summary>
        protected void Invoke(ServiceMethod method)
        {
            ServiceMethod <Nothing> functionMethod = (client) =>
            {
                method.Invoke(client);
                return(new Nothing());
            };

            this.Invoke(functionMethod);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Invokes service method that returns a value of type T.
        /// </summary>
        protected T Invoke <T>(ServiceMethod <T> method)
        {
            const int maxRetryCount = 1;
            int       retryCount    = 0;

            while (true)
            {
                var client = _AcquireClient();
                try
                {
                    return(method.Invoke(client));
                }
                catch (Exception e)
                {
                    if (ProxyAuthenticationErrorHandler.HandleError(e))
                    {
                        continue;
                    }

                    var isTokenError = _IsTokenError(e);
                    if (retryCount >= maxRetryCount || !isTokenError)
                    {
                        if (ServiceHelper.IsCommunicationError(e))
                        {
                            throw ServiceHelper.CreateCommException(
                                      _connection.Title,
                                      e);
                        }

                        throw;
                    }

                    if (isTokenError)
                    {
                        _connection.GenerateToken();
                        _CloseClient(client);
                        client = null;
                    }

                    ++retryCount;
                }
                finally
                {
                    _ReleaseClient(client);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 调用服务方法
        /// </summary>
        /// <param name="method">方法</param>
        /// <param name="packet">数据</param>
        private void InvokeService(ServiceMethod method, FastPacket packet)
        {
            try
            {
                var items      = packet.GetBodyParameter();
                var parameters = new object[items.Count];
                for (var i = 0; i < items.Count; i++)
                {
                    parameters[i] = this.Serializer.Deserialize(items[i], method.ParameterTypes[i]);
                }

                var returnValue = method.Invoke(this, parameters);
                if (method.HasReturn && this.IsConnected)
                {
                    packet.SetBodyBinary(this.Serializer, returnValue);
                    this.Send(packet);
                }
            }
            catch (Exception ex)
            {
                this.OnException(ex, packet);
            }
        }