FileDescriptor

public final class FileDescriptor

A file descriptor used to access a file or other input/output resource, such as a pipe or network socket.

  • File descriptor handle.

    Declaration

    Swift

    public private(set) var handle: Handle
  • File descriptor handle.

    Declaration

    Swift

    public typealias Handle = Int32
  • Standard input file descriptor

    Declaration

    Swift

    public static var standardInput = try! FileDescriptor(STDIN_FILENO)
  • Standard output file descriptor

    Declaration

    Swift

    public static var standardOutput = try! FileDescriptor(STDOUT_FILENO)
  • Standard error file descriptor

    Declaration

    Swift

    public static var standardError = try! FileDescriptor(STDERR_FILENO)
  • Creates a FileDescriptor from a file descriptor handle.

    Warning

    This operation will configure the file descriptor as non-blocking.

    Throws

    The following errors might be thrown:

    VeniceError.invalidFileDescriptor

    Thrown when the operation is performed on an invalid file descriptor.

    Declaration

    Swift

    public init(_ handle: Handle) throws

    Parameters

    fileDescriptor

    Previously opened file descriptor.

  • Reads from the file descriptor.

    Throws

    The following errors might be thrown:

    VeniceError.readFailed

    Thrown when read operation fails.

    VeniceError.invalidFileDescriptor

    Thrown when handle is not an open file descriptor.

    Declaration

    Swift

    public func read(
            _ buffer: UnsafeMutableRawBufferPointer,
            deadline: Deadline
        ) throws -> UnsafeRawBufferPointer

    Parameters

    buffer

    Buffer in which the data will be read to.

    deadline

    deadline is a point in time when the operation should timeout. Use the .fromNow() function to get the current point in time. Use .immediate if the operation needs to be performed without blocking. Use .never to allow the operation to block forever if needed.

    Return Value

    Buffer containing the amount of bytes read.

  • Writes to the file descriptor.

    Throws

    The following errors might be thrown:

    VeniceError.writeFailed

    Thrown when write operation fails.

    VeniceError.invalidFileDescriptor

    Thrown when handle is not an open file descriptor.

    Declaration

    Swift

    public func write(_ buffer: UnsafeRawBufferPointer, deadline: Deadline) throws

    Parameters

    buffer

    Buffer which will be written to the file descriptor.

    deadline

    deadline is a point in time when the operation should timeout. Use the .fromNow() function to get the current point in time. Use .immediate if the operation needs to be performed without blocking. Use .never to allow the operation to block forever if needed.

  • Closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks held on the file it was associated with, and owned by the process, are removed (regardless of the file descriptor that was used to obtain the lock).

    Warning

    If handle is the last file descriptor referring to the underlying open file description, the resources associated with the open file description are freed; if the file descriptor was the last reference to a file which has been removed using unlink, the file is deleted.

    Throws

    The following errors might be thrown:

    VeniceError.invalidFileDescriptor

    Thrown when handle is not an open file descriptor.

    Declaration

    Swift

    public func close() throws
  • Detaches the underlying handle. After detach any operation on the FileDescriptor will throw an error.

    Declaration

    Swift

    @discardableResult public func detach() throws -> Handle

    Return Value

    The underlying file descriptor.

  • Waits for the file descriptor to become either readable/writable or to get into an error state. Either case leads to a successful return from the function. To distinguish the two outcomes, follow up with a read/write operation on the file descriptor.

    Throws

    The following errors might be thrown:

    VeniceError.invalidFileDescriptor

    Thrown when the operation is performed on an invalid file descriptor.

    VeniceError.canceledCoroutine

    Thrown when the operation is performed within a canceled coroutine.

    VeniceError.fileDescriptorBlockedInAnotherCoroutine

    Thrown when another coroutine is already blocked on poll with this file descriptor.

    VeniceError.deadlineReached

    Thrown when the operation reaches the deadline.

    Declaration

    Swift

    public static func poll(_ handle: Handle, event: PollEvent, deadline: Deadline) throws

    Parameters

    event

    Use .read to wait for the file descriptor to become readable. Use .write to wait for the file descriptor to become writable.

    deadline

    deadline is a point in time when the operation should timeout. Use the .fromNow() function to get the current point in time. Use .immediate if the operation needs to be performed without blocking. Use .never to allow the operation to block forever if needed.

  • Erases cached info about a file descriptor.

    This function drops any state that Venice associates with the file descriptor.

    Warning

    clean has to be called with file descriptors provided by third-party libraries, just before returning them back to their original owners. Otherwise the behavior is undefined.

    Declaration

    Swift

    public static func clean(_ handle: Handle)
  • Event used to poll file descriptors for reading or writing.

    See more

    Declaration

    Swift

    public enum PollEvent