Skip to content

Commit

Permalink
DtlsTransmitter: more java friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Jun 24, 2022
1 parent 0c527c1 commit c385a67
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,26 @@ class DtlsTransmitter private constructor(
return Executors.newSingleThreadExecutor { Thread(it, "dtls-" + threadIndex.getAndIncrement()) }
}

@JvmStatic
@JvmOverloads
fun connect(server: DtlsServer, conf: SslConfig): CompletableFuture<DtlsTransmitter> {
return connect(InetSocketAddress(InetAddress.getLocalHost(), server.localPort()), conf)
}

@JvmStatic
@JvmOverloads
fun connect(peerCnnTrans: ConnectedDatagramTransmitter, conf: SslConfig, bindPort: Int = 0): CompletableFuture<DtlsTransmitter> {
return connect(peerCnnTrans.localAddress(), conf, bindPort)
}

@JvmStatic
@JvmOverloads
fun connect(dest: InetSocketAddress, conf: SslConfig, bindPort: Int = 0): CompletableFuture<DtlsTransmitter> {
return connect(conf, ConnectedDatagramTransmitter.connect(dest, bindPort))
}

@JvmStatic
@JvmOverloads
fun connect(conf: SslConfig, channel: ConnectedDatagramTransmitter, executor: ExecutorService = newSingleExecutor()): CompletableFuture<DtlsTransmitter> {
return executor.supply {
connect0(conf, channel, executor)
Expand Down Expand Up @@ -92,10 +100,14 @@ class DtlsTransmitter private constructor(
return sslContext as SslSession
}

@JvmStatic
@JvmOverloads
fun create(dest: InetSocketAddress, sslSession: SslSession, bindPort: Int = 0): DtlsTransmitter {
return create(sslSession, ConnectedDatagramTransmitter.connect(dest, bindPort))
}

@JvmStatic
@JvmOverloads
fun create(sslSession: SslSession, cnnTransmitter: ConnectedDatagramTransmitter): DtlsTransmitter {
return DtlsTransmitter(cnnTransmitter, sslSession, newSingleExecutor())
}
Expand Down Expand Up @@ -145,6 +157,8 @@ interface ConnectedDatagramTransmitter : Closeable {
fun remoteAddress(): InetSocketAddress

companion object {
@JvmStatic
@JvmOverloads
fun connect(dest: InetSocketAddress, listenPort: Int = 0): ConnectedDatagramTransmitter {
val channel: DatagramChannel = DatagramChannel.open()
if (listenPort > 0) channel.bind(InetSocketAddress("0.0.0.0", listenPort))
Expand Down

0 comments on commit c385a67

Please sign in to comment.