CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-io-ktor--ktor-network-tvosx64

Ktor network utilities - tvOS x64 target implementation of asynchronous TCP/UDP sockets and related networking functionality for the Ktor framework

Pending
Overview
Eval results
Files

socket-configuration.mddocs/

Socket Configuration

Comprehensive socket configuration options for tuning socket behavior including timeouts, buffer sizes, and protocol-specific options.

Capabilities

Base Socket Options

Base configuration options available for all socket types.

/**
 * Socket options builder - base class for all socket configuration
 */
sealed class SocketOptions {
    /**
     * ToS value, TypeOfService.UNDEFINED by default, may not work with old JDK (will be silently ignored)
     */
    var typeOfService: TypeOfService = TypeOfService.UNDEFINED

    /**
     * SO_REUSEADDR option
     */
    var reuseAddress: Boolean = false

    /**
     * SO_REUSEPORT option, may not work with old JDK (will be silently ignored)
     */
    var reusePort: Boolean = false
}

TCP Server Socket Options

Configuration options specific to TCP server sockets.

/**
 * TCP server socket options
 */
class AcceptorOptions : SocketOptions {
    /**
     * Represents TCP server socket backlog size. When a client attempts to connect,
     * the request is added to the backlog until it will be accepted.
     * Once accept() is invoked, a client socket is removed from the backlog.
     * If the backlog is too small, it may overflow and upcoming requests will be
     * rejected by the underlying TCP implementation.
     * Default: 511
     */
    var backlogSize: Int = 511
}

Usage Example:

import io.ktor.network.sockets.*
import io.ktor.network.selector.*

val selectorManager = SelectorManager()

// Configure TCP server socket
val serverSocket = aSocket(selectorManager).tcp().bind("0.0.0.0", 8080) {
    backlogSize = 100
    reuseAddress = true
    reusePort = true
    typeOfService = TypeOfService.IPTOS_RELIABILITY
}

println("Server listening on port ${serverSocket.port}")
println("Backlog size: ${serverSocket.options.backlogSize}")

Peer Socket Options

Base configuration options for client sockets and peer connections.

/**
 * Represents TCP client or UDP socket options
 */
open class PeerSocketOptions : SocketOptions {
    /**
     * Socket outgoing buffer size (SO_SNDBUF), -1 or 0 to make system decide
     * Default: -1
     */
    var sendBufferSize: Int = -1

    /**
     * Socket incoming buffer size (SO_RCVBUF), -1 or 0 to make system decide
     * Default: -1
     */
    var receiveBufferSize: Int = -1
}

TCP Client Socket Options

Configuration options specific to TCP client sockets.

/**
 * Represents TCP client socket options
 */
class TCPClientSocketOptions : PeerSocketOptions {
    /**
     * TCP_NODELAY socket option, useful to disable Nagle algorithm
     * Default: true
     */
    var noDelay: Boolean = true

    /**
     * SO_LINGER option applied at socket close, not recommended to set to 0 however useful for debugging
     * Value of -1 is the default and means that it is not set and system-dependent
     * Default: -1
     */
    var lingerSeconds: Int = -1

    /**
     * SO_KEEPALIVE option is to enable/disable TCP keep-alive
     * Default: null (system default)
     */
    var keepAlive: Boolean? = null

    /**
     * Socket timeout (read and write) in milliseconds
     * Default: Long.MAX_VALUE (infinite)
     */
    var socketTimeout: Long = Long.MAX_VALUE
}

Usage Examples:

import io.ktor.network.sockets.*
import io.ktor.network.selector.*

val selectorManager = SelectorManager()

// Configure TCP client with performance options
val fastSocket = aSocket(selectorManager).tcp().connect("localhost", 8080) {
    noDelay = true              // Disable Nagle algorithm for low latency
    keepAlive = true            // Enable keep-alive
    socketTimeout = 30000       // 30 second timeout
    sendBufferSize = 64 * 1024  // 64KB send buffer
    receiveBufferSize = 64 * 1024 // 64KB receive buffer
}

// Configure TCP client with reliability options
val reliableSocket = aSocket(selectorManager).tcp().connect("remote-server.com", 443) {
    noDelay = false             // Enable Nagle for better throughput
    keepAlive = true            // Enable keep-alive
    lingerSeconds = 5           // Wait up to 5 seconds for data to be sent on close
    socketTimeout = 60000       // 60 second timeout
    typeOfService = TypeOfService.IPTOS_RELIABILITY
}

// Configure TCP client for debugging
val debugSocket = aSocket(selectorManager).tcp().connect("localhost", 8080) {
    noDelay = true
    lingerSeconds = 0           // Immediate close (useful for debugging)
    socketTimeout = 5000        // Short timeout for testing
}

UDP Socket Options

Configuration options specific to UDP sockets.

/**
 * Represents UDP socket options
 */
class UDPSocketOptions : PeerSocketOptions {
    /**
     * SO_BROADCAST socket option - enables broadcast datagram transmission
     * Default: false
     */
    var broadcast: Boolean = false
}

Usage Examples:

import io.ktor.network.sockets.*
import io.ktor.network.selector.*

val selectorManager = SelectorManager()

// Configure UDP socket for broadcast
val broadcastSocket = aSocket(selectorManager).udp().bind("0.0.0.0", 8080) {
    broadcast = true            // Enable broadcast
    reuseAddress = true         // Allow multiple processes to bind
    receiveBufferSize = 1024 * 1024 // 1MB receive buffer
}

// Configure UDP socket for unicast with large buffers
val unicastSocket = aSocket(selectorManager).udp().connect(
    InetSocketAddress("server.example.com", 8080)
) {
    broadcast = false
    sendBufferSize = 512 * 1024     // 512KB send buffer
    receiveBufferSize = 512 * 1024  // 512KB receive buffer
    typeOfService = TypeOfService.IPTOS_THROUGHPUT
}

Type of Service

Value class for IP Type of Service (ToS) field configuration.

/**
 * An inline class to hold a IP ToS value
 * @property value an unsigned byte IP_TOS value
 */
value class TypeOfService(val value: UByte) {
    /**
     * Creates ToS by integer value discarding extra high bits
     */
    constructor(value: Int) : this(value.toUByte())

    /**
     * Integer representation of this ToS
     */
    val intValue: Int

    companion object {
        val UNDEFINED: TypeOfService = TypeOfService(0u)
        val IPTOS_LOWCOST: TypeOfService = TypeOfService(0x02u)
        val IPTOS_RELIABILITY: TypeOfService = TypeOfService(0x04u)
        val IPTOS_THROUGHPUT: TypeOfService = TypeOfService(0x08u)
        val IPTOS_LOWDELAY: TypeOfService = TypeOfService(0x10u)
    }
}

Usage Example:

import io.ktor.network.sockets.*
import io.ktor.network.selector.*

val selectorManager = SelectorManager()

// Configure sockets with different ToS values for different use cases
val gameSocket = aSocket(selectorManager).tcp().connect("game-server.com", 7777) {
    typeOfService = TypeOfService.IPTOS_LOWDELAY  // Gaming requires low latency
    noDelay = true
    socketTimeout = 10000
}

val fileTransferSocket = aSocket(selectorManager).tcp().connect("file-server.com", 21) {
    typeOfService = TypeOfService.IPTOS_THROUGHPUT  // File transfer needs high throughput
    noDelay = false  // Allow Nagle for better bulk transfer
}

val backupSocket = aSocket(selectorManager).tcp().connect("backup-server.com", 22) {
    typeOfService = TypeOfService.IPTOS_RELIABILITY  // Backup needs reliability
    keepAlive = true
    socketTimeout = 300000  // 5 minute timeout
}

val costOptimizedSocket = aSocket(selectorManager).tcp().connect("service.com", 80) {
    typeOfService = TypeOfService.IPTOS_LOWCOST  // Cost-sensitive traffic
}

// Custom ToS value
val customSocket = aSocket(selectorManager).tcp().connect("custom-server.com", 8080) {
    typeOfService = TypeOfService(0x20)  // Custom ToS value
}

println("Game socket ToS: ${gameSocket.options.typeOfService.intValue}")

Configuration Patterns

Common configuration patterns for different use cases.

Complete Configuration Examples:

import io.ktor.network.sockets.*
import io.ktor.network.selector.*

val selectorManager = SelectorManager()

// High-performance TCP server
val highPerfServer = aSocket(selectorManager).tcp().bind("0.0.0.0", 8080) {
    backlogSize = 1000
    reuseAddress = true
    reusePort = true
    typeOfService = TypeOfService.IPTOS_THROUGHPUT
}

// Robust TCP client with timeouts
val robustClient = aSocket(selectorManager).tcp().connect("api.example.com", 443) {
    noDelay = true
    keepAlive = true
    socketTimeout = 30000
    lingerSeconds = 5
    sendBufferSize = 128 * 1024
    receiveBufferSize = 128 * 1024
    typeOfService = TypeOfService.IPTOS_RELIABILITY
}

// UDP multicast sender
val multicastSender = aSocket(selectorManager).udp().bind() {
    broadcast = true
    reuseAddress = true
    sendBufferSize = 256 * 1024
    typeOfService = TypeOfService.IPTOS_THROUGHPUT
}

// Debug-friendly configuration
val debugSocket = aSocket(selectorManager).tcp().connect("localhost", 8080) {
    noDelay = true
    lingerSeconds = 0     // Immediate close
    socketTimeout = 5000  // Short timeout
    keepAlive = false
}

// Memory-constrained configuration
val lightweightSocket = aSocket(selectorManager).tcp().connect("service.com", 80) {
    sendBufferSize = 8 * 1024    // Small buffers
    receiveBufferSize = 8 * 1024
    socketTimeout = 15000        // Shorter timeout
    typeOfService = TypeOfService.IPTOS_LOWCOST
}

Install with Tessl CLI

npx tessl i tessl/maven-io-ktor--ktor-network-tvosx64

docs

index.md

io-operations.md

selector-management.md

socket-addresses.md

socket-configuration.md

socket-creation.md

tcp-operations.md

udp-operations.md

tile.json