Skip to content
Snippets Groups Projects
Commit bd3913ab authored by Tim Emiola's avatar Tim Emiola
Browse files

Merge pull request #1030 from jtattermusch/csharp_immutablecollections

Started using immutable collections and other code cleanup
parents 7459a063 286975fd
No related branches found
No related tags found
No related merge requests found
Showing
with 81 additions and 47 deletions
...@@ -38,8 +38,6 @@ using Grpc.Core.Internal; ...@@ -38,8 +38,6 @@ using Grpc.Core.Internal;
namespace Grpc.Core namespace Grpc.Core
{ {
// NOTE: this class is work-in-progress
/// <summary> /// <summary>
/// Helper methods for generated stubs to make RPC calls. /// Helper methods for generated stubs to make RPC calls.
/// </summary> /// </summary>
......
...@@ -36,6 +36,9 @@ using Grpc.Core.Internal; ...@@ -36,6 +36,9 @@ using Grpc.Core.Internal;
namespace Grpc.Core namespace Grpc.Core
{ {
/// <summary>
/// gRPC Channel
/// </summary>
public class Channel : IDisposable public class Channel : IDisposable
{ {
readonly ChannelSafeHandle handle; readonly ChannelSafeHandle handle;
......
...@@ -30,6 +30,7 @@ ...@@ -30,6 +30,7 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
...@@ -37,33 +38,18 @@ using Grpc.Core.Internal; ...@@ -37,33 +38,18 @@ using Grpc.Core.Internal;
namespace Grpc.Core namespace Grpc.Core
{ {
// TODO: should we be using the builder pattern? /// <summary>
/// gRPC channel options.
/// </summary>
public class ChannelArgs public class ChannelArgs
{ {
public const string SslTargetNameOverrideKey = "grpc.ssl_target_name_override"; public const string SslTargetNameOverrideKey = "grpc.ssl_target_name_override";
public class Builder readonly ImmutableDictionary<string, string> stringArgs;
{
Dictionary<string, string> stringArgs = new Dictionary<string, string>();
// TODO: AddInteger not supported yet.
public Builder AddString(string key, string value)
{
stringArgs.Add(key, value);
return this;
}
public ChannelArgs Build()
{
return new ChannelArgs(stringArgs);
}
}
Dictionary<string, string> stringArgs;
private ChannelArgs(Dictionary<string, string> stringArgs) private ChannelArgs(ImmutableDictionary<string, string> stringArgs)
{ {
// TODO: use immutable dict? this.stringArgs = stringArgs;
this.stringArgs = new Dictionary<string, string>(stringArgs);
} }
public string GetSslTargetNameOverride() public string GetSslTargetNameOverride()
...@@ -76,11 +62,28 @@ namespace Grpc.Core ...@@ -76,11 +62,28 @@ namespace Grpc.Core
return null; return null;
} }
public static Builder NewBuilder() public static Builder CreateBuilder()
{ {
return new Builder(); return new Builder();
} }
public class Builder
{
readonly Dictionary<string, string> stringArgs = new Dictionary<string, string>();
// TODO: AddInteger not supported yet.
public Builder AddString(string key, string value)
{
stringArgs.Add(key, value);
return this;
}
public ChannelArgs Build()
{
return new ChannelArgs(stringArgs.ToImmutableDictionary());
}
}
/// <summary> /// <summary>
/// Creates native object for the channel arguments. /// Creates native object for the channel arguments.
/// </summary> /// </summary>
......
...@@ -36,6 +36,9 @@ using Grpc.Core.Internal; ...@@ -36,6 +36,9 @@ using Grpc.Core.Internal;
namespace Grpc.Core namespace Grpc.Core
{ {
/// <summary>
/// Client-side credentials.
/// </summary>
public abstract class Credentials public abstract class Credentials
{ {
/// <summary> /// <summary>
......
...@@ -31,6 +31,9 @@ ...@@ -31,6 +31,9 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Collections.Immutable">
<HintPath>..\packages\System.Collections.Immutable.1.1.34-rc\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Internal\GrpcLog.cs" /> <Compile Include="Internal\GrpcLog.cs" />
...@@ -54,7 +57,7 @@ ...@@ -54,7 +57,7 @@
<Compile Include="Internal\ServerSafeHandle.cs" /> <Compile Include="Internal\ServerSafeHandle.cs" />
<Compile Include="Method.cs" /> <Compile Include="Method.cs" />
<Compile Include="ServerCalls.cs" /> <Compile Include="ServerCalls.cs" />
<Compile Include="ServerCallHandler.cs" /> <Compile Include="Internal\ServerCallHandler.cs" />
<Compile Include="Marshaller.cs" /> <Compile Include="Marshaller.cs" />
<Compile Include="ServerServiceDefinition.cs" /> <Compile Include="ServerServiceDefinition.cs" />
<Compile Include="Utils\RecordingObserver.cs" /> <Compile Include="Utils\RecordingObserver.cs" />
...@@ -77,6 +80,9 @@ ...@@ -77,6 +80,9 @@
<Compile Include="Internal\ServerCredentialsSafeHandle.cs" /> <Compile Include="Internal\ServerCredentialsSafeHandle.cs" />
<Compile Include="ServerCredentials.cs" /> <Compile Include="ServerCredentials.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Choose> <Choose>
<!-- Under older versions of Monodevelop, Choose is not supported and is just <!-- Under older versions of Monodevelop, Choose is not supported and is just
ignored, which gives us the desired effect. --> ignored, which gives us the desired effect. -->
......
...@@ -36,7 +36,7 @@ using System.Linq; ...@@ -36,7 +36,7 @@ using System.Linq;
using Grpc.Core.Internal; using Grpc.Core.Internal;
using Grpc.Core.Utils; using Grpc.Core.Utils;
namespace Grpc.Core namespace Grpc.Core.Internal
{ {
internal interface IServerCallHandler internal interface IServerCallHandler
{ {
......
...@@ -66,6 +66,9 @@ namespace Grpc.Core ...@@ -66,6 +66,9 @@ namespace Grpc.Core
} }
} }
/// <summary>
/// Utilities for creating marshallers.
/// </summary>
public static class Marshallers public static class Marshallers
{ {
public static Marshaller<T> Create<T>(Func<T, byte[]> serializer, Func<byte[], T> deserializer) public static Marshaller<T> Create<T>(Func<T, byte[]> serializer, Func<byte[], T> deserializer)
......
...@@ -35,6 +35,9 @@ using System; ...@@ -35,6 +35,9 @@ using System;
namespace Grpc.Core namespace Grpc.Core
{ {
/// <summary>
/// Thrown when remote procedure call fails.
/// </summary>
public class RpcException : Exception public class RpcException : Exception
{ {
private readonly Status status; private readonly Status status;
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#endregion #endregion
using System; using System;
using Grpc.Core.Internal;
namespace Grpc.Core namespace Grpc.Core
{ {
......
...@@ -33,10 +33,14 @@ ...@@ -33,10 +33,14 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using Grpc.Core.Internal; using Grpc.Core.Internal;
namespace Grpc.Core namespace Grpc.Core
{ {
/// <summary>
/// Server side credentials.
/// </summary>
public abstract class ServerCredentials public abstract class ServerCredentials
{ {
/// <summary> /// <summary>
...@@ -51,8 +55,8 @@ namespace Grpc.Core ...@@ -51,8 +55,8 @@ namespace Grpc.Core
/// </summary> /// </summary>
public class KeyCertificatePair public class KeyCertificatePair
{ {
string certChain; readonly string certChain;
string privateKey; readonly string privateKey;
public KeyCertificatePair(string certChain, string privateKey) public KeyCertificatePair(string certChain, string privateKey)
{ {
...@@ -82,10 +86,9 @@ namespace Grpc.Core ...@@ -82,10 +86,9 @@ namespace Grpc.Core
/// </summary> /// </summary>
public class SslServerCredentials : ServerCredentials public class SslServerCredentials : ServerCredentials
{ {
// TODO: immutable list... ImmutableList<KeyCertificatePair> keyCertPairs;
List<KeyCertificatePair> keyCertPairs;
public SslServerCredentials(List<KeyCertificatePair> keyCertPairs) public SslServerCredentials(ImmutableList<KeyCertificatePair> keyCertPairs)
{ {
this.keyCertPairs = keyCertPairs; this.keyCertPairs = keyCertPairs;
} }
......
...@@ -33,22 +33,26 @@ ...@@ -33,22 +33,26 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using Grpc.Core.Internal;
namespace Grpc.Core namespace Grpc.Core
{ {
/// <summary>
/// Mapping of method names to server call handlers.
/// </summary>
public class ServerServiceDefinition public class ServerServiceDefinition
{ {
readonly string serviceName; readonly string serviceName;
// TODO: we would need an immutable dictionary here... readonly ImmutableDictionary<string, IServerCallHandler> callHandlers;
readonly Dictionary<string, IServerCallHandler> callHandlers;
private ServerServiceDefinition(string serviceName, Dictionary<string, IServerCallHandler> callHandlers) private ServerServiceDefinition(string serviceName, ImmutableDictionary<string, IServerCallHandler> callHandlers)
{ {
this.serviceName = serviceName; this.serviceName = serviceName;
this.callHandlers = new Dictionary<string, IServerCallHandler>(callHandlers); this.callHandlers = callHandlers;
} }
internal Dictionary<string, IServerCallHandler> CallHandlers internal ImmutableDictionary<string, IServerCallHandler> CallHandlers
{ {
get get
{ {
...@@ -89,7 +93,7 @@ namespace Grpc.Core ...@@ -89,7 +93,7 @@ namespace Grpc.Core
public ServerServiceDefinition Build() public ServerServiceDefinition Build()
{ {
return new ServerServiceDefinition(serviceName, callHandlers); return new ServerServiceDefinition(serviceName, callHandlers.ToImmutableDictionary());
} }
} }
} }
......
...@@ -35,9 +35,9 @@ using System; ...@@ -35,9 +35,9 @@ using System;
namespace Grpc.Core namespace Grpc.Core
{ {
// TODO: element names should changed to comply with C# naming conventions.
/// <summary> /// <summary>
/// based on grpc_status_code from grpc/status.h /// Result of a remote procedure call.
/// Based on grpc_status_code from grpc/status.h
/// </summary> /// </summary>
public enum StatusCode public enum StatusCode
{ {
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="System.Collections.Immutable" version="1.1.34-rc" targetFramework="net45" />
</packages>
\ No newline at end of file
...@@ -39,6 +39,10 @@ ...@@ -39,6 +39,10 @@
<Reference Include="Google.ProtocolBuffers"> <Reference Include="Google.ProtocolBuffers">
<HintPath>..\packages\Google.ProtocolBuffers.2.4.1.521\lib\net40\Google.ProtocolBuffers.dll</HintPath> <HintPath>..\packages\Google.ProtocolBuffers.2.4.1.521\lib\net40\Google.ProtocolBuffers.dll</HintPath>
</Reference> </Reference>
<Reference Include="System.Collections.Immutable, Version=1.1.34.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\System.Collections.Immutable.1.1.34-rc\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
...@@ -76,8 +80,5 @@ ...@@ -76,8 +80,5 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup />
<Folder Include="proto\" />
<Folder Include="data\" />
</ItemGroup>
</Project> </Project>
\ No newline at end of file
...@@ -109,7 +109,7 @@ namespace Grpc.IntegrationTesting ...@@ -109,7 +109,7 @@ namespace Grpc.IntegrationTesting
ChannelArgs channelArgs = null; ChannelArgs channelArgs = null;
if (!string.IsNullOrEmpty(options.serverHostOverride)) if (!string.IsNullOrEmpty(options.serverHostOverride))
{ {
channelArgs = ChannelArgs.NewBuilder() channelArgs = ChannelArgs.CreateBuilder()
.AddString(ChannelArgs.SslTargetNameOverrideKey, options.serverHostOverride).Build(); .AddString(ChannelArgs.SslTargetNameOverrideKey, options.serverHostOverride).Build();
} }
......
...@@ -62,7 +62,7 @@ namespace Grpc.IntegrationTesting ...@@ -62,7 +62,7 @@ namespace Grpc.IntegrationTesting
int port = server.AddPort(host + ":0", TestCredentials.CreateTestServerCredentials()); int port = server.AddPort(host + ":0", TestCredentials.CreateTestServerCredentials());
server.Start(); server.Start();
var channelArgs = ChannelArgs.NewBuilder() var channelArgs = ChannelArgs.CreateBuilder()
.AddString(ChannelArgs.SslTargetNameOverrideKey, TestCredentials.DefaultHostOverride).Build(); .AddString(ChannelArgs.SslTargetNameOverrideKey, TestCredentials.DefaultHostOverride).Build();
channel = new Channel(host + ":" + port, TestCredentials.CreateTestClientCredentials(true), channelArgs); channel = new Channel(host + ":" + port, TestCredentials.CreateTestClientCredentials(true), channelArgs);
......
...@@ -33,6 +33,7 @@ ...@@ -33,6 +33,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
...@@ -77,7 +78,7 @@ namespace Grpc.IntegrationTesting ...@@ -77,7 +78,7 @@ namespace Grpc.IntegrationTesting
var keyCertPair = new KeyCertificatePair( var keyCertPair = new KeyCertificatePair(
File.ReadAllText(ServerCertChainPath), File.ReadAllText(ServerCertChainPath),
File.ReadAllText(ServerPrivateKeyPath)); File.ReadAllText(ServerPrivateKeyPath));
return new SslServerCredentials(new List<KeyCertificatePair> { keyCertPair }); return new SslServerCredentials(ImmutableList.Create(keyCertPair));
} }
} }
} }
...@@ -2,4 +2,5 @@ ...@@ -2,4 +2,5 @@
<packages> <packages>
<package id="Google.ProtocolBuffers" version="2.4.1.521" targetFramework="net45" /> <package id="Google.ProtocolBuffers" version="2.4.1.521" targetFramework="net45" />
<package id="NUnit" version="2.6.4" targetFramework="net45" /> <package id="NUnit" version="2.6.4" targetFramework="net45" />
<package id="System.Collections.Immutable" version="1.1.34-rc" targetFramework="net45" />
</packages> </packages>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment