IncomingSocketHandler

public class IncomingSocketHandler

This class handles incoming sockets to the HTTPServer. The data sent by the client is read and passed to the current IncomingDataProcessor.

Note

The IncomingDataProcessor can change due to an Upgrade request.

Note

This class uses different underlying technologies depending on:

1. On Linux, if no special compile time options are specified, epoll is used
2. On OSX, DispatchSource is used
3. On Linux, if the compile time option -Xswiftc -DGCD_ASYNCH is specified,
   DispatchSource is used, as it is used on OSX.

Usage Example:

 func upgrade(handler: IncomingSocketHandler, request: ServerRequest, response: ServerResponse) -> (IncomingSocketProcessor?, Data?, String?) {
     let (processor, responseText) = upgrade(handler: handler, request: request, response: response)

     if let responseText = responseText {
         return (processor, responseText.data(using: .utf8), "text/plain")
     }
     return (processor, nil, nil)
 }
  • The IncomingSocketProcessor instance that processes data read from the underlying socket.

    Usage Example:

    processor?.inProgress = false
    

    Declaration

    Swift

    public var processor: IncomingSocketProcessor?
  • Handle data read in while the processor couldn’t process it, if there is any

    Note

    On Linux, the IncomingSocketManager should call handleBufferedReadDataHelper directly.

    Usage Example:

    handler?.handleBufferedReadData()
    

    Declaration

    Swift

    public func handleBufferedReadData()
  • Write as much data to the socket as possible, buffering the rest

    Usage Example:

    try response.write(from: "No protocol specified in the Upgrade header")
    

    Declaration

    Swift

    public func write(from data: NSData)

    Parameters

    data

    The NSData object containing the bytes to write to the socket.

  • Write a sequence of bytes in an array to the socket

    Usage Example:

    processor.write(from: utf8, length: utf8Length)
    

    Declaration

    Swift

    public func write(from bytes: UnsafeRawPointer, length: Int)

    Parameters

    from

    An UnsafeRawPointer to the sequence of bytes to be written to the socket.

    length

    The number of bytes to write to the socket.

  • If there is data waiting to be written, set a flag and the socket will be closed when all the buffered data has been written. Otherwise, immediately close the socket.

    Usage Example:

    handler?.prepareToClose()
    

    Declaration

    Swift

    public func prepareToClose()