request.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef LIB_RPC_RPC_REQUEST_H
  19. #define LIB_RPC_RPC_REQUEST_H
  20. #include "hdfspp/status.h"
  21. #include "common/util.h"
  22. #include "common/new_delete.h"
  23. #include <string>
  24. #include <google/protobuf/message_lite.h>
  25. #include <google/protobuf/io/coded_stream.h>
  26. #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
  27. #include <asio/deadline_timer.hpp>
  28. namespace hdfs {
  29. class LockFreeRpcEngine;
  30. class SaslProtocol;
  31. /*
  32. * Internal bookkeeping for an outstanding request from the consumer.
  33. *
  34. * Threading model: not thread-safe; should only be accessed from a single
  35. * thread at a time
  36. */
  37. class Request {
  38. public:
  39. MEMCHECKED_CLASS(Request)
  40. typedef std::function<void(::google::protobuf::io::CodedInputStream *is,
  41. const Status &status)> Handler;
  42. Request(LockFreeRpcEngine *engine, const std::string &method_name, int call_id,
  43. const std::string &request, Handler &&callback);
  44. Request(LockFreeRpcEngine *engine, const std::string &method_name, int call_id,
  45. const ::google::protobuf::MessageLite *request, Handler &&callback);
  46. // Null request (with no actual message) used to track the state of an
  47. // initial Connect call
  48. Request(LockFreeRpcEngine *engine, Handler &&handler);
  49. int call_id() const { return call_id_; }
  50. std::string method_name() const { return method_name_; }
  51. ::asio::deadline_timer &timer() { return timer_; }
  52. int IncrementRetryCount() { return retry_count_++; }
  53. int IncrementFailoverCount();
  54. void GetPacket(std::string *res) const;
  55. void OnResponseArrived(::google::protobuf::io::CodedInputStream *is,
  56. const Status &status);
  57. int get_failover_count() {return failover_count_;}
  58. std::string GetDebugString() const;
  59. private:
  60. LockFreeRpcEngine *const engine_;
  61. const std::string method_name_;
  62. const int call_id_;
  63. ::asio::deadline_timer timer_;
  64. std::string payload_;
  65. const Handler handler_;
  66. int retry_count_;
  67. int failover_count_;
  68. };
  69. } // end namespace hdfs
  70. #endif // end include guard