Skip to content

Commit

Permalink
refactor byte strings
Browse files Browse the repository at this point in the history
  • Loading branch information
andreypfau committed Feb 21, 2024
1 parent f6a756e commit 7eb82e9
Show file tree
Hide file tree
Showing 97 changed files with 431 additions and 907 deletions.
92 changes: 0 additions & 92 deletions adnl/src/Adnl.kt

This file was deleted.

125 changes: 0 additions & 125 deletions adnl/src/AdnlPeers.kt

This file was deleted.

3 changes: 1 addition & 2 deletions adnl/src/connection/AdnlClient.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package org.ton.adnl.connection

import io.ktor.utils.io.core.*
import kotlin.time.Duration

public interface AdnlClient {
public suspend fun sendQuery(data: ByteReadPacket, timeout: Duration): ByteReadPacket
public suspend fun sendQuery(data: ByteArray, timeout: Duration): ByteArray
}
24 changes: 9 additions & 15 deletions adnl/src/connection/AdnlClientImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,33 @@ package org.ton.adnl.connection
import io.ktor.utils.io.core.*
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.withTimeout
import kotlinx.io.bytestring.ByteString
import org.ton.api.adnl.message.AdnlMessageAnswer
import org.ton.api.adnl.message.AdnlMessageQuery
import org.ton.api.liteserver.LiteServerDesc
import org.ton.tl.ByteString
import org.ton.tl.asByteString
import kotlin.random.Random
import kotlin.time.Duration

public class AdnlClientImpl(
private val liteServerDesc: LiteServerDesc
) : AdnlClient {
override suspend fun sendQuery(data: ByteReadPacket, timeout: Duration): ByteReadPacket {
override suspend fun sendQuery(data: ByteArray, timeout: Duration): ByteArray {
val adnlConnection = connectionPool.selectConnection(liteServerDesc)
val queryId = ByteString.of(*Random.nextBytes(32))
val queryId = ByteString(*Random.nextBytes(32))
val context = SupervisorJob()
val queryData = data.readBytes()
try {
return withTimeout(timeout) {
val response = adnlConnection.execute(
AdnlRequestData(
buildPacket {
AdnlMessageQuery.encodeBoxed(
this, AdnlMessageQuery(queryId, queryData.asByteString())
)
}.readBytes(),
AdnlMessageQuery.encodeToByteArray(
AdnlMessageQuery(queryId, ByteString(*data)), true
),
context
), context
)
ByteReadPacket(
AdnlMessageAnswer.decodeBoxed(
response.body
).answer.toByteArray()
)
AdnlMessageAnswer.decodeBoxed(
response.body.readBytes()
).answer.toByteArray()
}
} catch (e: Throwable) {
throw e
Expand Down
7 changes: 3 additions & 4 deletions adnl/src/connection/AdnlConnection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import kotlinx.datetime.Instant
import org.ton.adnl.network.TcpClient
import org.ton.api.liteserver.LiteServerDesc
import org.ton.crypto.SecureRandom
import org.ton.tl.writeByteString
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration
import kotlin.time.Duration.Companion.ZERO
Expand Down Expand Up @@ -67,7 +66,7 @@ public class AdnlConnection(
}

connection.output.writePacket {
writeByteString(liteServerDesc.id.toAdnlIdShort().id)
writeFully(liteServerDesc.id.toAdnlIdShort().id.toByteArray())
writeFully(liteServerDesc.id.encrypt(nonce))
}
connection.output.flush()
Expand Down Expand Up @@ -185,8 +184,8 @@ public class AdnlConnection(
val payload = packet.readBytes()

val hash = SHA256().apply {
write(nonce)
write(payload)
update(nonce)
update(payload)
}.digest()

val data = buildPacket {
Expand Down
3 changes: 2 additions & 1 deletion adnl/src/network/IPAddress.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.ton.adnl.network

import kotlinx.io.bytestring.ByteString
import org.ton.adnl.ipv4
import org.ton.api.adnl.AdnlAddress
import org.ton.api.adnl.AdnlAddressUdp
Expand Down Expand Up @@ -54,7 +55,7 @@ public data class IPv6Address(
override val host: String
get() = TODO()

override fun toAdnlAddress(): AdnlAddressUdp6 = AdnlAddressUdp6(address, port)
override fun toAdnlAddress(): AdnlAddressUdp6 = AdnlAddressUdp6(ByteString(address), port)

override fun toString(): String = "$host:$port"
}
45 changes: 0 additions & 45 deletions adnl/src/peer/AdnlMessagePartDecoder.kt

This file was deleted.

Loading

0 comments on commit 7eb82e9

Please sign in to comment.