-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-1166 Fix UDP socket send exception (#2164)
* KTOR-1166 Fix UDP socket send exception
- Loading branch information
Showing
6 changed files
with
397 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
157 changes: 157 additions & 0 deletions
157
ktor-network/jvm/src/io/ktor/network/sockets/DatagramSendChannel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
/* | ||
* Copyright 2014-2020 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package io.ktor.network.sockets | ||
|
||
import io.ktor.network.selector.* | ||
import io.ktor.network.util.* | ||
import io.ktor.utils.io.core.* | ||
import io.ktor.utils.io.pool.* | ||
import kotlinx.atomicfu.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.channels.* | ||
import kotlinx.coroutines.selects.* | ||
import kotlinx.coroutines.sync.* | ||
import java.net.* | ||
import java.nio.* | ||
import java.nio.channels.* | ||
import kotlin.coroutines.* | ||
|
||
private val CLOSED: (Throwable?) -> Unit = {} | ||
private val CLOSED_INVOKED: (Throwable?) -> Unit = {} | ||
|
||
internal class DatagramSendChannel( | ||
val channel: DatagramChannel, | ||
val socket: DatagramSocketImpl | ||
) : SendChannel<Datagram> { | ||
private val onCloseHandler = atomic<((Throwable?) -> Unit)?>(null) | ||
private val closed = atomic(false) | ||
private val closedCause = atomic<Throwable?>(null) | ||
|
||
@ExperimentalCoroutinesApi | ||
override val isClosedForSend: Boolean | ||
get() = socket.isClosed | ||
|
||
@ExperimentalCoroutinesApi | ||
override val isFull: Boolean | ||
get() = if (isClosedForSend) false else lock.isLocked | ||
|
||
private val lock = Mutex() | ||
|
||
override fun close(cause: Throwable?): Boolean { | ||
if (!closed.compareAndSet(false, true)) { | ||
return false | ||
} | ||
|
||
closedCause.value = cause | ||
|
||
if (!socket.isClosed) { | ||
socket.close() | ||
} | ||
|
||
closeAndCheckHandler() | ||
|
||
return true | ||
} | ||
|
||
|
||
override fun offer(element: Datagram): Boolean { | ||
if (!lock.tryLock()) return false | ||
|
||
var result = false | ||
|
||
try { | ||
DefaultDatagramByteBufferPool.useInstance { buffer -> | ||
element.packet.copy().readAvailable(buffer) | ||
result = channel.send(buffer, element.address) == 0 | ||
} | ||
} finally { | ||
lock.unlock() | ||
} | ||
|
||
if (result) { | ||
element.packet.release() | ||
} | ||
|
||
return result | ||
} | ||
|
||
override suspend fun send(element: Datagram) { | ||
lock.withLock { | ||
withContext(Dispatchers.IO) { | ||
DefaultDatagramByteBufferPool.useInstance { buffer -> | ||
element.writeMessageTo(buffer) | ||
|
||
val rc = channel.send(buffer, element.address) | ||
if (rc != 0) { | ||
socket.interestOp(SelectInterest.WRITE, false) | ||
return@useInstance | ||
} | ||
|
||
sendSuspend(buffer, element.address) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private suspend fun sendSuspend(buffer: ByteBuffer, address: SocketAddress) { | ||
while (true) { | ||
socket.interestOp(SelectInterest.WRITE, true) | ||
socket.selector.select(socket, SelectInterest.WRITE) | ||
|
||
if (channel.send(buffer, address) != 0) { | ||
socket.interestOp(SelectInterest.WRITE, false) | ||
break | ||
} | ||
} | ||
} | ||
|
||
override val onSend: SelectClause2<Datagram, SendChannel<Datagram>> | ||
get() = TODO("[DatagramSendChannel] doesn't support [onSend] select clause") | ||
|
||
@ExperimentalCoroutinesApi | ||
override fun invokeOnClose(handler: (cause: Throwable?) -> Unit) { | ||
if (onCloseHandler.compareAndSet(null, handler)) { | ||
return | ||
} | ||
|
||
if (onCloseHandler.value === CLOSED) { | ||
require(onCloseHandler.compareAndSet(CLOSED, CLOSED_INVOKED)) | ||
handler(closedCause.value) | ||
return | ||
} | ||
|
||
failInvokeOnClose(onCloseHandler.value) | ||
} | ||
|
||
private fun closeAndCheckHandler() { | ||
while (true) { | ||
val handler = onCloseHandler.value | ||
if (handler === CLOSED_INVOKED) break | ||
if (handler == null) { | ||
if (onCloseHandler.compareAndSet(null, CLOSED)) break | ||
continue | ||
} | ||
|
||
require(onCloseHandler.compareAndSet(handler, CLOSED_INVOKED)) | ||
handler(closedCause.value) | ||
break | ||
} | ||
} | ||
} | ||
|
||
private fun failInvokeOnClose(handler: ((cause: Throwable?) -> Unit)?) { | ||
val message = if (handler === CLOSED_INVOKED) { | ||
"Another handler was already registered and successfully invoked" | ||
} else { | ||
"Another handler was already registered: $handler" | ||
} | ||
|
||
throw IllegalStateException(message) | ||
} | ||
|
||
private fun Datagram.writeMessageTo(buffer: ByteBuffer) { | ||
packet.readAvailable(buffer) | ||
buffer.flip() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.