Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(netty): use connect instead of bind in DtlsClientHandshakeChannelHandler #68

Merged
merged 1 commit into from
Sep 2, 2024
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 @@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandlerContext
import io.netty.channel.ChannelPromise
import io.netty.channel.socket.DatagramPacket
import io.netty.util.concurrent.ScheduledFuture
import org.opencoap.ssl.SslConfig
import org.opencoap.ssl.SslException
import org.opencoap.ssl.SslHandshakeContext
import org.opencoap.ssl.SslSession
Expand All @@ -33,15 +34,17 @@ import java.nio.channels.ClosedChannelException
import java.util.concurrent.TimeUnit

class DtlsClientHandshakeChannelHandler(
private val sslHandshakeContext: SslHandshakeContext,
private val peerAddress: InetSocketAddress,
sslConfig: SslConfig,
private val sessionWriter: SessionWriter
) : ChannelDuplexHandler() {
private val logger = LoggerFactory.getLogger(javaClass)
private lateinit var ctx: ChannelHandlerContext
private val outboundMessages: MutableList<Pair<DatagramPacket, ChannelPromise>> = mutableListOf()
private var scheduledRetransmission: ScheduledFuture<*>? = null

private val peerAddress: InetSocketAddress get() = ctx.channel().remoteAddress() as InetSocketAddress
private val sslHandshakeContext: SslHandshakeContext by lazy { sslConfig.newContext(peerAddress) }

private fun write(packet: ByteBuffer) {
val dtlsPacket = DatagramPacket(packet.toByteBuf(), peerAddress)
ctx.writeAndFlush(dtlsPacket)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class NettyTransportAdapter(
return Bootstrap()
.group(group)
.channel(NioDatagramChannel::class.java)
.handler(DtlsClientHandshakeChannelHandler(sslConfig.newContext(destinationAddress), destinationAddress, sessionWriter))
.handler(DtlsClientHandshakeChannelHandler(sslConfig, sessionWriter))
.also(bootstrapConfig)
.bind(0)
.connect(destinationAddress)
.sync()
.channel()
.let { NettyTransportAdapter(it as DatagramChannel, destinationAddress) }
Expand Down