Stream.swift 6.5 KB

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