Stream.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Stream.swift
  2. //
  3. // Copyright (c) 2014–2016 Alamofire Software Foundation (http://alamofire.org/)
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. import Foundation
  23. #if !os(watchOS)
  24. @available(iOS 9.0, OSX 10.11, tvOS 9.0, *)
  25. extension Manager {
  26. private enum Streamable {
  27. case Stream(String, Int)
  28. case NetService(NSNetService)
  29. }
  30. private func stream(streamable: Streamable) -> Request {
  31. var streamTask: NSURLSessionStreamTask!
  32. switch streamable {
  33. case .Stream(let hostName, let port):
  34. dispatch_sync(queue) {
  35. streamTask = self.session.streamTaskWithHostName(hostName, port: port)
  36. }
  37. case .NetService(let netService):
  38. dispatch_sync(queue) {
  39. streamTask = self.session.streamTaskWithNetService(netService)
  40. }
  41. }
  42. let request = Request(session: session, task: streamTask)
  43. delegate[request.delegate.task] = request.delegate
  44. if startRequestsImmediately {
  45. request.resume()
  46. }
  47. return request
  48. }
  49. /**
  50. Creates a request for bidirectional streaming with the given hostname and port.
  51. - parameter hostName: The hostname of the server to connect to.
  52. - parameter port: The port of the server to connect to.
  53. :returns: The created stream request.
  54. */
  55. public func stream(hostName hostName: String, port: Int) -> Request {
  56. return stream(.Stream(hostName, port))
  57. }
  58. /**
  59. Creates a request for bidirectional streaming with the given `NSNetService`.
  60. - parameter netService: The net service used to identify the endpoint.
  61. - returns: The created stream request.
  62. */
  63. public func stream(netService netService: NSNetService) -> Request {
  64. return stream(.NetService(netService))
  65. }
  66. }
  67. // MARK: -
  68. @available(iOS 9.0, OSX 10.11, tvOS 9.0, *)
  69. extension Manager.SessionDelegate: NSURLSessionStreamDelegate {
  70. // MARK: Override Closures
  71. /// Overrides default behavior for NSURLSessionStreamDelegate method `URLSession:readClosedForStreamTask:`.
  72. public var streamTaskReadClosed: ((NSURLSession, NSURLSessionStreamTask) -> Void)? {
  73. get {
  74. return _streamTaskReadClosed as? (NSURLSession, NSURLSessionStreamTask) -> Void
  75. }
  76. set {
  77. _streamTaskReadClosed = newValue
  78. }
  79. }
  80. /// Overrides default behavior for NSURLSessionStreamDelegate method `URLSession:writeClosedForStreamTask:`.
  81. public var streamTaskWriteClosed: ((NSURLSession, NSURLSessionStreamTask) -> Void)? {
  82. get {
  83. return _streamTaskWriteClosed as? (NSURLSession, NSURLSessionStreamTask) -> Void
  84. }
  85. set {
  86. _streamTaskWriteClosed = newValue
  87. }
  88. }
  89. /// Overrides default behavior for NSURLSessionStreamDelegate method `URLSession:betterRouteDiscoveredForStreamTask:`.
  90. public var streamTaskBetterRouteDiscovered: ((NSURLSession, NSURLSessionStreamTask) -> Void)? {
  91. get {
  92. return _streamTaskBetterRouteDiscovered as? (NSURLSession, NSURLSessionStreamTask) -> Void
  93. }
  94. set {
  95. _streamTaskBetterRouteDiscovered = newValue
  96. }
  97. }
  98. /// Overrides default behavior for NSURLSessionStreamDelegate method `URLSession:streamTask:didBecomeInputStream:outputStream:`.
  99. public var streamTaskDidBecomeInputStream: ((NSURLSession, NSURLSessionStreamTask, NSInputStream, NSOutputStream) -> Void)? {
  100. get {
  101. return _streamTaskDidBecomeInputStream as? (NSURLSession, NSURLSessionStreamTask, NSInputStream, NSOutputStream) -> Void
  102. }
  103. set {
  104. _streamTaskDidBecomeInputStream = newValue
  105. }
  106. }
  107. // MARK: Delegate Methods
  108. /**
  109. Tells the delegate that the read side of the connection has been closed.
  110. - parameter session: The session.
  111. - parameter streamTask: The stream task.
  112. */
  113. public func URLSession(session: NSURLSession, readClosedForStreamTask streamTask: NSURLSessionStreamTask) {
  114. streamTaskReadClosed?(session, streamTask)
  115. }
  116. /**
  117. Tells the delegate that the write side of the connection has been closed.
  118. - parameter session: The session.
  119. - parameter streamTask: The stream task.
  120. */
  121. public func URLSession(session: NSURLSession, writeClosedForStreamTask streamTask: NSURLSessionStreamTask) {
  122. streamTaskWriteClosed?(session, streamTask)
  123. }
  124. /**
  125. Tells the delegate that the system has determined that a better route to the host is available.
  126. - parameter session: The session.
  127. - parameter streamTask: The stream task.
  128. */
  129. public func URLSession(session: NSURLSession, betterRouteDiscoveredForStreamTask streamTask: NSURLSessionStreamTask) {
  130. streamTaskBetterRouteDiscovered?(session, streamTask)
  131. }
  132. /**
  133. Tells the delegate that the stream task has been completed and provides the unopened stream objects.
  134. - parameter session: The session.
  135. - parameter streamTask: The stream task.
  136. - parameter inputStream: The new input stream.
  137. - parameter outputStream: The new output stream.
  138. */
  139. public func URLSession(
  140. session: NSURLSession,
  141. streamTask: NSURLSessionStreamTask,
  142. didBecomeInputStream inputStream: NSInputStream,
  143. outputStream: NSOutputStream)
  144. {
  145. streamTaskDidBecomeInputStream?(session, streamTask, inputStream, outputStream)
  146. }
  147. }
  148. #endif