ServerTransport.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Copyright 2023, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /// A protocol server transport implementations must conform to.
  17. @available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
  18. public protocol ServerTransport: Sendable {
  19. typealias Inbound = RPCAsyncSequence<RPCRequestPart, any Error>
  20. typealias Outbound = RPCWriter<RPCResponsePart>.Closable
  21. /// Starts the transport.
  22. ///
  23. /// Implementations will typically bind to a listening port when this function is called
  24. /// and start accepting new connections. Each accepted inbound RPC stream will be handed over to
  25. /// the provided `streamHandler` to handle accordingly.
  26. ///
  27. /// You can call ``stopListening()`` to stop the transport from accepting new streams. Existing
  28. /// streams must be allowed to complete naturally. However, transports may also enforce a grace
  29. /// period after which any open streams may be cancelled. You can also cancel the task running
  30. /// ``listen(_:)`` to abruptly close connections and streams.
  31. func listen(
  32. _ streamHandler: @escaping @Sendable (RPCStream<Inbound, Outbound>) async -> Void
  33. ) async throws
  34. /// Indicates to the transport that no new streams should be accepted.
  35. ///
  36. /// Existing streams are permitted to run to completion. However, the transport may also enforce
  37. /// a grace period, after which remaining streams are cancelled.
  38. func stopListening()
  39. }