UDPSendReceiveServer

class UDPSendReceiveServer @JvmOverloads constructor(targetAddress: InetAddress, sendPort: Int, listenPort: Int, receiveBufferBytes: Int = MultiConnectionUDPServer.DEFAULT_RECEIVE_BUFFER_BYTES) : AutoCloseable(source)

A simple bidirectional UDP socket pair: one socket for sending to a fixed target address/port, and a separate socket bound to a local port for asynchronously receiving incoming datagrams on a background thread.

Every fallible operation returns a Result; the only exception is close, which must return Unit to satisfy AutoCloseable and therefore logs its failures instead. Use shutDown when you want the outcome as a value.

Maximum payload

A received datagram is decoded whole up to 65507 bytes (the maximum UDP payload over IPv4, and the default receiveBufferBytes). A datagram larger than the configured buffer is delivered truncated, not rejected (a WARN is logged). For real-network use keep frames under the path MTU (~1200 bytes) to avoid IP fragmentation and the loss it brings.

Parameters

targetAddress

the address messages sent via send are delivered to

sendPort

the port on targetAddress that messages sent via send are delivered to

listenPort

the local port this server binds to receive incoming datagrams

receiveBufferBytes

size of the datagram receive buffer, 512..65507; defaults to 65507

No DeliveryMode / UdpChannel surface, by design

This type deliberately has no reliable-ordered channel, and never will. It is a standalone two-socket primitive (sendSocket plus a separately-bound listenSocket) with no handshake and no registration - there is no Connection here, and therefore no registered peer to key a ReliableChannelEngine or a retransmit schedule to. A reliable channel needs both; giving this type one would mean standing up a second, parallel reliability stack for a primitive nobody builds a session on. See docs/webtools-udp-architecture.md for the full reasoning.

Constructors

Link copied to clipboard
constructor(targetAddress: InetAddress, sendPort: Int, listenPort: Int, receiveBufferBytes: Int = MultiConnectionUDPServer.DEFAULT_RECEIVE_BUFFER_BYTES)

Types

Link copied to clipboard
object Companion

Holds the logger and the private constant the instances share; it exposes no public member.

Functions

Link copied to clipboard
open override fun close()

AutoCloseable entry point, so this server can be used with use { }. Delegates to shutDown and discards the outcome - the interface fixes the return type as Unit, so failures are only logged. Call shutDown directly if you need to react to them.

Link copied to clipboard
fun send(byteArray: ByteArray): Result<Unit>

Sends raw bytes to targetAddress:sendPort.

fun send(message: String): Result<Unit>

Encodes message as UTF-8 and sends it to targetAddress:sendPort.

Link copied to clipboard

Stops listening, closes both the send and listen sockets, and waits (up to LISTENER_JOIN_TIMEOUT_MILLIS) for the listener thread to terminate.

Link copied to clipboard
fun startListening(onMessage: (message: String, senderAddress: InetAddress) -> Unit): Result<Unit>

Starts a background thread that blocks on listenSocket's receive, decoding each incoming datagram as a UTF-8 string and passing it to onMessage along with the sender's address.

Link copied to clipboard

Signals the background listener loop to stop and closes listenSocket, interrupting any in-progress blocking receive() call.