Commit ea659848 authored by Mygod's avatar Mygod

allocateDirect for NIO

parent c102406c
...@@ -72,7 +72,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd ...@@ -72,7 +72,7 @@ class LocalDnsServer(private val localResolver: suspend (String) -> Array<InetAd
configureBlocking(false) configureBlocking(false)
socket().bind(listen) socket().bind(listen)
monitor.register(this, SelectionKey.OP_READ) { monitor.register(this, SelectionKey.OP_READ) {
val buffer = ByteBuffer.allocate(UDP_PACKET_SIZE) val buffer = ByteBuffer.allocateDirect(UDP_PACKET_SIZE)
val source = receive(buffer)!! val source = receive(buffer)!!
buffer.flip() buffer.flip()
launch { launch {
......
...@@ -49,7 +49,7 @@ class Socks5Endpoint(host: String, port: Int) { ...@@ -49,7 +49,7 @@ class Socks5Endpoint(host: String, port: Int) {
fun tcpWrap(message: ByteBuffer): ByteBuffer { fun tcpWrap(message: ByteBuffer): ByteBuffer {
check(message.remaining() < 65536) { "TCP message too large" } check(message.remaining() < 65536) { "TCP message too large" }
return ByteBuffer.allocate(8 + dest.size + message.remaining()).apply { return ByteBuffer.allocateDirect(8 + dest.size + message.remaining()).apply {
put(Socks5Message.SOCKS_VERSION.toByte()) put(Socks5Message.SOCKS_VERSION.toByte())
put(1) // nmethods put(1) // nmethods
put(0) // no authentication required put(0) // no authentication required
...@@ -64,7 +64,7 @@ class Socks5Endpoint(host: String, port: Int) { ...@@ -64,7 +64,7 @@ class Socks5Endpoint(host: String, port: Int) {
flip() flip()
} }
} }
fun tcpReceiveBuffer(size: Int) = ByteBuffer.allocate(headerReserved + 4 + size) fun tcpReceiveBuffer(size: Int) = ByteBuffer.allocateDirect(headerReserved + 4 + size)
suspend fun tcpUnwrap(buffer: ByteBuffer, reader: (ByteBuffer) -> Int, wait: suspend () -> Unit) { suspend fun tcpUnwrap(buffer: ByteBuffer, reader: (ByteBuffer) -> Int, wait: suspend () -> Unit) {
suspend fun readBytes(till: Int) { suspend fun readBytes(till: Int) {
if (buffer.position() >= till) return if (buffer.position() >= till) return
...@@ -98,7 +98,7 @@ class Socks5Endpoint(host: String, port: Int) { ...@@ -98,7 +98,7 @@ class Socks5Endpoint(host: String, port: Int) {
buffer.reset() buffer.reset()
} }
fun udpWrap(packet: ByteBuffer) = ByteBuffer.allocate(3 + dest.size + packet.remaining()).apply { fun udpWrap(packet: ByteBuffer) = ByteBuffer.allocateDirect(3 + dest.size + packet.remaining()).apply {
// header // header
putShort(0) // reserved putShort(0) // reserved
put(0) // fragment number put(0) // fragment number
...@@ -107,7 +107,7 @@ class Socks5Endpoint(host: String, port: Int) { ...@@ -107,7 +107,7 @@ class Socks5Endpoint(host: String, port: Int) {
put(packet) put(packet)
flip() flip()
} }
fun udpReceiveBuffer(size: Int) = ByteBuffer.allocate(headerReserved + size) fun udpReceiveBuffer(size: Int) = ByteBuffer.allocateDirect(headerReserved + size)
fun udpUnwrap(packet: ByteBuffer) { fun udpUnwrap(packet: ByteBuffer) {
packet.position(3) packet.position(3)
packet.position(6 + when (packet.get()) { packet.position(6 + when (packet.get()) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment