private void Input_OnReceive(object sender, ChannelReceivedEventArgs e) { MbapHeader header = MbapHeader.Decode(e.Message); if (header == null) { logger?.LogWarning("MBAP Header returned null"); return; //assume keep alive } if (!map.HasItem(header.UnitId)) { byte[] errorMsg = ModbusErrorMessage.Create(e.Message, ErrorCode.GatewayPathsNotAvailable); this.InputChannel.SendAsync(errorMsg).GetAwaiter(); return; } if (!map.GetItem(header.UnitId).Authorize(e.Message)) { byte[] errorMsg = ModbusErrorMessage.Create(e.Message, ErrorCode.IllegalAddress); this.InputChannel.SendAsync(errorMsg).GetAwaiter(); return; } byte[] message = e.Message; byte[] msg = null; foreach (var filter in InputFilters) { msg = filter.Execute(message); msg ??= message; } OutputChannel.SendAsync(msg).GetAwaiter(); }
private void Input_OnReceive(object sender, ChannelReceivedEventArgs e) { byte[] message = e.Message; byte[] msg = message; try { MbapHeader header = MbapHeader.Decode(message); if (header == null) { return; } Slave slave = config.Slaves.Where(s => s.UnitId == header.UnitId).FirstOrDefault(); byte? alias = slave?.Alias.Value; foreach (var filter in InputFilters) { msg = filter.Execute(message, alias); msg ??= message; logger?.LogDebug("Filter executed."); } OutputChannel.SendAsync(msg).GetAwaiter(); logger?.LogDebug("Message sent to output channel."); } catch (Exception ex) { logger?.LogError(ex, "Fault input channel receive."); } }