1. more optimization

This commit is contained in:
vadym 2020-06-15 14:21:42 -07:00
parent b48b39d94c
commit 954608b220

View File

@ -67,9 +67,10 @@ class YggdrasilTunService : VpnService() {
tunInputStream = FileInputStream(tunInterface!!.fileDescriptor) tunInputStream = FileInputStream(tunInterface!!.fileDescriptor)
tunOutputStream = FileOutputStream(tunInterface!!.fileDescriptor) tunOutputStream = FileOutputStream(tunInterface!!.fileDescriptor)
readCoroutine = GlobalScope.launch { readCoroutine = GlobalScope.launch {
var buffer = ByteArray(2048)
while (true) { while (true) {
try{ try{
readPacketsFromTun() readPacketsFromTun(buffer)
} catch (e: IOException){ } catch (e: IOException){
e.printStackTrace() e.printStackTrace()
} }
@ -118,15 +119,14 @@ class YggdrasilTunService : VpnService() {
return config return config
} }
private fun readPacketsFromTun() { private fun readPacketsFromTun(buffer: ByteArray) {
if(tunInputStream != null) { if(tunInputStream != null) {
var packet = ByteArray(2048)
// Read the outgoing packet from the input stream. // Read the outgoing packet from the input stream.
var length = tunInputStream!!.read(packet) var length = tunInputStream!!.read(buffer)
if (length > 0) { if (length > 0) {
var buffer = ByteBuffer.allocate(length); var byteBuffer = ByteBuffer.allocate(length);
buffer.put(packet, 0, length) byteBuffer.put(buffer, 0, length)
yggConduitEndpoint.send(buffer.array()) yggConduitEndpoint.send(byteBuffer.array())
} else { } else {
Thread.sleep(10) Thread.sleep(10)
} }