Skip to content

Commit

Permalink
refactor(netty): use connect instead of bind in DtlsClientHandshakeCh…
Browse files Browse the repository at this point in the history
…annelHandler
  • Loading branch information
szysas committed Sep 2, 2024
1 parent 47c43f6 commit 088a066
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
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

0 comments on commit 088a066

Please sign in to comment.