ServerContext.swift 3.2 KB

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