From dc28c3f9e8b54a1fd8adbbab95afc5926c49d3a7 Mon Sep 17 00:00:00 2001 From: Robinson Date: Sun, 27 Mar 2022 21:14:55 +0200 Subject: [PATCH] Updated comments, guarantee that a return value is returned via `send`. Moved common method to object --- src/dorkbox/network/connection/Connection.kt | 2 +- src/dorkbox/network/connection/EndPoint.kt | 59 ++++++++++---------- 2 files changed, 31 insertions(+), 30 deletions(-) diff --git a/src/dorkbox/network/connection/Connection.kt b/src/dorkbox/network/connection/Connection.kt index aa44702a..d1b01967 100644 --- a/src/dorkbox/network/connection/Connection.kt +++ b/src/dorkbox/network/connection/Connection.kt @@ -216,7 +216,7 @@ open class Connection(connectionParameters: ConnectionParams<*>) { /** * Safely sends objects to a destination. * - * @return true if the message was successfully sent by aeron + * @return true if the message was successfully sent, false otherwise. Exceptions are caught and NOT rethrown! */ suspend fun send(message: Any): Boolean { messagesInProgress.getAndIncrement() diff --git a/src/dorkbox/network/connection/EndPoint.kt b/src/dorkbox/network/connection/EndPoint.kt index 5dc7174b..39ca724a 100644 --- a/src/dorkbox/network/connection/EndPoint.kt +++ b/src/dorkbox/network/connection/EndPoint.kt @@ -83,6 +83,32 @@ internal constructor(val type: Class<*>, protected constructor(config: Configuration, connectionFunc: (connectionParameters: ConnectionParams) -> CONNECTION) : this(Client::class.java, config, connectionFunc) protected constructor(config: ServerConfiguration, connectionFunc: (connectionParameters: ConnectionParams) -> CONNECTION) : this(Server::class.java, config, connectionFunc) + companion object { + /** + * @return the error code text for the specified number + */ + private fun errorCodeName(result: Long): String { + return when (result) { + // The publication is not connected to a subscriber, this can be an intermittent state as subscribers come and go. + Publication.NOT_CONNECTED -> "Not connected" + + // The offer failed due to back pressure from the subscribers preventing further transmission. + Publication.BACK_PRESSURED -> "Back pressured" + + // The action is an operation such as log rotation which is likely to have succeeded by the next retry attempt. + Publication.ADMIN_ACTION -> "Administrative action" + + // The Publication has been closed and should no longer be used. + Publication.CLOSED -> "Publication is closed" + + // If this happens then the publication should be closed and a new one added. To make it less likely to happen then increase the term buffer length. + Publication.MAX_POSITION_EXCEEDED -> "Maximum term position exceeded" + + else -> throw IllegalStateException("Unknown error code: $result") + } + } + } + val logger: KLogger = KotlinLogging.logger(type.simpleName) internal val actionDispatch = config.dispatch @@ -321,7 +347,7 @@ internal constructor(val type: Class<*>, * @return true if the message was successfully sent by aeron */ @Suppress("DuplicatedCode") - internal fun writeHandshakeMessage(publication: Publication, message: HandshakeMessage): Boolean { + internal fun writeHandshakeMessage(publication: Publication, message: HandshakeMessage) { // The handshake sessionId IS NOT globally unique logger.trace { "[${publication.sessionId()}] send HS: $message" @@ -338,7 +364,7 @@ internal constructor(val type: Class<*>, result = publication.offer(internalBuffer, 0, objectSize) if (result >= 0) { // success! - return true + return } /** @@ -487,7 +513,7 @@ internal constructor(val type: Class<*>, /** * NOTE: this **MUST** stay on the same co-routine that calls "send". This cannot be re-dispatched onto a different coroutine! * - * @return true if the message was successfully sent by aeron + * @return true if the message was successfully sent by aeron, false otherwise. Exceptions are caught and NOT rethrown! */ @Suppress("DuplicatedCode", "UNCHECKED_CAST") internal suspend fun send(message: Any, publication: Publication, connection: Connection): Boolean { @@ -552,7 +578,7 @@ internal constructor(val type: Class<*>, logger.error("Aeron error!", exception) listenerManager.notifyError(connection, exception) - throw exception + return false } } catch (e: Exception) { if (message is MethodResponse && message.result is Exception) { @@ -571,31 +597,6 @@ internal constructor(val type: Class<*>, return false } - - /** - * @return the error code text for the specified number - */ - private fun errorCodeName(result: Long): String { - return when (result) { - // The publication is not connected to a subscriber, this can be an intermittent state as subscribers come and go. - Publication.NOT_CONNECTED -> "Not connected" - - // The offer failed due to back pressure from the subscribers preventing further transmission. - Publication.BACK_PRESSURED -> "Back pressured" - - // The action is an operation such as log rotation which is likely to have succeeded by the next retry attempt. - Publication.ADMIN_ACTION -> "Administrative action" - - // The Publication has been closed and should no longer be used. - Publication.CLOSED -> "Publication is closed" - - // If this happens then the publication should be closed and a new one added. To make it less likely to happen then increase the term buffer length. - Publication.MAX_POSITION_EXCEEDED -> "Maximum term position exceeded" - - else -> throw IllegalStateException("Unknown error code: $result") - } - } - override fun toString(): String { return "EndPoint [${type.simpleName}]" }