Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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