Module: Perfect-Net
class NetNamedPipe : NetTCP
This sub-class of NetTCP handles networking over an AF_UNIX named pipe connection.
func convenience init(fd: Int32)
Initialize the object using an existing file descriptor.
func bind(address addr: String) throws
Bind the socket to the address path
- parameter address: The path on the file system at which to create and bind the socket
- throws: `PerfectError.NetworkError`
func connect(address addr: String, timeoutSeconds: Double, callBack: @escaping (NetNamedPipe?) -> ()) throws
Connect to the indicated server socket
- parameter address: The server socket file.
- parameter timeoutSeconds: The number of seconds to wait for the connection to complete. A timeout of negative one indicates that there is no timeout.
- parameter callBack: The closure which will be called when the connection completes. If the connection completes successfully then the current NetNamedPipe instance will be passed to the callback, otherwise, a nil object will be passed.
- returns: `PerfectError.NetworkError`
func sendFd(_ fd: Int32, callBack: @escaping (Bool) -> ()) throws
Send the existing opened file descriptor over the connection to the recipient
- parameter fd: The file descriptor to send
- parameter callBack: The callback to call when the send completes. The parameter passed will be `true` if the send completed without error.
- throws: `PerfectError.NetworkError`
func receiveFd(callBack cb: @escaping (Int32) -> ()) throws
Receive an existing opened file descriptor from the sender
- parameter callBack: The callback to call when the receive completes. The parameter passed will be the received file descriptor or invalidSocket.
- throws: `PerfectError.NetworkError`
func receiveNetTCP(callBack: @escaping (NetTCP?) -> ()) throws
Receive an existing opened `NetTCP` descriptor from the sender
- parameter callBack: The callback to call when the receive completes. The parameter passed will be the received `NetTCP` object or nil.
- throws: `PerfectError.NetworkError`
func receiveNetNamedPipe(callBack: @escaping (NetNamedPipe?) -> ()) throws
Receive an existing opened `NetNamedPipe` descriptor from the sender
- parameter callBack: The callback to call when the receive completes. The parameter passed will be the received `NetNamedPipe` object or nil.
- throws: `PerfectError.NetworkError`
class NetTCP
Provides an asynchronous IO wrapper around a file descriptor.
Fully realized for TCP socket types but can also serve as a base for sockets from other families, such as with `NetNamedPipe`/AF_UNIX.
func init()
Create a new object with an initially invalid socket file descriptor.
func convenience init(fd: Int32)
Creates an instance which will use the given file descriptor
- parameter fd: The pre-existing file descriptor
func initSocket(family: Int32)
Allocates a new socket if it has not already been done.
The functions `bind` and `connect` will call this method to ensure the socket has been allocated.
Sub-classes should override this function in order to create their specialized socket.
All sub-class sockets should be switched to utilize non-blocking IO by calling `SocketFileDescriptor.switchToNBIO()`.
func bind(port prt: UInt16, address: String = "0.0.0.0") throws
Bind the socket on the given port and optional local address
- parameter port: The port on which to bind
- parameter address: The the local address, given as a string, on which to bind. Defaults to "0.0.0.0".
- throws: PerfectError.NetworkError
func listen(backlog: Int32 = 128)
Switches the socket to server mode. Socket should have been previously bound using the `bind` function.
func close()
Shuts down and closes the socket.
The object may be reused.
func readBytesFully(count cnt: Int, timeoutSeconds: Double, completion: @escaping ([UInt8]?) -> ())
Read the indicated number of bytes and deliver them on the provided callback.
- parameter count: The number of bytes to read
- parameter timeoutSeconds: The number of seconds to wait for the requested number of bytes. A timeout value of negative one indicates that the request should have no timeout.
- parameter completion: The callback on which the results will be delivered. If the timeout occurs before the requested number of bytes have been read, a nil object will be delivered to the callback.
func readSomeBytes(count cnt: Int, completion: @escaping ([UInt8]?) -> ())
Read up to the indicated number of bytes and deliver them on the provided callback.
- parameter count: The maximum number of bytes to read.
- parameter completion: The callback on which to deliver the results. If an error occurs during the read then a nil object will be passed to the callback, otherwise, the immediately available number of bytes, which may be zero, will be passed.
func write(string strng: String, completion: @escaping (Int) -> ())
Write the string and call the callback with the number of bytes which were written.
- parameter s: The string to write. The string will be written based on its UTF-8 encoding.
- parameter completion: The callback which will be called once the write has completed. The callback will be passed the number of bytes which were successfuly written, which may be zero.
func write(bytes byts: [UInt8], completion: @escaping (Int) -> ())
Write the indicated bytes and call the callback with the number of bytes which were written.
- parameter bytes: The array of UInt8 to write.
- parameter completion: The callback which will be called once the write has completed. The callback will be passed the number of bytes which were successfuly written, which may be zero.
func writeFully(bytes byts: [UInt8]) -> Bool
Write the indicated bytes and return true if all data was sent.
- parameter bytes: The array of UInt8 to write.
func write(bytes: [UInt8], offsetBy: Int, count: Int, completion: @escaping (Int) -> ())
Write the indicated bytes and call the callback with the number of bytes which were written.
- parameter bytes: The array of UInt8 to write.
- parameter offsetBy: The offset within `bytes` at which to begin writing.
- parameter count: The number of bytes to write.
- parameter completion: The callback which will be called once the write has completed. The callback will be passed the number of bytes which were successfuly written, which may be zero.
func connect(address addrs: String, port: UInt16, timeoutSeconds: Double, callBack: @escaping (NetTCP?) -> ()) throws
Connect to the indicated server
- parameter address: The server's address, expressed as a string.
- parameter port: The port on which to connect.
- parameter timeoutSeconds: The number of seconds to wait for the connection to complete. A timeout of negative one indicates that there is no timeout.
- parameter callBack: The closure which will be called when the connection completes. If the connection completes successfully then the current NetTCP instance will be passed to the callback, otherwise, a nil object will be passed.
- returns: `PerfectError.NetworkError`
func accept(timeoutSeconds timeout: Double, callBack: @escaping (NetTCP?) -> ()) throws
Accept a new client connection and pass the result to the callback.
- parameter timeoutSeconds: The number of seconds to wait for a new connection to arrive. A timeout value of negative one indicates that there is no timeout.
- parameter callBack: The closure which will be called when the accept completes. the parameter will be a newly allocated instance of NetTCP which represents the client.
- returns: `PerfectError.NetworkError`
func forEachAccept(callBack: @escaping (NetTCP?) -> ())
Accept a series of new client connections and pass them to the callback. This function does not return outside of a catastrophic error.
- parameter callBack: The closure which will be called when the accept completes. the parameter will be a newly allocated instance of NetTCP which represents the client.