Remove RPC support.

It is expected that third parties will generate service/RPC code themselves - see gRPC as an example.
pull/317/head
Jon Skeet 10 years ago
parent e8310aa259
commit cc058e1118
  1. 30
      csharp/protos/extest/unittest_generic_services.proto
  2. 41
      csharp/protos/extest/unittest_rpc_interop.proto
  3. 42
      csharp/protos/extest/unittest_rpc_interop_lite.proto
  4. 28
      csharp/src/ProtocolBuffers.Serialization/Extensions.cs
  5. 63
      csharp/src/ProtocolBuffers/IRpcChannel.cs
  6. 125
      csharp/src/ProtocolBuffers/IRpcController.cs
  7. 78
      csharp/src/ProtocolBuffers/IRpcDispatch.cs
  8. 102
      csharp/src/ProtocolBuffers/IService.cs
  9. 5
      csharp/src/ProtocolBuffers/ProtocolBuffers.csproj
  10. 1
      csharp/src/ProtocolBuffers/ProtocolBuffersLite.csproj
  11. 79
      csharp/src/ProtocolBuffers/RpcUtil.cs

@ -1,30 +0,0 @@
syntax = "proto2";
// Additional options required for C# generation. File from copyright
// line onwards is as per original distribution.
import "google/protobuf/unittest.proto";
import "google/protobuf/unittest_custom_options.proto";
option csharp_namespace = "Google.ProtocolBuffers.TestProtos";
// option (google.protobuf.csharp_file_options).service_generator_type = GENERIC;
// We don't put this in a package within proto2 because we need to make sure
// that the generated code doesn't depend on being in the proto2 namespace.
package protobuf_unittest;
option optimize_for = SPEED;
service TestGenericService {
rpc Foo(FooRequest) returns (FooResponse);
rpc Bar(BarRequest) returns (BarResponse);
}
service TestGenericServiceWithCustomOptions {
option (service_opt1) = -9876543210;
rpc Foo(CustomOptionFooRequest) returns (CustomOptionFooResponse) {
option (method_opt1) = METHODOPT1_VAL2;
}
}

@ -1,41 +0,0 @@
// Additional options required for C# generation. File from copyright
// line onwards is as per original distribution.
import "google/protobuf/csharp_options.proto";
option (google.protobuf.csharp_file_options).namespace = "Google.ProtocolBuffers.TestProtos";
option (google.protobuf.csharp_file_options).umbrella_classname = "UnitTestRpcInterop";
option (google.protobuf.csharp_file_options).service_generator_type = IRPCDISPATCH;
option optimize_for = SPEED;
message SearchRequest {
repeated string Criteria = 1;
}
message SearchResponse {
message ResultItem {
required string url = 1;
optional string name = 2;
}
repeated ResultItem results = 1;
}
message RefineSearchRequest {
repeated string Criteria = 1;
required SearchResponse previous_results = 2;
}
service SearchService {
/*
Add this option to specify the GuidAttribute on the service interface
option (google.protobuf.csharp_service_options).interface_id = "{A65F0925-FD11-4f94-B166-89AC4F027205}";
*/
rpc Search (SearchRequest) returns (SearchResponse)
/*
Add this option to specify the DispIdAttribute on the service interface
{ option (google.protobuf.csharp_method_options).dispatch_id = 5; }
*/ ;
rpc RefineSearch (RefineSearchRequest) returns (SearchResponse);
}

@ -1,42 +0,0 @@
// Additional options required for C# generation. File from copyright
// line onwards is as per original distribution.
import "google/protobuf/csharp_options.proto";
option (google.protobuf.csharp_file_options).namespace = "Google.ProtocolBuffers.TestProtos";
option (google.protobuf.csharp_file_options).umbrella_classname = "UnitTestRpcInteropLite";
option (google.protobuf.csharp_file_options).service_generator_type = IRPCDISPATCH;
option optimize_for = LITE_RUNTIME;
package unittest_rpc_interop_lite;
message SearchRequest {
repeated string Criteria = 1;
}
message SearchResponse {
message ResultItem {
required string url = 1;
optional string name = 2;
}
repeated ResultItem results = 1;
}
message RefineSearchRequest {
repeated string Criteria = 1;
required SearchResponse previous_results = 2;
}
service SearchService {
/*
Add this option to specify the GuidAttribute on the service interface
option (google.protobuf.csharp_service_options).interface_id = "{A65F0925-FD11-4f94-B166-89AC4F027205}";
*/
rpc Search (SearchRequest) returns (SearchResponse)
/*
Add this option to specify the DispIdAttribute on the service interface
{ option (google.protobuf.csharp_method_options).dispatch_id = 5; }
*/ ;
rpc RefineSearch (RefineSearchRequest) returns (SearchResponse);
}

@ -180,34 +180,6 @@ namespace Google.ProtocolBuffers
return builder;
}
#endregion
#region IRpcServerStub Extensions
/// <summary>
/// Used to implement a service endpoint on an HTTP server. This works with services generated with the
/// service_generator_type option set to IRPCDISPATCH.
/// </summary>
/// <param name="stub">The service execution stub</param>
/// <param name="methodName">The name of the method being invoked</param>
/// <param name="options">optional arguments for the format reader/writer</param>
/// <param name="contentType">The mime type for the input stream</param>
/// <param name="input">The input stream</param>
/// <param name="responseType">The mime type for the output stream</param>
/// <param name="output">The output stream</param>
public static void HttpCallMethod(
#if !NOEXTENSIONS
this
#endif
IRpcServerStub stub, string methodName, MessageFormatOptions options,
string contentType, Stream input, string responseType, Stream output)
{
ICodedInputStream codedInput = MessageFormatFactory.CreateInputStream(options, contentType, input);
codedInput.ReadMessageStart();
IMessageLite response = stub.CallMethod(methodName, codedInput, options.ExtensionRegistry);
codedInput.ReadMessageEnd();
WriteTo(response, options, responseType, output);
}
#endregion
}
}

@ -1,63 +0,0 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers
{
/// <summary>
/// Interface for an RPC channel. A channel represents a communication line to
/// a service (IService implementation) which can be used to call that service's
/// methods. The service may be running on another machine. Normally, you should
/// not call an IRpcChannel directly, but instead construct a stub wrapping it.
/// Generated service classes contain a CreateStub method for precisely this purpose.
/// </summary>
public interface IRpcChannel
{
/// <summary>
/// Calls the given method of the remote service. This method is similar
/// to <see cref="IService.CallMethod" /> with one important difference: the
/// caller decides the types of the IMessage objects, not the implementation.
/// The request may be of any type as long as <c>request.Descriptor == method.InputType</c>.
/// The response passed to the callback will be of the same type as
/// <paramref name="responsePrototype"/> (which must be such that
/// <c>responsePrototype.Descriptor == method.OutputType</c>).
/// </summary>
void CallMethod(MethodDescriptor method, IRpcController controller,
IMessage request, IMessage responsePrototype, Action<IMessage> done);
}
}

@ -1,125 +0,0 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
namespace Google.ProtocolBuffers
{
/// <summary>
/// Mediates a single method call. The primary purpose of the controller
/// is to provide a way to manipulate settings specific to the
/// RPC implementation and to find out about RPC-level errors.
///
/// The methods provided by this interface are intended to be a "least
/// common denominator" set of features which we expect all implementations to
/// support. Specific implementations may provide more advanced features,
/// (e.g. deadline propagation).
/// </summary>
public interface IRpcController
{
#region Client side calls
// These calls may be made from the client side only. Their results
// are undefined on the server side (may throw exceptions).
/// <summary>
/// Resets the controller to its initial state so that it may be reused in
/// a new call. This can be called from the client side only. It must not
/// be called while an RPC is in progress.
/// </summary>
void Reset();
/// <summary>
/// After a call has finished, returns true if the call failed. The possible
/// reasons for failure depend on the RPC implementation. Failed must
/// only be called on the client side, and must not be called before a call has
/// finished.
/// </summary>
bool Failed { get; }
/// <summary>
/// If Failed is true, ErrorText returns a human-readable description of the error.
/// </summary>
string ErrorText { get; }
/// <summary>
/// Advises the RPC system that the caller desires that the RPC call be
/// canceled. The RPC system may cancel it immediately, may wait awhile and
/// then cancel it, or may not even cancel the call at all. If the call is
/// canceled, the "done" callback will still be called and the RpcController
/// will indicate that the call failed at that time.
/// </summary>
void StartCancel();
#endregion
#region Server side calls
// These calls may be made from the server side only. Their results
// are undefined on the client side (may throw exceptions).
/// <summary>
/// Causes Failed to return true on the client side. <paramref name="reason"/>
/// will be incorporated into the message returned by ErrorText.
/// If you find you need to return machine-readable information about
/// failures, you should incorporate it into your response protocol buffer
/// and should *not* call SetFailed.
/// </summary>
void SetFailed(string reason);
/// <summary>
/// If true, indicates that the client canceled the RPC, so the server may as
/// well give up on replying to it. This method must be called on the server
/// side only. The server should still call the final "done" callback.
/// </summary>
bool IsCanceled();
/// <summary>
/// Requests that the given callback be called when the RPC is canceled.
/// The parameter passed to the callback will always be null. The callback will
/// be called exactly once. If the RPC completes without being canceled, the
/// callback will be called after completion. If the RPC has already been canceled
/// when NotifyOnCancel is called, the callback will be called immediately.
///
/// NotifyOnCancel must be called no more than once per request. It must be
/// called on the server side only.
/// </summary>
/// <param name="callback"></param>
void NotifyOnCancel(Action<object> callback);
#endregion
}
}

@ -1,78 +0,0 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
namespace Google.ProtocolBuffers
{
/// <summary>
/// Provides an entry-point for transport listeners to call a specified method on a service
/// </summary>
public interface IRpcServerStub : IDisposable
{
/// <summary>
/// Calls the method identified by methodName and returns the message
/// </summary>
/// <param name="methodName">The method name on the service descriptor (case-sensitive)</param>
/// <param name="input">The ICodedInputStream to deserialize the call parameter from</param>
/// <param name="registry">The extension registry to use when deserializing the call parameter</param>
/// <returns>The message that was returned from the service's method</returns>
IMessageLite CallMethod(string methodName, ICodedInputStream input, ExtensionRegistry registry);
}
/// <summary>
/// Used to forward an invocation of a service method to a transport sender implementation
/// </summary>
public interface IRpcDispatch
{
/// <summary>
/// Calls the service member on the endpoint connected. This is generally done by serializing
/// the message, sending the bytes over a transport, and then deserializing the call parameter
/// to invoke the service's actual implementation via IRpcServerStub. Once the call has
/// completed the result message is serialized and returned to the originating endpoint.
/// </summary>
/// <typeparam name="TMessage">The type of the response message</typeparam>
/// <typeparam name="TBuilder">The type of of the response builder</typeparam>
/// <param name="method">The name of the method on the service</param>
/// <param name="request">The message instance provided to the service call</param>
/// <param name="response">The builder used to deserialize the response</param>
/// <returns>The resulting message of the service call</returns>
TMessage CallMethod<TMessage, TBuilder>(string method, IMessageLite request,
IBuilderLite<TMessage, TBuilder> response)
where TMessage : IMessageLite<TMessage, TBuilder>
where TBuilder : IBuilderLite<TMessage, TBuilder>;
}
}

@ -1,102 +0,0 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
using Google.ProtocolBuffers.Descriptors;
namespace Google.ProtocolBuffers
{
/// <summary>
/// Base interface for protocol-buffer-based RPC services. Services themselves
/// are abstract classes (implemented either by servers or as stubs) but they
/// implement this itnerface. The methods of this interface can be used to call
/// the methods of the service without knowing its exact type at compile time
/// (analagous to the IMessage interface).
/// </summary>
public interface IService
{
/// <summary>
/// The ServiceDescriptor describing this service and its methods.
/// </summary>
ServiceDescriptor DescriptorForType { get; }
/// <summary>
/// Call a method of the service specified by MethodDescriptor. This is
/// normally implemented as a simple switch that calls the standard
/// definitions of the service's methods.
/// <para>
/// Preconditions
/// <list>
/// <item><c>method.Service == DescriptorForType</c></item>
/// <item>request is of the exact same class as the object returned by GetRequestPrototype(method)</item>
/// <item>controller is of the correct type for the RPC implementation being used by this service.
/// For stubs, the "correct type" depends on the IRpcChannel which the stub is using. Server-side
/// implementations are expected to accept whatever type of IRpcController the server-side RPC implementation
/// uses.</item>
/// </list>
/// </para>
/// <para>
/// Postconditions
/// <list>
/// <item><paramref name="done" /> will be called when the method is complete.
/// This may before CallMethod returns or it may be at some point in the future.</item>
/// <item>The parameter to <paramref name="done"/> is the response. It will be of the
/// exact same type as would be returned by <see cref="GetResponsePrototype"/>.</item>
/// <item>If the RPC failed, the parameter to <paramref name="done"/> will be null.
/// Further details about the failure can be found by querying <paramref name="controller"/>.</item>
/// </list>
/// </para>
/// </summary>
void CallMethod(MethodDescriptor method, IRpcController controller,
IMessage request, Action<IMessage> done);
/// <summary>
/// CallMethod requires that the request passed in is of a particular implementation
/// of IMessage. This method gets the default instance of this type of a given method.
/// You can then call WeakCreateBuilderForType to create a builder to build an object which
/// you can then pass to CallMethod.
/// </summary>
IMessage GetRequestPrototype(MethodDescriptor method);
/// <summary>
/// Like GetRequestPrototype, but returns a prototype of the response message.
/// This is generally not needed because the IService implementation contructs
/// the response message itself, but it may be useful in some cases to know ahead
/// of time what type of object will be returned.
/// </summary>
IMessage GetResponsePrototype(MethodDescriptor method);
}
}

@ -123,16 +123,11 @@
<Compile Include="IMessage.cs" />
<Compile Include="IMessageLite.cs" />
<Compile Include="InvalidProtocolBufferException.cs" />
<Compile Include="IRpcChannel.cs" />
<Compile Include="IRpcController.cs" />
<Compile Include="IRpcDispatch.cs" />
<Compile Include="IService.cs" />
<Compile Include="MessageStreamIterator.cs" />
<Compile Include="MessageStreamWriter.cs" />
<Compile Include="MessageUtil.cs" />
<Compile Include="NameHelpers.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RpcUtil.cs" />
<Compile Include="SortedList.cs" />
<Compile Include="TextFormat.cs" />
<Compile Include="TextGenerator.cs" />

@ -75,7 +75,6 @@
<Compile Include="GeneratedMessageLite.cs" />
<Compile Include="ICodedInputStream.cs" />
<Compile Include="ICodedOutputStream.cs" />
<Compile Include="IRpcDispatch.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ByteString.cs" />
<Compile Include="CodedInputStream.cs" />

@ -1,79 +0,0 @@
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://github.com/jskeet/dotnet-protobufs/
// Original C++/Java/Python code:
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#endregion
using System;
namespace Google.ProtocolBuffers
{
/// <summary>
/// Grab-bag of utility functions useful when dealing with RPCs.
/// </summary>
public static class RpcUtil
{
/// <summary>
/// Converts an Action[IMessage] to an Action[T].
/// </summary>
public static Action<T> SpecializeCallback<T>(Action<IMessage> action)
where T : IMessage<T>
{
return message => action(message);
}
/// <summary>
/// Converts an Action[T] to an Action[IMessage].
/// The generalized action will accept any message object which has
/// the same descriptor, and will convert it to the correct class
/// before calling the original action. However, if the generalized
/// callback is given a message with a different descriptor, an
/// exception will be thrown.
/// </summary>
public static Action<IMessage> GeneralizeCallback<TMessage, TBuilder>(Action<TMessage> action,
TMessage defaultInstance)
where TMessage : class, IMessage<TMessage, TBuilder>
where TBuilder : IBuilder<TMessage, TBuilder>
{
return message =>
{
TMessage castMessage = message as TMessage;
if (castMessage == null)
{
castMessage = defaultInstance.CreateBuilderForType().MergeFrom(message).Build();
}
action(castMessage);
};
}
}
}
Loading…
Cancel
Save