You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2022/01/27 07:51:24 UTC

[thrift] branch master updated: THRIFT-5504 CA2254 Message template should be compile time constant Client netstd Patch: Jens Geyer

This is an automated email from the ASF dual-hosted git repository.

jensg pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/thrift.git


The following commit(s) were added to refs/heads/master by this push:
     new 561bc9a  THRIFT-5504 CA2254 Message template should be compile time constant Client netstd Patch: Jens Geyer
561bc9a is described below

commit 561bc9ab7918bfddcfd0ecc722b162e43a9bc46b
Author: Jens Geyer <je...@apache.org>
AuthorDate: Wed Jan 26 22:38:04 2022 +0100

    THRIFT-5504 CA2254 Message template should be compile time constant
    Client netstd
    Patch: Jens Geyer
---
 .../ITAsyncProcessor.cs => GlobalSuppressions.cs}  | 27 ++++++++++++----------
 lib/netstd/Thrift/Processor/ITAsyncProcessor.cs    |  2 +-
 .../Thrift/Processor/TMultiplexedProcessor.cs      |  5 ++--
 lib/netstd/Thrift/Protocol/TBinaryProtocol.cs      |  2 --
 lib/netstd/Thrift/Protocol/TCompactProtocol.cs     |  2 --
 lib/netstd/Thrift/Protocol/TJSONProtocol.cs        |  3 ---
 lib/netstd/Thrift/Protocol/TProtocol.cs            |  1 +
 lib/netstd/Thrift/Protocol/ToString.cs             |  8 +++----
 lib/netstd/Thrift/Server/TServer.cs                |  8 +++----
 lib/netstd/Thrift/Server/TServerEventHandler.cs    | 12 ++++++++--
 lib/netstd/Thrift/Server/TSimpleAsyncServer.cs     |  2 --
 lib/netstd/Thrift/Server/TThreadPoolAsyncServer.cs |  2 --
 .../Thrift/Transport/Client/THttpTransport.cs      |  2 --
 .../Transport/Client/TMemoryBufferTransport.cs     |  2 --
 .../Thrift/Transport/Layered/TBufferedTransport.cs |  9 +++-----
 .../Thrift/Transport/Layered/TFramedTransport.cs   |  6 ++---
 .../Transport/Server/TServerSocketTransport.cs     |  5 +++-
 17 files changed, 46 insertions(+), 52 deletions(-)

diff --git a/lib/netstd/Thrift/Processor/ITAsyncProcessor.cs b/lib/netstd/Thrift/GlobalSuppressions.cs
similarity index 50%
copy from lib/netstd/Thrift/Processor/ITAsyncProcessor.cs
copy to lib/netstd/Thrift/GlobalSuppressions.cs
index f5b8d16..eb7d3d7 100644
--- a/lib/netstd/Thrift/Processor/ITAsyncProcessor.cs
+++ b/lib/netstd/Thrift/GlobalSuppressions.cs
@@ -5,9 +5,9 @@
 // to you under the Apache License, Version 2.0 (the
 // "License"); you may not use this file except in compliance
 // with the License. You may obtain a copy of the License at
-// 
+//
 //     http://www.apache.org/licenses/LICENSE-2.0
-// 
+//
 // Unless required by applicable law or agreed to in writing,
 // software distributed under the License is distributed on an
 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
@@ -15,14 +15,17 @@
 // specific language governing permissions and limitations
 // under the License.
 
-using System.Threading;
-using System.Threading.Tasks;
-using Thrift.Protocol;
 
-namespace Thrift.Processor
-{
-    public interface ITAsyncProcessor
-    {
-        Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken = default(CancellationToken));
-    }
-}
+// This file is used by Code Analysis to maintain SuppressMessage
+// attributes that are applied to this project.
+// Project-level suppressions either have no target or are given
+// a specific target and scoped to a namespace, type, member, etc.
+
+using System.Diagnostics.CodeAnalysis;
+
+// suppress certain messages for compatibility reasons with older C# versions we want to support
+[assembly: SuppressMessage("Style", "IDE0057", Justification = "compatibility", Scope = "module")]
+[assembly: SuppressMessage("Style", "IDE0066", Justification = "compatibility", Scope = "module")]
+[assembly: SuppressMessage("Style", "IDE0090", Justification = "compatibility", Scope = "module")]
+[assembly: SuppressMessage("Style", "IDE0063", Justification = "compatibility", Scope = "module")]
+
diff --git a/lib/netstd/Thrift/Processor/ITAsyncProcessor.cs b/lib/netstd/Thrift/Processor/ITAsyncProcessor.cs
index f5b8d16..0ef683d 100644
--- a/lib/netstd/Thrift/Processor/ITAsyncProcessor.cs
+++ b/lib/netstd/Thrift/Processor/ITAsyncProcessor.cs
@@ -23,6 +23,6 @@ namespace Thrift.Processor
 {
     public interface ITAsyncProcessor
     {
-        Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken = default(CancellationToken));
+        Task<bool> ProcessAsync(TProtocol iprot, TProtocol oprot, CancellationToken cancellationToken = default);
     }
 }
