KiloSession.onConnected handler added

This commit is contained in:
Sergey Chernov 2023-11-17 17:43:38 +03:00
parent c6ac6f5907
commit e7abbe6d1d
6 changed files with 11 additions and 3 deletions

View File

@ -12,7 +12,7 @@ import net.sergeych.utools.pack
private var clientIds = 0
class KiloClientConnection<S>(
private val clientInterface: LocalInterface<KiloScope<S>>,
private val clientInterface: KiloInterface<S>,
private val device: Transport.Device,
private val session: S,
private val secretIdKey: Key.Signing? = null,
@ -81,6 +81,7 @@ class KiloClientConnection<S>(
kiloRemoteInterface.complete(
KiloRemoteInterface(deferredParams, clientInterface)
)
clientInterface.onConnectHandler?.invoke(params.scope)
onConnectedStateChanged?.invoke(true)
job.join()

View File

@ -7,6 +7,11 @@ package net.sergeych.kiloparsec
* BAse implementation registers relevant exceptions.
*/
class KiloInterface<S> : LocalInterface<KiloScope<S>>() {
internal var onConnectHandler: (KiloScope<S>.()->Unit) ? = null
fun onConnected(f: KiloScope<S>.()->Unit) { onConnectHandler = f }
init {
registerError { RemoteInterface.UnknownCommand() }
registerError { RemoteInterface.ClosedException(it) }

View File

@ -83,6 +83,7 @@ class KiloServerConnection<S>(
kiloRemoteInterface.complete(
KiloRemoteInterface(deferredParams, clientInterface)
)
clientInterface.onConnectHandler?.invoke(p.scope)
}
}

View File

@ -49,7 +49,6 @@ fun String.toNetworkAddress() : NetworkAddress {
return NetworkAddress(host, port.toInt())
}
expect fun acceptTcpDevice(port: Int): Flow<InetTransportDevice>
expect suspend fun connectTcpDevice(address: NetworkAddress): InetTransportDevice

View File

@ -34,6 +34,7 @@ actual fun acceptTcpDevice(port: Int): Flow<InetTransportDevice> {
}
suspend fun connectTcpDevice(address: String) = connectTcpDevice(address.toNetworkAddress())
@Suppress("unused")
suspend fun connectTcpDevice(host: String, port: Int) = connectTcpDevice(NetworkAddress(host,port))
actual suspend fun connectTcpDevice(address: NetworkAddress): InetTransportDevice {
address as JvmNetworkAddress

View File

@ -30,6 +30,7 @@ class ClientTest {
val cmdLoad by command<Unit,String>()
val cli = KiloInterface<Session>().apply {
onConnected { session.data = "start" }
on(cmdSave) { session.data = it }
on(cmdLoad) {
println("load!")
@ -41,7 +42,7 @@ class ClientTest {
}
println(client.call(cmdLoad))
assertEquals("unknown", client.call(cmdLoad))
assertEquals("start", client.call(cmdLoad))
client.call(cmdSave, "foobar")
assertEquals("foobar", client.call(cmdLoad))
server.close()