From e7497c50a940edbf24986938227749badf3c98fb Mon Sep 17 00:00:00 2001 From: vadym Date: Wed, 9 Dec 2020 11:20:51 +0200 Subject: [PATCH] 1. PeerInfo optimization. 2. InetAddress deserialization error fix --- .../github/chronosx88/yggdrasil/DNSListActivity.kt | 6 +++--- .../github/chronosx88/yggdrasil/PeerListActivity.kt | 13 +++++-------- .../github/chronosx88/yggdrasil/models/PeerInfo.kt | 3 --- .../chronosx88/yggdrasil/models/config/Utils.kt | 6 +++--- 4 files changed, 11 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/DNSListActivity.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/DNSListActivity.kt index 7baa89c..8bdd14b 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/DNSListActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/DNSListActivity.kt @@ -59,11 +59,11 @@ class DNSListActivity : AppCompatActivity() { try { for (d in cd) { - var ping = ping(d.address, 53) + var ping = ping(d.address.hostAddress, 53) d.ping = ping } for (dns in allDNS) { - var ping = ping(dns.address, 53) + var ping = ping(dns.address.hostAddress, 53) dns.ping = ping runOnUiThread( Runnable @@ -110,7 +110,7 @@ class DNSListActivity : AppCompatActivity() { thread(start = true) { var di = DNSInfo(InetAddress.getByName("["+ip+"]"), ccp, "User DNS") try { - var ping = ping(di.address, 53) + var ping = ping(di.address.hostAddress, 53) di.ping = ping } catch(e: Throwable){ di.ping = Int.MAX_VALUE diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt index ea112af..7c2325d 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/PeerListActivity.kt @@ -99,14 +99,14 @@ class PeerListActivity : AppCompatActivity() { extras!!.getStringArrayList(MainActivity.PEER_LIST)!! ) for (pi in cp) { - var ping = ping(pi.address, pi.port) + var ping = ping(pi.hostName, pi.port) pi.ping = ping } try { var peerInfoCache = peerInfoListCache.get(ONLINE_PEERINFO_LIST) if (peerInfoCache != null && peerInfoCache.isNotEmpty()) { for (peerInfo in peerInfoCache) { - var ping = ping(peerInfo.address, peerInfo.port) + var ping = ping(peerInfo.hostName, peerInfo.port) peerInfo.ping = ping if (cp.contains(peerInfo)) { continue @@ -145,7 +145,7 @@ class PeerListActivity : AppCompatActivity() { url.port, ccp.nameCode ) - var ping = ping(address, url.port) + var ping = ping(url.host, url.port) peerInfo.ping = ping if (cp.contains(peerInfo)) { continue @@ -179,7 +179,7 @@ class PeerListActivity : AppCompatActivity() { var onlinePeerInfoList = peerInfoListCache.get(ONLINE_PEERINFO_LIST) if (onlinePeerInfoList != null) { for (peerInfo in onlinePeerInfoList) { - var ping = ping(peerInfo.address, peerInfo.port) + var ping = ping(peerInfo.hostName, peerInfo.port) peerInfo.ping = ping if (cp.contains(peerInfo)) { continue @@ -283,7 +283,7 @@ class PeerListActivity : AppCompatActivity() { GlobalScope.launch { var pi = PeerInfo(schema, InetAddress.getByName(ip), port, ccp) try { - var ping = ping(pi.address, pi.port) + var ping = ping(pi.hostName, pi.port) pi.ping = ping } catch (e: Throwable){ pi.ping = Int.MAX_VALUE @@ -375,9 +375,6 @@ class SizeOfPeerList: SizeOf> { override fun sizeOf(obj: List): Int{ var size = 0 for (o in obj) { - if (o.address != null) { - size += o.address.toString().length * 2 - } if (o.hostName != null) { size += o.hostName.length * 2 } diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/models/PeerInfo.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/models/PeerInfo.kt index 5cf6d8f..c17a327 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/models/PeerInfo.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/models/PeerInfo.kt @@ -14,7 +14,6 @@ class PeerInfo { constructor(schema: String, address: InetAddress, port: Int, countryCode: String){ this.schema = schema - this.address = address var a = address.toString(); if(a.lastIndexOf('/')>0){ this.hostName = a.split("/")[0] @@ -27,7 +26,6 @@ class PeerInfo { constructor(schema: String, address: InetAddress, port: Int, countryCode: String?, isMeshPeer: Boolean){ this.schema = schema - this.address = address var a = address.toString(); if(a.lastIndexOf('/')>0){ this.hostName = a.split("/")[0] @@ -40,7 +38,6 @@ class PeerInfo { } lateinit var schema: String - lateinit var address: InetAddress lateinit var hostName: String var port = 0 var countryCode: String?=null diff --git a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/Utils.kt b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/Utils.kt index abbbeef..d6078f2 100644 --- a/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/Utils.kt +++ b/app/src/main/java/io/github/chronosx88/yggdrasil/models/config/Utils.kt @@ -77,15 +77,15 @@ class Utils { } @JvmStatic - fun ping(address: InetAddress, port:Int): Int { + fun ping(hostname: String, port:Int): Int { val start = System.currentTimeMillis() val socket = Socket() try { - socket.connect(InetSocketAddress(address, port), 5000) + socket.connect(InetSocketAddress(hostname, port), 5000) socket.close() } catch (e: Exception) { e.printStackTrace() - print(address) + print(hostname) return Int.MAX_VALUE } return (System.currentTimeMillis() - start).toInt()