PageRenderTime 23ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/src/google/protobuf/service.h

https://bitbucket.org/Abd4llA/test
C Header | 287 lines | 63 code | 28 blank | 196 comment | 0 complexity | 4a8afaee788306daf07dddb5b49ab553 MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This module declares the abstract interfaces underlying proto2 RPC
  35. // services. These are intented to be independent of any particular RPC
  36. // implementation, so that proto2 services can be used on top of a variety
  37. // of implementations.
  38. //
  39. //
  40. // When you use the protocol compiler to compile a service definition, it
  41. // generates two classes: An abstract interface for the service (with
  42. // methods matching the service definition) and a "stub" implementation.
  43. // A stub is just a type-safe wrapper around an RpcChannel which emulates a
  44. // local implementation of the service.
  45. //
  46. // For example, the service definition:
  47. // service MyService {
  48. // rpc Foo(MyRequest) returns(MyResponse);
  49. // }
  50. // will generate abstract interface "MyService" and class "MyService::Stub".
  51. // You could implement a MyService as follows:
  52. // class MyServiceImpl : public MyService {
  53. // public:
  54. // MyServiceImpl() {}
  55. // ~MyServiceImpl() {}
  56. //
  57. // // implements MyService ---------------------------------------
  58. //
  59. // void Foo(google::protobuf::RpcController* controller,
  60. // const MyRequest* request,
  61. // MyResponse* response,
  62. // Closure* done) {
  63. // // ... read request and fill in response ...
  64. // done->Run();
  65. // }
  66. // };
  67. // You would then register an instance of MyServiceImpl with your RPC server
  68. // implementation. (How to do that depends on the implementation.)
  69. //
  70. // To call a remote MyServiceImpl, first you need an RpcChannel connected to it.
  71. // How to construct a channel depends, again, on your RPC implementation.
  72. // Here we use a hypothentical "MyRpcChannel" as an example:
  73. // MyRpcChannel channel("rpc:hostname:1234/myservice");
  74. // MyRpcController controller;
  75. // MyServiceImpl::Stub stub(&channel);
  76. // FooRequest request;
  77. // FooRespnose response;
  78. //
  79. // // ... fill in request ...
  80. //
  81. // stub.Foo(&controller, request, &response, NewCallback(HandleResponse));
  82. //
  83. // On Thread-Safety:
  84. //
  85. // Different RPC implementations may make different guarantees about what
  86. // threads they may run callbacks on, and what threads the application is
  87. // allowed to use to call the RPC system. Portable software should be ready
  88. // for callbacks to be called on any thread, but should not try to call the
  89. // RPC system from any thread except for the ones on which it received the
  90. // callbacks. Realistically, though, simple software will probably want to
  91. // use a single-threaded RPC system while high-end software will want to
  92. // use multiple threads. RPC implementations should provide multiple
  93. // choices.
  94. #ifndef GOOGLE_PROTOBUF_SERVICE_H__
  95. #define GOOGLE_PROTOBUF_SERVICE_H__
  96. #include <string>
  97. #include <google/protobuf/stubs/common.h>
  98. namespace google {
  99. namespace protobuf {
  100. // Defined in this file.
  101. class Service;
  102. class RpcController;
  103. class RpcChannel;
  104. // Defined in other files.
  105. class Descriptor; // descriptor.h
  106. class ServiceDescriptor; // descriptor.h
  107. class MethodDescriptor; // descriptor.h
  108. class Message; // message.h
  109. // Abstract base interface for protocol-buffer-based RPC services. Services
  110. // themselves are abstract interfaces (implemented either by servers or as
  111. // stubs), but they subclass this base interface. The methods of this
  112. // interface can be used to call the methods of the Service without knowing
  113. // its exact type at compile time (analogous to Reflection).
  114. class LIBPROTOBUF_EXPORT Service {
  115. public:
  116. inline Service() {}
  117. virtual ~Service();
  118. // When constructing a stub, you may pass STUB_OWNS_CHANNEL as the second
  119. // parameter to the constructor to tell it to delete its RpcChannel when
  120. // destroyed.
  121. enum ChannelOwnership {
  122. STUB_OWNS_CHANNEL,
  123. STUB_DOESNT_OWN_CHANNEL
  124. };
  125. // Get the ServiceDescriptor describing this service and its methods.
  126. virtual const ServiceDescriptor* GetDescriptor() = 0;
  127. // Call a method of the service specified by MethodDescriptor. This is
  128. // normally implemented as a simple switch() that calls the standard
  129. // definitions of the service's methods.
  130. //
  131. // Preconditions:
  132. // * method->service() == GetDescriptor()
  133. // * request and response are of the exact same classes as the objects
  134. // returned by GetRequestPrototype(method) and
  135. // GetResponsePrototype(method).
  136. // * After the call has started, the request must not be modified and the
  137. // response must not be accessed at all until "done" is called.
  138. // * "controller" is of the correct type for the RPC implementation being
  139. // used by this Service. For stubs, the "correct type" depends on the
  140. // RpcChannel which the stub is using. Server-side Service
  141. // implementations are expected to accept whatever type of RpcController
  142. // the server-side RPC implementation uses.
  143. //
  144. // Postconditions:
  145. // * "done" will be called when the method is complete. This may be
  146. // before CallMethod() returns or it may be at some point in the future.
  147. // * If the RPC succeeded, "response" contains the response returned by
  148. // the server.
  149. // * If the RPC failed, "response"'s contents are undefined. The
  150. // RpcController can be queried to determine if an error occurred and
  151. // possibly to get more information about the error.
  152. virtual void CallMethod(const MethodDescriptor* method,
  153. RpcController* controller,
  154. const Message* request,
  155. Message* response,
  156. Closure* done) = 0;
  157. // CallMethod() requires that the request and response passed in are of a
  158. // particular subclass of Message. GetRequestPrototype() and
  159. // GetResponsePrototype() get the default instances of these required types.
  160. // You can then call Message::New() on these instances to construct mutable
  161. // objects which you can then pass to CallMethod().
  162. //
  163. // Example:
  164. // const MethodDescriptor* method =
  165. // service->GetDescriptor()->FindMethodByName("Foo");
  166. // Message* request = stub->GetRequestPrototype (method)->New();
  167. // Message* response = stub->GetResponsePrototype(method)->New();
  168. // request->ParseFromString(input);
  169. // service->CallMethod(method, *request, response, callback);
  170. virtual const Message& GetRequestPrototype(
  171. const MethodDescriptor* method) const = 0;
  172. virtual const Message& GetResponsePrototype(
  173. const MethodDescriptor* method) const = 0;
  174. private:
  175. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Service);
  176. };
  177. // An RpcController mediates a single method call. The primary purpose of
  178. // the controller is to provide a way to manipulate settings specific to the
  179. // RPC implementation and to find out about RPC-level errors.
  180. //
  181. // The methods provided by the RpcController interface are intended to be a
  182. // "least common denominator" set of features which we expect all
  183. // implementations to support. Specific implementations may provide more
  184. // advanced features (e.g. deadline propagation).
  185. class LIBPROTOBUF_EXPORT RpcController {
  186. public:
  187. inline RpcController() {}
  188. virtual ~RpcController();
  189. // Client-side methods ---------------------------------------------
  190. // These calls may be made from the client side only. Their results
  191. // are undefined on the server side (may crash).
  192. // Resets the RpcController to its initial state so that it may be reused in
  193. // a new call. Must not be called while an RPC is in progress.
  194. virtual void Reset() = 0;
  195. // After a call has finished, returns true if the call failed. The possible
  196. // reasons for failure depend on the RPC implementation. Failed() must not
  197. // be called before a call has finished. If Failed() returns true, the
  198. // contents of the response message are undefined.
  199. virtual bool Failed() const = 0;
  200. // If Failed() is true, returns a human-readable description of the error.
  201. virtual string ErrorText() const = 0;
  202. // Advises the RPC system that the caller desires that the RPC call be
  203. // canceled. The RPC system may cancel it immediately, may wait awhile and
  204. // then cancel it, or may not even cancel the call at all. If the call is
  205. // canceled, the "done" callback will still be called and the RpcController
  206. // will indicate that the call failed at that time.
  207. virtual void StartCancel() = 0;
  208. // Server-side methods ---------------------------------------------
  209. // These calls may be made from the server side only. Their results
  210. // are undefined on the client side (may crash).
  211. // Causes Failed() to return true on the client side. "reason" will be
  212. // incorporated into the message returned by ErrorText(). If you find
  213. // you need to return machine-readable information about failures, you
  214. // should incorporate it into your response protocol buffer and should
  215. // NOT call SetFailed().
  216. virtual void SetFailed(const string& reason) = 0;
  217. // If true, indicates that the client canceled the RPC, so the server may
  218. // as well give up on replying to it. The server should still call the
  219. // final "done" callback.
  220. virtual bool IsCanceled() const = 0;
  221. // Asks that the given callback be called when the RPC is canceled. The
  222. // callback will always be called exactly once. If the RPC completes without
  223. // being canceled, the callback will be called after completion. If the RPC
  224. // has already been canceled when NotifyOnCancel() is called, the callback
  225. // will be called immediately.
  226. //
  227. // NotifyOnCancel() must be called no more than once per request.
  228. virtual void NotifyOnCancel(Closure* callback) = 0;
  229. private:
  230. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcController);
  231. };
  232. // Abstract interface for an RPC channel. An RpcChannel represents a
  233. // communication line to a Service which can be used to call that Service's
  234. // methods. The Service may be running on another machine. Normally, you
  235. // should not call an RpcChannel directly, but instead construct a stub Service
  236. // wrapping it. Example:
  237. // RpcChannel* channel = new MyRpcChannel("remotehost.example.com:1234");
  238. // MyService* service = new MyService::Stub(channel);
  239. // service->MyMethod(request, &response, callback);
  240. class LIBPROTOBUF_EXPORT RpcChannel {
  241. public:
  242. inline RpcChannel() {}
  243. virtual ~RpcChannel();
  244. // Call the given method of the remote service. The signature of this
  245. // procedure looks the same as Service::CallMethod(), but the requirements
  246. // are less strict in one important way: the request and response objects
  247. // need not be of any specific class as long as their descriptors are
  248. // method->input_type() and method->output_type().
  249. virtual void CallMethod(const MethodDescriptor* method,
  250. RpcController* controller,
  251. const Message* request,
  252. Message* response,
  253. Closure* done) = 0;
  254. private:
  255. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcChannel);
  256. };
  257. } // namespace protobuf
  258. } // namespace google
  259. #endif // GOOGLE_PROTOBUF_SERVICE_H__