TK 6年前
コミット
670c6518e3
13個のファイルの変更176行の追加223行の削除
  1. +1
    -1
      README.md
  2. +2
    -0
      api/README.md
  3. +15
    -0
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ErrorExtensions.cs
  4. +30
    -0
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ServerCallContextExtensions.cs
  5. +0
    -6
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto
  6. +17
    -192
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs
  7. +4
    -18
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs
  8. +41
    -0
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs
  9. +47
    -0
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs
  10. +16
    -3
      src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs
  11. +1
    -1
      src/JT808.DotNetty.Dashbord.sln
  12. +1
    -1
      src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs
  13. +1
    -1
      src/JT808.Protocol

+ 1
- 1
README.md ファイルの表示

@@ -90,7 +90,7 @@ static async Task Main(string[] args)
// webapi客户端调用 // webapi客户端调用
services.AddHttpApi<IJT808DotNettyWebApi>().ConfigureHttpApiConfig((c, p) => services.AddHttpApi<IJT808DotNettyWebApi>().ConfigureHttpApiConfig((c, p) =>
{ {
c.HttpHost = new Uri("http://localhost:12828/");
c.HttpHost = new Uri("http://localhost:12828/jt808api/");
c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
c.LoggerFactory = p.GetRequiredService<ILoggerFactory>(); c.LoggerFactory = p.GetRequiredService<ILoggerFactory>();
}); });


+ 2
- 0
api/README.md ファイルの表示

@@ -2,6 +2,8 @@


基地址:127.0.0.1:828/jt808api/ 基地址:127.0.0.1:828/jt808api/


> 注意url格式

数据格式:只支持Json格式 数据格式:只支持Json格式


默认端口:828 默认端口:828


+ 15
- 0
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ErrorExtensions.cs ファイルの表示

@@ -0,0 +1,15 @@
using Grpc.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace JT808.DotNetty.Dashbord.GrpcProtocol.Extensions
{
public static class ErrorExtensions
{
public static void Unauthenticated(string msg= "Invalid Token")
{
throw new Grpc.Core.RpcException(new Status(StatusCode.Unauthenticated, msg));
}
}
}

+ 30
- 0
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ServerCallContextExtensions.cs ファイルの表示

@@ -0,0 +1,30 @@
using Grpc.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace JT808.DotNetty.Dashbord.GrpcProtocol.Extensions
{
public static class ServerCallContextExtensions
{
public static void SetResultStatus(this ServerCallContext serverCallContext, StatusCode statusCode, string detail)
{
serverCallContext.Status = new Status(statusCode, detail);
}

public static void Ok(this ServerCallContext serverCallContext,string detail="")
{
serverCallContext.Status = new Status(StatusCode.OK, detail);
}

public static void Auth(this ServerCallContext serverCallContext, string detail = "")
{
serverCallContext.Status = new Status(StatusCode.Unauthenticated, detail);
}

public static void InternalError(this ServerCallContext serverCallContext, string detail = "")
{
serverCallContext.Status = new Status(StatusCode.Internal, detail);
}
}
}

+ 0
- 6
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto ファイルの表示

@@ -2,7 +2,6 @@


package JT808.GrpcDashbord.AtomicCounterGrpcService; package JT808.GrpcDashbord.AtomicCounterGrpcService;


import "ResultReply.proto";
import "EmptyRequest.proto"; import "EmptyRequest.proto";


