Skip to content

Commit d52b9b8

Browse files
committed
Add read timeout option
1 parent d4efa94 commit d52b9b8

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

src/main/kotlin/com/theevilroot/asyncsocket/CoroutineSocket.kt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ import java.net.InetSocketAddress
44
import java.nio.ByteBuffer
55
import java.nio.channels.AsynchronousSocketChannel
66
import java.nio.channels.CompletionHandler
7+
import java.util.concurrent.TimeUnit
78
import kotlin.coroutines.Continuation
89
import kotlin.coroutines.resume
910
import kotlin.coroutines.resumeWithException
1011
import kotlin.coroutines.suspendCoroutine
1112

12-
open class CoroutineSocket(private val socket: AsynchronousSocketChannel) {
13+
open class CoroutineSocket(
14+
private val socket: AsynchronousSocketChannel,
15+
private val readTimeout: Pair<Long, TimeUnit>?
16+
) {
1317

1418
var isConnected: Boolean = false
1519
private set
@@ -25,7 +29,12 @@ open class CoroutineSocket(private val socket: AsynchronousSocketChannel) {
2529

2630
open suspend fun read(buffer: ByteBuffer): Int {
2731
return suspendCoroutine {
28-
socket.read(buffer, it, ContinuationHandler<Int>())
32+
if (readTimeout != null) {
33+
socket.read(buffer, readTimeout.first,
34+
readTimeout.second, it, ContinuationHandler<Int>())
35+
} else {
36+
socket.read(buffer, it, ContinuationHandler<Int>())
37+
}
2938
}
3039
}
3140

src/main/kotlin/com/theevilroot/asyncsocket/Socks4CoroutineSocket.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import java.net.InetAddress
44
import java.net.InetSocketAddress
55
import java.nio.ByteBuffer
66
import java.nio.channels.AsynchronousSocketChannel
7+
import java.util.concurrent.TimeUnit
78

89
class Socks4CoroutineSocket(
910
val socksIsa: InetSocketAddress,
1011
channel: AsynchronousSocketChannel,
11-
val userId: String
12-
) : CoroutineSocket(channel) {
12+
val userId: String,
13+
readTimeout: Pair<Long, TimeUnit>? = null
14+
) : CoroutineSocket(channel, readTimeout) {
1315

1416
lateinit var remoteIsa: InetSocketAddress
1517

src/main/kotlin/com/theevilroot/asyncsocket/SocksCoroutineSocket.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import java.net.InetAddress
55
import java.net.InetSocketAddress
66
import java.nio.ByteBuffer
77
import java.nio.channels.AsynchronousSocketChannel
8+
import java.util.concurrent.TimeUnit
89

910
class SocksCoroutineSocket(
1011
val socksIsa: InetSocketAddress,
1112
channel: AsynchronousSocketChannel,
12-
val credentials: Pair<String, String>? = null
13-
) : CoroutineSocket(channel) {
13+
val credentials: Pair<String, String>? = null,
14+
readTimeout: Pair<Long, TimeUnit>? = null
15+
) : CoroutineSocket(channel, readTimeout) {
1416

1517
enum class Method { NO_AUTH, USER_PASS }
1618

src/test/kotlin/Core.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ suspend fun runSocksHttpRequestTest(channel: AsynchronousSocketChannel) {
5252

5353
suspend fun runRawHttpRequestTest(channel: AsynchronousSocketChannel) {
5454
println("opening raw socket...")
55-
val raw = CoroutineSocket(channel)
55+
val raw = CoroutineSocket(channel, null)
5656
raw.connect(InetSocketAddress("api.ipify.org", 80))
5757
ByteBuffer.wrap(req).let { raw.write(it) }
5858

@@ -71,7 +71,7 @@ suspend fun runRawHttpRequestTest(channel: AsynchronousSocketChannel) {
7171
}
7272

7373
suspend fun runCoroutineSocketTest(channel: AsynchronousSocketChannel, dispatcher: CoroutineDispatcher) = withContext(dispatcher) {
74-
val socket = CoroutineSocket(channel)
74+
val socket = CoroutineSocket(channel, null)
7575

7676
launch {
7777
socket.connect(InetSocketAddress("localhost", 9999))

0 commit comments

Comments
 (0)