Ktor network utilities - tvOS x64 target implementation of asynchronous TCP/UDP sockets and related networking functionality for the Ktor framework
npx @tessl/cli install tessl/maven-io-ktor--ktor-network-tvosx64@3.2.0Ktor Network provides asynchronous TCP and UDP socket operations for the tvOS x64 platform using Kotlin coroutines. This package enables developers to create network applications with socket-based communication, supporting both client and server scenarios. It includes features for connection management, socket lifecycle handling, and integration with Kotlin's coroutine-based concurrency model.
implementation("io.ktor:ktor-network-tvosx64:3.2.0")import io.ktor.network.sockets.*
import io.ktor.network.selector.*
import kotlinx.io.*import io.ktor.network.sockets.*
import io.ktor.network.selector.*
import kotlinx.coroutines.*
import kotlinx.io.*
// Create a selector manager for managing socket operations
val selectorManager = SelectorManager()
// TCP Client Example
val socket = aSocket(selectorManager).tcp().connect("localhost", 8080)
val connection = socket.connection()
// Send data
connection.output.writeStringUtf8("Hello, Server!")
connection.output.flush()
// Receive data
val response = connection.input.readUTF8Line()
println("Server response: $response")
// Close connection
socket.close()
// TCP Server Example
val serverSocket = aSocket(selectorManager).tcp().bind("0.0.0.0", 8080)
while (true) {
val clientSocket = serverSocket.accept()
launch {
val connection = clientSocket.connection()
val request = connection.input.readUTF8Line()
connection.output.writeStringUtf8("Echo: $request")
connection.output.flush()
clientSocket.close()
}
}
// UDP Example
val udpSocket = aSocket(selectorManager).udp().bind()
val packet = Buffer().apply { writeString("Hello UDP") }
val datagram = Datagram(packet, InetSocketAddress("localhost", 9090))
udpSocket.send(datagram)
val receivedDatagram = udpSocket.receive()
// Clean up
selectorManager.close()Ktor Network is built around several key components:
ASocket, Socket, ServerSocket) that define socket lifecycle and operationsSocketBuilder, TcpSocketBuilder, UDPSocketBuilder) for creating and configuring socketsSelectorManager) for handling multiple socket operations concurrentlySocketAddress, InetSocketAddress, UnixSocketAddress) for different addressing schemesSocketOptions) for tuning socket behaviorCore socket creation functionality using builder pattern with configurable options. Supports both TCP and UDP sockets with comprehensive lifecycle management.
fun aSocket(selector: SelectorManager): SocketBuilder
class SocketBuilder {
fun tcp(): TcpSocketBuilder
fun udp(): UDPSocketBuilder
}TCP client and server socket operations with support for connection establishment, data transmission, and server socket binding.
class TcpSocketBuilder {
suspend fun connect(hostname: String, port: Int): Socket
suspend fun connect(remoteAddress: SocketAddress): Socket
suspend fun bind(hostname: String = "0.0.0.0", port: Int = 0): ServerSocket
suspend fun bind(localAddress: SocketAddress? = null): ServerSocket
}
interface Socket : ReadWriteSocket, ABoundSocket, AConnectedSocket, CoroutineScope
interface ServerSocket : ASocket, ABoundSocket, Acceptable<Socket>UDP datagram socket operations supporting both bound and connected datagram sockets with packet-based communication.
class UDPSocketBuilder {
suspend fun bind(localAddress: SocketAddress? = null): BoundDatagramSocket
suspend fun connect(remoteAddress: SocketAddress, localAddress: SocketAddress? = null): ConnectedDatagramSocket
}
interface BoundDatagramSocket : ASocket, ABoundSocket, DatagramReadWriteChannel
interface ConnectedDatagramSocket : ASocket, ABoundSocket, AConnectedSocket, DatagramReadWriteChannelSocket address types and utilities for different addressing schemes including Internet addresses and Unix domain sockets.
sealed class SocketAddress
class InetSocketAddress(hostname: String, port: Int) : SocketAddress {
val hostname: String
val port: Int
fun resolveAddress(): ByteArray?
}
class UnixSocketAddress(path: String) : SocketAddress {
val path: String
companion object {
fun isSupported(): Boolean
}
}Comprehensive socket configuration options for tuning socket behavior including timeouts, buffer sizes, and protocol-specific options.
sealed class SocketOptions {
var typeOfService: TypeOfService
var reuseAddress: Boolean
var reusePort: Boolean
class TCPClientSocketOptions : PeerSocketOptions {
var noDelay: Boolean
var lingerSeconds: Int
var keepAlive: Boolean?
var socketTimeout: Long
}
class UDPSocketOptions : PeerSocketOptions {
var broadcast: Boolean
}
class AcceptorOptions : SocketOptions {
var backlogSize: Int
}
}Socket I/O operations using ByteChannel integration for reading and writing data with support for both stream-based and packet-based communication.
interface ReadWriteSocket : ASocket, AReadable, AWritable
interface AReadable {
fun attachForReading(channel: ByteChannel): WriterJob
}
interface AWritable {
fun attachForWriting(channel: ByteChannel): ReaderJob
}
class Connection(
val socket: Socket,
val input: ByteReadChannel,
val output: ByteWriteChannel
)Asynchronous I/O management for handling multiple socket operations concurrently using coroutine-based selection.
interface SelectorManager : CoroutineScope, Closeable {
fun notifyClosed(selectable: Selectable)
suspend fun select(selectable: Selectable, interest: SelectInterest)
}
fun SelectorManager(dispatcher: CoroutineContext = EmptyCoroutineContext): SelectorManager
enum class SelectInterest {
READ, WRITE, ACCEPT, CONNECT
}