diff --git a/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyRtuServerTransport.java b/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyRtuServerTransport.java index 0d99150..f1920e7 100644 --- a/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyRtuServerTransport.java +++ b/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyRtuServerTransport.java @@ -72,6 +72,8 @@ public void channelInactive(ChannelHandlerContext ctx) { } }) .addLast(new ModbusRtuServerFrameReceiver()); + + config.pipelineCustomizer().accept(ch.pipeline()); } else { ch.close(); } @@ -82,6 +84,8 @@ public void channelInactive(ChannelHandlerContext ctx) { bootstrap.option(ChannelOption.SO_REUSEADDR, Boolean.TRUE); bootstrap.childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE); + config.bootstrapCustomizer().accept(bootstrap); + bootstrap.bind(config.bindAddress(), config.port()) .addListener((ChannelFutureListener) channelFuture -> { if (channelFuture.isSuccess()) { diff --git a/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyServerTransportConfig.java b/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyServerTransportConfig.java index 9bb7160..5bb49c8 100644 --- a/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyServerTransportConfig.java +++ b/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyServerTransportConfig.java @@ -2,6 +2,8 @@ import com.digitalpetri.modbus.Modbus; import com.digitalpetri.modbus.Netty; +import io.netty.bootstrap.ServerBootstrap; +import io.netty.channel.ChannelPipeline; import io.netty.channel.EventLoopGroup; import java.util.concurrent.ExecutorService; import java.util.function.Consumer; @@ -13,12 +15,18 @@ * @param port the port to bind to. * @param eventLoopGroup the {@link EventLoopGroup} to use. * @param executor the {@link ExecutorService} to use. + * @param bootstrapCustomizer a {@link Consumer} that can be used to customize the Netty + * {@link ServerBootstrap}. + * @param pipelineCustomizer a {@link Consumer} that can be used to customize the Netty + * {@link ChannelPipeline}. */ public record NettyServerTransportConfig( String bindAddress, int port, EventLoopGroup eventLoopGroup, - ExecutorService executor + ExecutorService executor, + Consumer bootstrapCustomizer, + Consumer pipelineCustomizer ) { /** @@ -56,6 +64,16 @@ public static class Builder { */ public ExecutorService executor; + /** + * A {@link Consumer} that can be used to customize the Netty {@link ServerBootstrap}. + */ + public Consumer bootstrapCustomizer = b -> {}; + + /** + * A {@link Consumer} that can be used to customize the Netty {@link ChannelPipeline}. + */ + public Consumer pipelineCustomizer = p -> {}; + public NettyServerTransportConfig build() { if (eventLoopGroup == null) { eventLoopGroup = Netty.sharedEventLoop(); @@ -68,7 +86,9 @@ public NettyServerTransportConfig build() { bindAddress, port, eventLoopGroup, - executor + executor, + bootstrapCustomizer, + pipelineCustomizer ); } } diff --git a/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyTcpServerTransport.java b/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyTcpServerTransport.java index 6c84e65..09d1e96 100644 --- a/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyTcpServerTransport.java +++ b/modbus-tcp/src/main/java/com/digitalpetri/modbus/server/NettyTcpServerTransport.java @@ -73,6 +73,8 @@ public void channelInactive(ChannelHandlerContext ctx) { }) .addLast(new ModbusTcpCodec()) .addLast(new ModbusTcpFrameHandler()); + + config.pipelineCustomizer().accept(ch.pipeline()); } }); @@ -80,6 +82,8 @@ public void channelInactive(ChannelHandlerContext ctx) { bootstrap.option(ChannelOption.SO_REUSEADDR, Boolean.TRUE); bootstrap.childOption(ChannelOption.TCP_NODELAY, Boolean.TRUE); + config.bootstrapCustomizer().accept(bootstrap); + bootstrap.bind(config.bindAddress(), config.port()) .addListener((ChannelFutureListener) channelFuture -> { if (channelFuture.isSuccess()) {