Swift file descriptor library. fd is a minimal layer on-top of the underlying system file descriptor and socket APIs.
FileDescriptor is a protocol containing a single property (fileNumber).
protocol FileDescriptor {
var fileNumber: FileNumber { get }
}
There are various protocol extensions to FileDescriptor to add functionality.
You may close a file descriptor.
try descriptor.close()
You may use the select function to examine which file descriptors are ready for reading, writing or have error conditions.
let readable = try select(reads: [descriptor]).reads
You may read from a readable file descriptor.
let bytes = try descriptor.read(1024)
You may write to a writable file descriptor.
try descriptor.write([1])
You may use the pipe function to create a unidirectional data flow. The reader allows you to read data which was previously written to the writer.
let (reader, writer) = try pipe()
try writer.write([1])
let bytes = try reader.read(1)
A listener is a file descriptor which can accept connections.
let connection = try listener.accept()
let listener = try TCPListener(address: "127.0.0.1", port: 8000)
UNIXListener is a Unix domain socket listener.
let listener = try UNIXListener(path: "/tmp/my-unix-listener")
A connection conforms to FileDescriptor so you can use the previously
documented read
and write
capabilities.