예제 #1
0
 public async Task SendResult <T>(DeviceIdentity devId, string path, T data, CancellationToken cancel)
 {
     using (var strm = new MemoryStream())
     {
         PayloadHelper.WriteData(strm, new PayloadPacketHeader
         {
             PacketId = GetPacketId(),
             Path     = path,
         });
         PayloadHelper.WriteData(strm, data);
         strm.Position = 0;
         await SendData(devId.SystemId, devId.ComponentId, PayloadHelper.DefaultNetworkId, PayloadHelper.DefaultSuccessMessageType, strm, cancel);
     }
 }
예제 #2
0
        public async Task <TOut> Send <TIn, TOut>(string path, TIn data, CancellationToken cancel) where TOut : new()
        {
            using (var strm = new MemoryStream())
            {
                PayloadHelper.WriteData(strm, new PayloadPacketHeader
                {
                    PacketId = GetPacketId(),
                    Path     = path,
                });
                PayloadHelper.WriteData(strm, data);
                var eve = new AsyncAutoResetEvent(false);

                var result  = default(Result <TOut>);
                var dispose = Register <TOut>(path).FirstAsync(_ => true).Subscribe(_ =>
                {
                    result = _;
                    eve.Set();
                });
                try
                {
                    strm.Position = 0;
                    await SendData(PayloadHelper.DefaultNetworkId, PayloadHelper.DefaultSuccessMessageType, strm, cancel);

                    await eve.WaitAsync(cancel);

                    if (result.IsError)
                    {
                        throw new PayloadClientException(path, result.Error);
                    }
                    Debug.Assert(result.Value != null);
                    return(result.Value);
                }
                catch (Exception e)
                {
                    _logger.Error(e, $"Error to get interface implementations:{e.Message}");
                    throw;
                }
                finally
                {
                    dispose.Dispose();
                }
            }
        }
예제 #3
0
 public async Task SendError(DeviceIdentity devId, string path, ErrorType errorType, string message, CancellationToken cancel)
 {
     try
     {
         message = message.TrimToMaxLength(PayloadHelper.MaxErrorMessageSize);
         using (var strm = new MemoryStream())
         {
             PayloadHelper.WriteData(strm, new PayloadPacketHeader
             {
                 PacketId = GetPacketId(),
                 Path     = path,
             });
             PayloadHelper.WriteData(strm, new ErrorCode {
                 Msg = message, Res = errorType
             });
             strm.Position = 0;
             await SendData(devId.SystemId, devId.ComponentId, PayloadHelper.DefaultNetworkId, PayloadHelper.DefaultErrorMessageType, strm, cancel);
         }
     }
     catch (Exception e)
     {
         _logger.Error(e, $"Exception occured to send error. Type:{errorType:G}, Message:{message}");
     }
 }