Exemplo n.º 1
0
        /// <summary>
        /// 发送消息。
        /// </summary>
        /// <param name="message">远程调用消息模型。</param>
        /// <returns>远程调用消息的传输消息。</returns>
        public async Task<RemoteInvokeResultMessage> SendAsync(RemoteInvokeMessage message)
        {
            try
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug("准备发送消息。");

                var transportMessage = TransportMessage.CreateInvokeMessage(message);

                //注册结果回调
                var callbackTask = RegisterResultCallbackAsync(transportMessage.Id);

                //发送
                await _messageSender.SendAndFlushAsync(transportMessage);

                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug("消息发送成功。");

                return await callbackTask;
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError("消息发送失败。", exception);
                throw;
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 创建一个调用传输消息。
 /// </summary>
 /// <param name="invokeMessage">调用实例。</param>
 /// <returns>调用传输消息。</returns>
 public static TransportMessage CreateInvokeMessage(RemoteInvokeMessage invokeMessage)
 {
     return new TransportMessage
     {
         Id = Guid.NewGuid().ToString("N"),
         Content = invokeMessage,
         ContentType = typeof(RemoteInvokeMessage).FullName
     };
 }
Exemplo n.º 3
0
        /// <summary>
        /// 发送消息。
        /// </summary>
        /// <param name="message">远程调用消息模型。</param>
        /// <returns>远程调用消息的传输消息。</returns>
        public async Task<TransportMessage> SendAsync(RemoteInvokeMessage message)
        {
            try
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug("准备发送消息。");

                var transportMessage = TransportMessage.CreateInvokeMessage(message);

                await _messageSender.SendAndFlushAsync(transportMessage);

                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.LogDebug("消息发送成功。");

                return transportMessage;
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Error))
                    _logger.LogError("消息发送失败。", exception);
                throw;
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 发送消息。
        /// </summary>
        /// <param name="message">远程调用消息模型。</param>
        /// <returns>一个任务。</returns>
        public async Task SendAsync(RemoteInvokeMessage message)
        {
            try
            {
                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.Debug("准备发送消息。");

                var data = _serializer.Serialize(message);
                var buffer = Unpooled.Buffer(data.Length);

                if (_logger.IsEnabled(LogLevel.Debug))
                    _logger.Debug($"数据包大小为:{data.Length}。");

                buffer.WriteBytes(data);
                await _messageSender.SendAndFlushAsync(buffer);
            }
            catch (Exception exception)
            {
                if (_logger.IsEnabled(LogLevel.Fatal))
                    _logger.Fatal("消息发送失败。", exception);
                throw;
            }
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
 /// <summary>
 /// 定位服务条目。
 /// </summary>
 /// <param name="invokeMessage">远程调用消息。</param>
 /// <returns>服务条目。</returns>
 public ServiceEntry Locate(RemoteInvokeMessage invokeMessage)
 {
     var serviceEntries = _serviceEntryManager.GetEntries();
     return serviceEntries.SingleOrDefault(i => i.Descriptor.Id == invokeMessage.ServiceId);
 }
 public ProtoBufferRemoteInvokeMessage(RemoteInvokeMessage message)
 {
     ServiceId = message.ServiceId;
     Parameters = message.Parameters?.Select(i => new ParameterItem(i)).ToArray();
 }