Operation.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /// Abstract representation of gRPC Operations
  38. class Operation {
  39. /// Pointer to underlying C representation
  40. var observer: UnsafeMutableRawPointer
  41. /// Initializes an Operation Observer
  42. ///
  43. /// - Parameter observer: the underlying C representation
  44. init(observer: UnsafeMutableRawPointer) {
  45. self.observer = observer
  46. }
  47. deinit {
  48. cgrpc_observer_destroy(observer);
  49. }
  50. }
  51. /// SendInitialMetadata operation
  52. class Operation_SendInitialMetadata : Operation {
  53. /// Initializes an Operation Observer
  54. ///
  55. /// - Parameter metadata: the initial metadata to send
  56. init(metadata:Metadata) {
  57. super.init(observer:cgrpc_observer_create_send_initial_metadata(metadata.array))
  58. }
  59. }
  60. /// SendMessage operation
  61. class Operation_SendMessage : Operation {
  62. /// Initializes an Operation Observer
  63. ///
  64. /// - Parameter message: the message to send
  65. init(message:ByteBuffer) {
  66. super.init(observer:cgrpc_observer_create_send_message())
  67. cgrpc_observer_send_message_set_message(observer, message.b);
  68. }
  69. }
  70. /// SendCloseFromClient operation
  71. class Operation_SendCloseFromClient : Operation {
  72. /// Initializes an Operation Observer
  73. init() {
  74. super.init(observer:cgrpc_observer_create_send_close_from_client())
  75. }
  76. }
  77. /// SendStatusFrom Server operation
  78. class Operation_SendStatusFromServer : Operation {
  79. /// Initializes an Operation Observer
  80. ///
  81. /// - Parameter status: the status code to send with the response
  82. /// - Parameter statusDetails: the status message to send with the response
  83. /// - Parameter metadata: the trailing metadata to send with the response
  84. init(status:Int,
  85. statusDetails:String,
  86. metadata:Metadata) {
  87. super.init(observer:cgrpc_observer_create_send_status_from_server(metadata.array))
  88. cgrpc_observer_send_status_from_server_set_status(observer, Int32(status));
  89. cgrpc_observer_send_status_from_server_set_status_details(observer, statusDetails);
  90. }
  91. }
  92. /// ReceiveInitialMetadata operation
  93. class Operation_ReceiveInitialMetadata : Operation {
  94. /// Initializes an Operation Observer
  95. init() {
  96. super.init(observer:cgrpc_observer_create_recv_initial_metadata())
  97. }
  98. /// Gets the initial metadata that was received
  99. ///
  100. /// - Returns: metadata
  101. func metadata() -> Metadata {
  102. return Metadata(array:cgrpc_observer_recv_initial_metadata_get_metadata(observer));
  103. }
  104. }
  105. /// ReceiveMessage operation
  106. class Operation_ReceiveMessage : Operation {
  107. /// Initializes an Operation Observer
  108. init() {
  109. super.init(observer:cgrpc_observer_create_recv_message())
  110. }
  111. /// Gets the message that was received
  112. ///
  113. /// - Returns: message
  114. func message() -> ByteBuffer? {
  115. if let b = cgrpc_observer_recv_message_get_message(observer) {
  116. return ByteBuffer(b:b)
  117. } else {
  118. return nil
  119. }
  120. }
  121. }
  122. /// ReceiveStatusOnClient operation
  123. class Operation_ReceiveStatusOnClient : Operation {
  124. /// Initializes an Operation Observer
  125. init() {
  126. super.init(observer:cgrpc_observer_create_recv_status_on_client())
  127. }
  128. /// Gets the trailing metadata that was received
  129. ///
  130. /// - Returns: metadata
  131. func metadata() -> Metadata {
  132. return Metadata(array:cgrpc_observer_recv_status_on_client_get_metadata(observer));
  133. }
  134. /// Gets the status code that was received
  135. ///
  136. /// - Returns: status code
  137. func status() -> Int {
  138. return cgrpc_observer_recv_status_on_client_get_status(observer);
  139. }
  140. /// Gets the status message that was received
  141. ///
  142. /// - Returns: status message
  143. func statusDetails() -> String {
  144. return String(cString:cgrpc_observer_recv_status_on_client_get_status_details(observer),
  145. encoding:String.Encoding.utf8)!
  146. }
  147. }
  148. /// ReceiveCloseOnServer operation
  149. class Operation_ReceiveCloseOnServer : Operation {
  150. /// Initializes an Operation Observer
  151. init() {
  152. super.init(observer:cgrpc_observer_create_recv_close_on_server())
  153. }
  154. }