2
0

CompletionQueue.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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
  37. internal enum CompletionType {
  38. case queueShutdown
  39. case queueTimeout
  40. case complete
  41. case unknown
  42. static func completionType(grpcCompletionType value: grpc_completion_type) -> CompletionType {
  43. switch(value) {
  44. case GRPC_QUEUE_SHUTDOWN:
  45. return .queueShutdown
  46. case GRPC_QUEUE_TIMEOUT:
  47. return .queueTimeout
  48. case GRPC_OP_COMPLETE:
  49. return .complete
  50. default:
  51. return .unknown
  52. }
  53. }
  54. }
  55. internal struct CompletionQueueEvent {
  56. internal var type: CompletionType
  57. internal var success: Int32
  58. internal var tag: Int64
  59. init(_ event: grpc_event) {
  60. type = CompletionType.completionType(grpcCompletionType: event.type)
  61. success = event.success
  62. tag = cgrpc_event_tag(event)
  63. }
  64. }
  65. /// A gRPC Completion Queue
  66. internal class CompletionQueue {
  67. /// Optional user-provided name for the queue
  68. internal var name : String!
  69. /// Pointer to underlying C representation
  70. private var underlyingCompletionQueue : UnsafeMutableRawPointer
  71. /// Operation groups that are awaiting completion, keyed by tag
  72. internal var operationGroups : [Int64 : OperationGroup] = [:]
  73. /// Initializes a CompletionQueue
  74. ///
  75. /// - Parameter cq: the underlying C representation
  76. init(underlyingCompletionQueue: UnsafeMutableRawPointer) {
  77. // NOT OWNED, so we don't dealloc it in a deinit
  78. self.underlyingCompletionQueue = underlyingCompletionQueue
  79. }
  80. /// Waits for an event to complete
  81. ///
  82. /// - Parameter timeout: a timeout value in seconds
  83. /// - Returns: a grpc_completion_type code indicating the result of waiting
  84. internal func wait(timeout: TimeInterval) -> CompletionQueueEvent {
  85. let event = cgrpc_completion_queue_get_next_event(underlyingCompletionQueue, timeout);
  86. return CompletionQueueEvent(event)
  87. }
  88. /// Run a completion queue
  89. internal func run(completion:@escaping () -> Void) {
  90. DispatchQueue.global().async {
  91. var running = true
  92. while (running) {
  93. let event = cgrpc_completion_queue_get_next_event(self.underlyingCompletionQueue, -1.0)
  94. switch (event.type) {
  95. case GRPC_OP_COMPLETE:
  96. let tag = cgrpc_event_tag(event)
  97. if let operations = self.operationGroups[tag] {
  98. print("[\(self.name)] event success=\(event.success)")
  99. if event.success == 0 {
  100. print("something bad happened")
  101. } else {
  102. // call the operation group completion handler
  103. operations.completion(event.success == 1)
  104. }
  105. self.operationGroups[tag] = nil
  106. }
  107. break
  108. case GRPC_QUEUE_SHUTDOWN:
  109. running = false
  110. break
  111. case GRPC_QUEUE_TIMEOUT:
  112. break
  113. default:
  114. break
  115. }
  116. }
  117. DispatchQueue.main.async {
  118. // call the queue completion handler
  119. completion()
  120. }
  121. }
  122. }
  123. /// Shutdown a completion queue
  124. internal func shutdown() -> Void {
  125. cgrpc_completion_queue_shutdown(underlyingCompletionQueue)
  126. }
  127. }