Exemple #1
0
 /// <summary>
 /// 创建一个调用结果传输消息。
 /// </summary>
 /// <param name="id">消息Id。</param>
 /// <param name="invokeResultMessage">调用结果实例。</param>
 /// <returns>调用结果传输消息。</returns>
 public static TransportMessage CreateInvokeResultMessage(long id, RemoteInvokeResultMessage invokeResultMessage)
 {
     return(new TransportMessage(invokeResultMessage)
     {
         Id = id
     });
 }
Exemple #2
0
 /// <summary>
 /// 创建一个调用结果传输消息。
 /// </summary>
 /// <param name="id">消息Id。</param>
 /// <param name="invokeResultMessage">调用结果实例。</param>
 /// <returns>调用结果传输消息。</returns>
 public static TransportMessage CreateInvokeResultMessage(string id, RemoteInvokeResultMessage invokeResultMessage)
 {
     return new TransportMessage
     {
         Id = id,
         Content = invokeResultMessage,
         ContentType = typeof(RemoteInvokeResultMessage).FullName
     };
 }
Exemple #3
0
 /// <summary>
 /// 创建一个调用结果传输消息。
 /// </summary>
 /// <param name="id">消息Id。</param>
 /// <param name="invokeResultMessage">调用结果实例。</param>
 /// <returns>调用结果传输消息。</returns>
 public static TransportMessage CreateInvokeResultMessage(string id, RemoteInvokeResultMessage invokeResultMessage)
 {
     return(new TransportMessage
     {
         Id = id,
         Content = invokeResultMessage,
         ContentType = typeof(RemoteInvokeResultMessage).FullName
     });
 }
        /// <summary>
        /// 执行。
        /// </summary>
        /// <param name="sender">消息发送者。</param>
        /// <param name="message">调用消息。</param>
        public async Task ExecuteAsync(IMessageSender sender, TransportMessage message)
        {
            if (_logger.IsEnabled(LogLevel.Information))
                _logger.LogInformation("接收到消息。");

            if (!message.IsInvokeMessage())
                return;

            RemoteInvokeMessage remoteInvokeMessage;
            try
            {
                remoteInvokeMessage = message.GetContent<RemoteInvokeMessage>();
            }
            catch (Exception exception)
            {
                _logger.LogError("将接收到的消息反序列化成 TransportMessage<RemoteInvokeMessage> 时发送了错误。", exception);
                return;
            }

            var entry = _serviceEntryLocate.Locate(remoteInvokeMessage);

            if (entry == null)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError($"根据服务Id:{remoteInvokeMessage.ServiceId},找不到服务条目。");
                return;
            }

            if (_logger.IsEnabled(LogLevel.Debug))
                _logger.LogDebug("准备执行本地逻辑。");

            var resultMessage = new RemoteInvokeResultMessage();

            //是否需要等待执行。
            if (entry.Descriptor.WaitExecution())
            {
                //执行本地代码。
                await LocalExecuteAsync(entry, remoteInvokeMessage, resultMessage);
                //向客户端发送调用结果。
                await SendRemoteInvokeResult(sender, message.Id, resultMessage);
            }
            else
            {
                //通知客户端已接收到消息。
                await SendRemoteInvokeResult(sender, message.Id, resultMessage);
                //确保新起一个线程执行,不堵塞当前线程。
                await Task.Factory.StartNew(async () =>
                {
                    //执行本地代码。
                    await LocalExecuteAsync(entry, remoteInvokeMessage, resultMessage);
                }, TaskCreationOptions.LongRunning);
            }
        }
        private async Task LocalExecuteAsync(ServiceEntry entry, RemoteInvokeMessage remoteInvokeMessage, RemoteInvokeResultMessage resultMessage)
        {
            try
            {
                var result = await entry.Func(remoteInvokeMessage.Parameters);
                var task = result as Task;

                if (task == null)
                {
                    resultMessage.Result = result;
                }
                else
                {
                    task.Wait();

                    var taskType = task.GetType().GetTypeInfo();
                    if (taskType.IsGenericType)
                        resultMessage.Result = taskType.GetProperty("Result").GetValue(task);
                }
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError("执行本地逻辑时候发生了错误。", exception);
                resultMessage.ExceptionMessage = GetExceptionMessage(exception);
            }
        }
        private async Task SendRemoteInvokeResult(IMessageSender sender, string messageId, RemoteInvokeResultMessage resultMessage)
        {
            try
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug("准备发送响应消息。");

                await sender.SendAndFlushAsync(TransportMessage.CreateInvokeResultMessage(messageId, resultMessage));
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug("响应消息发送成功。");
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError("发送响应消息时候发生了异常。", exception);
            }
        }
Exemple #7
0
        /// <summary>
        /// 执行。
        /// </summary>
        /// <param name="sender">消息发送者。</param>
        /// <param name="message">调用消息。</param>
        public async Task ExecuteAsync(IMessageSender sender, object message)
        {
            var buffer = (IByteBuffer)message;

            if (_logger.IsEnabled(LogLevel.Information))
                _logger.Information($"接收到消息:{buffer.ToString(Encoding.UTF8)}。");

            var content = buffer.ToArray();
            RemoteInvokeMessage remoteInvokeMessage;
            try
            {
                remoteInvokeMessage = _serializer.Deserialize<byte[], RemoteInvokeMessage>(content);
            }
            catch (Exception exception)
            {
                _logger.Error($"将接收到的消息反序列化成 TransportMessage<RemoteInvokeMessage> 时发送了错误,消息内容:{buffer.ToString(Encoding.UTF8)}。", exception);
                return;
            }

            var entry = _serviceEntryLocate.Locate(remoteInvokeMessage);

            if (entry == null)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.Error($"根据服务Id:{remoteInvokeMessage.ServiceId},找不到服务条目。");
                return;
            }

            if (_logger.IsEnabled(LogLevel.Debug))
                _logger.Debug("准备执行本地逻辑。");

            var resultMessage = new RemoteInvokeResultMessage
            {
                Id = remoteInvokeMessage.Id
            };
            try
            {
                var result = entry.Func(remoteInvokeMessage.Parameters);
                var task = result as Task;

                if (task == null)
                {
                    resultMessage.Result = result;
                }
                else
                {
                    task.Wait();

                    var taskType = task.GetType();
                    if (taskType.IsGenericType)
                        resultMessage.Result = taskType.GetProperty("Result").GetValue(task);
                }
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.Error("执行本地逻辑时候发生了错误。", exception);
                resultMessage.ExceptionMessage = GetExceptionMessage(exception);
            }

            try
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.Debug("准备发送响应消息。");
                var resultData = _serializer.Serialize(resultMessage);

                buffer = Unpooled.Buffer(resultData.Length);
                buffer.WriteBytes(resultData);
                await sender.SendAsync(buffer);
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.Debug("响应消息发送成功。");
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.Error("发送响应消息时候发生了异常。", exception);
            }
        }
 public ProtoBufferRemoteInvokeResultMessage(RemoteInvokeResultMessage message)
 {
     ExceptionMessage = message.ExceptionMessage;
     Result = message.Result == null ? null : new ProtoBufferDynamicItem(message.Result);
 }
        /// <summary>
        /// 执行。
        /// </summary>
        /// <param name="sender">消息发送者。</param>
        /// <param name="message">调用消息。</param>
        public async Task ExecuteAsync(IMessageSender sender, TransportMessage message)
        {
            if (_logger.IsEnabled(LogLevel.Information))
                _logger.LogInformation("接收到消息。");

            if (!message.IsInvokeMessage())
                return;

            RemoteInvokeMessage remoteInvokeMessage;
            try
            {
                remoteInvokeMessage = message.GetContent<RemoteInvokeMessage>();
            }
            catch (Exception exception)
            {
                _logger.LogError("将接收到的消息反序列化成 TransportMessage<RemoteInvokeMessage> 时发送了错误。", exception);
                return;
            }

            var entry = _serviceEntryLocate.Locate(remoteInvokeMessage);

            if (entry == null)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError($"根据服务Id:{remoteInvokeMessage.ServiceId},找不到服务条目。");
                return;
            }

            if (_logger.IsEnabled(LogLevel.Debug))
                _logger.LogDebug("准备执行本地逻辑。");

            var resultMessage = new RemoteInvokeResultMessage();
            try
            {
                var result = entry.Func(remoteInvokeMessage.Parameters);
                var task = result as Task;

                if (task == null)
                {
                    resultMessage.Result = result;
                }
                else
                {
                    task.Wait();

                    var taskType = task.GetType().GetTypeInfo();
                    if (taskType.IsGenericType)
                        resultMessage.Result = taskType.GetProperty("Result").GetValue(task);
                }
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError("执行本地逻辑时候发生了错误。", exception);
                resultMessage.ExceptionMessage = GetExceptionMessage(exception);
            }

            try
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug("准备发送响应消息。");

                await sender.SendAsync(TransportMessage.CreateInvokeResultMessage(message.Id, resultMessage));
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug("响应消息发送成功。");
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError("发送响应消息时候发生了异常。", exception);
            }
        }