Pārlūkot izejas kodu

拦截器测试

tags/v1.1.0
SmallChi pirms 6 gadiem
vecāks
revīzija
ceb41a541c
4 mainītis faili ar 91 papildinājumiem un 4 dzēšanām
  1. +41
    -0
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs
  2. +41
    -0
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs
  3. +8
    -3
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs
  4. +1
    -1
      src/JT808.Protocol

+ 41
- 0
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs Parādīt failu

@@ -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);
}
}
}

+ 41
- 0
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs Parādīt failu

@@ -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;
}
}
}

+ 8
- 3
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs Parādīt failu

@@ -1,5 +1,7 @@
using Grpc.Core;
using Grpc.Core.Interceptors;
using JT808.DotNetty.Dashbord.GrpcServer.GrpcImpls;
using JT808.DotNetty.Dashbord.GrpcServer.Interceptors;
using JT808.GrpcDashbord.AtomicCounterGrpcService;
using System;
using System.Threading;
@@ -14,7 +16,8 @@ namespace JT808.DotNetty.Dashbord.GrpcServer
var server = new Server
{
Services = {
BindService(new JT808AtomicCounterServiceGrpcImpl()),
BindService(new JT808AtomicCounterServiceGrpcImpl())
.Intercept(new DemoInterceptor()),
},
Ports = {
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));
}
server.Start();

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();
server.ShutdownAsync().Wait();
}


+ 1
- 1
src/JT808.Protocol

@@ -1 +1 @@
Subproject commit 641907ad8373cf62d8973dedf76b84aaa894e52f
Subproject commit 16b8bfcd87686c809912b3ed722c5c035e1121cf

Notiek ielāde…
Atcelt
Saglabāt