0001    // Stream.swift
0002    //
0003    // Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
0004    //
0005    // Permission is hereby granted, free of charge, to any person obtaining a copy
0006    // of this software and associated documentation files (the "Software"), to deal
0007    // in the Software without restriction, including without limitation the rights
0008    // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0009    // copies of the Software, and to permit persons to whom the Software is
0010    // furnished to do so, subject to the following conditions:
0011    //
0012    // The above copyright notice and this permission notice shall be included in
0013    // all copies or substantial portions of the Software.
0014    //
0015    // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0016    // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0017    // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0018    // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0019    // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0020    // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0021    // THE SOFTWARE.
0022    
0023    import Foundation
0024    
0025    #if !os(watchOS)
0026    
0027    @available(iOS 9.0, OSX 10.11, tvOS 9.0, *)
0028    extension Manager {
0029        private enum Streamable
Stream.swift:34
    private func stream(streamable: Streamable) -> Request {
{ 0030 case Stream
Stream.swift:38
        case .Stream(let hostName, let port):
Stream.swift:68
        return stream(.Stream(hostName, port))
(String, Int) 0031 case NetService
Stream.swift:42
        case .NetService(let netService):
Stream.swift:79
        return stream(.NetService(netService))
(NSNetService) 0032 } 0033 0034 private func stream
Stream.swift:68
        return stream(.Stream(hostName, port))
Stream.swift:79
        return stream(.NetService(netService))
(streamable: Streamable) -> Request { 0035 var streamTask: NSURLSessionStreamTask! 0036 0037 switch streamable { 0038 case .Stream(let hostName, let port): 0039 dispatch_sync(queue) { 0040 streamTask = self.session.streamTaskWithHostName(hostName, port: port) 0041 } 0042 case .NetService(let netService): 0043 dispatch_sync(queue) { 0044 streamTask = self.session.streamTaskWithNetService(netService) 0045 } 0046 } 0047 0048 let request = Request(session: session, task: streamTask) 0049 0050 delegate[request.delegate.task] = request.delegate 0051 0052 if startRequestsImmediately { 0053 request.resume() 0054 } 0055 0056 return request 0057 } 0058 0059 /** 0060 Creates a request for bidirectional streaming with the given hostname and port. 0061 0062 - parameter hostName: The hostname of the server to connect to. 0063 - parameter port: The port of the server to connect to. 0064 0065 :returns: The created stream request. 0066 */ 0067 public func stream(hostName hostName: String, port: Int) -> Request { 0068 return stream(.Stream(hostName, port)) 0069 } 0070 0071 /** 0072 Creates a request for bidirectional streaming with the given `NSNetService`. 0073 0074 - parameter netService: The net service used to identify the endpoint. 0075 0076 - returns: The created stream request. 0077 */ 0078 public func stream(netService netService: NSNetService) -> Request { 0079 return stream(.NetService(netService)) 0080 } 0081 } 0082 0083 // MARK: - 0084 0085 @available(iOS 9.0, OSX 10.11, tvOS 9.0, *) 0086 extension Manager.SessionDelegate: NSURLSessionStreamDelegate { 0087 0088 // MARK: Override Closures 0089 0090 /// Overrides default behavior for NSURLSessionStreamDelegate method `URLSession:readClosedForStreamTask:`. 0091 public var streamTaskReadClosed
Stream.swift:139
        streamTaskReadClosed?(session, streamTask)
: ((NSURLSession, NSURLSessionStreamTask) -> Void)? { 0092 get { 0093 return _streamTaskReadClosed as? (NSURLSession, NSURLSessionStreamTask) -> Void 0094 } 0095 set { 0096 _streamTaskReadClosed = newValue 0097 } 0098 } 0099 0100 /// Overrides default behavior for NSURLSessionStreamDelegate method `URLSession:writeClosedForStreamTask:`. 0101 public var streamTaskWriteClosed
Stream.swift:149
        streamTaskWriteClosed?(session, streamTask)
: ((NSURLSession, NSURLSessionStreamTask) -> Void)? { 0102 get { 0103 return _streamTaskWriteClosed as? (NSURLSession, NSURLSessionStreamTask) -> Void 0104 } 0105 set { 0106 _streamTaskWriteClosed = newValue 0107 } 0108 } 0109 0110 /// Overrides default behavior for NSURLSessionStreamDelegate method `URLSession:betterRouteDiscoveredForStreamTask:`. 0111 public var streamTaskBetterRouteDiscovered
Stream.swift:159
        streamTaskBetterRouteDiscovered?(session, streamTask)
: ((NSURLSession, NSURLSessionStreamTask) -> Void)? { 0112 get { 0113 return _streamTaskBetterRouteDiscovered as? (NSURLSession, NSURLSessionStreamTask) -> Void 0114 } 0115 set { 0116 _streamTaskBetterRouteDiscovered = newValue 0117 } 0118 } 0119 0120 /// Overrides default behavior for NSURLSessionStreamDelegate method `URLSession:streamTask:didBecomeInputStream:outputStream:`. 0121 public var streamTaskDidBecomeInputStream
Stream.swift:176
        streamTaskDidBecomeInputStream?(session, streamTask, inputStream, outputStream)
: ((NSURLSession, NSURLSessionStreamTask, NSInputStream, NSOutputStream) -> Void)? { 0122 get { 0123 return _streamTaskDidBecomeInputStream as? (NSURLSession, NSURLSessionStreamTask, NSInputStream, NSOutputStream) -> Void 0124 } 0125 set { 0126 _streamTaskDidBecomeInputStream = newValue 0127 } 0128 } 0129 0130 // MARK: Delegate Methods 0131 0132 /** 0133 Tells the delegate that the read side of the connection has been closed. 0134 0135 - parameter session: The session. 0136 - parameter streamTask: The stream task. 0137 */ 0138 public func URLSession(session: NSURLSession, readClosedForStreamTask streamTask: NSURLSessionStreamTask) { 0139 streamTaskReadClosed?(session, streamTask) 0140 } 0141 0142 /** 0143 Tells the delegate that the write side of the connection has been closed. 0144 0145 - parameter session: The session. 0146 - parameter streamTask: The stream task. 0147 */ 0148 public func URLSession(session: NSURLSession, writeClosedForStreamTask streamTask: NSURLSessionStreamTask) { 0149 streamTaskWriteClosed?(session, streamTask) 0150 } 0151 0152 /** 0153 Tells the delegate that the system has determined that a better route to the host is available. 0154 0155 - parameter session: The session. 0156 - parameter streamTask: The stream task. 0157 */ 0158 public func URLSession(session: NSURLSession, betterRouteDiscoveredForStreamTask streamTask: NSURLSessionStreamTask) { 0159 streamTaskBetterRouteDiscovered?(session, streamTask) 0160 } 0161 0162 /** 0163 Tells the delegate that the stream task has been completed and provides the unopened stream objects. 0164 0165 - parameter session: The session. 0166 - parameter streamTask: The stream task. 0167 - parameter inputStream: The new input stream. 0168 - parameter outputStream: The new output stream. 0169 */ 0170 public func URLSession( 0171 session: NSURLSession, 0172 streamTask: NSURLSessionStreamTask, 0173 didBecomeInputStream inputStream: NSInputStream, 0174 outputStream: NSOutputStream) 0175 { 0176 streamTaskDidBecomeInputStream?(session, streamTask, inputStream, outputStream) 0177 } 0178 } 0179 0180 #endif 0181