Skip to content
Open
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
11 changes: 10 additions & 1 deletion core/src/main/java/org/netcrusher/tcp/TcpAcceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class TcpAcceptor implements NetFreezer {
BufferOptions bufferOptions) throws IOException
{
this.crusher = crusher;
this.bindAddress = bindAddress;
this.connectAddress = connectAddress;
this.bindBeforeConnectAddress = bindBeforeConnectAddress;
this.socketOptions = socketOptions;
Expand All @@ -79,6 +78,12 @@ class TcpAcceptor implements NetFreezer {
this.serverSocketChannel.bind(bindAddress);
}

if (bindAddress.getPort() == 0 && serverSocketChannel.getLocalAddress() instanceof InetSocketAddress) {
this.bindAddress = (InetSocketAddress) serverSocketChannel.getLocalAddress();
} else {
this.bindAddress = bindAddress;
}

this.serverSelectionKey = reactor.getSelector()
.register(serverSocketChannel, 0, (selectionKey) -> this.accept());

Expand Down Expand Up @@ -252,6 +257,10 @@ public boolean isFrozen() {
return state.isAnyOf(State.FROZEN | State.CLOSED);
}

public InetSocketAddress getBindAddress() {
return bindAddress;
}

private static final class State extends BitState {

private static final int OPEN = bit(0);
Expand Down
6 changes: 5 additions & 1 deletion core/src/main/java/org/netcrusher/tcp/TcpCrusher.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class TcpCrusher implements NetCrusher {

private static final int DEFAULT_PAIR_CAPACITY = 32;

private final InetSocketAddress bindAddress;
private InetSocketAddress bindAddress;

private final InetSocketAddress connectAddress;

Expand Down Expand Up @@ -128,6 +128,10 @@ public void open() {

state.set(State.FROZEN);

if (bindAddress.getPort() == 0) {
this.bindAddress = this.acceptor.getBindAddress();
}

LOGGER.info("TcpCrusher <{}>-<{}> is open", bindAddress, connectAddress);

unfreeze();
Expand Down