@@ -0,0 +1,41 @@ | |||||
using System; | |||||
using System.Linq; | |||||
using System.Threading.Tasks; | |||||
using Grpc.Core; | |||||
namespace JT808.DotNetty.Dashbord.GrpcServer | |||||
{ | |||||
public class DemoServerHandler<TRequest, TResponse> | |||||
where TRequest : class | |||||
where TResponse : class | |||||
{ | |||||
private readonly ServerCallContext _context; | |||||
public DemoServerHandler(ServerCallContext context) | |||||
{ | |||||
_context = context; | |||||
} | |||||
public async Task<TResponse> UnaryServerHandler(TRequest request, UnaryServerMethod<TRequest, TResponse> continuation) | |||||
{ | |||||
var response = await continuation(request, _context).ConfigureAwait(false); | |||||
return response; | |||||
} | |||||
public async Task<TResponse> ClientStreamingServerHandler(IAsyncStreamReader<TRequest> requestStream, ClientStreamingServerMethod<TRequest, TResponse> continuation) | |||||
{ | |||||
var response = await continuation(requestStream, _context).ConfigureAwait(false); | |||||
return response; | |||||
} | |||||
public async Task ServerStreamingServerHandler(TRequest request, IServerStreamWriter<TResponse> responseStream, ServerStreamingServerMethod<TRequest, TResponse> continuation) | |||||
{ | |||||
await continuation(request, responseStream, _context).ConfigureAwait(false); | |||||
} | |||||
public async Task DuplexStreamingServerHandler(IAsyncStreamReader<TRequest> requestStream, IServerStreamWriter<TResponse> responseStream, DuplexStreamingServerMethod<TRequest, TResponse> continuation) | |||||
{ | |||||
await continuation(requestStream, responseStream, _context).ConfigureAwait(false); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,41 @@ | |||||
using Grpc.Core; | |||||
using Grpc.Core.Interceptors; | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
namespace JT808.DotNetty.Dashbord.GrpcServer.Interceptors | |||||
{ | |||||
/// <summary> | |||||
/// | |||||
/// https://github.com/grpc/grpc/blob/master/doc/server_side_auth.md | |||||
/// https://github.com/Falco20019/grpc-opentracing | |||||
/// </summary> | |||||
class DemoInterceptor : Interceptor | |||||
{ | |||||
public override Task<TResponse> UnaryServerHandler<TRequest, TResponse>(TRequest request, ServerCallContext context, UnaryServerMethod<TRequest, TResponse> continuation) | |||||
{ | |||||
if(TryGetValue(context.RequestHeaders,"token",out var str)) | |||||
{ | |||||
//context.Status = new Status(StatusCode.Unauthenticated, "Invalid token"); | |||||
return default(Task<TResponse>); | |||||
} | |||||
return continuation(request, context); | |||||
} | |||||
private bool TryGetValue(Metadata metadata,string key,out string value) | |||||
{ | |||||
foreach(var item in metadata) | |||||
{ | |||||
if(item.Key== key) | |||||
{ | |||||
value = item.Value; | |||||
return true; | |||||
} | |||||
} | |||||
value = ""; | |||||
return false; | |||||
} | |||||
} | |||||
} |
@@ -1,5 +1,7 @@ | |||||
using Grpc.Core; | using Grpc.Core; | ||||
using Grpc.Core.Interceptors; | |||||
using JT808.DotNetty.Dashbord.GrpcServer.GrpcImpls; | using JT808.DotNetty.Dashbord.GrpcServer.GrpcImpls; | ||||
using JT808.DotNetty.Dashbord.GrpcServer.Interceptors; | |||||
using JT808.GrpcDashbord.AtomicCounterGrpcService; | using JT808.GrpcDashbord.AtomicCounterGrpcService; | ||||
using System; | using System; | ||||
using System.Threading; | using System.Threading; | ||||
@@ -14,7 +16,8 @@ namespace JT808.DotNetty.Dashbord.GrpcServer | |||||
var server = new Server | var server = new Server | ||||
{ | { | ||||
Services = { | Services = { | ||||
BindService(new JT808AtomicCounterServiceGrpcImpl()), | |||||
BindService(new JT808AtomicCounterServiceGrpcImpl()) | |||||
.Intercept(new DemoInterceptor()), | |||||
}, | }, | ||||
Ports = { | Ports = { | ||||
new ServerPort("0.0.0.0", 14000,ServerCredentials.Insecure) | new ServerPort("0.0.0.0", 14000,ServerCredentials.Insecure) | ||||
@@ -26,9 +29,11 @@ namespace JT808.DotNetty.Dashbord.GrpcServer | |||||
Console.WriteLine(string.Format("RPC server {0} listening on port {1}", item.Host, item.Port)); | Console.WriteLine(string.Format("RPC server {0} listening on port {1}", item.Host, item.Port)); | ||||
} | } | ||||
server.Start(); | server.Start(); | ||||
AtomicCounterServiceClient client = new AtomicCounterServiceClient(new Channel("127.0.0.1:14000", ChannelCredentials.Insecure)); | AtomicCounterServiceClient client = new AtomicCounterServiceClient(new Channel("127.0.0.1:14000", ChannelCredentials.Insecure)); | ||||
var result=client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest()); | |||||
Metadata metadata = new Metadata(); | |||||
metadata.Add("token", "test"); | |||||
metadata.Add("request", "web"); | |||||
var result=client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest(), metadata); | |||||
Console.ReadKey(); | Console.ReadKey(); | ||||
server.ShutdownAsync().Wait(); | server.ShutdownAsync().Wait(); | ||||
} | } | ||||
@@ -1 +1 @@ | |||||
Subproject commit 641907ad8373cf62d8973dedf76b84aaa894e52f | |||||
Subproject commit 16b8bfcd87686c809912b3ed722c5c035e1121cf |