From f7c4e70dd6b11feb2bc6040c83ad6ad353b535b5 Mon Sep 17 00:00:00 2001 From: vadym Date: Mon, 29 Jun 2020 05:53:24 -0700 Subject: [PATCH] 1. moved to blocking IO mode --- .../yggdrasil/YggdrasilTunService.kt | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt index 523107e..fb1d39d 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/YggdrasilTunService.kt @@ -70,6 +70,7 @@ class YggdrasilTunService : VpnService() { .addAddress(address, 7) .allowFamily(OsConstants.AF_INET) .setMtu(MAX_PACKET_SIZE) + .setBlocking(true) if (dns.size > 0) { builder.addDnsServer(address) for (d in dns) { @@ -159,17 +160,12 @@ class YggdrasilTunService : VpnService() { } private fun readPacketsFromTun(yggConduitEndpoint: ConduitEndpoint, buffer: ByteArray) { - // Read the outgoing packet from the input stream. - try{ - val length = tunInputStream!!.read(buffer) - if (length > 0) { - val byteBuffer = ByteBuffer.allocate(length) - byteBuffer.put(buffer, 0, length) - yggConduitEndpoint.send(byteBuffer.array()) - } else { - Thread.sleep(10) - } - }catch(e: IOException){ + if (tunInputStream == null) return + try { + // Read the outgoing packet from the input stream. + val length = tunInputStream?.read(buffer) ?: 1 + yggConduitEndpoint.send(buffer.sliceArray(IntRange(0, length - 1))) + } catch (e: IOException) { e.printStackTrace() } }