Skip to content

Commit

Permalink
Bootstrap and pipeline customizers in Netty server transports (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinherron authored Nov 13, 2024
1 parent 6ba5c82 commit 4a476fe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ public void channelInactive(ChannelHandlerContext ctx) {
}
})
.addLast(new ModbusRtuServerFrameReceiver());

config.pipelineCustomizer().accept(ch.pipeline());
} else {
ch.close();
}
Expand All @@ -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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ServerBootstrap> bootstrapCustomizer,
Consumer<ChannelPipeline> pipelineCustomizer
) {

/**
Expand Down Expand Up @@ -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<ServerBootstrap> bootstrapCustomizer = b -> {};

/**
* A {@link Consumer} that can be used to customize the Netty {@link ChannelPipeline}.
*/
public Consumer<ChannelPipeline> pipelineCustomizer = p -> {};

public NettyServerTransportConfig build() {
if (eventLoopGroup == null) {
eventLoopGroup = Netty.sharedEventLoop();
Expand All @@ -68,7 +86,9 @@ public NettyServerTransportConfig build() {
bindAddress,
port,
eventLoopGroup,
executor
executor,
bootstrapCustomizer,
pipelineCustomizer
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ public void channelInactive(ChannelHandlerContext ctx) {
})
.addLast(new ModbusTcpCodec())
.addLast(new ModbusTcpFrameHandler());

config.pipelineCustomizer().accept(ch.pipeline());
}
});

bootstrap.group(config.eventLoopGroup());
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()) {
Expand Down

0 comments on commit 4a476fe

Please sign in to comment.