diff options
author | Edoardo La Greca | 2025-08-06 19:51:23 +0200 |
---|---|---|
committer | Edoardo La Greca | 2025-08-06 19:51:23 +0200 |
commit | f1c9e5d4acaa7bd27204fad193f08fc67a864c86 (patch) | |
tree | 6acc446fd12885cfb3b4822d841112ac297f3fbb /src/main | |
parent | 6a7229c74825478bcae3ed67ed7fabd1ad366d6b (diff) |
add checkedInMessage
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/kotlin/Connection.kt | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/main/kotlin/Connection.kt b/src/main/kotlin/Connection.kt index 203b5e2..e0b7f18 100644 --- a/src/main/kotlin/Connection.kt +++ b/src/main/kotlin/Connection.kt @@ -50,6 +50,28 @@ class Connection(transLay: TransportLayer) : ProtocolTranslator { this.tl.close() } + /** + * Handy function to create an [InMessage] instance and check for errors. It uses [tl] and [maxSize] for instancing + * the [InMessage] class. + * + * @return A pair of: (1) a nullable string (which can be: `null` if no error occurred, empty if an error occurred + * with no message, or non-empty with the error message) and (2) the optional [InMessage] instance (null if an error + * occurred). + */ + private fun checkedInMessage(reqTag: UShort): Pair<String?, InMessage?> { + val imsg: InMessage + try { + imsg = InMessage(this.tl, this.maxSize, reqTag) + } catch (ime: InvalidMessageException) { + return Pair(ime.message!!, null) + } + if (imsg.type == NinePMessageType.RERROR) { + imsg.applyField(InMessage.Field("ename", InMessage.Field.Type.STRING, 0u)) + return Pair(imsg.fieldsStr["ename"]!!, null) + } + return Pair(null, imsg) + } + override fun version(msize: UInt, version: String): String? { val omsg = OutMessage(NinePMessageType.TVERSION, this.NOTAG, listOf("msize", "version"), mapOf( |