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

nativeMain.kotlinx.io.streams.PosixIo.kt Maven / Gradle / Ivy

There is a newer version: 0.1.16
Show newest version
@file:Suppress("EXPERIMENTAL_FEATURE_WARNING")

package kotlinx.io.streams

import kotlinx.cinterop.*
import kotlinx.io.core.*
import kotlinx.io.internal.utils.*
import platform.posix.*

@ExperimentalIoApi
inline fun  CPointer.use(block: (CPointer) -> R): R {
    return try {
        block(this)
    } finally {
        fclose(this) // TODO handle errors
    }
}

@ExperimentalIoApi
fun fwrite(buffer: IoBuffer, stream: CPointer): size_t {
    var written: size_t = 0u

    buffer.readDirect { pointer ->
        val result = fwrite(pointer, 1.convert(), buffer.readRemaining.convert(), stream)
        written = result

        // it is completely safe to convert since the returned value will be never greater than Int.MAX_VALUE
        result.convert()
    }

    return written
}

@ExperimentalIoApi
fun write(fildes: Int, buffer: IoBuffer): ssize_t {
    var written: ssize_t = 0

    buffer.readDirect { pointer ->
        val result = write(fildes, pointer, buffer.readRemaining.convert())
        written = result.convert()

        // it is completely safe to convert since the returned value will be never greater than Int.MAX_VALUE
        // however the returned value could be -1 so clamp it
        result.convert().coerceAtLeast(0)
    }

    return written
}

@ExperimentalIoApi
fun send(socket: KX_SOCKET, buffer: IoBuffer, flags: Int): ssize_t {
    var written: ssize_t = 0

    buffer.readDirect { pointer ->
        val result = send(socket, pointer, buffer.readRemaining.convert(), flags)
        written = result.convert()

        // it is completely safe to convert since the returned value will be never greater than Int.MAX_VALUE
        // however the returned value could be -1 so clamp it
        result.convert().coerceAtLeast(0)
    }

    return written
}

@ExperimentalIoApi
fun fread(buffer: IoBuffer, stream: CPointer): size_t {
    var bytesRead: size_t = 0u

    buffer.writeDirect { pointer ->
        val result = fread(pointer, 1.convert(), buffer.writeRemaining.convert(), stream)
        bytesRead = result

        // it is completely safe to convert since the returned value will be never greater than Int.MAX_VALUE
        result.convert()
    }

    return bytesRead
}

@ExperimentalIoApi
fun read(fildes: Int, buffer: IoBuffer): ssize_t {
    var bytesRead: ssize_t = 0

    buffer.writeDirect { pointer ->
        val size = minOf(
            ssize_t.MAX_VALUE.toULong(),
            SSIZE_MAX.toULong(),
            buffer.writeRemaining.toULong()
        ).convert()

        val result = read(fildes, pointer, size.convert())
        bytesRead = result.convert()

        // it is completely safe to convert since the returned value will be never greater than Int.MAX_VALUE
        // however the returned value could be -1 so clamp it
        result.convert().coerceAtLeast(0)
    }

    return bytesRead
}

@ExperimentalIoApi
fun recv(socket: KX_SOCKET, buffer: IoBuffer, flags: Int): ssize_t {
    var bytesRead: ssize_t = 0

    buffer.writeDirect { pointer ->
        val result = recv(socket, pointer, buffer.writeRemaining.convert(), flags)
        bytesRead = result.convert()

        // it is completely safe to convert since the returned value will be never greater than Int.MAX_VALUE
        // however the returned value could be -1 so clamp it
        result.convert().coerceAtLeast(0)
    }

    return bytesRead
}

@ExperimentalIoApi
fun recvfrom(
    socket: KX_SOCKET,
    buffer: IoBuffer,
    flags: Int,
    addr: CValuesRef,
    addr_len: CValuesRef
): ssize_t {
    var bytesRead: ssize_t = 0

    buffer.writeDirect { pointer ->
        val result = recvfrom(socket, pointer, buffer.writeRemaining.convert(), flags, addr, addr_len)
        bytesRead = result.convert()

        // it is completely safe to convert since the returned value will be never greater than Int.MAX_VALUE
        // however the returned value could be -1 so clamp it
        result.convert().coerceAtLeast(0)
    }

    return bytesRead
}

@ExperimentalIoApi
fun sendto(
    socket: KX_SOCKET, buffer: IoBuffer, flags: Int,
    addr: CValuesRef,
    addr_len: KX_SOCKADDR_LEN
): ssize_t {
    var written: ssize_t = 0

    buffer.readDirect { pointer ->
        val result = sendto(socket, pointer, buffer.readRemaining.convert(), flags, addr, addr_len)
        written = result.convert()

        // it is completely safe to convert since the returned value will be never greater than Int.MAX_VALUE
        // however the returned value could be -1 so clamp it
        result.convert().coerceAtLeast(0)
    }

    return written
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy