From ceb41a541ca5964dab4197b004a20895c1905773 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Tue, 15 Jan 2019 23:40:34 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=8B=A6=E6=88=AA=E5=99=A8=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Handlers/DemoServerHandler.cs | 41 +++++++++++++++++++ .../Interceptors/DemoInterceptor.cs | 41 +++++++++++++++++++ .../Program.cs | 11 +++-- src/JT808.Protocol | 2 +- 4 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs create mode 100644 src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Handlers/DemoServerHandler.cs new file mode 100644 index 0000000..a11d92e --- /dev/null +++ b/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 + where TRequest : class + where TResponse : class + { + private readonly ServerCallContext _context; + + public DemoServerHandler(ServerCallContext context) + { + _context = context; + } + + public async Task UnaryServerHandler(TRequest request, UnaryServerMethod continuation) + { + var response = await continuation(request, _context).ConfigureAwait(false); + return response; + } + + public async Task ClientStreamingServerHandler(IAsyncStreamReader requestStream, ClientStreamingServerMethod continuation) + { + var response = await continuation(requestStream, _context).ConfigureAwait(false); + return response; + } + + public async Task ServerStreamingServerHandler(TRequest request, IServerStreamWriter responseStream, ServerStreamingServerMethod continuation) + { + await continuation(request, responseStream, _context).ConfigureAwait(false); + } + + public async Task DuplexStreamingServerHandler(IAsyncStreamReader requestStream, IServerStreamWriter responseStream, DuplexStreamingServerMethod continuation) + { + await continuation(requestStream, responseStream, _context).ConfigureAwait(false); + } + } +} \ No newline at end of file diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs new file mode 100644 index 0000000..8bf0c51 --- /dev/null +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs @@ -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 +{ + /// + /// + /// https://github.com/grpc/grpc/blob/master/doc/server_side_auth.md + /// https://github.com/Falco20019/grpc-opentracing + /// + class DemoInterceptor : Interceptor + { + public override Task UnaryServerHandler(TRequest request, ServerCallContext context, UnaryServerMethod continuation) + { + if(TryGetValue(context.RequestHeaders,"token",out var str)) + { + //context.Status = new Status(StatusCode.Unauthenticated, "Invalid token"); + return default(Task); + } + 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; + } + } +} diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs index a571b5d..508a1c0 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs @@ -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(); } diff --git a/src/JT808.Protocol b/src/JT808.Protocol index 641907a..16b8bfc 160000 --- a/src/JT808.Protocol +++ b/src/JT808.Protocol @@ -1 +1 @@ -Subproject commit 641907ad8373cf62d8973dedf76b84aaa894e52f +Subproject commit 16b8bfcd87686c809912b3ed722c5c035e1121cf From 3e0c5fa865ce1de9899f0a2d2b165110e8bfcc36 Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Wed, 16 Jan 2019 19:23:30 +0800 Subject: [PATCH 2/3] =?UTF-8?q?grpc=E6=8B=A6=E6=88=AA=E5=99=A8=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/ErrorExtensions.cs | 15 ++ .../Extensions/ServerCallContextExtensions.cs | 30 +++ .../JT808AtomicCounterService.proto | 6 - .../csharp/JT808AtomicCounterService.cs | 209 ++---------------- .../JT808AtomicCounterServiceGrpcImpl.cs | 22 +- .../Interceptors/DemoInterceptor.cs | 12 +- .../Program.cs | 10 +- src/JT808.DotNetty.Dashbord.sln | 2 +- 8 files changed, 85 insertions(+), 221 deletions(-) create mode 100644 src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ErrorExtensions.cs create mode 100644 src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ServerCallContextExtensions.cs diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ErrorExtensions.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ErrorExtensions.cs new file mode 100644 index 0000000..69d94e5 --- /dev/null +++ b/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)); + } + } +} diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ServerCallContextExtensions.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/Extensions/ServerCallContextExtensions.cs new file mode 100644 index 0000000..27ad2bf --- /dev/null +++ b/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); + } + } +} diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto index d093018..f840985 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/JT808AtomicCounterService.proto @@ -2,7 +2,6 @@ package JT808.GrpcDashbord.AtomicCounterGrpcService; -import "ResultReply.proto"; import "EmptyRequest.proto"; service AtomicCounterService{ @@ -11,11 +10,6 @@ service AtomicCounterService{ } message AtomicCounterReply{ - AtomicCounterInfo AtomicCounterInfo = 1; - JT808.GrpcDashbord.ServiceGrpcBase.ResultReply ResultReply = 2; -} - -message AtomicCounterInfo{ int32 MsgSuccessCount = 1; int32 MsgFailCount = 2; } \ No newline at end of file diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs index b0f3cd2..84216f3 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcProtocol/csharp/JT808AtomicCounterService.cs +++ b/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( string.Concat( "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, - 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(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 @@ -76,183 +70,14 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] 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); - } - - /// Field number for the "AtomicCounterInfo" field. - 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; - } - } - - /// Field number for the "ResultReply" field. - 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 { - private static readonly pb::MessageParser _parser = new pb::MessageParser(() => new AtomicCounterInfo()); - private pb::UnknownFieldSet _unknownFields; - [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public static pb::MessageParser 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_; msgFailCount_ = other.msgFailCount_; _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public AtomicCounterInfo Clone() { - return new AtomicCounterInfo(this); + public AtomicCounterReply Clone() { + return new AtomicCounterReply(this); } /// Field number for the "MsgSuccessCount" field. @@ -279,11 +104,11 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService { [global::System.Diagnostics.DebuggerNonUserCodeAttribute] public override bool Equals(object other) { - return Equals(other as AtomicCounterInfo); + return Equals(other as AtomicCounterReply); } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public bool Equals(AtomicCounterInfo other) { + public bool Equals(AtomicCounterReply other) { if (ReferenceEquals(other, null)) { return false; } @@ -342,7 +167,7 @@ namespace JT808.GrpcDashbord.AtomicCounterGrpcService { } [global::System.Diagnostics.DebuggerNonUserCodeAttribute] - public void MergeFrom(AtomicCounterInfo other) { + public void MergeFrom(AtomicCounterReply other) { if (other == null) { return; } diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs index 4e42c6e..e4ed823 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/GrpcImpls/JT808AtomicCounterServiceGrpcImpl.cs @@ -14,30 +14,16 @@ namespace JT808.DotNetty.Dashbord.GrpcServer.GrpcImpls public override Task GetTcpAtomicCounter(EmptyRequest request, ServerCallContext context) { 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); } public override Task GetUdpAtomicCounter(EmptyRequest request, ServerCallContext context) { 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); } } diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs index 8bf0c51..dd7d3d9 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Interceptors/DemoInterceptor.cs @@ -1,5 +1,7 @@ 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; @@ -11,17 +13,21 @@ namespace JT808.DotNetty.Dashbord.GrpcServer.Interceptors /// /// 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 /// class DemoInterceptor : Interceptor { - public override Task UnaryServerHandler(TRequest request, ServerCallContext context, UnaryServerMethod continuation) + public override Task UnaryServerHandler(TRequest request, ServerCallContext context, UnaryServerMethod continuation) { if(TryGetValue(context.RequestHeaders,"token",out var str)) { - //context.Status = new Status(StatusCode.Unauthenticated, "Invalid token"); - return default(Task); + //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) diff --git a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs index 508a1c0..f5cc394 100644 --- a/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs +++ b/src/JT808.DotNetty.Admin/JT808.DotNetty.Dashbord.GrpcServer/Program.cs @@ -33,7 +33,15 @@ namespace JT808.DotNetty.Dashbord.GrpcServer Metadata metadata = new Metadata(); metadata.Add("token", "test"); metadata.Add("request", "web"); - var result=client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest(), metadata); + + try + { + var result = client.GetTcpAtomicCounter(new GrpcDashbord.ServiceGrpcBase.EmptyRequest(), metadata); + } + catch (RpcException ex) + { + + } Console.ReadKey(); server.ShutdownAsync().Wait(); } diff --git a/src/JT808.DotNetty.Dashbord.sln b/src/JT808.DotNetty.Dashbord.sln index 191353c..0dacb2d 100644 --- a/src/JT808.DotNetty.Dashbord.sln +++ b/src/JT808.DotNetty.Dashbord.sln @@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JT808.DotNetty.Dashbord.Grp 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}" 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 Global GlobalSection(SolutionConfigurationPlatforms) = preSolution From 32325c0d135a8a5aa17915c058f08d8dd5b9b3de Mon Sep 17 00:00:00 2001 From: SmallChi <564952747@qq.com> Date: Thu, 24 Jan 2019 18:04:22 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- api/README.md | 2 ++ src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a0e372b..da7272a 100644 --- a/README.md +++ b/README.md @@ -90,7 +90,7 @@ static async Task Main(string[] args) // webapi客户端调用 services.AddHttpApi().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.LoggerFactory = p.GetRequiredService(); }); diff --git a/api/README.md b/api/README.md index 1253caa..1a24160 100644 --- a/api/README.md +++ b/api/README.md @@ -2,6 +2,8 @@ 基地址:127.0.0.1:828/jt808api/ +> 注意url格式 + 数据格式:只支持Json格式 默认端口:828 diff --git a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs index 1831706..70a63ed 100644 --- a/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs +++ b/src/JT808.DotNetty.Tests/JT808.DotNetty.Hosting/Program.cs @@ -55,7 +55,7 @@ namespace JT808.DotNetty.Hosting // webapi客户端调用 services.AddHttpApi().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.LoggerFactory = p.GetRequiredService(); });