On 7 Feb 2017, at 20:40, Daryle Walker <[email protected]> wrote:

> Do session tasks still use resume …?

Yes.

> If resume is used, how would you open a connection in implicit-SSL mode?

I’m not sure what you mean by “implicit-SSL mode” but:

* If you’re working with a protocol that assumes TLS (like HTTPS), call 
`startSecureConnection()` before doing any I/O on the stream

* If you’re working with a protocol that uses STARTTLS, there’s comments in 
<Foundation/NSURLSession.h> that explain how `startSecureConnection()` 
interacts with pending I/O.

Pasted in below is a simple example of how to use this API.  There’s a few 
lines that you can uncomment to test TLS.

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

---------------------------------------------------------------------------
@IBAction func startStopAction(_ sender: Any) {
    if let stream = self.stream {
        self.stop(stream: stream)
    } else {
        self.start()
    }
}

lazy var session: URLSession = {
    let config = URLSessionConfiguration.default
    return URLSession(configuration: config, delegate: self, delegateQueue: 
OperationQueue.main)
}()

var stream: URLSessionStreamTask? = nil

func start() {
    NSLog("start")

    // let stream = self.session.streamTask(withHostName: "imap.mail.me.com", 
port: 993)
    let stream = self.session.streamTask(withHostName: "sully.local.", port: 
12345)
    self.stream = stream
    stream.resume()
    // stream.startSecureConnection()
    
    self.startRead(stream: stream)
    
    
    let hello = "a001 NOOP\r\n".data(using: .utf8)!
    stream.write(hello, timeout: 60.0) { (error) in
        if let error = error {
            NSLog("write error")
        } else {
            NSLog("write complete")
        }
    }
}

func startRead(stream: URLSessionStreamTask) {
    stream.readData(ofMinLength: 1, maxLength: 2048, timeout: 60.0) { (data, 
eof, error) in
        if let error = error {
            NSLog("read error")
        }
        if let data = data {
            NSLog("read data %@", data as NSData)
        }
        NSLog("read eof %@", eof ? "true" : "false")
        if error == nil && !eof {
            self.startRead(stream: stream)
        }
    }
}

func stop(stream: URLSessionStreamTask) {
    NSLog("stop")
    stream.closeRead()
    stream.closeWrite()
    self.stream = nil
}
---------------------------------------------------------------------------


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Macnetworkprog mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/macnetworkprog/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to