-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
working nodejs TCP server and client
* not published yet, because of transitive dependency on kotlinx.nodejs, which is only on jcenter
- Loading branch information
olme04
committed
Dec 13, 2021
1 parent
1115e7f
commit 7e580b9
Showing
12 changed files
with
232 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 0 additions & 160 deletions
160
examples/nodejs-tcp-transport/src/jsMain/kotlin/Server.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...-tcp/src/jsMain/kotlin/io/rsocket/kotlin/transport/nodejs/tcp/FrameWithLengthAssembler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package io.rsocket.kotlin.transport.nodejs.tcp | ||
|
||
import io.ktor.utils.io.core.* | ||
import io.rsocket.kotlin.frame.io.* | ||
|
||
internal fun ByteReadPacket.withLength(): ByteReadPacket = buildPacket { | ||
@Suppress("INVISIBLE_MEMBER") writeLength(this@withLength.remaining.toInt()) | ||
writePacket(this@withLength) | ||
} | ||
|
||
internal class FrameWithLengthAssembler(private val onFrame: (frame: ByteReadPacket) -> Unit) { | ||
private var expectedFrameLength = 0 //TODO atomic for native | ||
private val packetBuilder: BytePacketBuilder = BytePacketBuilder() | ||
inline fun write(write: BytePacketBuilder.() -> Unit) { | ||
packetBuilder.write() | ||
loop() | ||
} | ||
|
||
private fun loop() { | ||
while (true) when { | ||
expectedFrameLength == 0 && packetBuilder.size < 3 -> return // no length | ||
expectedFrameLength == 0 -> withTemp { // has length | ||
expectedFrameLength = @Suppress("INVISIBLE_MEMBER") it.readLength() | ||
if (it.remaining >= expectedFrameLength) build(it) // if has length and frame | ||
} | ||
packetBuilder.size < expectedFrameLength -> return // not enough bytes to read frame | ||
else -> withTemp { build(it) } // enough bytes to read frame | ||
} | ||
} | ||
|
||
private fun build(from: ByteReadPacket) { | ||
val frame = buildPacket { | ||
writePacket(from, expectedFrameLength) | ||
} | ||
expectedFrameLength = 0 | ||
onFrame(frame) | ||
} | ||
|
||
private inline fun withTemp(block: (tempPacket: ByteReadPacket) -> Unit) { | ||
val tempPacket = packetBuilder.build() | ||
block(tempPacket) | ||
packetBuilder.writePacket(tempPacket) | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...nodejs-tcp/src/jsMain/kotlin/io/rsocket/kotlin/transport/nodejs/tcp/TcpClientTransport.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.rsocket.kotlin.transport.nodejs.tcp | ||
|
||
import io.ktor.utils.io.core.internal.* | ||
import io.ktor.utils.io.pool.* | ||
import io.rsocket.kotlin.* | ||
import io.rsocket.kotlin.transport.* | ||
import kotlinx.coroutines.* | ||
import kotlin.coroutines.* | ||
|
||
public class TcpClientTransport( | ||
private val port: Int, | ||
private val hostname: String, | ||
private val pool: ObjectPool<ChunkBuffer> = ChunkBuffer.Pool, | ||
coroutineContext: CoroutineContext = EmptyCoroutineContext | ||
) : ClientTransport { | ||
|
||
override val coroutineContext: CoroutineContext = coroutineContext + SupervisorJob(coroutineContext[Job]) | ||
|
||
@TransportApi | ||
override suspend fun connect(): Connection { | ||
val socket = net.connect(port, hostname) | ||
return TcpConnection(coroutineContext, pool, socket) | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...port-nodejs-tcp/src/jsMain/kotlin/io/rsocket/kotlin/transport/nodejs/tcp/TcpConnection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package io.rsocket.kotlin.transport.nodejs.tcp | ||
|
||
import Buffer | ||
import io.ktor.utils.io.core.* | ||
import io.ktor.utils.io.core.internal.* | ||
import io.ktor.utils.io.js.* | ||
import io.ktor.utils.io.pool.* | ||
import io.rsocket.kotlin.* | ||
import io.rsocket.kotlin.internal.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.channels.* | ||
import net.* | ||
import org.khronos.webgl.* | ||
import kotlin.coroutines.* | ||
|
||
@TransportApi | ||
internal class TcpConnection( | ||
override val coroutineContext: CoroutineContext, | ||
override val pool: ObjectPool<ChunkBuffer>, | ||
private val socket: Socket | ||
) : Connection { | ||
|
||
private val sendChannel = @Suppress("INVISIBLE_MEMBER") SafeChannel<ByteReadPacket>(8) | ||
private val receiveChannel = @Suppress("INVISIBLE_MEMBER") SafeChannel<ByteReadPacket>(Channel.UNLIMITED) | ||
|
||
init { | ||
launch { | ||
sendChannel.consumeEach { packet -> | ||
socket.write(Uint8Array(packet.withLength().readArrayBuffer())) | ||
} | ||
} | ||
|
||
coroutineContext.job.invokeOnCompletion { | ||
when (it) { | ||
null -> socket.destroy() | ||
else -> socket.destroy(Error(it.message, it.cause)) | ||
} | ||
} | ||
|
||
val frameAssembler = FrameWithLengthAssembler { receiveChannel.trySend(it) } //TODO | ||
socket.on("data") { buffer: Buffer -> | ||
frameAssembler.write { writeFully(buffer.buffer) } | ||
} | ||
socket.on("error") { error: Error -> | ||
coroutineContext.job.cancel("Socket error", error) | ||
} | ||
socket.on("close") { hadError: Boolean -> | ||
if (!hadError) coroutineContext.job.cancel("Socket closed") | ||
} | ||
} | ||
|
||
override suspend fun send(packet: ByteReadPacket) { | ||
sendChannel.send(packet) | ||
} | ||
|
||
override suspend fun receive(): ByteReadPacket { | ||
return receiveChannel.receive() | ||
} | ||
} |
Oops, something went wrong.