ServerContext.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2024, 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. /// Additional information about an RPC handled by a server.
  17. public struct ServerContext: Sendable {
  18. /// Protocol used to help identify transport specific context fields
  19. public protocol TransportSpecific: Sendable {}
  20. /// A description of the method being called.
  21. public var descriptor: MethodDescriptor
  22. /// A description of the remote peer.
  23. ///
  24. /// The format of the description should follow the pattern "<transport>:<address>" where
  25. /// "<transport>" indicates the underlying network transport (such as "ipv4", "unix", or
  26. /// "in-process"). This is a guideline for how descriptions should be formatted; different
  27. /// implementations may not follow this format so you shouldn't make assumptions based on it.
  28. ///
  29. /// Some examples include:
  30. /// - "ipv4:127.0.0.1:31415",
  31. /// - "ipv6:[::1]:443",
  32. /// - "in-process:27182".
  33. public var remotePeer: String
  34. /// A description of the local peer.
  35. ///
  36. /// The format of the description should follow the pattern "<transport>:<address>" where
  37. /// "<transport>" indicates the underlying network transport (such as "ipv4", "unix", or
  38. /// "in-process"). This is a guideline for how descriptions should be formatted; different
  39. /// implementations may not follow this format so you shouldn't make assumptions based on it.
  40. ///
  41. /// Some examples include:
  42. /// - "ipv4:127.0.0.1:31415",
  43. /// - "ipv6:[::1]:443",
  44. /// - "in-process:27182".
  45. public var localPeer: String
  46. /// An optional field for transports to store specific data
  47. ///
  48. /// Refer to the transport documentation to understand what type of
  49. /// value this field will contain, if any.
  50. ///
  51. /// An example of what this field can be used for, would be to store
  52. /// things like a peer certificate from a mTLS connection
  53. public var transportSpecific: (any TransportSpecific)?
  54. /// A handle for checking the cancellation status of an RPC.
  55. public var cancellation: RPCCancellationHandle
  56. /// Create a new server context.
  57. ///
  58. /// - Parameters:
  59. /// - descriptor: A description of the method being called.
  60. /// - remotePeer: A description of the remote peer.
  61. /// - localPeer: A description of the local peer.
  62. /// - cancellation: A cancellation handle. You can create a cancellation handle
  63. /// using ``withServerContextRPCCancellationHandle(_:)``.
  64. public init(
  65. descriptor: MethodDescriptor,
  66. remotePeer: String,
  67. localPeer: String,
  68. cancellation: RPCCancellationHandle
  69. ) {
  70. self.descriptor = descriptor
  71. self.remotePeer = remotePeer
  72. self.localPeer = localPeer
  73. self.cancellation = cancellation
  74. }
  75. }