All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonJvmMain.com.ditchoom.socket.nio.util.SocketAddressExtensions.kt Maven / Gradle / Ivy

package com.ditchoom.socket.nio.util

import com.ditchoom.socket.SocketUnknownHostException
import java.net.InetAddress
import java.net.InetSocketAddress
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

suspend fun buildInetAddress(
    port: Int,
    hostname: String?,
): InetSocketAddress {
    return if (hostname != null) {
        try {
            InetSocketAddress(hostname.asInetAddress(), port)
        } catch (e: Exception) {
            throw SocketUnknownHostException(hostname, cause = e)
        }
    } else {
        suspendCoroutine {
            try {
                it.resume(InetSocketAddress(InetAddress.getLocalHost(), port))
            } catch (e: Exception) {
                it.resumeWithException(
                    SocketUnknownHostException(
                        "hostname is null",
                        cause = e,
                    ),
                )
            }
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy