private void OnData(V2ExtensionPacket v2ExtensionPacket) { try { using (var ms = new MemoryStream(v2ExtensionPacket.Payload.Payload)) { var header = PayloadHelper.ReadData <PayloadPacketHeader>(ms); if (FilterDoublePackets(header) == false) { Interlocked.Increment(ref _doublePacketsCount); return; } if (!_dataCallbacks.TryGetValue(header.Path, out var callback)) { //_logger.Warn($"Receive unsupported data '{packetInfo.Path}'"); return; } callback(new DeviceIdentity { ComponentId = v2ExtensionPacket.ComponenId, SystemId = v2ExtensionPacket.SystemId }, ms); } } catch (Exception e) { _logger.Warn($"Error execute data:{e.Message}"); } }
public void Register <TIn, TOut>(string path, DataDelegate <TIn, TOut> callback) where TIn : new() { _dataCallbacks[path] = async(devId, strm) => { TIn data; try { data = PayloadHelper.ReadData <TIn>(strm); } catch (Exception e) { _logger.Warn(e, $"Error to deserialize input data '{path}'. Payload type {typeof(TIn).Name}"); await SendError(devId, path, ErrorType.ArgsError, e.Message, CancellationToken.None); return; } try { var result = await callback(devId, data); await SendResult(devId, path, result, CancellationToken.None); } catch (Exception e) { Status.Error($"Execute {path}:{e.Message}"); _logger.Warn(e, $"Error to execute '{path}':{e.Message}"); await SendError(devId, path, ErrorType.InternalError, e.Message, CancellationToken.None); } }; }
protected void Register <TIn, TOut>(string path, DataDelegate <TIn, TOut> callback) where TIn : new() { if (!path.StartsWith(_name, StringComparison.InvariantCultureIgnoreCase)) { var absolutePath = PayloadHelper.PathJoin(_name, path); _server.Register(absolutePath, callback); } }
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); } }
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(); } } }
public IObservable <Result <TOut> > Register <TOut>(string path) where TOut : new() { return(_onData.Where(_ => _.Header.Path == path).Select(_ => { using (var strm = new MemoryStream(_.Data)) { try { if (!path.Equals(_.Header.Path)) { return default(Result <TOut>); } if (_.MessageType == PayloadHelper.DefaultErrorMessageType) { var err = PayloadHelper.ReadData <ErrorCode>(strm); return new Result <TOut> { Error = err, IsError = true, }; } if (_.MessageType == PayloadHelper.DefaultSuccessMessageType) { var data = PayloadHelper.ReadData <TOut>(strm); return new Result <TOut> { IsError = false, Value = data, }; } return default(Result <TOut>); } catch (Exception e) { _logger.Warn(e, $"Error to deserialize input data '{path}'. Payload type {typeof(TOut).Name}"); return default(Result <TOut>); } } }).Where(_ => !object.Equals(_, default(TOut)))); }
private void OnData(V2ExtensionPacket v2ExtensionPacket) { try { Interlocked.Increment(ref _txPacketsCounter); PayloadPacketHeader packetInfo = null; using (var ms = new MemoryStream(v2ExtensionPacket.Payload.Payload)) { packetInfo = PayloadHelper.ReadData <PayloadPacketHeader>(ms); if (FilterDoublePackets(packetInfo) == false) { Interlocked.Increment(ref _doublePacketsCounter); return; } var data = new byte[ms.Length - ms.Position]; ms.Read(data, 0, data.Length); var pkt = new V2Packet { Device = new DeviceIdentity { ComponentId = v2ExtensionPacket.ComponenId, SystemId = v2ExtensionPacket.SystemId }, Header = packetInfo, Data = data, MessageType = v2ExtensionPacket.Payload.MessageType, }; _onData.OnNext(pkt); } } catch (Exception e) { _logger.Warn($"Error execute data:{e.Message}"); } }
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}"); } }
protected Task <TOut> Send <TIn, TOut>(string path, TIn data, TimeSpan attemptTimeout, int attemptsCount, CancellationToken cancel, Action <int> progressCallback) where TOut : new() { return(_client.Send <TIn, TOut>(PayloadHelper.PathJoin(_name, path), data, attemptTimeout, attemptsCount, cancel, progressCallback)); }
protected IObservable <Result <TOut> > Register <TOut>(string path) where TOut : new() { var absolutePath = PayloadHelper.PathJoin(_name, path); return(_client.Register <TOut>(absolutePath)); }
public MavlinkPayloadClientInterfaceBase(string name) { _name = name; PayloadHelper.ValidateName(name); }
protected Task SendError(DeviceIdentity devId, string path, ErrorType errorCode, string message, CancellationToken cancel) { return(_server.SendError(devId, PayloadHelper.PathJoin(_name, path), errorCode, message, cancel)); }
protected Task Send <T>(DeviceIdentity devId, string path, T data, CancellationToken cancel) { return(_server.SendResult(devId, PayloadHelper.PathJoin(_name, path), data, cancel)); }
protected PayloadServerInterfaceBase(string name) { _name = name; PayloadHelper.ValidateName(name); }