ServerSession.swift 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright 2018, 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. import Dispatch
  17. import Foundation
  18. import SwiftProtobuf
  19. public struct ServerStatus: Error {
  20. public let code: StatusCode
  21. public let message: String
  22. public let trailingMetadata: Metadata
  23. public init(code: StatusCode, message: String, trailingMetadata: Metadata = Metadata()) {
  24. self.code = code
  25. self.message = message
  26. self.trailingMetadata = trailingMetadata
  27. }
  28. public static let ok = ServerStatus(code: .ok, message: "OK")
  29. public static let processingError = ServerStatus(code: .internalError, message: "unknown error processing request")
  30. public static let noRequestData = ServerStatus(code: .invalidArgument, message: "no request data received")
  31. public static let sendingInitialMetadataFailed = ServerStatus(code: .internalError, message: "sending initial metadata failed")
  32. }
  33. public protocol ServerSession: class {
  34. var requestMetadata: Metadata { get }
  35. var initialMetadata: Metadata { get set }
  36. func cancel()
  37. }
  38. open class ServerSessionBase: ServerSession {
  39. public var handler: Handler
  40. public var requestMetadata: Metadata { return handler.requestMetadata }
  41. public var initialMetadata: Metadata = Metadata()
  42. public var call: Call { return handler.call }
  43. public init(handler: Handler) {
  44. self.handler = handler
  45. }
  46. public func cancel() {
  47. call.cancel()
  48. }
  49. }
  50. open class ServerSessionTestStub: ServerSession {
  51. open var requestMetadata = Metadata()
  52. open var initialMetadata = Metadata()
  53. public init() {}
  54. open func cancel() {}
  55. }