update kotlin version

This commit is contained in:
tursom 2020-08-19 14:08:50 +08:00
parent f42934be66
commit c01916332c
5 changed files with 43 additions and 9 deletions

View File

@ -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'
}

View File

@ -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(">>>")

View File

@ -1,5 +1,5 @@
buildscript {
ext.kotlinVersion = '1.3.72'
ext.kotlinVersion = '1.4.0'
repositories {
mavenLocal()

View File

@ -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 <T, R> Iterable<T>.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
}

View File

@ -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 <reified T : Any> String.fromJson(): T = gson.fromJson(this, T::class.java)
//inline fun <reified T : Any> String.fromJson(): T = gson.fromJson(this, T::class.java)
inline fun <reified T : Any> Gson.fromJson(json: String) = this.fromJson(json, T::class.java)!!
inline fun <reified T : Any> Gson.fromJson(json: String) = this.fromJson<T>(json, object : TypeToken<T>() {}.type)
inline fun <reified T : Any> String.fromJson(gson: Gson = cn.tursom.utils.gson) = gson.fromJson<T>(this, object : TypeToken<T>() {}.type)!!
suspend fun <T> io(block: suspend CoroutineScope.() -> T): T {
return withContext(Dispatchers.IO, block)