websocket client now includes transport device to use in higher order protocols
This commit is contained in:
parent
4098358233
commit
9545ca28cf
@ -8,7 +8,7 @@ plugins {
|
|||||||
}
|
}
|
||||||
|
|
||||||
group = "net.sergeych"
|
group = "net.sergeych"
|
||||||
version = "0.4.5-SNAPSHOT"
|
version = "0.4.6-SNAPSHOT"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
@ -11,10 +11,7 @@ import kotlinx.coroutines.channels.ClosedReceiveChannelException
|
|||||||
import kotlinx.coroutines.channels.ClosedSendChannelException
|
import kotlinx.coroutines.channels.ClosedSendChannelException
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import net.sergeych.crypto2.SigningKey
|
import net.sergeych.crypto2.SigningKey
|
||||||
import net.sergeych.kiloparsec.KiloClient
|
import net.sergeych.kiloparsec.*
|
||||||
import net.sergeych.kiloparsec.KiloConnectionData
|
|
||||||
import net.sergeych.kiloparsec.KiloInterface
|
|
||||||
import net.sergeych.kiloparsec.RemoteInterface
|
|
||||||
import net.sergeych.mp_logger.LogTag
|
import net.sergeych.mp_logger.LogTag
|
||||||
import net.sergeych.mp_logger.exception
|
import net.sergeych.mp_logger.exception
|
||||||
import net.sergeych.mp_logger.info
|
import net.sergeych.mp_logger.info
|
||||||
@ -24,23 +21,39 @@ import net.sergeych.tools.AtomicCounter
|
|||||||
|
|
||||||
private val counter = AtomicCounter()
|
private val counter = AtomicCounter()
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shortcut to create websocket client. Use [webSocketTransportDevice] with [KiloClient]
|
||||||
|
* for fine-grained control.
|
||||||
|
*/
|
||||||
fun <S> websocketClient(
|
fun <S> websocketClient(
|
||||||
path: String,
|
path: String,
|
||||||
clientInterface: KiloInterface<S> = KiloInterface(),
|
clientInterface: KiloInterface<S> = KiloInterface(),
|
||||||
client: HttpClient = HttpClient { install(WebSockets) },
|
|
||||||
secretKey: SigningKey? = null,
|
secretKey: SigningKey? = null,
|
||||||
sessionMaker: () -> S = {
|
sessionMaker: () -> S = {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
Unit as S
|
Unit as S
|
||||||
},
|
},
|
||||||
): KiloClient<S> {
|
): KiloClient<S> {
|
||||||
|
return KiloClient(clientInterface, secretKey) {
|
||||||
|
KiloConnectionData(webSocketTransportDevice(path), sessionMaker())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create kilopaarsec transport over websocket (ws or wss).
|
||||||
|
* @param path websocket path (must start with ws:// or wss:// and contain a path part)
|
||||||
|
* @client use default [HttpClient], it installs [WebSockets] plugin
|
||||||
|
*/
|
||||||
|
fun webSocketTransportDevice(
|
||||||
|
path: String,
|
||||||
|
client: HttpClient = HttpClient { install(WebSockets) },
|
||||||
|
): Transport.Device {
|
||||||
var u = Url(path)
|
var u = Url(path)
|
||||||
if (u.encodedPath.length <= 1)
|
if (u.encodedPath.length <= 1)
|
||||||
u = URLBuilder(u).apply {
|
u = URLBuilder(u).apply {
|
||||||
encodedPath = "/kp"
|
encodedPath = "/kp"
|
||||||
}.build()
|
}.build()
|
||||||
|
|
||||||
return KiloClient(clientInterface, secretKey) {
|
|
||||||
val input = Channel<UByteArray>()
|
val input = Channel<UByteArray>()
|
||||||
val output = Channel<UByteArray>()
|
val output = Channel<UByteArray>()
|
||||||
val closeHandle = CompletableDeferred<Boolean>()
|
val closeHandle = CompletableDeferred<Boolean>()
|
||||||
@ -66,9 +79,8 @@ fun <S> websocketClient(
|
|||||||
if (closeHandle.isActive) closeHandle.complete(true)
|
if (closeHandle.isActive) closeHandle.complete(true)
|
||||||
} catch (_: ClosedSendChannelException) {
|
} catch (_: ClosedSendChannelException) {
|
||||||
log.info { "send channel closed" }
|
log.info { "send channel closed" }
|
||||||
}
|
} catch (_: CancellationException) {
|
||||||
catch(_: CancellationException) {}
|
} catch (t: Throwable) {
|
||||||
catch(t: Throwable) {
|
|
||||||
log.info { "unexpected exception in websock sender: ${t.stackTraceToString()}" }
|
log.info { "unexpected exception in websock sender: ${t.stackTraceToString()}" }
|
||||||
closeHandle.completeExceptionally(t)
|
closeHandle.completeExceptionally(t)
|
||||||
}
|
}
|
||||||
@ -94,7 +106,7 @@ fun <S> websocketClient(
|
|||||||
if (closeHandle.isActive) closeHandle.complete(false)
|
if (closeHandle.isActive) closeHandle.complete(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(!closeHandle.await()) {
|
if (!closeHandle.await()) {
|
||||||
log.warning { "Client is closing with error" }
|
log.warning { "Client is closing with error" }
|
||||||
throw RemoteInterface.ClosedException()
|
throw RemoteInterface.ClosedException()
|
||||||
}
|
}
|
||||||
@ -109,6 +121,6 @@ fun <S> websocketClient(
|
|||||||
closeHandle.complete(true)
|
closeHandle.complete(true)
|
||||||
// job.cancel()
|
// job.cancel()
|
||||||
}
|
}
|
||||||
KiloConnectionData(device, sessionMaker())
|
return device
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user