service AtomicCounterService{ service AtomicCounterService{
@@ -11,11 +10,6 @@ service AtomicCounterService{
} }


message AtomicCounterReply{ message AtomicCounterReply{
AtomicCounterInfo AtomicCounterInfo = 1;
JT808.GrpcDashbord.ServiceGrpcBase.ResultReply ResultReply = 2;
}

message AtomicCounterInfo{
int32 MsgSuccessCount = 1; int32 MsgSuccessCount = 1;
int32 MsgFailCount = 2; int32 MsgFailCount = 2;
} }

+ 17
- 192
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs ファイルの表示

@@ -25,26 +25,20 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService {
byte[] descriptorData = global::System.Convert.FromBase64String( byte[] descriptorData = global::System.Convert.FromBase64String(
string.Concat( string.Concat(
"Ch9KVDgwOEF0b21pY0NvdW50ZXJTZXJ2aWNlLnByb3RvEitKVDgwOC5HcnBj", "Ch9KVDgwOEF0b21pY0NvdW50ZXJTZXJ2aWNlLnByb3RvEitKVDgwOC5HcnBj",
"RGFzaGJvcmQuQXRvbWljQ291bnRlckdycGNTZXJ2aWNlGhFSZXN1bHRSZXBs",
"eS5wcm90bxoSRW1wdHlSZXF1ZXN0LnByb3RvIrUBChJBdG9taWNDb3VudGVy",
"UmVwbHkSWQoRQXRvbWljQ291bnRlckluZm8YASABKAsyPi5KVDgwOC5HcnBj",
"RGFzaGJvcmQuQXRvbWljQ291bnRlckdycGNTZXJ2aWNlLkF0b21pY0NvdW50",
"ZXJJbmZvEkQKC1Jlc3VsdFJlcGx5GAIgASgLMi8uSlQ4MDguR3JwY0Rhc2hi",
"b3JkLlNlcnZpY2VHcnBjQmFzZS5SZXN1bHRSZXBseSJCChFBdG9taWNDb3Vu",
"dGVySW5mbxIXCg9Nc2dTdWNjZXNzQ291bnQYASABKAUSFAoMTXNnRmFpbENv",
"dW50GAIgASgFMrACChRBdG9taWNDb3VudGVyU2VydmljZRKKAQoTR2V0VGNw",
"QXRvbWljQ291bnRlchIwLkpUODA4LkdycGNEYXNoYm9yZC5TZXJ2aWNlR3Jw",
"Y0Jhc2UuRW1wdHlSZXF1ZXN0Gj8uSlQ4MDguR3JwY0Rhc2hib3JkLkF0b21p",
"Y0NvdW50ZXJHcnBjU2VydmljZS5BdG9taWNDb3VudGVyUmVwbHkiABKKAQoT",
"R2V0VWRwQXRvbWljQ291bnRlchIwLkpUODA4LkdycGNEYXNoYm9yZC5TZXJ2",
"aWNlR3JwY0Jhc2UuRW1wdHlSZXF1ZXN0Gj8uSlQ4MDguR3JwY0Rhc2hib3Jk",
"LkF0b21pY0NvdW50ZXJHcnBjU2VydmljZS5BdG9taWNDb3VudGVyUmVwbHki",
"AGIGcHJvdG8z"));
"RGFzaGJvcmQuQXRvbWljQ291bnRlckdycGNTZXJ2aWNlGhJFbXB0eVJlcXVl",
"c3QucHJvdG8iQwoSQXRvbWljQ291bnRlclJlcGx5EhcKD01zZ1N1Y2Nlc3ND",
"b3VudBgBIAEoBRIUCgxNc2dGYWlsQ291bnQYAiABKAUysAIKFEF0b21pY0Nv",
"dW50ZXJTZXJ2aWNlEooBChNHZXRUY3BBdG9taWNDb3VudGVyEjAuSlQ4MDgu",
"R3JwY0Rhc2hib3JkLlNlcnZpY2VHcnBjQmFzZS5FbXB0eVJlcXVlc3QaPy5K",
"VDgwOC5HcnBjRGFzaGJvcmQuQXRvbWljQ291bnRlckdycGNTZXJ2aWNlLkF0",
"b21pY0NvdW50ZXJSZXBseSIAEooBChNHZXRVZHBBdG9taWNDb3VudGVyEjAu",
"SlQ4MDguR3JwY0Rhc2hib3JkLlNlcnZpY2VHcnBjQmFzZS5FbXB0eVJlcXVl",
"c3QaPy5KVDgwOC5HcnBjRGFzaGJvcmQuQXRvbWljQ291bnRlckdycGNTZXJ2",
"aWNlLkF0b21pY0NvdW50ZXJSZXBseSIAYgZwcm90bzM="));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReplyReflection.Descriptor, global::JT808.GrpcDashbord.ServiceGrpcBase.EmptyRequestReflection.Descriptor, },
new pbr::FileDescriptor[] { global::JT808.GrpcDashbord.ServiceGrpcBase.EmptyRequestReflection.Descriptor, },
new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(null, new pbr::GeneratedClrTypeInfo[] {
new pbr::GeneratedClrTypeInfo(typeof(global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterReply), global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterReply.Parser, new[]{ "AtomicCounterInfo", "ResultReply" }, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo), global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo.Parser, new[]{ "MsgSuccessCount", "MsgFailCount" }, null, null, null)
new pbr::GeneratedClrTypeInfo(typeof(global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterReply), global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterReply.Parser, new[]{ "MsgSuccessCount", "MsgFailCount" }, null, null, null)
})); }));
} }
#endregion #endregion
@@ -76,183 +70,14 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService {


[global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public AtomicCounterReply(AtomicCounterReply other) : this() { public AtomicCounterReply(AtomicCounterReply other) : this() {
atomicCounterInfo_ = other.atomicCounterInfo_ != null ? other.atomicCounterInfo_.Clone() : null;
resultReply_ = other.resultReply_ != null ? other.resultReply_.Clone() : null;
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public AtomicCounterReply Clone() {
return new AtomicCounterReply(this);
}

/// <summary>Field number for the "AtomicCounterInfo" field.</summary>
public const int AtomicCounterInfoFieldNumber = 1;
private global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo atomicCounterInfo_;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo AtomicCounterInfo {
get { return atomicCounterInfo_; }
set {
atomicCounterInfo_ = value;
}
}

/// <summary>Field number for the "ResultReply" field.</summary>
public const int ResultReplyFieldNumber = 2;
private global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReply resultReply_;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReply ResultReply {
get { return resultReply_; }
set {
resultReply_ = value;
}
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public override bool Equals(object other) {
return Equals(other as AtomicCounterReply);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public bool Equals(AtomicCounterReply other) {
if (ReferenceEquals(other, null)) {
return false;
}
if (ReferenceEquals(other, this)) {
return true;
}
if (!object.Equals(AtomicCounterInfo, other.AtomicCounterInfo)) return false;
if (!object.Equals(ResultReply, other.ResultReply)) return false;
return Equals(_unknownFields, other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public override int GetHashCode() {
int hash = 1;
if (atomicCounterInfo_ != null) hash ^= AtomicCounterInfo.GetHashCode();
if (resultReply_ != null) hash ^= ResultReply.GetHashCode();
if (_unknownFields != null) {
hash ^= _unknownFields.GetHashCode();
}
return hash;
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public override string ToString() {
return pb::JsonFormatter.ToDiagnosticString(this);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public void WriteTo(pb::CodedOutputStream output) {
if (atomicCounterInfo_ != null) {
output.WriteRawTag(10);
output.WriteMessage(AtomicCounterInfo);
}
if (resultReply_ != null) {
output.WriteRawTag(18);
output.WriteMessage(ResultReply);
}
if (_unknownFields != null) {
_unknownFields.WriteTo(output);
}
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public int CalculateSize() {
int size = 0;
if (atomicCounterInfo_ != null) {
size += 1 + pb::CodedOutputStream.ComputeMessageSize(AtomicCounterInfo);
}
if (resultReply_ != null) {
size += 1 + pb::CodedOutputStream.ComputeMessageSize(ResultReply);
}
if (_unknownFields != null) {
size += _unknownFields.CalculateSize();
}
return size;
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public void MergeFrom(AtomicCounterReply other) {
if (other == null) {
return;
}
if (other.atomicCounterInfo_ != null) {
if (atomicCounterInfo_ == null) {
atomicCounterInfo_ = new global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo();
}
AtomicCounterInfo.MergeFrom(other.AtomicCounterInfo);
}
if (other.resultReply_ != null) {
if (resultReply_ == null) {
resultReply_ = new global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReply();
}
ResultReply.MergeFrom(other.ResultReply);
}
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public void MergeFrom(pb::CodedInputStream input) {
uint tag;
while ((tag = input.ReadTag()) != 0) {
switch(tag) {
default:
_unknownFields = pb::UnknownFieldSet.MergeFieldFrom(_unknownFields, input);
break;
case 10: {
if (atomicCounterInfo_ == null) {
atomicCounterInfo_ = new global::JT808.GrpcDashbord.AtomicCounterGrpcService.AtomicCounterInfo();
}
input.ReadMessage(atomicCounterInfo_);
break;
}
case 18: {
if (resultReply_ == null) {
resultReply_ = new global::JT808.GrpcDashbord.ServiceGrpcBase.ResultReply();
}
input.ReadMessage(resultReply_);
break;
}
}
}
}

}

public sealed partial class AtomicCounterInfo : pb::IMessage<AtomicCounterInfo> {
private static readonly pb::MessageParser<AtomicCounterInfo> _parser = new pb::MessageParser<AtomicCounterInfo>(() => new AtomicCounterInfo());
private pb::UnknownFieldSet _unknownFields;
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public static pb::MessageParser<AtomicCounterInfo> Parser { get { return _parser; } }

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public static pbr::MessageDescriptor Descriptor {
get { return global::JT808.GrpcDashbord.AtomicCounterGrpcService.JT808AtomicCounterServiceReflection.Descriptor.MessageTypes[1]; }
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
pbr::MessageDescriptor pb::IMessage.Descriptor {
get { return Descriptor; }
}

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public AtomicCounterInfo() {
OnConstruction();
}

partial void OnConstruction();

[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public AtomicCounterInfo(AtomicCounterInfo other) : this() {
msgSuccessCount_ = other.msgSuccessCount_; msgSuccessCount_ = other.msgSuccessCount_;
msgFailCount_ = other.msgFailCount_; msgFailCount_ = other.msgFailCount_;
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
} }


[global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public AtomicCounterInfo Clone() {
return new AtomicCounterInfo(this);
public AtomicCounterReply Clone() {
return new AtomicCounterReply(this);
} }


/// <summary>Field number for the "MsgSuccessCount" field.</summary> /// <summary>Field number for the "MsgSuccessCount" field.</summary>
@@ -279,11 +104,11 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService {


[global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public override bool Equals(object other) { public override bool Equals(object other) {
return Equals(other as AtomicCounterInfo);
return Equals(other as AtomicCounterReply);
} }


[global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public bool Equals(AtomicCounterInfo other) {
public bool Equals(AtomicCounterReply other) {
if (ReferenceEquals(other, null)) { if (ReferenceEquals(other, null)) {
return false; return false;
} }
@@ -342,7 +167,7 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService {
} }


[global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
public void MergeFrom(AtomicCounterInfo other) {
public void MergeFrom(AtomicCounterReply other) {
if (other == null) { if (other == null) {
return; return;
} }


+ 4
- 18
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs ファイルの表示

@@ -14,30 +14,16 @@ namespace JT808.DotNetty.Dashbord.GrpcServer.GrpcImpls
public override Task<AtomicCounterReply> GetTcpAtomicCounter(EmptyRequest request, ServerCallContext context) public override Task<AtomicCounterReply> GetTcpAtomicCounter(EmptyRequest request, ServerCallContext context)
{ {
AtomicCounterReply atomicCounterReply = new AtomicCounterReply(); AtomicCounterReply atomicCounterReply = new AtomicCounterReply();
atomicCounterReply.AtomicCounterInfo = new AtomicCounterInfo
{
MsgFailCount = 10,
MsgSuccessCount = 11111
};
atomicCounterReply.ResultReply = new ResultReply
{
Code = ResultReply.Types.StatusCode.Success,
};
atomicCounterReply.MsgFailCount = 10;
atomicCounterReply.MsgSuccessCount = 1111;
return Task.FromResult(atomicCounterReply); return Task.FromResult(atomicCounterReply);
} }


public override Task<AtomicCounterReply> GetUdpAtomicCounter(EmptyRequest request, ServerCallContext context) public override Task<AtomicCounterReply> GetUdpAtomicCounter(EmptyRequest request, ServerCallContext context)
{ {
AtomicCounterReply atomicCounterReply = new AtomicCounterReply(); AtomicCounterReply atomicCounterReply = new AtomicCounterReply();
atomicCounterReply.AtomicCounterInfo = new AtomicCounterInfo
{
MsgFailCount = 50,
MsgSuccessCount = 10000
};
atomicCounterReply.ResultReply = new ResultReply
{
Code = ResultReply.Types.StatusCode.Success,
};
atomicCounterReply.MsgFailCount = 50;
atomicCounterReply.MsgSuccessCount = 1111;
return Task.FromResult(atomicCounterReply); return Task.FromResult(atomicCounterReply);
} }
} }


+ 41
- 0
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs ファイルの表示

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

+ 47
- 0
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs ファイルの表示

@@ -0,0 +1,47 @@
using Grpc.Core;
using Grpc.Core.Interceptors;
using JT808.DotNetty.Dashbord.GrpcProtocol.Extensions;
using JT808.GrpcDashbord.ServiceGrpcBase;
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
/// https://github.com/grpc/proposal/blob/master/L12-csharp-interceptors.md
/// https://stackoverflow.com/questions/52950210/populate-authcontext-in-grpc-c-sharp-from-jwt-authentication
/// https://github.com/grpc/grpc/tree/master/doc
/// </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))
{
//ErrorExtensions.Unauthenticated();
}
return continuation(request, context);
//return Task.FromResult(default(TResponse));
//return await 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;
}
}
}

+ 16
- 3
src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs ファイルの表示

@@ -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,19 @@ 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");
try
{
var result = client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest(), metadata);
}
catch (RpcException ex)
{

}
Console.ReadKey(); Console.ReadKey();
server.ShutdownAsync().Wait(); server.ShutdownAsync().Wait();
} }


+ 1
- 1
src/JT808.DotNetty.Dashbord.sln ファイルの表示

@@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Dashbord.Grp
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Dashbord", "JT808.DotNetty.Admin\JT808.DotNetty.Dashbord\JT808.DotNetty.Dashbord.csproj", "{80D1505D-44E1-4128-8900-B5C0AE7C69E5}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Dashbord", "JT808.DotNetty.Admin\JT808.DotNetty.Dashbord\JT808.DotNetty.Dashbord.csproj", "{80D1505D-44E1-4128-8900-B5C0AE7C69E5}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JT808.DotNetty.Dashbord.GrpcServer", "JT808.DotNetty.Admin\JT808.DotNetty.Dashbord.GrpcServer\JT808.DotNetty.Dashbord.GrpcServer.csproj", "{9F143F03-D90D-477F-9CE4-0BD1E2A1E379}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Dashbord.GrpcServer", "JT808.DotNetty.Admin\JT808.DotNetty.Dashbord.GrpcServer\JT808.DotNetty.Dashbord.GrpcServer.csproj", "{9F143F03-D90D-477F-9CE4-0BD1E2A1E379}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution


+ 1
- 1
src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs ファイルの表示

@@ -55,7 +55,7 @@ namespace JT808.DotNetty.Hosting
// webapi客户端调用 // webapi客户端调用
services.AddHttpApi<IJT808DotNettyWebApi>().ConfigureHttpApiConfig((c, p) => services.AddHttpApi<IJT808DotNettyWebApi>().ConfigureHttpApiConfig((c, p) =>
{ {
c.HttpHost = new Uri("http://localhost:12828/");
c.HttpHost = new Uri("http://localhost:12828/jt808api/");
c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff"; c.FormatOptions.DateTimeFormat = "yyyy-MM-dd HH:mm:ss.fff";
c.LoggerFactory = p.GetRequiredService<ILoggerFactory>(); c.LoggerFactory = p.GetRequiredService<ILoggerFactory>();
}); });


+ 1
- 1
src/JT808.Protocol

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

読み込み中…
キャンセル
保存