Skip to content

Commit

Permalink
connect
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Aug 30, 2024
1 parent fcc2737 commit c22473b
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ protected void initChannel(DatagramChannel ch) {
private Bootstrap createClientBootstrap(InetSocketAddress destinationAddress) {
return new Bootstrap()
.group(eventLoopGroup)
.localAddress(0)
.remoteAddress(destinationAddress)
.channel(NioDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private String connectAndReadCurrentThreadName() throws IOException, CoapExcepti
InetSocketAddress serverAddress = localhost(((InetSocketAddress) serverBootstrap.config().localAddress()).getPort());
Bootstrap clientBootstrap = new Bootstrap()
.group(eventLoopGroup)
.localAddress(0)
.remoteAddress(serverAddress)
.channel(EpollDatagramChannel.class)
.handler(new ChannelInitializer<DatagramChannel>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ public NettyCoapTransport(Bootstrap bootstrap, Function<DatagramPacket, Transpor

@Override
public void start() {
init(bootstrap.bind().syncUninterruptibly().channel());

// for random port update bootstrap with actual port but only if SO_REUSEPORT is enabled
if (bindToRandomPort() && reusePortIsEnabled()) {
bootstrap.localAddress(channel.localAddress());
if (bootstrap.config().remoteAddress() != null) {
init(bootstrap.connect().syncUninterruptibly().channel());
} else {
init(bootstrap.bind().syncUninterruptibly().channel());

// for random port update bootstrap with actual port but only if SO_REUSEPORT is enabled
if (bindToRandomPort() && reusePortIsEnabled()) {
bootstrap.localAddress(channel.localAddress());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2022-2023 java-coap contributors (https://github.com/open-coap/java-coap)
* Copyright (C) 2022-2024 java-coap contributors (https://github.com/open-coap/java-coap)
* SPDX-License-Identifier: Apache-2.0
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,7 @@
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.util.concurrent.DefaultThreadFactory;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -83,13 +84,14 @@ void should_exchange_messages() throws Exception {
@Test
void should_exchange_messages_with_multiple_clients() throws Exception {
// given
InetSocketAddress serverAddress = localhost(server.getLocalSocketAddress().getPort());
List<CoapClient> clients = new ArrayList(10);
for (int i = 0; i < 10; i++) {
clients.add(
CoapServer.builder()
.transport(new NettyCoapTransport(createBootstrap(0), EMPTY_RESOLVER))
.transport(new NettyCoapTransport(createBootstrap(0).remoteAddress(serverAddress), EMPTY_RESOLVER))
.executor(eventLoopGroup)
.buildClient(localhost(server.getLocalSocketAddress().getPort()))
.buildClient(serverAddress)
);
}

Expand Down

0 comments on commit c22473b

Please sign in to comment.