mirror of
https://github.com/tursom/TursomServer.git
synced 2025-02-01 07:11:18 +08:00
添加了两个Demo
This commit is contained in:
parent
cc8cb7c333
commit
acfe8fda0a
42
database/src/test/kotlin/Demo.kt
Normal file
42
database/src/test/kotlin/Demo.kt
Normal file
@ -0,0 +1,42 @@
|
||||
import cn.tursom.database.annotation.*
|
||||
import cn.tursom.database.clauses.ClauseMaker
|
||||
import cn.tursom.database.select
|
||||
import cn.tursom.database.sqlite.SQLiteHelper
|
||||
import cn.tursom.database.SqlUtils.tableName
|
||||
import org.jetbrains.annotations.NotNull
|
||||
|
||||
/**
|
||||
* 用来访问数据库的数据类
|
||||
*/
|
||||
// 自定义表名
|
||||
@TableName("demo")
|
||||
// 如果未自定义表名,则以类名作为表名
|
||||
data class Demo(
|
||||
// 定义一个非空,自增的整数列 `id` 作为主键
|
||||
@NotNull @AutoIncrement @PrimaryKey val id: Int? = null,
|
||||
// 定义一个非空,唯一的字符串列 `name`
|
||||
@NotNull @Unique @FieldType("varchar(32)") val name: String,
|
||||
// 定义一个非空小数列 `money`,其默认值为0
|
||||
@NotNull @Default("0") @FieldName("money") val money: Double = 0.0
|
||||
)
|
||||
|
||||
fun main() {
|
||||
// 获取数据库访问协助对象
|
||||
val helper = SQLiteHelper("demo.db")
|
||||
|
||||
// 插入数据
|
||||
helper.insert(Demo(name = "tursom"))
|
||||
|
||||
// 更新数据
|
||||
helper.update(Demo(name = "tursom", money = 100.0), where = ClauseMaker.make {
|
||||
!Demo::name equal "tursom"
|
||||
})
|
||||
|
||||
// 获取数据
|
||||
val data = helper.select(Demo::class.java, where = ClauseMaker {
|
||||
(!Demo::id greaterThan !0) and (!Demo::id lessThan !10)
|
||||
})
|
||||
|
||||
// 删除数据
|
||||
helper.delete(Demo::class.java.tableName, where = ClauseMaker.make { !Demo::name equal "tursom" })
|
||||
}
|
30
socket/socket-async/src/test/kotlin/Demo.kt
Normal file
30
socket/socket-async/src/test/kotlin/Demo.kt
Normal file
@ -0,0 +1,30 @@
|
||||
import cn.tursom.core.bytebuffer.ByteArrayAdvanceByteBuffer
|
||||
import cn.tursom.core.pool.DirectMemoryPool
|
||||
import cn.tursom.core.pool.usingAdvanceByteBuffer
|
||||
import cn.tursom.socket.server.AsyncNioServer
|
||||
|
||||
fun main() {
|
||||
// 服务器端口,可任意指定
|
||||
val port = 12345
|
||||
|
||||
// 创建一个直接内存池,每个块是1024字节,共有256个快
|
||||
val memoryPool = DirectMemoryPool(1024, 256)
|
||||
// 创建服务器对象
|
||||
val server = AsyncNioServer(port) {
|
||||
// 这里处理业务逻辑,套接字对象被以 this 的方式传进来
|
||||
// 从内存池中获取一个内存块
|
||||
memoryPool.usingAdvanceByteBuffer {
|
||||
// 检查是否获取成功,不成功就创建一个堆缓冲
|
||||
val buffer = it ?: ByteArrayAdvanceByteBuffer(1024)
|
||||
// 从套接字中读数据,五秒之内没有数据就抛出异常
|
||||
read(buffer, 5000)
|
||||
// 输出读取到的数据
|
||||
println("${System.currentTimeMillis()}: recv from ${channel.remoteAddress}: ${buffer.toString(buffer.readableSize)}")
|
||||
// 原封不动的返回数据
|
||||
write(buffer)
|
||||
// 代码块结束后,框架会自动释放连接
|
||||
}
|
||||
}
|
||||
// 创建一个新线程去启动服务器
|
||||
Thread(server, "echoServerStarter").start()
|
||||
}
|
@ -11,7 +11,7 @@ import java.util.concurrent.atomic.AtomicBoolean
|
||||
class ThreadPoolNioThread(
|
||||
val threadName: String = "",
|
||||
override val selector: Selector = Selector.open(),
|
||||
override val isDaemon: Boolean = true,
|
||||
override val isDaemon: Boolean = false,
|
||||
override val workLoop: (thread: INioThread) -> Unit
|
||||
) : INioThread {
|
||||
private var onWakeup: AtomicBoolean = AtomicBoolean(false)
|
||||
|
@ -6,7 +6,7 @@ import java.nio.channels.SelectableChannel
|
||||
class ThreadPoolWorkerGroup(
|
||||
val poolSize: Int = Runtime.getRuntime().availableProcessors(),
|
||||
val groupName: String = "",
|
||||
override val isDaemon: Boolean = true,
|
||||
override val isDaemon: Boolean = false,
|
||||
val worker: (thread: INioThread) -> Unit
|
||||
) : IWorkerGroup {
|
||||
val workerGroup = Array(poolSize) {
|
||||
|
@ -10,7 +10,7 @@ import java.util.concurrent.atomic.AtomicBoolean
|
||||
class WorkerLoopNioThread(
|
||||
val threadName: String = "nioLoopThread",
|
||||
override val selector: Selector = Selector.open(),
|
||||
override val isDaemon: Boolean = true,
|
||||
override val isDaemon: Boolean = false,
|
||||
override val workLoop: (thread: INioThread) -> Unit
|
||||
) : INioThread {
|
||||
private var onWakeup: AtomicBoolean = AtomicBoolean(false)
|
||||
|
@ -30,7 +30,7 @@ class NioServer(
|
||||
protocol: INioProtocol,
|
||||
backLog: Int = 50
|
||||
) : this(port, protocol, backLog, { name, workLoop ->
|
||||
WorkerLoopNioThread(name, workLoop = workLoop)
|
||||
WorkerLoopNioThread(name, workLoop = workLoop, isDaemon = false)
|
||||
})
|
||||
|
||||
override fun run() {
|
||||
|
@ -178,6 +178,12 @@ interface AdvanceByteBuffer {
|
||||
|
||||
|
||||
fun getString(size: Int = readableSize): String = String(getBytes())
|
||||
fun toString(size: Int): String {
|
||||
val rp = readPosition
|
||||
val bytes = getBytes()
|
||||
readPosition = rp
|
||||
return String(bytes)
|
||||
}
|
||||
|
||||
fun writeTo(buffer: ByteArray, bufferOffset: Int = 0, size: Int = min(readableSize, buffer.size)): Int {
|
||||
val readSize = min(readableSize, size)
|
||||
|
Loading…
Reference in New Issue
Block a user