forked from ktorio/ktor
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! KTOR-1159 Add support for UDP sockets on native
- Loading branch information
1 parent
0a8a211
commit a09e961
Showing
2 changed files
with
92 additions
and
39 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
ktor-network/posix/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,89 @@ | ||
/* | ||
* 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.errors.* | ||
import kotlinx.cinterop.* | ||
import kotlinx.coroutines.* | ||
import kotlinx.coroutines.channels.* | ||
import kotlinx.coroutines.selects.* | ||
import kotlinx.coroutines.sync.* | ||
import platform.posix.* | ||
|
||
internal class DatagramSendChannel( | ||
val descriptor: Int, | ||
val socket: DatagramSocketImpl | ||
) : SendChannel<Datagram> { | ||
@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 (socket.isClosed) { | ||
// return false | ||
// } | ||
// | ||
// socket.close() | ||
return true | ||
} | ||
|
||
|
||
override fun offer(element: Datagram): Boolean { | ||
TODO("not implemented") | ||
} | ||
|
||
override suspend fun send(element: Datagram) { | ||
lock.withLock { | ||
sendImpl(element) | ||
} | ||
} | ||
|
||
private tailrec suspend fun sendImpl( | ||
datagram: Datagram, | ||
bytes: ByteArray = datagram.packet.readBytes() | ||
) { | ||
var bytesWritten: Int? = null | ||
bytes.usePinned { pinned -> | ||
datagram.address.address.nativeAddress { address, addressSize -> | ||
bytesWritten = sendto( | ||
descriptor, | ||
pinned.addressOf(0), | ||
bytes.size.convert(), | ||
0, | ||
address, | ||
addressSize | ||
).toInt() | ||
} | ||
} | ||
when (bytesWritten ?: error("bytesWritten cannot be null")) { | ||
0 -> throw IOException("Failed writing to closed socket") | ||
-1 -> { | ||
if (errno == EAGAIN) { | ||
socket.selector.select(socket.selectable, SelectInterest.WRITE) | ||
sendImpl(datagram, bytes) | ||
} else { | ||
throw PosixException.forErrno() | ||
} | ||
} | ||
} | ||
} | ||
|
||
override val onSend: SelectClause2<Datagram, SendChannel<Datagram>> | ||
get() = TODO("[DatagramSendChannel] doesn't support [onSend] select clause") | ||
|
||
@ExperimentalCoroutinesApi | ||
override fun invokeOnClose(handler: (cause: Throwable?) -> Unit) { | ||
TODO("[DatagramSendChannel] doesn't support [invokeOnClose] operation.") | ||
} | ||
} |
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