gRPC.swift 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. *
  3. * Copyright 2016, Google Inc.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #if SWIFT_PACKAGE
  34. import CgRPC
  35. #endif
  36. import Foundation // for String.Encoding
  37. /// Initializes gRPC system
  38. public func initialize() {
  39. grpc_init()
  40. }
  41. /// Shuts down gRPC system
  42. public func shutdown() {
  43. grpc_shutdown()
  44. }
  45. /// Returns version of underlying gRPC library
  46. ///
  47. /// Returns: gRPC version string
  48. public func version() -> String {
  49. return String(cString:grpc_version_string(), encoding:String.Encoding.utf8)!
  50. }
  51. /// Status codes for gRPC operations (replicated from status_code_enum.h)
  52. enum StatusCode: Int {
  53. /// Not an error; returned on success.
  54. case ok = 0
  55. /// The operation was cancelled (typically by the caller).
  56. case cancelled = 1
  57. /// Unknown error. An example of where this error may be returned is if a
  58. /// Status value received from another address space belongs to an error-space
  59. /// that is not known in this address space. Also errors raised by APIs that
  60. /// do not return enough error information may be converted to this error.
  61. case unknown = 2
  62. /// Client specified an invalid argument. Note that this differs from
  63. /// FAILED_PRECONDITION. INVALID_ARGUMENT indicates arguments that are
  64. /// problematic regardless of the state of the system (e.g., a malformed file
  65. /// name).
  66. case invalidArgument = 3
  67. /// Deadline expired before operation could complete. For operations that
  68. /// change the state of the system, this error may be returned even if the
  69. /// operation has completed successfully. For example, a successful response
  70. /// from a server could have been delayed long enough for the deadline to
  71. /// expire.
  72. case deadlineExceeded = 4
  73. /// Some requested entity (e.g., file or directory) was not found.
  74. case notFound = 5
  75. /// Some entity that we attempted to create (e.g., file or directory) already
  76. /// exists.
  77. case alreadyExists = 6
  78. /// The caller does not have permission to execute the specified operation.
  79. /// PERMISSION_DENIED must not be used for rejections caused by exhausting
  80. /// some resource (use RESOURCE_EXHAUSTED instead for those errors).
  81. /// PERMISSION_DENIED must not be used if the caller can not be identified
  82. /// (use UNAUTHENTICATED instead for those errors).
  83. case permissionDenied = 7
  84. /// The request does not have valid authentication credentials for the
  85. /// operation.
  86. case unauthenticated = 16
  87. /// Some resource has been exhausted, perhaps a per-user quota, or perhaps the
  88. /// entire file system is out of space.
  89. case resourceExhausted = 8
  90. /// Operation was rejected because the system is not in a state required for
  91. /// the operation's execution. For example, directory to be deleted may be
  92. /// non-empty, an rmdir operation is applied to a non-directory, etc.
  93. ///
  94. /// A litmus test that may help a service implementor in deciding
  95. /// between FAILED_PRECONDITION, ABORTED, and UNAVAILABLE:
  96. /// (a) Use UNAVAILABLE if the client can retry just the failing call.
  97. /// (b) Use ABORTED if the client should retry at a higher-level
  98. /// (e.g., restarting a read-modify-write sequence).
  99. /// (c) Use FAILED_PRECONDITION if the client should not retry until
  100. /// the system state has been explicitly fixed. E.g., if an "rmdir"
  101. /// fails because the directory is non-empty, FAILED_PRECONDITION
  102. /// should be returned since the client should not retry unless
  103. /// they have first fixed up the directory by deleting files from it.
  104. /// (d) Use FAILED_PRECONDITION if the client performs conditional
  105. /// REST Get/Update/Delete on a resource and the resource on the
  106. /// server does not match the condition. E.g., conflicting
  107. /// read-modify-write on the same resource.
  108. case failedPrecondition = 9
  109. /// The operation was aborted, typically due to a concurrency issue like
  110. /// sequencer check failures, transaction aborts, etc.
  111. ///
  112. /// See litmus test above for deciding between FAILED_PRECONDITION, ABORTED,
  113. /// and UNAVAILABLE.
  114. case aborted = 10
  115. /// Operation was attempted past the valid range. E.g., seeking or reading
  116. /// past end of file.
  117. ///
  118. /// Unlike INVALID_ARGUMENT, this error indicates a problem that may be fixed
  119. /// if the system state changes. For example, a 32-bit file system will
  120. /// generate INVALID_ARGUMENT if asked to read at an offset that is not in the
  121. /// range [0,2^32-1], but it will generate OUT_OF_RANGE if asked to read from
  122. /// an offset past the current file size.
  123. ///
  124. /// There is a fair bit of overlap between FAILED_PRECONDITION and
  125. /// OUT_OF_RANGE. We recommend using OUT_OF_RANGE (the more specific error)
  126. /// when it applies so that callers who are iterating through a space can
  127. /// easily look for an OUT_OF_RANGE error to detect when they are done.
  128. case outOfRange = 11
  129. /// Operation is not implemented or not supported/enabled in this service.
  130. case unimplemented = 12
  131. /// Internal errors. Means some invariants expected by underlying System has
  132. /// been broken. If you see one of these errors, Something is very broken.
  133. case internalError = 13
  134. /// The service is currently unavailable. This is a most likely a transient
  135. /// condition and may be corrected by retrying with a backoff.
  136. ///
  137. /// See litmus test above for deciding between FAILED_PRECONDITION, ABORTED,
  138. /// and UNAVAILABLE.
  139. case unavailable = 14
  140. /// Unrecoverable data loss or corruption.
  141. case dataLoss = 15
  142. /// Force users to include a default branch:
  143. case doNotUse = -1
  144. }