Skip to content

Commit

Permalink
minor code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
froks committed Apr 15, 2022
1 parent 244f908 commit 221215d
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
3 changes: 1 addition & 2 deletions src/main/kotlin/DataStorage.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import java.util.concurrent.ConcurrentHashMap
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

Expand All @@ -20,7 +19,7 @@ class StoragePropertyDelegate<T>(
}

open class DataStorage {
private val internalDataStorage: MutableMap<String, Any?> = ConcurrentHashMap()
private val internalDataStorage: MutableMap<String, Any?> = mutableMapOf()

/**
* Create a persistent property by means of delegation, with an initival value
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package library

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.slf4j.MDC
Expand Down Expand Up @@ -143,5 +145,7 @@ fun DoipEntity.hasAlreadyActiveConnection(sourceAddress: Short, exclude: DoipTcp
&& it != exclude
}

fun OutputStream.writeFully(byteArray: ByteArray) =
this.write(byteArray)
suspend fun OutputStream.writeFully(byteArray: ByteArray) =
withContext(Dispatchers.IO) {
this@writeFully.write(byteArray)
}
3 changes: 0 additions & 3 deletions src/main/kotlin/library/DoipClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import java.nio.ByteBuffer
import kotlin.concurrent.fixedRateTimer
import kotlin.concurrent.thread
import kotlin.time.Duration
import kotlin.time.ExperimentalTime

class DoipClient(
private val broadcastAddress: SocketAddress = InetSocketAddress("255.255.255.255", 13400),
Expand All @@ -38,7 +37,6 @@ class DoipClient(
}
}

@OptIn(ExperimentalTime::class)
fun waitForVAM(timeout: Duration = Duration.INFINITE, logicalAddress: Short? = null): Boolean =
runBlocking {
withTimeoutOrNull(timeout) {
Expand Down Expand Up @@ -126,7 +124,6 @@ class DoipTcpConnection(socket: Socket, private val testerAddress: Short) {
}
}

@OptIn(ExperimentalTime::class)
fun sendDiagnosticMessage(
targetAddress: Short,
message: ByteArray,
Expand Down
5 changes: 2 additions & 3 deletions src/main/kotlin/library/DoipUdpMessageParser.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package library

import io.ktor.utils.io.core.*
import kotlin.experimental.xor
import kotlin.experimental.inv

open class HeaderNegAckException(message: String) : RuntimeException(message)

Expand All @@ -12,8 +12,7 @@ class UnknownPayloadType(message: String) : HeaderNegAckException(message)

object DoipUdpMessageParser {
private fun checkSyncPattern(protocolVersion: Byte, inverseProtocolVersion: Byte): Boolean {
if (protocolVersion != inverseProtocolVersion xor 0xFF.toByte()
) {
if (protocolVersion != inverseProtocolVersion.inv()) {
return false
}
return true
Expand Down

0 comments on commit 221215d

Please sign in to comment.