Commit 264a6e1f authored by 李朝发's avatar 李朝发

update

parent 028094f0
...@@ -26,8 +26,6 @@ import java.nio.ByteBuffer ...@@ -26,8 +26,6 @@ import java.nio.ByteBuffer
import java.nio.ByteOrder import java.nio.ByteOrder
import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong import java.util.concurrent.atomic.AtomicLong
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
// This is a helper for safely working with byte buffers returned from the Rust code. // This is a helper for safely working with byte buffers returned from the Rust code.
// A rust-owned buffer is represented by its capacity, its current length, and a // A rust-owned buffer is represented by its capacity, its current length, and a
...@@ -44,7 +42,7 @@ open class RustBuffer : Structure() { ...@@ -44,7 +42,7 @@ open class RustBuffer : Structure() {
companion object { companion object {
internal fun alloc(size: Int = 0) = rustCall() { status -> internal fun alloc(size: Int = 0) = rustCall() { status ->
_UniFFILib.INSTANCE.ffi_NativeSocket_2a33_rustbuffer_alloc(size, status).also { _UniFFILib.INSTANCE.ffi_NativeSocket_2c48_rustbuffer_alloc(size, status).also {
if(it.data == null) { if(it.data == null) {
throw RuntimeException("RustBuffer.alloc() returned null data pointer (size=${size})") throw RuntimeException("RustBuffer.alloc() returned null data pointer (size=${size})")
} }
...@@ -52,7 +50,7 @@ open class RustBuffer : Structure() { ...@@ -52,7 +50,7 @@ open class RustBuffer : Structure() {
} }
internal fun free(buf: RustBuffer.ByValue) = rustCall() { status -> internal fun free(buf: RustBuffer.ByValue) = rustCall() { status ->
_UniFFILib.INSTANCE.ffi_NativeSocket_2a33_rustbuffer_free(buf, status) _UniFFILib.INSTANCE.ffi_NativeSocket_2c48_rustbuffer_free(buf, status)
} }
} }
...@@ -257,99 +255,95 @@ internal interface _UniFFILib : Library { ...@@ -257,99 +255,95 @@ internal interface _UniFFILib : Library {
companion object { companion object {
internal val INSTANCE: _UniFFILib by lazy { internal val INSTANCE: _UniFFILib by lazy {
loadIndirect<_UniFFILib>(componentName = "NativeSocket") loadIndirect<_UniFFILib>(componentName = "NativeSocket")
.also { lib: _UniFFILib ->
FfiConverterTypeLeastOnceListener.register(lib)
FfiConverterTypeMostOnceListener.register(lib)
}
} }
} }
fun ffi_NativeSocket_2a33_NativeSocketBuilder_object_free(`ptr`: Pointer, fun ffi_NativeSocket_2c48_NativeSocketBuilder_object_free(`ptr`: Pointer,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Unit ): Unit
fun NativeSocket_2a33_NativeSocketBuilder_new( fun NativeSocket_2c48_NativeSocketBuilder_new(
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_host(`ptr`: Pointer,`host`: RustBuffer.ByValue, fun NativeSocket_2c48_NativeSocketBuilder_host(`ptr`: Pointer,`host`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_token(`ptr`: Pointer,`token`: RustBuffer.ByValue, fun NativeSocket_2c48_NativeSocketBuilder_token(`ptr`: Pointer,`token`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_brand(`ptr`: Pointer,`brand`: Int, fun NativeSocket_2c48_NativeSocketBuilder_brand(`ptr`: Pointer,`brand`: Int,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_store(`ptr`: Pointer,`store`: Int, fun NativeSocket_2c48_NativeSocketBuilder_store(`ptr`: Pointer,`store`: Int,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_device(`ptr`: Pointer,`device`: RustBuffer.ByValue, fun NativeSocket_2c48_NativeSocketBuilder_device(`ptr`: Pointer,`device`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_type(`ptr`: Pointer,`type`: Int, fun NativeSocket_2c48_NativeSocketBuilder_ty(`ptr`: Pointer,`type`: Int,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_topic(`ptr`: Pointer,`topic`: RustBuffer.ByValue, fun NativeSocket_2c48_NativeSocketBuilder_clean_start(`ptr`: Pointer,`cleanStart`: Byte,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_clean_start(`ptr`: Pointer,`cleanStart`: Byte, fun NativeSocket_2c48_NativeSocketBuilder_cache_dir(`ptr`: Pointer,`cacheDir`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_cache_dir(`ptr`: Pointer,`cacheDir`: RustBuffer.ByValue, fun NativeSocket_2c48_NativeSocketBuilder_log_level(`ptr`: Pointer,`logLevel`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_log_level(`ptr`: Pointer,`logLevel`: RustBuffer.ByValue, fun NativeSocket_2c48_NativeSocketBuilder_version(`ptr`: Pointer,`version`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Pointer ): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_sentry(`ptr`: Pointer,`sentry`: Byte, fun NativeSocket_2c48_NativeSocketBuilder_build(`ptr`: Pointer,
_uniffi_out_err: RustCallStatus
): Pointer
fun NativeSocket_2a33_NativeSocketBuilder_build(`ptr`: Pointer,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): RustBuffer.ByValue ): RustBuffer.ByValue
fun ffi_NativeSocket_2a33_NativeSocket_object_free(`ptr`: Pointer, fun ffi_NativeSocket_2c48_NativeSocket_object_free(`ptr`: Pointer,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Unit ): Unit
fun NativeSocket_2a33_NativeSocket_start(`ptr`: Pointer,`mostOnce`: RustBuffer.ByValue,`leastOnce`: RustBuffer.ByValue, fun NativeSocket_2c48_NativeSocket_poll(`ptr`: Pointer,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Unit ): RustBuffer.ByValue
fun ffi_NativeSocket_2a33_MostOnceListener_init_callback(`callbackStub`: ForeignCallback, fun NativeSocket_2c48_NativeSocket_try_poll(`ptr`: Pointer,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Unit ): RustBuffer.ByValue
fun ffi_NativeSocket_2a33_LeastOnceListener_init_callback(`callbackStub`: ForeignCallback, fun NativeSocket_2c48_NativeSocket_poll_timeout(`ptr`: Pointer,`timeout`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Unit ): RustBuffer.ByValue
fun ffi_NativeSocket_2a33_rustbuffer_alloc(`size`: Int, fun NativeSocket_2c48_NativeSocket_invoke(`ptr`: Pointer,`input`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): RustBuffer.ByValue ): RustBuffer.ByValue
fun ffi_NativeSocket_2a33_rustbuffer_from_bytes(`bytes`: ForeignBytes.ByValue, fun ffi_NativeSocket_2c48_rustbuffer_alloc(`size`: Int,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): RustBuffer.ByValue ): RustBuffer.ByValue
fun ffi_NativeSocket_2a33_rustbuffer_free(`buf`: RustBuffer.ByValue, fun ffi_NativeSocket_2c48_rustbuffer_from_bytes(`bytes`: ForeignBytes.ByValue,
_uniffi_out_err: RustCallStatus
): RustBuffer.ByValue
fun ffi_NativeSocket_2c48_rustbuffer_free(`buf`: RustBuffer.ByValue,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): Unit ): Unit
fun ffi_NativeSocket_2a33_rustbuffer_reserve(`buf`: RustBuffer.ByValue,`additional`: Int, fun ffi_NativeSocket_2c48_rustbuffer_reserve(`buf`: RustBuffer.ByValue,`additional`: Int,
_uniffi_out_err: RustCallStatus _uniffi_out_err: RustCallStatus
): RustBuffer.ByValue ): RustBuffer.ByValue
...@@ -486,6 +480,44 @@ public object FfiConverterString: FfiConverter<String, RustBuffer.ByValue> { ...@@ -486,6 +480,44 @@ public object FfiConverterString: FfiConverter<String, RustBuffer.ByValue> {
} }
public object FfiConverterDuration: FfiConverterRustBuffer<java.time.Duration> {
override fun read(buf: ByteBuffer): java.time.Duration {
// Type mismatch (should be u64) but we check for overflow/underflow below
val seconds = buf.getLong()
// Type mismatch (should be u32) but we check for overflow/underflow below
val nanoseconds = buf.getInt().toLong()
if (seconds < 0) {
throw java.time.DateTimeException("Duration exceeds minimum or maximum value supported by uniffi")
}
if (nanoseconds < 0) {
throw java.time.DateTimeException("Duration nanoseconds exceed minimum or maximum supported by uniffi")
}
return java.time.Duration.ofSeconds(seconds, nanoseconds)
}
// 8 bytes for seconds, 4 bytes for nanoseconds
override fun allocationSize(value: java.time.Duration) = 12
override fun write(value: java.time.Duration, buf: ByteBuffer) {
if (value.seconds < 0) {
// Rust does not support negative Durations
throw IllegalArgumentException("Invalid duration, must be non-negative")
}
if (value.nano < 0) {
// Java docs provide guarantee that nano will always be positive, so this should be impossible
// See: https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html
throw IllegalArgumentException("Invalid duration, nano value must be non-negative")
}
// Type mismatch (should be u64) but since Rust doesn't support negative durations we should be OK
buf.putLong(value.seconds)
// Type mismatch (should be u32) but since values will always be between 0 and 999,999,999 it should be OK
buf.putInt(value.nano)
}
}
// Interface implemented by anything that can contain an object reference. // Interface implemented by anything that can contain an object reference.
// //
// Such types expose a `destroy()` method that must be called to cleanly // Such types expose a `destroy()` method that must be called to cleanly
...@@ -650,7 +682,13 @@ abstract class FFIObject( ...@@ -650,7 +682,13 @@ abstract class FFIObject(
public interface NativeSocketInterface { public interface NativeSocketInterface {
fun `start`(`mostOnce`: MostOnceListener?, `leastOnce`: LeastOnceListener?) fun `poll`(): Message?
fun `tryPoll`(): Message?
fun `pollTimeout`(`timeout`: java.time.Duration): Message?
fun `invoke`(`input`: InvokeInput): InvokeOutput
} }
...@@ -668,17 +706,42 @@ class NativeSocket( ...@@ -668,17 +706,42 @@ class NativeSocket(
*/ */
override protected fun freeRustArcPtr() { override protected fun freeRustArcPtr() {
rustCall() { status -> rustCall() { status ->
_UniFFILib.INSTANCE.ffi_NativeSocket_2a33_NativeSocket_object_free(this.pointer, status) _UniFFILib.INSTANCE.ffi_NativeSocket_2c48_NativeSocket_object_free(this.pointer, status)
} }
} }
override fun `start`(`mostOnce`: MostOnceListener?, `leastOnce`: LeastOnceListener?) = override fun `poll`(): Message? =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocket_start(it, FfiConverterOptionalTypeMostOnceListener.lower(`mostOnce`), FfiConverterOptionalTypeLeastOnceListener.lower(`leastOnce`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocket_poll(it, _status)
} }
}.let {
FfiConverterOptionalTypeMessage.lift(it)
}
override fun `tryPoll`(): Message? =
callWithPointer {
rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocket_try_poll(it, _status)
}
}.let {
FfiConverterOptionalTypeMessage.lift(it)
}
override fun `pollTimeout`(`timeout`: java.time.Duration): Message? =
callWithPointer {
rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocket_poll_timeout(it, FfiConverterDuration.lower(`timeout`), _status)
}
}.let {
FfiConverterOptionalTypeMessage.lift(it)
}
override fun `invoke`(`input`: InvokeInput): InvokeOutput =
callWithPointer {
rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocket_invoke(it, FfiConverterTypeInvokeInput.lower(`input`), _status)
}
}.let {
FfiConverterTypeInvokeOutput.lift(it)
} }
...@@ -721,9 +784,7 @@ public interface NativeSocketBuilderInterface { ...@@ -721,9 +784,7 @@ public interface NativeSocketBuilderInterface {
fun `device`(`device`: String): NativeSocketBuilder fun `device`(`device`: String): NativeSocketBuilder
fun `type`(`type`: Int): NativeSocketBuilder fun `ty`(`type`: Int): NativeSocketBuilder
fun `topic`(`topic`: List<String>): NativeSocketBuilder
fun `cleanStart`(`cleanStart`: Boolean): NativeSocketBuilder fun `cleanStart`(`cleanStart`: Boolean): NativeSocketBuilder
...@@ -731,7 +792,7 @@ public interface NativeSocketBuilderInterface { ...@@ -731,7 +792,7 @@ public interface NativeSocketBuilderInterface {
fun `logLevel`(`logLevel`: LogLevel): NativeSocketBuilder fun `logLevel`(`logLevel`: LogLevel): NativeSocketBuilder
fun `sentry`(`sentry`: Boolean): NativeSocketBuilder fun `version`(`version`: String): NativeSocketBuilder
fun `build`(): NativeSocket? fun `build`(): NativeSocket?
...@@ -743,7 +804,7 @@ class NativeSocketBuilder( ...@@ -743,7 +804,7 @@ class NativeSocketBuilder(
constructor() : constructor() :
this( this(
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_new( _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_new( _status)
}) })
/** /**
...@@ -756,14 +817,14 @@ class NativeSocketBuilder( ...@@ -756,14 +817,14 @@ class NativeSocketBuilder(
*/ */
override protected fun freeRustArcPtr() { override protected fun freeRustArcPtr() {
rustCall() { status -> rustCall() { status ->
_UniFFILib.INSTANCE.ffi_NativeSocket_2a33_NativeSocketBuilder_object_free(this.pointer, status) _UniFFILib.INSTANCE.ffi_NativeSocket_2c48_NativeSocketBuilder_object_free(this.pointer, status)
} }
} }
override fun `host`(`host`: String): NativeSocketBuilder = override fun `host`(`host`: String): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_host(it, FfiConverterString.lower(`host`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_host(it, FfiConverterString.lower(`host`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
...@@ -771,7 +832,7 @@ class NativeSocketBuilder( ...@@ -771,7 +832,7 @@ class NativeSocketBuilder(
override fun `token`(`token`: String): NativeSocketBuilder = override fun `token`(`token`: String): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_token(it, FfiConverterString.lower(`token`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_token(it, FfiConverterString.lower(`token`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
...@@ -779,7 +840,7 @@ class NativeSocketBuilder( ...@@ -779,7 +840,7 @@ class NativeSocketBuilder(
override fun `brand`(`brand`: Int): NativeSocketBuilder = override fun `brand`(`brand`: Int): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_brand(it, FfiConverterInt.lower(`brand`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_brand(it, FfiConverterInt.lower(`brand`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
...@@ -787,7 +848,7 @@ class NativeSocketBuilder( ...@@ -787,7 +848,7 @@ class NativeSocketBuilder(
override fun `store`(`store`: Int): NativeSocketBuilder = override fun `store`(`store`: Int): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_store(it, FfiConverterInt.lower(`store`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_store(it, FfiConverterInt.lower(`store`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
...@@ -795,23 +856,15 @@ class NativeSocketBuilder( ...@@ -795,23 +856,15 @@ class NativeSocketBuilder(
override fun `device`(`device`: String): NativeSocketBuilder = override fun `device`(`device`: String): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_device(it, FfiConverterString.lower(`device`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_device(it, FfiConverterString.lower(`device`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
} }
override fun `type`(`type`: Int): NativeSocketBuilder = override fun `ty`(`type`: Int): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_type(it, FfiConverterInt.lower(`type`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_ty(it, FfiConverterInt.lower(`type`), _status)
}
}.let {
FfiConverterTypeNativeSocketBuilder.lift(it)
}
override fun `topic`(`topic`: List<String>): NativeSocketBuilder =
callWithPointer {
rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_topic(it, FfiConverterSequenceString.lower(`topic`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
...@@ -819,7 +872,7 @@ class NativeSocketBuilder( ...@@ -819,7 +872,7 @@ class NativeSocketBuilder(
override fun `cleanStart`(`cleanStart`: Boolean): NativeSocketBuilder = override fun `cleanStart`(`cleanStart`: Boolean): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_clean_start(it, FfiConverterBoolean.lower(`cleanStart`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_clean_start(it, FfiConverterBoolean.lower(`cleanStart`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
...@@ -827,7 +880,7 @@ class NativeSocketBuilder( ...@@ -827,7 +880,7 @@ class NativeSocketBuilder(
override fun `cacheDir`(`cacheDir`: String): NativeSocketBuilder = override fun `cacheDir`(`cacheDir`: String): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_cache_dir(it, FfiConverterString.lower(`cacheDir`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_cache_dir(it, FfiConverterString.lower(`cacheDir`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
...@@ -835,15 +888,15 @@ class NativeSocketBuilder( ...@@ -835,15 +888,15 @@ class NativeSocketBuilder(
override fun `logLevel`(`logLevel`: LogLevel): NativeSocketBuilder = override fun `logLevel`(`logLevel`: LogLevel): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_log_level(it, FfiConverterTypeLogLevel.lower(`logLevel`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_log_level(it, FfiConverterTypeLogLevel.lower(`logLevel`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
} }
override fun `sentry`(`sentry`: Boolean): NativeSocketBuilder = override fun `version`(`version`: String): NativeSocketBuilder =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_sentry(it, FfiConverterBoolean.lower(`sentry`), _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_version(it, FfiConverterString.lower(`version`), _status)
} }
}.let { }.let {
FfiConverterTypeNativeSocketBuilder.lift(it) FfiConverterTypeNativeSocketBuilder.lift(it)
...@@ -851,7 +904,7 @@ class NativeSocketBuilder( ...@@ -851,7 +904,7 @@ class NativeSocketBuilder(
override fun `build`(): NativeSocket? = override fun `build`(): NativeSocket? =
callWithPointer { callWithPointer {
rustCall() { _status -> rustCall() { _status ->
_UniFFILib.INSTANCE.NativeSocket_2a33_NativeSocketBuilder_build(it, _status) _UniFFILib.INSTANCE.NativeSocket_2c48_NativeSocketBuilder_build(it, _status)
} }
}.let { }.let {
FfiConverterOptionalTypeNativeSocket.lift(it) FfiConverterOptionalTypeNativeSocket.lift(it)
...@@ -889,6 +942,7 @@ public object FfiConverterTypeNativeSocketBuilder: FfiConverter<NativeSocketBuil ...@@ -889,6 +942,7 @@ public object FfiConverterTypeNativeSocketBuilder: FfiConverter<NativeSocketBuil
data class Message ( data class Message (
var `id`: Long, var `id`: Long,
var `ty`: Int, var `ty`: Int,
var `qos`: Qos,
var `data`: List<UByte>? var `data`: List<UByte>?
) { ) {
...@@ -899,6 +953,7 @@ public object FfiConverterTypeMessage: FfiConverterRustBuffer<Message> { ...@@ -899,6 +953,7 @@ public object FfiConverterTypeMessage: FfiConverterRustBuffer<Message> {
return Message( return Message(
FfiConverterLong.read(buf), FfiConverterLong.read(buf),
FfiConverterInt.read(buf), FfiConverterInt.read(buf),
FfiConverterTypeQos.read(buf),
FfiConverterOptionalSequenceUByte.read(buf), FfiConverterOptionalSequenceUByte.read(buf),
) )
} }
...@@ -906,12 +961,14 @@ public object FfiConverterTypeMessage: FfiConverterRustBuffer<Message> { ...@@ -906,12 +961,14 @@ public object FfiConverterTypeMessage: FfiConverterRustBuffer<Message> {
override fun allocationSize(value: Message) = ( override fun allocationSize(value: Message) = (
FfiConverterLong.allocationSize(value.`id`) + FfiConverterLong.allocationSize(value.`id`) +
FfiConverterInt.allocationSize(value.`ty`) + FfiConverterInt.allocationSize(value.`ty`) +
FfiConverterTypeQos.allocationSize(value.`qos`) +
FfiConverterOptionalSequenceUByte.allocationSize(value.`data`) FfiConverterOptionalSequenceUByte.allocationSize(value.`data`)
) )
override fun write(value: Message, buf: ByteBuffer) { override fun write(value: Message, buf: ByteBuffer) {
FfiConverterLong.write(value.`id`, buf) FfiConverterLong.write(value.`id`, buf)
FfiConverterInt.write(value.`ty`, buf) FfiConverterInt.write(value.`ty`, buf)
FfiConverterTypeQos.write(value.`qos`, buf)
FfiConverterOptionalSequenceUByte.write(value.`data`, buf) FfiConverterOptionalSequenceUByte.write(value.`data`, buf)
} }
} }
...@@ -919,229 +976,186 @@ public object FfiConverterTypeMessage: FfiConverterRustBuffer<Message> { ...@@ -919,229 +976,186 @@ public object FfiConverterTypeMessage: FfiConverterRustBuffer<Message> {
enum class LogLevel { sealed class InvokeInput {
ERROR,WARN,INFO,DEBUG,TRACE; object Stop : InvokeInput()
}
data class Ack(
public object FfiConverterTypeLogLevel: FfiConverterRustBuffer<LogLevel> { val `messageId`: Long
override fun read(buf: ByteBuffer) = try { ) : InvokeInput()
LogLevel.values()[buf.getInt() - 1] data class Cache(
} catch (e: IndexOutOfBoundsException) { val `messageId`: Long
throw RuntimeException("invalid enum value, something is very wrong!!", e) ) : InvokeInput()
} object Online : InvokeInput()
override fun allocationSize(value: LogLevel) = 4
override fun write(value: LogLevel, buf: ByteBuffer) {
buf.putInt(value.ordinal + 1)
}
}
sealed class ConsumerException(message: String): Exception(message) {
// Each variant is a nested class
// Flat enums carries a string error message, so no special implementation is necessary.
class Exception(message: String) : ConsumerException(message)
companion object ErrorHandler : CallStatusErrorHandler<ConsumerException> {
override fun lift(error_buf: RustBuffer.ByValue): ConsumerException = FfiConverterTypeConsumerError.lift(error_buf)
}
} }
public object FfiConverterTypeConsumerError : FfiConverterRustBuffer<ConsumerException> { public object FfiConverterTypeInvokeInput : FfiConverterRustBuffer<InvokeInput>{
override fun read(buf: ByteBuffer): ConsumerException { override fun read(buf: ByteBuffer): InvokeInput {
return when(buf.getInt()) {
return when(buf.getInt()) { 1 -> InvokeInput.Stop
1 -> ConsumerException.Exception(FfiConverterString.read(buf)) 2 -> InvokeInput.Ack(
else -> throw RuntimeException("invalid error enum value, something is very wrong!!") FfiConverterLong.read(buf),
)
3 -> InvokeInput.Cache(
FfiConverterLong.read(buf),
)
4 -> InvokeInput.Online
else -> throw RuntimeException("invalid enum value, something is very wrong!!")
} }
} }
override fun allocationSize(value: ConsumerException): Int { override fun allocationSize(value: InvokeInput) = when(value) {
return 4 is InvokeInput.Stop -> {
// Add the size for the Int that specifies the variant plus the size needed for all fields
(
4
)
}
is InvokeInput.Ack -> {
// Add the size for the Int that specifies the variant plus the size needed for all fields
(
4
+ FfiConverterLong.allocationSize(value.`messageId`)
)
}
is InvokeInput.Cache -> {
// Add the size for the Int that specifies the variant plus the size needed for all fields
(
4
+ FfiConverterLong.allocationSize(value.`messageId`)
)
}
is InvokeInput.Online -> {
// Add the size for the Int that specifies the variant plus the size needed for all fields
(
4
)
}
} }
override fun write(value: ConsumerException, buf: ByteBuffer) { override fun write(value: InvokeInput, buf: ByteBuffer) {
when(value) { when(value) {
is ConsumerException.Exception -> { is InvokeInput.Stop -> {
buf.putInt(1) buf.putInt(1)
Unit Unit
} }
is InvokeInput.Ack -> {
buf.putInt(2)
FfiConverterLong.write(value.`messageId`, buf)
Unit
}
is InvokeInput.Cache -> {
buf.putInt(3)
FfiConverterLong.write(value.`messageId`, buf)
Unit
}
is InvokeInput.Online -> {
buf.putInt(4)
Unit
}
}.let { /* this makes the `when` an expression, which ensures it is exhaustive */ } }.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
} }
} }
internal typealias Handle = Long
internal class ConcurrentHandleMap<T>(
private val leftMap: MutableMap<Handle, T> = mutableMapOf(),
private val rightMap: MutableMap<T, Handle> = mutableMapOf()
) {
private val lock = java.util.concurrent.locks.ReentrantLock()
private val currentHandle = AtomicLong(0L)
private val stride = 1L
fun insert(obj: T): Handle =
lock.withLock {
rightMap[obj] ?:
currentHandle.getAndAdd(stride)
.also { handle ->
leftMap[handle] = obj
rightMap[obj] = handle
}
}
fun get(handle: Handle) = lock.withLock {
leftMap[handle]
}
fun delete(handle: Handle) {
this.remove(handle)
}
fun remove(handle: Handle): T? = sealed class InvokeOutput {
lock.withLock { object None : InvokeOutput()
leftMap.remove(handle)?.let { obj ->
rightMap.remove(obj) data class Cache(
obj val `message`: Message?
} ) : InvokeOutput()
} data class Online(
} val `status`: Boolean
) : InvokeOutput()
interface ForeignCallback : com.sun.jna.Callback {
public fun invoke(handle: Handle, method: Int, args: RustBuffer.ByValue, outBuf: RustBufferByReference): Int
} }
// Magic number for the Rust proxy to call using the same mechanism as every other method, public object FfiConverterTypeInvokeOutput : FfiConverterRustBuffer<InvokeOutput>{
// to free the callback once it's dropped by Rust. override fun read(buf: ByteBuffer): InvokeOutput {
internal const val IDX_CALLBACK_FREE = 0 return when(buf.getInt()) {
1 -> InvokeOutput.None
public abstract class FfiConverterCallbackInterface<CallbackInterface>( 2 -> InvokeOutput.Cache(
protected val foreignCallback: ForeignCallback FfiConverterOptionalTypeMessage.read(buf),
): FfiConverter<CallbackInterface, Handle> { )
private val handleMap = ConcurrentHandleMap<CallbackInterface>() 3 -> InvokeOutput.Online(
FfiConverterBoolean.read(buf),
// Registers the foreign callback with the Rust side. )
// This method is generated for each callback interface. else -> throw RuntimeException("invalid enum value, something is very wrong!!")
internal abstract fun register(lib: _UniFFILib) }
}
fun drop(handle: Handle): RustBuffer.ByValue { override fun allocationSize(value: InvokeOutput) = when(value) {
return handleMap.remove(handle).let { RustBuffer.ByValue() } is InvokeOutput.None -> {
// Add the size for the Int that specifies the variant plus the size needed for all fields
(
4
)
}
is InvokeOutput.Cache -> {
// Add the size for the Int that specifies the variant plus the size needed for all fields
(
4
+ FfiConverterOptionalTypeMessage.allocationSize(value.`message`)
)
}
is InvokeOutput.Online -> {
// Add the size for the Int that specifies the variant plus the size needed for all fields
(
4
+ FfiConverterBoolean.allocationSize(value.`status`)
)
}
} }
override fun lift(value: Handle): CallbackInterface { override fun write(value: InvokeOutput, buf: ByteBuffer) {
return handleMap.get(value) ?: throw InternalException("No callback in handlemap; this is a Uniffi bug") when(value) {
is InvokeOutput.None -> {
buf.putInt(1)
Unit
}
is InvokeOutput.Cache -> {
buf.putInt(2)
FfiConverterOptionalTypeMessage.write(value.`message`, buf)
Unit
}
is InvokeOutput.Online -> {
buf.putInt(3)
FfiConverterBoolean.write(value.`status`, buf)
Unit
}
}.let { /* this makes the `when` an expression, which ensures it is exhaustive */ }
} }
}
override fun read(buf: ByteBuffer) = lift(buf.getLong())
override fun lower(value: CallbackInterface) =
handleMap.insert(value).also {
assert(handleMap.get(it) === value) { "Handle map is not returning the object we just placed there. This is a bug in the HandleMap." }
}
override fun allocationSize(value: CallbackInterface) = 8
override fun write(value: CallbackInterface, buf: ByteBuffer) {
buf.putLong(lower(value))
}
}
// Declaration and FfiConverters for LeastOnceListener Callback Interface
public interface LeastOnceListener { enum class LogLevel {
fun `consumerMessage`(`message`: Message) ERROR,WARN,INFO,DEBUG,TRACE;
} }
// The ForeignCallback that is passed to Rust. public object FfiConverterTypeLogLevel: FfiConverterRustBuffer<LogLevel> {
internal class ForeignCallbackTypeLeastOnceListener : ForeignCallback { override fun read(buf: ByteBuffer) = try {
@Suppress("TooGenericExceptionCaught") LogLevel.values()[buf.getInt() - 1]
override fun invoke(handle: Handle, method: Int, args: RustBuffer.ByValue, outBuf: RustBufferByReference): Int { } catch (e: IndexOutOfBoundsException) {
val cb = FfiConverterTypeLeastOnceListener.lift(handle) throw RuntimeException("invalid enum value, something is very wrong!!", e)
return when (method) {
IDX_CALLBACK_FREE -> {
FfiConverterTypeLeastOnceListener.drop(handle)
// No return value.
// See docs of ForeignCallback in `uniffi/src/ffi/foreigncallbacks.rs`
0
}
1 -> {
// Call the method, write to outBuf and return a status code
// See docs of ForeignCallback in `uniffi/src/ffi/foreigncallbacks.rs` for info
try {
try {
val buffer = this.`invokeConsumerMessage`(cb, args)
// Success
outBuf.setValue(buffer)
1
} catch (e: ConsumerException) {
// Expected error
val buffer = FfiConverterTypeConsumerError.lowerIntoRustBuffer(e)
outBuf.setValue(buffer)
-2
}
} catch (e: Throwable) {
// Unexpected error
try {
// Try to serialize the error into a string
outBuf.setValue(FfiConverterString.lower(e.toString()))
} catch (e: Throwable) {
// If that fails, then it's time to give up and just return
}
-1
}
}
else -> {
// An unexpected error happened.
// See docs of ForeignCallback in `uniffi/src/ffi/foreigncallbacks.rs`
try {
// Try to serialize the error into a string
outBuf.setValue(FfiConverterString.lower("Invalid Callback index"))
} catch (e: Throwable) {
// If that fails, then it's time to give up and just return
}
-1
}
}
} }
override fun allocationSize(value: LogLevel) = 4
private fun `invokeConsumerMessage`(kotlinCallbackInterface: LeastOnceListener, args: RustBuffer.ByValue): RustBuffer.ByValue =
try {
val buf = args.asByteBuffer() ?: throw InternalException("No ByteBuffer in RustBuffer; this is a Uniffi bug")
kotlinCallbackInterface.`consumerMessage`(
FfiConverterTypeMessage.read(buf)
)
.let { RustBuffer.ByValue() }
// TODO catch errors and report them back to Rust.
// https://github.com/mozilla/uniffi-rs/issues/351
} finally {
RustBuffer.free(args)
}
}
// The ffiConverter which transforms the Callbacks in to Handles to pass to Rust. override fun write(value: LogLevel, buf: ByteBuffer) {
public object FfiConverterTypeLeastOnceListener: FfiConverterCallbackInterface<LeastOnceListener>( buf.putInt(value.ordinal + 1)
foreignCallback = ForeignCallbackTypeLeastOnceListener()
) {
override fun register(lib: _UniFFILib) {
rustCall() { status ->
lib.ffi_NativeSocket_2a33_LeastOnceListener_init_callback(this.foreignCallback, status)
}
} }
} }
...@@ -1150,97 +1164,29 @@ public object FfiConverterTypeLeastOnceListener: FfiConverterCallbackInterface<L ...@@ -1150,97 +1164,29 @@ public object FfiConverterTypeLeastOnceListener: FfiConverterCallbackInterface<L
// Declaration and FfiConverters for MostOnceListener Callback Interface enum class Qos {
AT_MOST_ONCE,AT_LEAST_ONCE;
public interface MostOnceListener {
fun `consumerMessage`(`message`: Message)
} }
// The ForeignCallback that is passed to Rust. public object FfiConverterTypeQos: FfiConverterRustBuffer<Qos> {
internal class ForeignCallbackTypeMostOnceListener : ForeignCallback { override fun read(buf: ByteBuffer) = try {
@Suppress("TooGenericExceptionCaught") Qos.values()[buf.getInt() - 1]
override fun invoke(handle: Handle, method: Int, args: RustBuffer.ByValue, outBuf: RustBufferByReference): Int { } catch (e: IndexOutOfBoundsException) {
val cb = FfiConverterTypeMostOnceListener.lift(handle) throw RuntimeException("invalid enum value, something is very wrong!!", e)
return when (method) {
IDX_CALLBACK_FREE -> {
FfiConverterTypeMostOnceListener.drop(handle)
// No return value.
// See docs of ForeignCallback in `uniffi/src/ffi/foreigncallbacks.rs`
0
}
1 -> {
// Call the method, write to outBuf and return a status code
// See docs of ForeignCallback in `uniffi/src/ffi/foreigncallbacks.rs` for info
try {
try {
val buffer = this.`invokeConsumerMessage`(cb, args)
// Success
outBuf.setValue(buffer)
1
} catch (e: ConsumerException) {
// Expected error
val buffer = FfiConverterTypeConsumerError.lowerIntoRustBuffer(e)
outBuf.setValue(buffer)
-2
}
} catch (e: Throwable) {
// Unexpected error
try {
// Try to serialize the error into a string
outBuf.setValue(FfiConverterString.lower(e.toString()))
} catch (e: Throwable) {
// If that fails, then it's time to give up and just return
}
-1
}
}
else -> {
// An unexpected error happened.
// See docs of ForeignCallback in `uniffi/src/ffi/foreigncallbacks.rs`
try {
// Try to serialize the error into a string
outBuf.setValue(FfiConverterString.lower("Invalid Callback index"))
} catch (e: Throwable) {
// If that fails, then it's time to give up and just return
}
-1
}
}
} }
override fun allocationSize(value: Qos) = 4
private fun `invokeConsumerMessage`(kotlinCallbackInterface: MostOnceListener, args: RustBuffer.ByValue): RustBuffer.ByValue =
try {
val buf = args.asByteBuffer() ?: throw InternalException("No ByteBuffer in RustBuffer; this is a Uniffi bug")
kotlinCallbackInterface.`consumerMessage`(
FfiConverterTypeMessage.read(buf)
)
.let { RustBuffer.ByValue() }
// TODO catch errors and report them back to Rust.
// https://github.com/mozilla/uniffi-rs/issues/351
} finally {
RustBuffer.free(args)
}
}
// The ffiConverter which transforms the Callbacks in to Handles to pass to Rust. override fun write(value: Qos, buf: ByteBuffer) {
public object FfiConverterTypeMostOnceListener: FfiConverterCallbackInterface<MostOnceListener>( buf.putInt(value.ordinal + 1)
foreignCallback = ForeignCallbackTypeMostOnceListener()
) {
override fun register(lib: _UniFFILib) {
rustCall() { status ->
lib.ffi_NativeSocket_2a33_MostOnceListener_init_callback(this.foreignCallback, status)
}
} }
} }
public object FfiConverterOptionalTypeNativeSocket: FfiConverterRustBuffer<NativeSocket?> { public object FfiConverterOptionalTypeNativeSocket: FfiConverterRustBuffer<NativeSocket?> {
override fun read(buf: ByteBuffer): NativeSocket? { override fun read(buf: ByteBuffer): NativeSocket? {
if (buf.get().toInt() == 0) { if (buf.get().toInt() == 0) {
...@@ -1270,57 +1216,28 @@ public object FfiConverterOptionalTypeNativeSocket: FfiConverterRustBuffer<Nativ ...@@ -1270,57 +1216,28 @@ public object FfiConverterOptionalTypeNativeSocket: FfiConverterRustBuffer<Nativ
public object FfiConverterOptionalTypeLeastOnceListener: FfiConverterRustBuffer<LeastOnceListener?> { public object FfiConverterOptionalTypeMessage: FfiConverterRustBuffer<Message?> {
override fun read(buf: ByteBuffer): LeastOnceListener? { override fun read(buf: ByteBuffer): Message? {
if (buf.get().toInt() == 0) {
return null
}
return FfiConverterTypeLeastOnceListener.read(buf)
}
override fun allocationSize(value: LeastOnceListener?): Int {
if (value == null) {
return 1
} else {
return 1 + FfiConverterTypeLeastOnceListener.allocationSize(value)
}
}
override fun write(value: LeastOnceListener?, buf: ByteBuffer) {
if (value == null) {
buf.put(0)
} else {
buf.put(1)
FfiConverterTypeLeastOnceListener.write(value, buf)
}
}
}
public object FfiConverterOptionalTypeMostOnceListener: FfiConverterRustBuffer<MostOnceListener?> {
override fun read(buf: ByteBuffer): MostOnceListener? {
if (buf.get().toInt() == 0) { if (buf.get().toInt() == 0) {
return null return null
} }
return FfiConverterTypeMostOnceListener.read(buf) return FfiConverterTypeMessage.read(buf)
} }
override fun allocationSize(value: MostOnceListener?): Int { override fun allocationSize(value: Message?): Int {
if (value == null) { if (value == null) {
return 1 return 1
} else { } else {
return 1 + FfiConverterTypeMostOnceListener.allocationSize(value) return 1 + FfiConverterTypeMessage.allocationSize(value)
} }
} }
override fun write(value: MostOnceListener?, buf: ByteBuffer) { override fun write(value: Message?, buf: ByteBuffer) {
if (value == null) { if (value == null) {
buf.put(0) buf.put(0)
} else { } else {
buf.put(1) buf.put(1)
FfiConverterTypeMostOnceListener.write(value, buf) FfiConverterTypeMessage.write(value, buf)
} }
} }
} }
...@@ -1380,28 +1297,3 @@ public object FfiConverterSequenceUByte: FfiConverterRustBuffer<List<UByte>> { ...@@ -1380,28 +1297,3 @@ public object FfiConverterSequenceUByte: FfiConverterRustBuffer<List<UByte>> {
} }
public object FfiConverterSequenceString: FfiConverterRustBuffer<List<String>> {
override fun read(buf: ByteBuffer): List<String> {
val len = buf.getInt()
return List<String>(len) {
FfiConverterString.read(buf)
}
}
override fun allocationSize(value: List<String>): Int {
val sizeForLength = 4
val sizeForItems = value.map { FfiConverterString.allocationSize(it) }.sum()
return sizeForLength + sizeForItems
}
override fun write(value: List<String>, buf: ByteBuffer) {
buf.putInt(value.size)
value.forEach {
FfiConverterString.write(it, buf)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment