Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private class CredentialsPluginState(
val coroutineContext: CoroutineContext,
)

@OptIn(UnsafeNumber::class)
private fun getMetadataCallback(
state: COpaquePointer?,
context: CValue<grpc_auth_metadata_context>,
Expand Down Expand Up @@ -121,6 +122,7 @@ private fun destroyCallback(state: COpaquePointer?) {
state?.asStableRef<CredentialsPluginState>()?.dispose()
}

@OptIn(UnsafeNumber::class)
internal fun GrpcCallCredentials.createRaw(
coroutineContext: CoroutineContext,
): CPointer<grpc_call_credentials>? = memScoped {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kotlinx.cinterop.ByteVar
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.CPointerVar
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.UnsafeNumber
import kotlinx.cinterop.alloc
import kotlinx.cinterop.allocArray
import kotlinx.cinterop.convert
Expand Down Expand Up @@ -326,6 +327,7 @@ internal class NativeClientCall<Request, Response>(
}
}

@OptIn(UnsafeNumber::class)
private fun sendAndReceiveInitialMetadata(headers: GrpcMetadata) {
// sending and receiving initial metadata
val arena = Arena()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import kotlinx.atomicfu.atomic
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.MemScope
import kotlinx.cinterop.UnsafeNumber
import kotlinx.cinterop.alloc
import kotlinx.cinterop.allocArray
import kotlinx.cinterop.convert
Expand Down Expand Up @@ -215,6 +216,7 @@ internal sealed class GrpcArg(val key: String) {
}
}

@OptIn(UnsafeNumber::class)
private fun List<GrpcArg>.toRaw(memScope: MemScope): grpc_channel_args {
with(memScope) {
val arr = allocArray<grpc_arg>(size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package kotlinx.rpc.grpc

import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.NativePlacement
import kotlinx.cinterop.UnsafeNumber
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.alloc
import kotlinx.cinterop.allocArray
Expand Down Expand Up @@ -62,6 +63,7 @@ public actual class GrpcMetadataKey<T> actual constructor(name: String, public v
public actual class GrpcMetadata actual constructor() {
internal val map: LinkedHashMap<String, MutableList<ByteArray>> = linkedMapOf()

@OptIn(UnsafeNumber::class)
public constructor(raw: grpc_metadata_array) : this() {
for (i in 0 until raw.count.toInt()) {
val metadata = raw.metadata?.get(i)
Expand All @@ -73,6 +75,7 @@ public actual class GrpcMetadata actual constructor() {
}
}

@OptIn(UnsafeNumber::class)
@InternalRpcApi
public fun NativePlacement.allocRawGrpcMetadata(): grpc_metadata_array {
val raw = alloc<grpc_metadata_array>()
Expand All @@ -91,7 +94,7 @@ public actual class GrpcMetadata actual constructor() {
for (entry in values) {
val size = entry.size.toULong()
val valSlice = entry.usePinned { pinned ->
grpc_slice_from_copied_buffer(pinned.addressOf(0), size)
grpc_slice_from_copied_buffer(pinned.addressOf(0), size.convert())
}
// we create a fresh reference for each entry
val keySliceRef = grpc_slice_ref(keySlice)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.cinterop.CFunction
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.StableRef
import kotlinx.cinterop.UnsafeNumber
import kotlinx.cinterop.alloc
import kotlinx.cinterop.asStableRef
import kotlinx.cinterop.convert
Expand Down Expand Up @@ -126,6 +127,7 @@ public class CompletionQueue {
* Submits a batch operation to the queue.
* See [BatchResult] for possible outcomes.
*/
@OptIn(UnsafeNumber::class)
public fun runBatch(call: CPointer<grpc_call>, ops: CPointer<grpc_op>, nOps: ULong): BatchResult {
if (_shutdownDone.isCompleted) return BatchResult.CQShutdown

Expand All @@ -149,7 +151,7 @@ public class CompletionQueue {
return BatchResult.CQShutdown
}

err = grpc_call_start_batch(call, ops, nOps, tag, null)
err = grpc_call_start_batch(call, ops, nOps.convert(), tag, null)
}

if (err != grpc_call_error.GRPC_CALL_OK) {
Expand Down Expand Up @@ -212,6 +214,7 @@ private fun shutdownCb(functor: CPointer<grpc_completion_queue_functor>?, ok: In
private val OPS_COMPLETE_CB = staticCFunction(::opsCompleteCb)
private val SHUTDOWN_CB = staticCFunction(::shutdownCb)

@OptIn(UnsafeNumber::class)
private fun newCbTag(
userData: Any,
cb: CPointer<CFunction<(CPointer<grpc_completion_queue_functor>?, Int) -> Unit>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kotlinx.cinterop.ByteVar
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.CValue
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.UnsafeNumber
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.alloc
import kotlinx.cinterop.allocArray
Expand Down Expand Up @@ -49,7 +50,7 @@ import platform.posix.memcpy


@InternalRpcApi
@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class)
@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class, UnsafeNumber::class)
public fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Long) {
var consumed = 0L
while (consumed < length) {
Expand All @@ -66,6 +67,7 @@ public fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Long
}
}

@OptIn(UnsafeNumber::class)
@InternalRpcApi
public fun grpc_slice.toByteArray(): ByteArray = memScoped {
val out = ByteArray(len().toInt())
Expand Down Expand Up @@ -98,6 +100,7 @@ public fun CPointer<grpc_byte_buffer>.toKotlin(): Buffer = memScoped {
return out
}

@OptIn(UnsafeNumber::class)
@InternalRpcApi
public fun Source.toGrpcByteBuffer(): CPointer<grpc_byte_buffer> {
if (this is Buffer) return toGrpcByteBuffer()
Expand All @@ -109,29 +112,30 @@ public fun Source.toGrpcByteBuffer(): CPointer<grpc_byte_buffer> {
val n = readAtMostTo(tmp, 0, tmp.size)
if (n <= 0) break
tmp.usePinned {
slices += grpc_slice_from_copied_buffer(it.addressOf(0), n.toULong())
slices += grpc_slice_from_copied_buffer(it.addressOf(0), n.convert())
}
}

return slices.toGrpcByteBuffer()
}

@InternalRpcApi
@OptIn(UnsafeIoApi::class)
@OptIn(UnsafeIoApi::class, UnsafeNumber::class)
public fun Buffer.toGrpcByteBuffer(): CPointer<grpc_byte_buffer> {
val slices = ArrayList<CValue<grpc_slice>>(4)

while (size > 0L) {
UnsafeBufferOperations.readFromHead(this) { arr, start, end ->
val len = end - start
arr.usePinned { p -> slices += grpc_slice_from_copied_buffer(p.addressOf(start), len.toULong()) }
arr.usePinned { p -> slices += grpc_slice_from_copied_buffer(p.addressOf(start), len.convert()) }
len
}
}

return slices.toGrpcByteBuffer()
}

@OptIn(UnsafeNumber::class)
private fun ArrayList<CValue<grpc_slice>>.toGrpcByteBuffer(): CPointer<grpc_byte_buffer> = memScoped {
val count = if (isEmpty()) 1 else size
val sliceArr = allocArray<grpc_slice>(count)
Expand All @@ -150,7 +154,7 @@ private fun ArrayList<CValue<grpc_slice>>.toGrpcByteBuffer(): CPointer<grpc_byte
}


val buf = grpc_raw_byte_buffer_create(sliceArr, count.toULong())!!
val buf = grpc_raw_byte_buffer_create(sliceArr, count.convert())!!
// unref each slice, as the buffer takes ownership
[email protected] { grpc_slice_unref(it) }

Expand All @@ -166,10 +170,11 @@ public fun grpc_slice.startPtr(): CPointer<ByteVar> {
}
}

@OptIn(UnsafeNumber::class)
@InternalRpcApi
public fun grpc_slice.len(): ULong {
return if (this.refcount != null) {
this.data.refcounted.length
this.data.refcounted.length.convert()
} else {
this.data.inlined.length.convert()
}
Expand Down Expand Up @@ -231,6 +236,7 @@ public fun grpc_metadata.destroy() {
grpc_slice_unref(value.readValue())
}

@OptIn(UnsafeNumber::class)
@InternalRpcApi
public fun grpc_metadata_array.destroyEntries() {
for (i in 0 until count.convert()) {
Expand Down
14 changes: 0 additions & 14 deletions grpc/grpc-ktor-server/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,4 @@
kotlinx.rpc.exclude.wasmWasi=true
kotlinx.rpc.exclude.js=true
kotlinx.rpc.exclude.wasmJs=true
kotlinx.rpc.exclude.iosArm64=true
kotlinx.rpc.exclude.iosX64=true
kotlinx.rpc.exclude.iosSimulatorArm64=true
kotlinx.rpc.exclude.linuxArm64=true
kotlinx.rpc.exclude.linuxX64=true
kotlinx.rpc.exclude.macosX64=true
kotlinx.rpc.exclude.mingwX64=true
kotlinx.rpc.exclude.tvosArm64=true
kotlinx.rpc.exclude.tvosSimulatorArm64=true
kotlinx.rpc.exclude.tvosX64=true
kotlinx.rpc.exclude.watchosArm32=true
kotlinx.rpc.exclude.watchosArm64=true
kotlinx.rpc.exclude.watchosDeviceArm64=true
kotlinx.rpc.exclude.watchosSimulatorArm64=true
kotlinx.rpc.exclude.watchosX64=true
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ internal class WireEncoderNative(private val sink: Sink) : WireEncoder {
*/
private inner class Ctx {
fun write(buf: CPointer<ByteVar>, size: Int): Boolean {
sink.writeFully(buf, 0L, size.toLong())
sink.writeFully(buf, 0L, size)
return true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import kotlinx.io.unsafe.UnsafeBufferOperations
import platform.posix.memcpy


@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class)
internal fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Long) {
@OptIn(ExperimentalForeignApi::class, InternalIoApi::class, UnsafeIoApi::class, UnsafeNumber::class)
internal fun Sink.writeFully(buffer: CPointer<ByteVar>, offset: Long, length: Int) {
var consumed = 0L
while (consumed < length) {
UnsafeBufferOperations.writeToTail(this.buffer, 1) { array, start, endExclusive ->
Expand Down
Loading