#region Copyright notice and license
// Copyright 2015 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Grpc.Core.Interceptors;
using Grpc.Core.Internal;
using Grpc.Core.Utils;
namespace Grpc.Core
{
///
/// Mapping of method names to server call handlers.
/// Normally, the ServerServiceDefinition objects will be created by the BindService factory method
/// that is part of the autogenerated code for a protocol buffers service definition.
///
public class ServerServiceDefinition
{
readonly ReadOnlyDictionary callHandlers;
internal ServerServiceDefinition(Dictionary callHandlers)
{
this.callHandlers = new ReadOnlyDictionary(callHandlers);
}
internal IDictionary CallHandlers
{
get
{
return this.callHandlers;
}
}
///
/// Creates a new builder object for ServerServiceDefinition.
///
/// The builder object.
public static Builder CreateBuilder()
{
return new Builder();
}
///
/// Builder class for .
///
public class Builder
{
readonly Dictionary callHandlers = new Dictionary();
///
/// Creates a new instance of builder.
///
public Builder()
{
}
///
/// Adds a definitions for a single request - single response method.
///
/// The request message class.
/// The response message class.
/// The method.
/// The method handler.
/// This builder instance.
public Builder AddMethod(
Method method,
UnaryServerMethod handler)
where TRequest : class
where TResponse : class
{
callHandlers.Add(method.FullName, ServerCalls.UnaryCall(method, handler));
return this;
}
///
/// Adds a definitions for a client streaming method.
///
/// The request message class.
/// The response message class.
/// The method.
/// The method handler.
/// This builder instance.
public Builder AddMethod(
Method method,
ClientStreamingServerMethod handler)
where TRequest : class
where TResponse : class
{
callHandlers.Add(method.FullName, ServerCalls.ClientStreamingCall(method, handler));
return this;
}
///
/// Adds a definitions for a server streaming method.
///
/// The request message class.
/// The response message class.
/// The method.
/// The method handler.
/// This builder instance.
public Builder AddMethod(
Method method,
ServerStreamingServerMethod handler)
where TRequest : class
where TResponse : class
{
callHandlers.Add(method.FullName, ServerCalls.ServerStreamingCall(method, handler));
return this;
}
///
/// Adds a definitions for a bidirectional streaming method.
///
/// The request message class.
/// The response message class.
/// The method.
/// The method handler.
/// This builder instance.
public Builder AddMethod(
Method method,
DuplexStreamingServerMethod handler)
where TRequest : class
where TResponse : class
{
callHandlers.Add(method.FullName, ServerCalls.DuplexStreamingCall(method, handler));
return this;
}
///
/// Creates an immutable ServerServiceDefinition from this builder.
///
/// The ServerServiceDefinition object.
public ServerServiceDefinition Build()
{
return new ServerServiceDefinition(callHandlers);
}
}
}
}