diff --git a/AsyncSocket/build.gradle b/AsyncSocket/build.gradle index 16e2921..c6107f7 100644 --- a/AsyncSocket/build.gradle +++ b/AsyncSocket/build.gradle @@ -2,5 +2,6 @@ dependencies { compile project(":") // kotlin 协程 - compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1' + compile 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9' + testRuntime group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-debug', version: '1.3.9' } \ No newline at end of file diff --git a/AsyncSocket/src/test/kotlin/cn/tursom/datagram/server/test.kt b/AsyncSocket/src/test/kotlin/cn/tursom/datagram/server/test.kt index 3627a03..a3a73cb 100644 --- a/AsyncSocket/src/test/kotlin/cn/tursom/datagram/server/test.kt +++ b/AsyncSocket/src/test/kotlin/cn/tursom/datagram/server/test.kt @@ -4,6 +4,7 @@ import cn.tursom.channel.BufferedAsyncChannel import cn.tursom.core.log import cn.tursom.core.pool.DirectMemoryPool import cn.tursom.datagram.AsyncDatagramClient +import cn.tursom.socket.NioClient import cn.tursom.socket.server.BuffedNioServer import kotlinx.coroutines.runBlocking @@ -11,6 +12,7 @@ val echoHandler: suspend BufferedAsyncChannel.() -> Unit = { while (true) { val buffer = read() log("$this recv from client $remoteAddress: ${buffer.toString(buffer.readable)}") + Throwable().printStackTrace() write(buffer) } } @@ -35,7 +37,7 @@ fun main() { runBlocking { val input = System.`in`.bufferedReader() - var client = AsyncDatagramClient.connect("127.0.0.1", port).getBuffed(pool) + var client = NioClient.connect("127.0.0.1", port).getBuffed(pool) while (true) { try { print(">>>") diff --git a/build.gradle b/build.gradle index 0bc736a..a846c3e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlinVersion = '1.3.72' + ext.kotlinVersion = '1.4.0' repositories { mavenLocal() diff --git a/src/main/kotlin/cn/tursom/core/Tools.kt b/src/main/kotlin/cn/tursom/core/Tools.kt index b01e46d..6746904 100644 --- a/src/main/kotlin/cn/tursom/core/Tools.kt +++ b/src/main/kotlin/cn/tursom/core/Tools.kt @@ -6,6 +6,7 @@ import sun.misc.Unsafe import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.lang.reflect.Field +import java.lang.reflect.Method import java.lang.reflect.ParameterizedType import java.lang.reflect.Type import java.net.URLDecoder @@ -406,3 +407,35 @@ fun Iterable.firstNotNull(selector: (T) -> R): R? { } return null } + +fun Class<*>.forAllMethods(action: (Method) -> Unit) { + var clazz = this + while (clazz != Any::class.java) { + clazz.declaredMethods.forEach(action) + clazz = clazz.superclass + } + clazz.declaredMethods.forEach(action) +} + +private val BASE62_DIGITS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray() + +tailrec fun Long.base62(sBuilder: StringBuilder = StringBuilder()): String { + return if (this == 0L) { + sBuilder.reverse().toString() + } else { + val remainder = (this % 62).toInt() + sBuilder.append(BASE62_DIGITS[remainder]) + (this / 62).base62(sBuilder) + } +} + +fun String.base62Decode(): Long { + var sum: Long = 0 + val len = length + var base = 62L + for (i in 0 until len) { + sum += BASE62_DIGITS.indexOf(this[len - i - 1]) * base + base *= 62 + } + return sum +} diff --git a/utils/src/main/kotlin/cn/tursom/utils/Tools.kt b/utils/src/main/kotlin/cn/tursom/utils/Tools.kt index 79a4fa2..8d7318d 100644 --- a/utils/src/main/kotlin/cn/tursom/utils/Tools.kt +++ b/utils/src/main/kotlin/cn/tursom/utils/Tools.kt @@ -2,15 +2,12 @@ package cn.tursom.utils import com.google.gson.Gson import com.google.gson.GsonBuilder +import com.google.gson.reflect.TypeToken import kotlinx.coroutines.* -import java.lang.reflect.InvocationHandler -import java.lang.reflect.Method -import java.lang.reflect.Proxy import java.util.concurrent.Executor import kotlin.coroutines.resume import kotlin.coroutines.resumeWithException import kotlin.coroutines.suspendCoroutine -import kotlin.reflect.jvm.javaMethod @Suppress("unused", "SpellCheckingInspection") val gson = GsonBuilder() @@ -25,9 +22,10 @@ val prettyGson = GsonBuilder() fun Any.toJson(): String = gson.toJson(this) fun Any.toPrettyJson(): String = prettyGson.toJson(this) -inline fun String.fromJson(): T = gson.fromJson(this, T::class.java) +//inline fun String.fromJson(): T = gson.fromJson(this, T::class.java) -inline fun Gson.fromJson(json: String) = this.fromJson(json, T::class.java)!! +inline fun Gson.fromJson(json: String) = this.fromJson(json, object : TypeToken() {}.type) +inline fun String.fromJson(gson: Gson = cn.tursom.utils.gson) = gson.fromJson(this, object : TypeToken() {}.type)!! suspend fun io(block: suspend CoroutineScope.() -> T): T { return withContext(Dispatchers.IO, block)