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 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 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 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.