|
||||
File indexing completed on 2025-01-31 10:12:26
0001 // Protocol Buffers - Google's data interchange format 0002 // Copyright 2008 Google Inc. All rights reserved. 0003 // 0004 // Use of this source code is governed by a BSD-style 0005 // license that can be found in the LICENSE file or at 0006 // https://developers.google.com/open-source/licenses/bsd 0007 0008 // Author: kenton@google.com (Kenton Varda) 0009 // Based on original Protocol Buffers design by 0010 // Sanjay Ghemawat, Jeff Dean, and others. 0011 // 0012 // DEPRECATED: This module declares the abstract interfaces underlying proto2 0013 // RPC services. These are intended to be independent of any particular RPC 0014 // implementation, so that proto2 services can be used on top of a variety 0015 // of implementations. Starting with version 2.3.0, RPC implementations should 0016 // not try to build on these, but should instead provide code generator plugins 0017 // which generate code specific to the particular RPC implementation. This way 0018 // the generated code can be more appropriate for the implementation in use 0019 // and can avoid unnecessary layers of indirection. 0020 // 0021 // 0022 // When you use the protocol compiler to compile a service definition, it 0023 // generates two classes: An abstract interface for the service (with 0024 // methods matching the service definition) and a "stub" implementation. 0025 // A stub is just a type-safe wrapper around an RpcChannel which emulates a 0026 // local implementation of the service. 0027 // 0028 // For example, the service definition: 0029 // service MyService { 0030 // rpc Foo(MyRequest) returns(MyResponse); 0031 // } 0032 // will generate abstract interface "MyService" and class "MyService::Stub". 0033 // You could implement a MyService as follows: 0034 // class MyServiceImpl : public MyService { 0035 // public: 0036 // MyServiceImpl() {} 0037 // ~MyServiceImpl() {} 0038 // 0039 // // implements MyService --------------------------------------- 0040 // 0041 // void Foo(google::protobuf::RpcController* controller, 0042 // const MyRequest* request, 0043 // MyResponse* response, 0044 // Closure* done) { 0045 // // ... read request and fill in response ... 0046 // done->Run(); 0047 // } 0048 // }; 0049 // You would then register an instance of MyServiceImpl with your RPC server 0050 // implementation. (How to do that depends on the implementation.) 0051 // 0052 // To call a remote MyServiceImpl, first you need an RpcChannel connected to it. 0053 // How to construct a channel depends, again, on your RPC implementation. 0054 // Here we use a hypothetical "MyRpcChannel" as an example: 0055 // MyRpcChannel channel("rpc:hostname:1234/myservice"); 0056 // MyRpcController controller; 0057 // MyServiceImpl::Stub stub(&channel); 0058 // FooRequest request; 0059 // FooResponse response; 0060 // 0061 // // ... fill in request ... 0062 // 0063 // stub.Foo(&controller, request, &response, NewCallback(HandleResponse)); 0064 // 0065 // On Thread-Safety: 0066 // 0067 // Different RPC implementations may make different guarantees about what 0068 // threads they may run callbacks on, and what threads the application is 0069 // allowed to use to call the RPC system. Portable software should be ready 0070 // for callbacks to be called on any thread, but should not try to call the 0071 // RPC system from any thread except for the ones on which it received the 0072 // callbacks. Realistically, though, simple software will probably want to 0073 // use a single-threaded RPC system while high-end software will want to 0074 // use multiple threads. RPC implementations should provide multiple 0075 // choices. 0076 0077 #ifndef GOOGLE_PROTOBUF_SERVICE_H__ 0078 #define GOOGLE_PROTOBUF_SERVICE_H__ 0079 0080 #include <string> 0081 0082 #include "google/protobuf/stubs/callback.h" 0083 #include "google/protobuf/stubs/common.h" 0084 #include "google/protobuf/port.h" 0085 0086 #ifdef SWIG 0087 #error "You cannot SWIG proto headers" 0088 #endif 0089 0090 // Must be included last. 0091 #include "google/protobuf/port_def.inc" 0092 0093 namespace google { 0094 namespace protobuf { 0095 0096 // Defined in this file. 0097 class Service; 0098 class RpcController; 0099 class RpcChannel; 0100 0101 // Defined in other files. 0102 class Descriptor; // descriptor.h 0103 class ServiceDescriptor; // descriptor.h 0104 class MethodDescriptor; // descriptor.h 0105 class Message; // message.h 0106 0107 // Abstract base interface for protocol-buffer-based RPC services. Services 0108 // themselves are abstract interfaces (implemented either by servers or as 0109 // stubs), but they subclass this base interface. The methods of this 0110 // interface can be used to call the methods of the Service without knowing 0111 // its exact type at compile time (analogous to Reflection). 0112 class PROTOBUF_EXPORT Service { 0113 public: 0114 inline Service() {} 0115 Service(const Service&) = delete; 0116 Service& operator=(const Service&) = delete; 0117 virtual ~Service(); 0118 0119 // When constructing a stub, you may pass STUB_OWNS_CHANNEL as the second 0120 // parameter to the constructor to tell it to delete its RpcChannel when 0121 // destroyed. 0122 enum ChannelOwnership { STUB_OWNS_CHANNEL, STUB_DOESNT_OWN_CHANNEL }; 0123 0124 // Get the ServiceDescriptor describing this service and its methods. 0125 virtual const ServiceDescriptor* GetDescriptor() = 0; 0126 0127 // Call a method of the service specified by MethodDescriptor. This is 0128 // normally implemented as a simple switch() that calls the standard 0129 // definitions of the service's methods. 0130 // 0131 // Preconditions: 0132 // * method->service() == GetDescriptor() 0133 // * request and response are of the exact same classes as the objects 0134 // returned by GetRequestPrototype(method) and 0135 // GetResponsePrototype(method). 0136 // * After the call has started, the request must not be modified and the 0137 // response must not be accessed at all until "done" is called. 0138 // * "controller" is of the correct type for the RPC implementation being 0139 // used by this Service. For stubs, the "correct type" depends on the 0140 // RpcChannel which the stub is using. Server-side Service 0141 // implementations are expected to accept whatever type of RpcController 0142 // the server-side RPC implementation uses. 0143 // 0144 // Postconditions: 0145 // * "done" will be called when the method is complete. This may be 0146 // before CallMethod() returns or it may be at some point in the future. 0147 // * If the RPC succeeded, "response" contains the response returned by 0148 // the server. 0149 // * If the RPC failed, "response"'s contents are undefined. The 0150 // RpcController can be queried to determine if an error occurred and 0151 // possibly to get more information about the error. 0152 virtual void CallMethod(const MethodDescriptor* method, 0153 RpcController* controller, const Message* request, 0154 Message* response, Closure* done) = 0; 0155 0156 // CallMethod() requires that the request and response passed in are of a 0157 // particular subclass of Message. GetRequestPrototype() and 0158 // GetResponsePrototype() get the default instances of these required types. 0159 // You can then call Message::New() on these instances to construct mutable 0160 // objects which you can then pass to CallMethod(). 0161 // 0162 // Example: 0163 // const MethodDescriptor* method = 0164 // service->GetDescriptor()->FindMethodByName("Foo"); 0165 // Message* request = stub->GetRequestPrototype (method)->New(); 0166 // Message* response = stub->GetResponsePrototype(method)->New(); 0167 // request->ParseFromString(input); 0168 // service->CallMethod(method, *request, response, callback); 0169 virtual const Message& GetRequestPrototype( 0170 const MethodDescriptor* method) const = 0; 0171 virtual const Message& GetResponsePrototype( 0172 const MethodDescriptor* method) const = 0; 0173 }; 0174 0175 // An RpcController mediates a single method call. The primary purpose of 0176 // the controller is to provide a way to manipulate settings specific to the 0177 // RPC implementation and to find out about RPC-level errors. 0178 // 0179 // The methods provided by the RpcController interface are intended to be a 0180 // "least common denominator" set of features which we expect all 0181 // implementations to support. Specific implementations may provide more 0182 // advanced features (e.g. deadline propagation). 0183 class PROTOBUF_EXPORT RpcController { 0184 public: 0185 inline RpcController() {} 0186 RpcController(const RpcController&) = delete; 0187 RpcController& operator=(const RpcController&) = delete; 0188 virtual ~RpcController(); 0189 0190 // Client-side methods --------------------------------------------- 0191 // These calls may be made from the client side only. Their results 0192 // are undefined on the server side (may crash). 0193 0194 // Resets the RpcController to its initial state so that it may be reused in 0195 // a new call. Must not be called while an RPC is in progress. 0196 virtual void Reset() = 0; 0197 0198 // After a call has finished, returns true if the call failed. The possible 0199 // reasons for failure depend on the RPC implementation. Failed() must not 0200 // be called before a call has finished. If Failed() returns true, the 0201 // contents of the response message are undefined. 0202 virtual bool Failed() const = 0; 0203 0204 // If Failed() is true, returns a human-readable description of the error. 0205 virtual std::string ErrorText() const = 0; 0206 0207 // Advises the RPC system that the caller desires that the RPC call be 0208 // canceled. The RPC system may cancel it immediately, may wait awhile and 0209 // then cancel it, or may not even cancel the call at all. If the call is 0210 // canceled, the "done" callback will still be called and the RpcController 0211 // will indicate that the call failed at that time. 0212 virtual void StartCancel() = 0; 0213 0214 // Server-side methods --------------------------------------------- 0215 // These calls may be made from the server side only. Their results 0216 // are undefined on the client side (may crash). 0217 0218 // Causes Failed() to return true on the client side. "reason" will be 0219 // incorporated into the message returned by ErrorText(). If you find 0220 // you need to return machine-readable information about failures, you 0221 // should incorporate it into your response protocol buffer and should 0222 // NOT call SetFailed(). 0223 virtual void SetFailed(const std::string& reason) = 0; 0224 0225 // If true, indicates that the client canceled the RPC, so the server may 0226 // as well give up on replying to it. The server should still call the 0227 // final "done" callback. 0228 virtual bool IsCanceled() const = 0; 0229 0230 // Asks that the given callback be called when the RPC is canceled. The 0231 // callback will always be called exactly once. If the RPC completes without 0232 // being canceled, the callback will be called after completion. If the RPC 0233 // has already been canceled when NotifyOnCancel() is called, the callback 0234 // will be called immediately. 0235 // 0236 // NotifyOnCancel() must be called no more than once per request. 0237 virtual void NotifyOnCancel(Closure* callback) = 0; 0238 }; 0239 0240 // Abstract interface for an RPC channel. An RpcChannel represents a 0241 // communication line to a Service which can be used to call that Service's 0242 // methods. The Service may be running on another machine. Normally, you 0243 // should not call an RpcChannel directly, but instead construct a stub Service 0244 // wrapping it. Example: 0245 // RpcChannel* channel = new MyRpcChannel("remotehost.example.com:1234"); 0246 // MyService* service = new MyService::Stub(channel); 0247 // service->MyMethod(request, &response, callback); 0248 class PROTOBUF_EXPORT RpcChannel { 0249 public: 0250 inline RpcChannel() {} 0251 RpcChannel(const RpcChannel&) = delete; 0252 RpcChannel& operator=(const RpcChannel&) = delete; 0253 virtual ~RpcChannel(); 0254 0255 // Call the given method of the remote service. The signature of this 0256 // procedure looks the same as Service::CallMethod(), but the requirements 0257 // are less strict in one important way: the request and response objects 0258 // need not be of any specific class as long as their descriptors are 0259 // method->input_type() and method->output_type(). 0260 virtual void CallMethod(const MethodDescriptor* method, 0261 RpcController* controller, const Message* request, 0262 Message* response, Closure* done) = 0; 0263 }; 0264 0265 } // namespace protobuf 0266 } // namespace google 0267 0268 #include "google/protobuf/port_undef.inc" 0269 0270 #endif // GOOGLE_PROTOBUF_SERVICE_H__
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |