misc. netstd improvements
Client: netstd
Patch: Jens Geyer
This closes #2344
diff --git a/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs b/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
index 28b7d29..9c23469 100644
--- a/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TBinaryProtocol.cs
@@ -23,6 +23,9 @@
using Thrift.Protocol.Entities;
using Thrift.Transport;
+#pragma warning disable IDE0079 // unnecessary suppression
+#pragma warning disable IDE0066 // use switch expression
+
namespace Thrift.Protocol
{
// ReSharper disable once InconsistentNaming
@@ -36,7 +39,7 @@
// minimize memory allocations by means of an preallocated bytes buffer
// The value of 128 is arbitrarily chosen, the required minimum size must be sizeof(long)
- private byte[] PreAllocatedBuffer = new byte[128];
+ private readonly byte[] PreAllocatedBuffer = new byte[128];
public TBinaryProtocol(TTransport trans)
: this(trans, false, true)
@@ -442,7 +445,7 @@
case TType.Map: return sizeof(int); // element count
case TType.Set: return sizeof(int); // element count
case TType.List: return sizeof(int); // element count
- default: throw new TTransportException(TTransportException.ExceptionType.Unknown, "unrecognized type code");
+ default: throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "unrecognized type code");
}
}
diff --git a/lib/netstd/Thrift/Protocol/TCompactProtocol.cs b/lib/netstd/Thrift/Protocol/TCompactProtocol.cs
index 066b327..3758174 100644
--- a/lib/netstd/Thrift/Protocol/TCompactProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TCompactProtocol.cs
@@ -26,9 +26,11 @@
using Thrift.Protocol.Entities;
using Thrift.Transport;
+#pragma warning disable IDE0079 // unnecessary suppression
+#pragma warning disable IDE0066 // use switch expression
+
namespace Thrift.Protocol
{
- //TODO: implementation of TProtocol
// ReSharper disable once InconsistentNaming
public class TCompactProtocol : TProtocol
@@ -805,7 +807,7 @@
case TType.Map: return sizeof(byte); // element count
case TType.Set: return sizeof(byte); // element count
case TType.List: return sizeof(byte); // element count
- default: throw new TTransportException(TTransportException.ExceptionType.Unknown, "unrecognized type code");
+ default: throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "unrecognized type code");
}
}
diff --git a/lib/netstd/Thrift/Protocol/TJSONProtocol.cs b/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
index 2f1ccdb..081f42e 100644
--- a/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
+++ b/lib/netstd/Thrift/Protocol/TJSONProtocol.cs
@@ -27,6 +27,10 @@
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
{
/// <summary>
@@ -88,7 +92,7 @@
/// even in cases where the protocol instance was in an undefined state due to
/// dangling/stale/obsolete contexts
/// </summary>
- private void resetContext()
+ private void ResetContext()
{
ContextStack.Clear();
Context = new JSONBaseContext(this);
@@ -277,7 +281,7 @@
public override async Task WriteMessageBeginAsync(TMessage message, CancellationToken cancellationToken)
{
- resetContext();
+ ResetContext();
await WriteJsonArrayStartAsync(cancellationToken);
await WriteJsonIntegerAsync(Version, cancellationToken);
@@ -430,7 +434,12 @@
// escaped?
if (ch != TJSONProtocolConstants.EscSequences[0])
{
+#if NETSTANDARD2_0
await buffer.WriteAsync(new[] {ch}, 0, 1, cancellationToken);
+#else
+ var wbuf = new[] { ch };
+ await buffer.WriteAsync(wbuf.AsMemory(0, 1), cancellationToken);
+#endif
continue;
}
@@ -444,7 +453,12 @@
throw new TProtocolException(TProtocolException.INVALID_DATA, "Expected control char");
}
ch = TJSONProtocolConstants.EscapeCharValues[off];
+#if NETSTANDARD2_0
await buffer.WriteAsync(new[] {ch}, 0, 1, cancellationToken);
+#else
+ var wbuf = new[] { ch };
+ await buffer.WriteAsync( wbuf.AsMemory(0, 1), cancellationToken);
+#endif
continue;
}
@@ -473,13 +487,21 @@
codeunits.Add((char) wch);
var tmp = Utf8Encoding.GetBytes(codeunits.ToArray());
+#if NETSTANDARD2_0
await buffer.WriteAsync(tmp, 0, tmp.Length, cancellationToken);
+#else
+ await buffer.WriteAsync(tmp.AsMemory(0, tmp.Length), cancellationToken);
+#endif
codeunits.Clear();
}
else
{
- var tmp = Utf8Encoding.GetBytes(new[] {(char) wch});
+ var tmp = Utf8Encoding.GetBytes(new[] { (char)wch });
+#if NETSTANDARD2_0
await buffer.WriteAsync(tmp, 0, tmp.Length, cancellationToken);
+#else
+ await buffer.WriteAsync(tmp.AsMemory( 0, tmp.Length), cancellationToken);
+#endif
}
}
@@ -655,7 +677,7 @@
{
var message = new TMessage();
- resetContext();
+ ResetContext();
await ReadJsonArrayStartAsync(cancellationToken);
if (await ReadJsonIntegerAsync(cancellationToken) != Version)
{
@@ -816,7 +838,7 @@
case TType.Map: return 2; // empty map
case TType.Set: return 2; // empty set
case TType.List: return 2; // empty list
- default: throw new TTransportException(TTransportException.ExceptionType.Unknown, "unrecognized type code");
+ default: throw new TProtocolException(TProtocolException.NOT_IMPLEMENTED, "unrecognized type code");
}
}