[optimize] merge network write

This commit is contained in:
acite
2025-09-14 01:02:39 +08:00
parent ffa70d9d34
commit c330a1e70c
5 changed files with 54 additions and 13 deletions

View File

@@ -278,15 +278,15 @@ class AbyssStream private constructor(
private fun sendPlaintextChunk(plaintext: ByteArray) {
if (closed) throw IllegalStateException("AbyssStream closed")
val ciphertextAndTag: ByteArray
val nonce = ByteArray(NONCE_LEN)
val ciphertextAndTag: ByteArray
val counterValue: Long
synchronized(sendLock) {
counterValue = sendCounter.getAndIncrement()
}
System.arraycopy(sendSalt, 0, nonce, 0, NONCE_SALT_LEN)
val bb = ByteBuffer.wrap(nonce, NONCE_SALT_LEN, 8)
bb.putLong(counterValue)
ByteBuffer.wrap(nonce, NONCE_SALT_LEN, 8).putLong(counterValue)
try {
ciphertextAndTag = aeadEncrypt(nonce, plaintext)
@@ -295,17 +295,22 @@ class AbyssStream private constructor(
}
val payloadLen = ciphertextAndTag.size
val header = ByteBuffer.allocate(4).putInt(payloadLen).array()
// header + ciphertextAndTag 一次性合并
val packet = ByteBuffer.allocate(4 + payloadLen)
.putInt(payloadLen)
.put(ciphertextAndTag)
.array()
try {
synchronized(output) {
output.write(header)
if (payloadLen > 0) output.write(ciphertextAndTag)
output.write(packet)
output.flush()
}
} finally {
// clear sensitive
ciphertextAndTag.fill(0)
plaintext.fill(0)
packet.fill(0)
}
}

View File

@@ -1,6 +1,7 @@
package com.acitelight.aether.service
import android.util.Log
import com.acitelight.aether.service.AuthManager.db64
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.first
@@ -102,7 +103,9 @@ class AbyssTunnelProxy @Inject constructor(
val read = localIn.read(buffer)
if (read <= 0)
break
Log.i("Delay Analyze", "Read $read Bytes from HttpClient")
abyss.write(buffer, 0, read)
Log.i("Delay Analyze", "Wrote $read Bytes to Remote Abyss")
}
}
@@ -113,7 +116,9 @@ class AbyssTunnelProxy @Inject constructor(
val n = abyss.read(buffer, 0, buffer.size)
if (n <= 0)
break
Log.i("Delay Analyze", "Read $n Bytes from Remote Abyss")
localOut.write(buffer, 0, n)
Log.i("Delay Analyze", "Wrote $n Bytes to HttpClient")
}
}
}

View File

@@ -224,6 +224,11 @@ object ApiClient {
}
api = createRetrofit().create(ApiInterface::class.java)
Log.i("Delay Analyze", "Start Abyss Hello")
val h = api!!.hello()
Log.i("Delay Analyze", "Abyss Hello: ${h.string()}")
return base
} catch (e: Exception) {
api = null

View File

@@ -4,12 +4,14 @@ import com.acitelight.aether.model.BookMark
import com.acitelight.aether.model.ChallengeResponse
import com.acitelight.aether.model.ComicResponse
import com.acitelight.aether.model.VideoResponse
import okhttp3.Response
import okhttp3.ResponseBody
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST
import retrofit2.http.Path
import retrofit2.http.Query
import retrofit2.http.Streaming
interface ApiInterface {
@GET("api/video")
@@ -56,4 +58,7 @@ interface ApiInterface {
@Path("user") user: String,
@Body challengeResponse: ChallengeResponse
): ResponseBody
@GET("api/abyss")
suspend fun hello(): ResponseBody
}

View File

@@ -1,5 +1,6 @@
package com.acitelight.aether.view
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
@@ -36,8 +37,13 @@ import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.compose.viewModel
import com.acitelight.aether.service.ApiClient.api
import com.acitelight.aether.viewModel.MeScreenViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable
fun MeScreen(meScreenViewModel: MeScreenViewModel = hiltViewModel()) {
@@ -189,14 +195,29 @@ fun MeScreen(meScreenViewModel: MeScreenViewModel = hiltViewModel()) {
Spacer(modifier = Modifier.height(24.dp))
// Save Button
Row{
Button(
onClick = {
meScreenViewModel.updateServer(url, cert, context)
},
modifier = Modifier.fillMaxWidth()
modifier = Modifier.fillMaxWidth(0.5f)
) {
Text("Save")
}
Button(
onClick = {
meScreenViewModel.viewModelScope.launch {
Log.i("Delay Analyze", "Start Abyss Hello")
val h = api!!.hello()
Log.i("Delay Analyze", "Abyss Hello: ${h.string()}")
}
},
modifier = Modifier.fillMaxWidth(0.5f)
) {
Text("Ping")
}
}
}
}
}