
jvmMain.com.ditchoom.socket.nio.NioClientSocket.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of socket-jvm Show documentation
Show all versions of socket-jvm Show documentation
Simple multiplatform kotlin coroutines based socket.
package com.ditchoom.socket.nio
import com.ditchoom.buffer.AllocationZone
import com.ditchoom.socket.ClientToServerSocket
import com.ditchoom.socket.SocketException
import com.ditchoom.socket.nio.util.aConfigureBlocking
import com.ditchoom.socket.nio.util.buildInetAddress
import com.ditchoom.socket.nio.util.connect
import com.ditchoom.socket.nio.util.openSocketChannel
import java.net.InetSocketAddress
import kotlin.time.Duration
class NioClientSocket(
allocationZone: AllocationZone,
blocking: Boolean = true
) : BaseClientSocket(allocationZone, blocking), ClientToServerSocket {
override suspend fun open(
port: Int,
timeout: Duration,
hostname: String?
) {
val socketAddress = buildInetAddress(port, hostname)
val socketChannel = openSocketChannel()
socketChannel.aConfigureBlocking(blocking)
this.socket = socketChannel
if (!socketChannel.connect(socketAddress, selector, timeout)) {
throw SocketException("Failed to connect client ${(socketAddress as? InetSocketAddress)?.port} $socketChannel")
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy