-
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 (#191)
* Run tests for new transport * Don't depend on kotlinx.nodejs * Support publishing plain JS module
- Loading branch information
olme04
authored
Jan 12, 2022
1 parent
ebdb0d9
commit 7a9ab2e
Showing
15 changed files
with
278 additions
and
174 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
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) | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
...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,25 @@ | ||
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 io.rsocket.kotlin.transport.nodejs.tcp.internal.* | ||
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 = connect(port, hostname) | ||
return TcpConnection(coroutineContext, pool, socket) | ||
} | ||
} |
Oops, something went wrong.