diff --git a/lib/netstd/Thrift/Processor/TMultiplexedProcessor.cs b/lib/netstd/Thrift/Processor/TMultiplexedProcessor.cs
index b192210..9759058 100644
--- a/lib/netstd/Thrift/Processor/TMultiplexedProcessor.cs
+++ b/lib/netstd/Thrift/Processor/TMultiplexedProcessor.cs
@@ -65,8 +65,7 @@ namespace Thrift.Processor
 
                 // Create a new TMessage, something that can be consumed by any TProtocol
                 var serviceName = message.Name.Substring(0, index);
-                ITAsyncProcessor actualProcessor;
-                if (!_serviceProcessorMap.TryGetValue(serviceName, out actualProcessor))
+                if (!_serviceProcessorMap.TryGetValue(serviceName, out ITAsyncProcessor actualProcessor))
                 {
                     await FailAsync(oprot, message, TApplicationException.ExceptionType.InternalError,
                         $"Service name not found: {serviceName}. Did you forget to call RegisterProcessor()?",
@@ -103,7 +102,7 @@ namespace Thrift.Processor
             _serviceProcessorMap.Add(serviceName, processor);
         }
 
-        private async Task FailAsync(TProtocol oprot, TMessage message, TApplicationException.ExceptionType extype,
+        private static async Task FailAsync(TProtocol oprot, TMessage message, TApplicationException.ExceptionType extype,
             string etxt, CancellationToken cancellationToken)
         {
             var appex = new TApplicationException(extype, etxt);
diff --git a/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs b/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
index 9c23469..eee137c 100644
--- a/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
@@ -23,8 +23,6 @@ using System.Threading.Tasks;
 using Thrift.Protocol.Entities;
 using Thrift.Transport;
 
-#pragma warning disable IDE0079 // unnecessary suppression
-#pragma warning disable IDE0066 // use switch expression
 
 namespace Thrift.Protocol
 {
diff --git a/lib/netstd/Thrift/Protocol/TCompactProtocol.cs b/lib/netstd/Thrift/Protocol/TCompactProtocol.cs
index 3758174..6893ad4 100644
--- a/lib/netstd/Thrift/Protocol/TCompactProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TCompactProtocol.cs
@@ -26,8 +26,6 @@ using System.Threading.Tasks;
 using Thrift.Protocol.Entities;
 using Thrift.Transport;
 
-#pragma warning disable IDE0079 // unnecessary suppression
-#pragma warning disable IDE0066 // use switch expression
 
 namespace Thrift.Protocol
 {
diff --git a/lib/netstd/Thrift/Protocol/TJSONProtocol.cs b/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
index 081f42e..8799026 100644
--- a/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
@@ -27,9 +27,6 @@ using Thrift.Protocol.Entities;
 using Thrift.Protocol.Utilities;
 using Thrift.Transport;
 
-#pragma warning disable IDE0079 // unnecessary suppression
-#pragma warning disable IDE0063 // simplify using
-#pragma warning disable IDE0066 // use switch expression
 
 namespace Thrift.Protocol
 {
diff --git a/lib/netstd/Thrift/Protocol/TProtocol.cs b/lib/netstd/Thrift/Protocol/TProtocol.cs
index 5b1bec1..cd93833 100644
--- a/lib/netstd/Thrift/Protocol/TProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TProtocol.cs
@@ -50,6 +50,7 @@ namespace Thrift.Protocol
         public void Dispose()
         {
             Dispose(true);
+            GC.SuppressFinalize(this);
         }
 
         public void IncrementRecursionDepth()
diff --git a/lib/netstd/Thrift/Protocol/ToString.cs b/lib/netstd/Thrift/Protocol/ToString.cs
index 14fa520..50dd6df 100644
--- a/lib/netstd/Thrift/Protocol/ToString.cs
+++ b/lib/netstd/Thrift/Protocol/ToString.cs
@@ -47,15 +47,15 @@ namespace Thrift.Protocol
                     if (first_child)
                         first_child = false;
                     else
-                        sb.Append(",");
+                        sb.Append(',');
 
                     sb.Append("{ ");
                     pair.Key.ToString(sb);
                     sb.Append(", ");
                     pair.Value.ToString(sb);
-                    sb.Append("}");
+                    sb.Append('}');
                 }
-                sb.Append("}");
+                sb.Append('}');
             }
             else if (self is IEnumerable)
             {
@@ -65,7 +65,7 @@ namespace Thrift.Protocol
                     elm.ToString(sb, first_child);
                     first_child = false;
                 }
-                sb.Append("}");
+                sb.Append('}');
             }
             else if (self is TBase)
             {
diff --git a/lib/netstd/Thrift/Server/TServer.cs b/lib/netstd/Thrift/Server/TServer.cs
index 3fd0bc5..3d4f3fc 100644
--- a/lib/netstd/Thrift/Server/TServer.cs
+++ b/lib/netstd/Thrift/Server/TServer.cs
@@ -35,7 +35,7 @@ namespace Thrift.Server
         protected TProtocolFactory OutputProtocolFactory;
         protected TTransportFactory OutputTransportFactory;
 
-        protected TServerEventHandler ServerEventHandler;
+        protected ITServerEventHandler ServerEventHandler;
         protected TServerTransport ServerTransport;
 
         protected TServer(ITProcessorFactory processorFactory, TServerTransport serverTransport,
@@ -52,12 +52,12 @@ namespace Thrift.Server
             Logger = logger; // null is absolutely legal
         }
 
-        public void SetEventHandler(TServerEventHandler seh)
+        public void SetEventHandler(ITServerEventHandler seh)
         {
             ServerEventHandler = seh;
         }
 
-        public TServerEventHandler GetEventHandler()
+        public ITServerEventHandler GetEventHandler()
         {
             return ServerEventHandler;
         }
@@ -66,7 +66,7 @@ namespace Thrift.Server
         protected void LogError( string msg)
         {
             if (Logger != null)
-                Logger.LogError(msg);
+                Logger.LogError("{Msg}",msg);  // NOTE: Log message template, not string interpolation!
         }
 
         public abstract void Stop();
diff --git a/lib/netstd/Thrift/Server/TServerEventHandler.cs b/lib/netstd/Thrift/Server/TServerEventHandler.cs
index 69314ef..9226b42 100644
--- a/lib/netstd/Thrift/Server/TServerEventHandler.cs
+++ b/lib/netstd/Thrift/Server/TServerEventHandler.cs
@@ -25,10 +25,18 @@ namespace Thrift.Server
     //TODO: replacement by event?
 
     /// <summary>
-    ///     Interface implemented by server users to handle events from the server
+    /// Interface implemented by server users to handle events from the server
     /// </summary>
+    /// <remarks>Replaced by ITServerEventHandler</remarks>
     // ReSharper disable once InconsistentNaming
-    public interface TServerEventHandler
+    #pragma warning disable IDE1006
+    public interface TServerEventHandler : ITServerEventHandler { }
+    #pragma warning restore IDE1006
+
+    /// <summary>
+    /// Interface implemented by server users to handle events from the server
+    /// </summary>
+    public interface ITServerEventHandler
     {
         /// <summary>
         ///     Called before the server begins */
diff --git a/lib/netstd/Thrift/Server/TSimpleAsyncServer.cs b/lib/netstd/Thrift/Server/TSimpleAsyncServer.cs
index d46d58a..f1f6277 100644
--- a/lib/netstd/Thrift/Server/TSimpleAsyncServer.cs
+++ b/lib/netstd/Thrift/Server/TSimpleAsyncServer.cs
@@ -23,8 +23,6 @@ using Thrift.Processor;
 using System.Threading.Tasks;
 using Microsoft.Extensions.Logging;
 
-#pragma warning disable IDE0079  // remove unnecessary pragmas
-#pragma warning disable IDE0063  // using can be simplified, we don't
 
 namespace Thrift.Server
 {
diff --git a/lib/netstd/Thrift/Server/TThreadPoolAsyncServer.cs b/lib/netstd/Thrift/Server/TThreadPoolAsyncServer.cs
index ba1834c..efa1698 100644
--- a/lib/netstd/Thrift/Server/TThreadPoolAsyncServer.cs
+++ b/lib/netstd/Thrift/Server/TThreadPoolAsyncServer.cs
@@ -29,8 +29,6 @@ using Thrift.Processor;
 using System.Threading.Tasks;
 using Microsoft.Extensions.Logging;
 
-#pragma warning disable IDE0079  // remove unnecessary pragmas
-#pragma warning disable IDE0063  // using can be simplified, we don't
 
 namespace Thrift.Server
 {
diff --git a/lib/netstd/Thrift/Transport/Client/THttpTransport.cs b/lib/netstd/Thrift/Transport/Client/THttpTransport.cs
index 4ca439e..60ed59c 100644
--- a/lib/netstd/Thrift/Transport/Client/THttpTransport.cs
+++ b/lib/netstd/Thrift/Transport/Client/THttpTransport.cs
@@ -25,8 +25,6 @@ using System.Security.Cryptography.X509Certificates;
 using System.Threading;
 using System.Threading.Tasks;
 
-#pragma warning disable IDE0079 // unnecessary suppression
-#pragma warning disable IDE0063 // simplify using
 
 namespace Thrift.Transport.Client
 {
diff --git a/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs b/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs
index a666c7c..5773d30 100644
--- a/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs
+++ b/lib/netstd/Thrift/Transport/Client/TMemoryBufferTransport.cs
@@ -21,8 +21,6 @@ using System.IO;
 using System.Threading;
 using System.Threading.Tasks;
 
-#pragma warning disable IDE0079  // unused suppression
-#pragma warning disable IDE0066  // requires C# 8 
 
 namespace Thrift.Transport.Client
 {
diff --git a/lib/netstd/Thrift/Transport/Layered/TBufferedTransport.cs b/lib/netstd/Thrift/Transport/Layered/TBufferedTransport.cs
index dee52dd..271a994 100644
--- a/lib/netstd/Thrift/Transport/Layered/TBufferedTransport.cs
+++ b/lib/netstd/Thrift/Transport/Layered/TBufferedTransport.cs
@@ -110,8 +110,7 @@ namespace Thrift.Transport
 
             // buffer a new chunk of bytes from the underlying transport
             ReadBuffer.Length = ReadBuffer.Capacity;
-            ArraySegment<byte> bufSegment;
-            ReadBuffer.TryGetBuffer(out bufSegment);
+            ReadBuffer.TryGetBuffer(out ArraySegment<byte> bufSegment);
             ReadBuffer.Length = await InnerTransport.ReadAsync(bufSegment.Array, 0, bufSegment.Count, cancellationToken);
             ReadBuffer.Position = 0;
 
@@ -134,8 +133,7 @@ namespace Thrift.Transport
             var free = WriteBuffer.Capacity - WriteBuffer.Length;
             if (length > free)
             {
-                ArraySegment<byte> bufSegment;
-                WriteBuffer.TryGetBuffer(out bufSegment);
+                WriteBuffer.TryGetBuffer(out ArraySegment<byte> bufSegment);
                 await InnerTransport.WriteAsync(bufSegment.Array, 0, bufSegment.Count, cancellationToken);
                 WriteBuffer.SetLength(0);
             }
@@ -163,8 +161,7 @@ namespace Thrift.Transport
 
             if (WriteBuffer.Length > 0)
             {
-                ArraySegment<byte> bufSegment;
-                WriteBuffer.TryGetBuffer(out bufSegment);
+                WriteBuffer.TryGetBuffer(out ArraySegment<byte> bufSegment);
                 await InnerTransport.WriteAsync(bufSegment.Array, 0, bufSegment.Count, cancellationToken);
                 WriteBuffer.SetLength(0);
             }
diff --git a/lib/netstd/Thrift/Transport/Layered/TFramedTransport.cs b/lib/netstd/Thrift/Transport/Layered/TFramedTransport.cs
index f560986..722df76 100644
--- a/lib/netstd/Thrift/Transport/Layered/TFramedTransport.cs
+++ b/lib/netstd/Thrift/Transport/Layered/TFramedTransport.cs
@@ -97,8 +97,7 @@ namespace Thrift.Transport
             ReadBuffer.SetLength(size);
             ReadBuffer.Seek(0, SeekOrigin.Begin);
 
-            ArraySegment<byte> bufSegment;
-            ReadBuffer.TryGetBuffer(out bufSegment);
+            ReadBuffer.TryGetBuffer(out ArraySegment<byte> bufSegment);
             await InnerTransport.ReadAllAsync(bufSegment.Array, 0, size, cancellationToken);
         }
 
@@ -129,8 +128,7 @@ namespace Thrift.Transport
                 throw new TTransportException(TTransportException.ExceptionType.NotOpen);
             }
 
-            ArraySegment<byte> bufSegment;
-            WriteBuffer.TryGetBuffer(out bufSegment);
+            WriteBuffer.TryGetBuffer(out ArraySegment<byte> bufSegment);
 
             int dataLen = bufSegment.Count - HeaderSize;
             if (dataLen < 0)
diff --git a/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs b/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
index d7421c9..3a4fed4 100644
--- a/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
+++ b/lib/netstd/Thrift/Transport/Server/TServerSocketTransport.cs
@@ -113,8 +113,11 @@ namespace Thrift.Transport.Server
             try
             {
                 TTransport tSocketTransport = null;
+                #if NET6_0_OR_GREATER
+                var tcpClient = await _server.AcceptTcpClientAsync(cancellationToken);
+                #else
                 var tcpClient = await _server.AcceptTcpClientAsync();
-
+                #endif
                 try
                 {
                     tSocketTransport = new TSocketTransport(tcpClient, Configuration)