/common/rpc/StreamRpcChannel.h

https://code.google.com/ · C Header · 158 lines · 101 code · 22 blank · 35 comment · 0 complexity · 83eb719f9934999831bbe866b5b91056 MD5 · raw file

  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU Library General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. *
  16. * StreamRpcChannel.h
  17. * Interface for the Stream RPC Channel
  18. * Copyright (C) 2005-2008 Simon Newton
  19. */
  20. #ifndef COMMON_RPC_STREAMRPCCHANNEL_H_
  21. #define COMMON_RPC_STREAMRPCCHANNEL_H_
  22. #include <stdint.h>
  23. #include <google/protobuf/service.h>
  24. #include <ola/Callback.h>
  25. #include <ola/io/Descriptor.h>
  26. #include <ola/io/SelectServer.h>
  27. #include "ola/ExportMap.h"
  28. #include "config.h"
  29. #include HASH_MAP_H
  30. namespace ola {
  31. namespace rpc {
  32. using google::protobuf::Message;
  33. using google::protobuf::MethodDescriptor;
  34. using google::protobuf::RpcChannel;
  35. using google::protobuf::RpcController;
  36. using google::protobuf::Service;
  37. class RpcMessage;
  38. class OutstandingRequest {
  39. /*
  40. * These are requests on the server end that haven't completed yet.
  41. */
  42. public:
  43. OutstandingRequest() {}
  44. ~OutstandingRequest() {}
  45. int id;
  46. RpcController *controller;
  47. Message *response;
  48. };
  49. class OutstandingResponse {
  50. /*
  51. * These are Requests on the client end that haven't completed yet.
  52. */
  53. public:
  54. OutstandingResponse() {}
  55. ~OutstandingResponse() {}
  56. int id;
  57. RpcController *controller;
  58. google::protobuf::Closure *callback;
  59. Message *reply;
  60. };
  61. class StreamRpcHeader {
  62. /*
  63. * The first 4 bytes are the header which contains the RPC protocol version
  64. * (this is separate from the protobuf version) and the size of the protobuf.
  65. */
  66. public:
  67. static void EncodeHeader(uint32_t *header, unsigned int version,
  68. unsigned int size);
  69. static void DecodeHeader(uint32_t header, unsigned int *version,
  70. unsigned int *size);
  71. private:
  72. static const unsigned int VERSION_MASK = 0xf0000000;
  73. static const unsigned int SIZE_MASK = 0x0fffffff;
  74. };
  75. class StreamRpcChannel: public RpcChannel {
  76. /*
  77. * Implements a RpcChannel over a descriptor.
  78. */
  79. public :
  80. StreamRpcChannel(Service *service,
  81. ola::io::ConnectedDescriptor *descriptor,
  82. ExportMap *export_map = NULL);
  83. ~StreamRpcChannel();
  84. void DescriptorReady();
  85. void SetOnClose(SingleUseCallback0<void> *closure);
  86. void CallMethod(
  87. const MethodDescriptor *method,
  88. RpcController *controller,
  89. const Message *request,
  90. Message *response,
  91. google::protobuf::Closure *done);
  92. void RequestComplete(OutstandingRequest *request);
  93. void SetService(Service *service) { m_service = service; }
  94. static const unsigned int PROTOCOL_VERSION = 1;
  95. private:
  96. bool SendMsg(RpcMessage *msg);
  97. int AllocateMsgBuffer(unsigned int size);
  98. int ReadHeader(unsigned int *version, unsigned int *size) const;
  99. bool HandleNewMsg(uint8_t *buffer, unsigned int size);
  100. void HandleRequest(RpcMessage *msg);
  101. void HandleStreamRequest(RpcMessage *msg);
  102. // server end
  103. void SendRequestFailed(OutstandingRequest *request);
  104. void SendNotImplemented(int msg_id);
  105. void DeleteOutstandingRequest(OutstandingRequest *request);
  106. // client end
  107. void HandleResponse(RpcMessage *msg);
  108. void HandleFailedResponse(RpcMessage *msg);
  109. void HandleCanceledResponse(RpcMessage *msg);
  110. void HandleNotImplemented(RpcMessage *msg);
  111. OutstandingResponse *GetOutstandingResponse(int msg_id);
  112. void InvokeCallbackAndCleanup(OutstandingResponse *response);
  113. Service *m_service; // service to dispatch requests to
  114. SingleUseCallback0<void> *m_on_close;
  115. // the descriptor to read/write to.
  116. class ola::io::ConnectedDescriptor *m_descriptor;
  117. uint32_t m_seq; // sequence number
  118. uint8_t *m_buffer; // buffer for incomming msgs
  119. unsigned int m_buffer_size; // size of the buffer
  120. unsigned int m_expected_size; // the total size of the current msg
  121. unsigned int m_current_size; // the amount of data read for the current msg
  122. HASH_NAMESPACE::HASH_MAP_CLASS<int, OutstandingRequest*> m_requests;
  123. HASH_NAMESPACE::HASH_MAP_CLASS<int, OutstandingResponse*> m_responses;
  124. ExportMap *m_export_map;
  125. UIntMap *m_recv_type_map;
  126. static const char K_RPC_RECEIVED_TYPE_VAR[];
  127. static const char K_RPC_RECEIVED_VAR[];
  128. static const char K_RPC_SENT_ERROR_VAR[];
  129. static const char K_RPC_SENT_VAR[];
  130. static const char STREAMING_NO_RESPONSE[];
  131. static const unsigned int INITIAL_BUFFER_SIZE = 1 << 11; // 2k
  132. static const unsigned int MAX_BUFFER_SIZE = 1 << 20; // 1M
  133. };
  134. } // rpc
  135. } // ola
  136. #endif // COMMON_RPC_STREAMRPCCHANNEL_H_