Skip to content

Commit

Permalink
添加测试异步socket
Browse files Browse the repository at this point in the history
  • Loading branch information
ZyElite committed Oct 23, 2018
1 parent e3f3bd7 commit 38e0ed1
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public String run() {
return var1;
}
} catch (NumberFormatException var3) {
;
}
}

Expand Down
5 changes: 4 additions & 1 deletion service/src/main/java/com/zy/service/AsyncServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ fun main(args: Array<String>) {
println("completed:$socketAddress")
//当获取到连接 重新等待新的连接接入
attachment?.accept(attachment, this)
result.read(ByteBuffer.allocate(1024), Conversation(), ReadCompletionHandler<ByteArray>())

val session = Attachment<ByteArray>()
session.setReadBuffer(ByteBuffer.allocate(1024))
result.read(session.getReadBuffer(), session, ReadCompletionHandler<ByteArray>())
}

override fun failed(exc: Throwable, attachment: AsynchronousServerSocketChannel) {
Expand Down
23 changes: 17 additions & 6 deletions service/src/main/java/com/zy/service/Attachment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,29 @@ class Attachment<T> {
*/
private var client: AsynchronousSocketChannel? = null
private var isReadModel: Boolean = false
private var buffer: ByteBuffer? = null
/**
* 读缓冲。
*
* 大小取决于AioQuickClient/AioQuickServer设置的setReadBufferSize
*/
private var readBuffer: ByteBuffer? = null
/**
* 写缓冲
*/
private var writeBuffer: ByteBuffer? = null
// fun setServer(serverSocketChannel: AsynchronousServerSocketChannel) {
// server = serverSocketChannel
// }
//
// fun getServer() = server


fun setReadBuffer(read: ByteBuffer) {
readBuffer = read
}

fun getReadBuffer() = readBuffer

fun setClient(client: AsynchronousSocketChannel) {
this.client = client
}
Expand All @@ -33,9 +49,4 @@ class Attachment<T> {

fun isReadModel() = isReadModel

fun setBuffer(buffer: ByteBuffer) {
this.buffer = buffer
}

fun getBuffer() = buffer
}
19 changes: 19 additions & 0 deletions service/src/main/java/com/zy/service/Protocol.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.zy.service

import org.smartboot.socket.transport.AioSession
import java.nio.ByteBuffer

/**
* 消息编解码
*/
interface Protocol<T> {
/**
* 数据按protocol进行解码
*/
fun decode(readBuffer: ByteBuffer, attachment: Attachment<T>): T

/**
* 数据按protocol进行编码
*/
fun encode(msg: T, attachment: Attachment<T>): ByteBuffer
}
8 changes: 4 additions & 4 deletions service/src/main/java/com/zy/service/ReadCompletionHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.zy.service

import java.nio.channels.CompletionHandler

class ReadCompletionHandler<T> : CompletionHandler<Int, Conversation<T>> {
class ReadCompletionHandler<T> : CompletionHandler<Int, Attachment<T>> {

override fun completed(result: Int, attachment: Conversation<T>) {
println("ReadCompletionHandler $result")
override fun completed(result: Int, attachment: Attachment<T>) {
println("ReadCompletionHandler $result attachment:${String(attachment.getReadBuffer()?.array()!!)}")
}

override fun failed(exc: Throwable, attachment: Conversation<T>) {
override fun failed(exc: Throwable, attachment: Attachment<T>) {
println("ReadCompletionHandler ${exc.message}")
}
}

0 comments on commit 38e0ed1

Please sign in to comment.