Transport.swift 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Transport.swift
  4. // Starscream
  5. //
  6. // Created by Dalton Cherry on 1/23/19.
  7. // Copyright © 2019 Vluxe. All rights reserved.
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //
  21. //////////////////////////////////////////////////////////////////////////////////////////////////
  22. import Foundation
  23. public enum ConnectionState {
  24. /// Ready connections can send and receive data
  25. case connected
  26. /// Waiting connections have not yet been started, or do not have a viable network
  27. case waiting
  28. /// Cancelled connections have been invalidated by the client and will send no more events
  29. case cancelled
  30. /// Failed connections are disconnected and can no longer send or receive data
  31. case failed(Error?)
  32. /// Viability (connection status) of the connection has updated
  33. /// e.g. connection is down, connection came back up, etc.
  34. case viability(Bool)
  35. /// Connection ca be upgraded to wifi from cellular.
  36. /// You should consider reconnecting to take advantage of this.
  37. case shouldReconnect(Bool)
  38. /// Received data
  39. case receive(Data)
  40. /// Remote peer has closed the network connection.
  41. case peerClosed
  42. }
  43. public protocol TransportEventClient: AnyObject {
  44. func connectionChanged(state: ConnectionState)
  45. }
  46. public protocol Transport: AnyObject {
  47. func register(delegate: TransportEventClient)
  48. func connect(url: URL, timeout: Double, certificatePinning: CertificatePinning?)
  49. func disconnect()
  50. func write(data: Data, completion: @escaping ((Error?) -> ()))
  51. var usingTLS: Bool { get }
  52. }