removed nullable array from transport device, as channels close() is more than enough

This commit is contained in:
sergeych committed 2023-11-20 15:49:58 +03:00
1 parent e7abbe6d1d
commit e619b45485
6 files changed
+30 -17

No files matched your search

@@ -41,7 +41,7 @@ class Transport<S>(
* Input blocks. When the device is disconnected, it should send one null to this channel
* to notify the owner. When [close] is called, the channel should be closed.
*/
val input: ReceiveChannel<UByteArray?>
val input: ReceiveChannel<UByteArray>
/**
* Send a binary block to a remote party where it should be received and put into [input]
@@ -139,7 +139,7 @@ class Transport<S>(
debug { "awaiting incoming blocks" }
while (isActive && !isClosed) {
try {
device.input.receive()?.let { packed ->
device.input.receive().let { packed ->
debug { "<<<\n${packed.toDump()}" }
val b = unpack<Block>(packed)
debug { "<<$ $b" }
@@ -181,9 +181,6 @@ class Transport<S>(
.also { debug { "command executed: ${b.name}" } }
}
}
} ?: run {
debug { "remote channel close received" }
isClosed = true
}
} catch (_: CancellationException) {
info { "loop is cancelled" }
@@ -7,7 +7,7 @@ import kotlinx.coroutines.channels.Channel
*/
@Suppress("unused")
class InetTransportDevice(
inputChannel: Channel<UByteArray?>,
inputChannel: Channel<UByteArray>,
outputChannel: Channel<UByteArray>,
val remoteAddress: NetworkAddress,
val flush: suspend ()->Unit = {},
@@ -6,11 +6,11 @@ import kotlinx.coroutines.channels.SendChannel
import net.sergeych.kiloparsec.Transport
open class ProxyDevice(
inputChannel: Channel<UByteArray?>,
inputChannel: Channel<UByteArray>,
outputChannel: Channel<UByteArray>,
private val onClose: suspend ()->Unit = {}): Transport.Device {
override val input: ReceiveChannel<UByteArray?> = inputChannel
override val input: ReceiveChannel<UByteArray> = inputChannel
override val output: SendChannel<UByteArray> = outputChannel
override suspend fun close() {
onClose()
+2 -3
View File
@@ -6,7 +6,6 @@ import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.test.runTest
import net.sergeych.crypto.Key
import net.sergeych.crypto.initCrypto
import net.sergeych.kiloparsec.command
import net.sergeych.kiloparsec.*
import net.sergeych.mp_logger.Log
import kotlin.test.*
@@ -17,7 +16,7 @@ fun createTestDevice(): Pair<Transport.Device, Transport.Device> {
val p2 = Channel<UByteArray>(256)
val id = ++dcnt
val d1 = object : Transport.Device {
override val input: ReceiveChannel<UByteArray?> = p1
override val input: ReceiveChannel<UByteArray> = p1
override val output: SendChannel<UByteArray> = p2
override suspend fun close() {
p2.close()
@@ -28,7 +27,7 @@ fun createTestDevice(): Pair<Transport.Device, Transport.Device> {
}
}
val d2 = object : Transport.Device {
override val input: ReceiveChannel<UByteArray?> = p2
override val input: ReceiveChannel<UByteArray> = p2
override val output: SendChannel<UByteArray> = p1
override suspend fun close() {
p1.close()
@@ -98,7 +98,7 @@ suspend fun asyncSocketToDevice(socket: AsynchronousSocketChannel): InetTranspor
}
}
// transport device copes with blocks:
val inputBlocks = Channel<UByteArray?>()
val inputBlocks = Channel<UByteArray>()
// decode blocks from a byte channel read from the socket:
launch {
try {
@@ -116,9 +116,7 @@ suspend fun asyncSocketToDevice(socket: AsynchronousSocketChannel): InetTranspor
}
}
} catch (_: CancellationException) {
inputBlocks.send(null)
} catch (_: ClosedReceiveChannelException) {
inputBlocks.send(null)
stop()
}
receiving.value = false