Skip to content

Commit 1c3f3aa

Browse files
Add CborProtocol extension project
1 parent 7ec2fe1 commit 1c3f3aa

15 files changed

+491
-100
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net472</TargetFramework>
5+
<AssemblyName>AWSSDK.Extensions.CborProtocol</AssemblyName>
6+
<PackageId>AWSSDK.Extensions.CborProtocol</PackageId>
7+
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
8+
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
9+
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
10+
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
11+
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
12+
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
13+
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
14+
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
15+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
16+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
17+
<NoWarn>$(NoWarn);CS1591</NoWarn>
18+
<SignAssembly>True</SignAssembly>
19+
</PropertyGroup>
20+
21+
<ItemGroup>
22+
<Compile Remove="**/obj/**" />
23+
</ItemGroup>
24+
25+
<Choose>
26+
<When Condition=" '$(AWSKeyFile)' == '' ">
27+
<PropertyGroup>
28+
<AssemblyOriginatorKeyFile>..\..\..\sdk\awssdk.dll.snk</AssemblyOriginatorKeyFile>
29+
</PropertyGroup>
30+
</When>
31+
<Otherwise>
32+
<PropertyGroup>
33+
<AssemblyOriginatorKeyFile>$(AWSKeyFile)</AssemblyOriginatorKeyFile>
34+
</PropertyGroup>
35+
</Otherwise>
36+
</Choose>
37+
38+
<ItemGroup>
39+
<PackageReference Include="System.Formats.Cbor" Version="9.0.5" />
40+
</ItemGroup>
41+
42+
<ItemGroup>
43+
<ProjectReference Include="..\..\..\sdk\src\Core\AWSSDK.Core.NetFramework.csproj" />
44+
</ItemGroup>
45+
</Project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net8.0</TargetFrameworks>
5+
<AssemblyName>AWSSDK.Extensions.CborProtocol</AssemblyName>
6+
<PackageId>AWSSDK.Extensions.CborProtocol</PackageId>
7+
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
8+
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
9+
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
10+
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
11+
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
12+
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
13+
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
14+
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
15+
<GenerateDocumentationFile>true</GenerateDocumentationFile>
16+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
17+
<SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings>
18+
<NoWarn>$(NoWarn);CS1591</NoWarn>
19+
<SignAssembly>True</SignAssembly>
20+
</PropertyGroup>
21+
22+
<ItemGroup>
23+
<Compile Remove="**/obj/**" />
24+
</ItemGroup>
25+
26+
<PropertyGroup Condition="'$(TargetFramework)' == 'net8.0'">
27+
<WarningsAsErrors>IL2026,IL2075</WarningsAsErrors>
28+
<IsTrimmable>true</IsTrimmable>
29+
</PropertyGroup>
30+
31+
<Choose>
32+
<When Condition=" '$(AWSKeyFile)' == '' ">
33+
<PropertyGroup>
34+
<AssemblyOriginatorKeyFile>..\..\..\sdk\awssdk.dll.snk</AssemblyOriginatorKeyFile>
35+
</PropertyGroup>
36+
</When>
37+
<Otherwise>
38+
<PropertyGroup>
39+
<AssemblyOriginatorKeyFile>$(AWSKeyFile)</AssemblyOriginatorKeyFile>
40+
</PropertyGroup>
41+
</Otherwise>
42+
</Choose>
43+
44+
<ItemGroup>
45+
<PackageReference Include="System.Formats.Cbor" Version="9.0.5" />
46+
</ItemGroup>
47+
48+
<ItemGroup>
49+
<ProjectReference Include="..\..\..\sdk\src\Core\AWSSDK.Core.NetStandard.csproj" />
50+
</ItemGroup>
51+
</Project>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
using Amazon.Runtime;
16+
using Amazon.Runtime.Internal;
17+
using Amazon.Runtime.Internal.Transform;
18+
using Amazon.Runtime.Internal.Util;
19+
using Amazon.Util;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Formats.Cbor;
23+
using System.IO;
24+
using System.Text.RegularExpressions;
25+
26+
namespace AWSSDK.Extensions.CborProtocol
27+
{
28+
public static class CborWriterExtensions
29+
{
30+
/// <summary>
31+
/// Writes the DateTime as UnixEpochSeconds which is the only type we support for CBOR.
32+
/// </summary>
33+
/// <param name="writer">The CBOR writer to use.</param>
34+
/// <param name="value">The DateTime value to write.</param>
35+
public static void WriteDateTime(this CborWriter writer, DateTime value)
36+
{
37+
writer.WriteTag(CborTag.UnixTimeSeconds);
38+
writer.WriteOptimizedNumber(AWSSDKUtils.ConvertToUnixEpochSecondsDouble(value));
39+
}
40+
41+
/// <summary>
42+
/// Writes a double using the smallest CBOR representation that preserves value and precision.
43+
/// </summary>
44+
/// <param name="writer">The CBOR writer to use.</param>
45+
/// <param name="value">The double value to write.</param>
46+
public static void WriteOptimizedNumber(this CborWriter writer, double value)
47+
{
48+
if (double.IsNaN(value) || double.IsInfinity(value))
49+
{
50+
writer.WriteDouble(value);
51+
return;
52+
}
53+
54+
if (value % 1 == 0)
55+
{
56+
if (value >= long.MinValue && value <= long.MaxValue)
57+
{
58+
writer.WriteInt64((long)value);
59+
return;
60+
}
61+
62+
if (value >= 0 && value <= ulong.MaxValue)
63+
{
64+
writer.WriteUInt64((ulong)value);
65+
return;
66+
}
67+
}
68+
69+
writer.WriteDouble(value);
70+
}
71+
72+
/// <summary>
73+
/// Writes a float using the smallest CBOR representation that preserves value and precision.
74+
/// </summary>
75+
/// <param name="writer">The CBOR writer to use.</param>
76+
/// <param name="value">The float value to write.</param>
77+
public static void WriteOptimizedNumber(this CborWriter writer, float value)
78+
{
79+
if (float.IsNaN(value) || float.IsInfinity(value))
80+
{
81+
writer.WriteSingle(value);
82+
return;
83+
}
84+
85+
if (value % 1 == 0)
86+
{
87+
if (value >= long.MinValue && value <= long.MaxValue)
88+
{
89+
writer.WriteInt64((long)value);
90+
return;
91+
}
92+
93+
if (value >= 0 && value <= ulong.MaxValue)
94+
{
95+
writer.WriteUInt64((ulong)value);
96+
return;
97+
}
98+
}
99+
100+
writer.WriteSingle(value);
101+
}
102+
}
103+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
<Project>
3+
<PropertyGroup>
4+
<BaseIntermediateOutputPath>$(MSBuildProjectDirectory)\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
5+
</PropertyGroup>
6+
</Project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
using Amazon.Runtime;
16+
using Amazon.Runtime.Internal;
17+
using Amazon.Runtime.Internal.Transform;
18+
using Amazon.Runtime.Internal.Util;
19+
using Amazon.Util;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Formats.Cbor;
23+
using System.IO;
24+
25+
namespace AWSSDK.Extensions.CborProtocol.Internal
26+
{
27+
public class CborMarshallerContext : MarshallerContext
28+
{
29+
public CborWriter Writer { get; private set; }
30+
31+
public CborMarshallerContext(IRequest request, CborWriter writer)
32+
: base(request)
33+
{
34+
Writer = writer;
35+
}
36+
}
37+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
using System;
16+
using System.Collections.Concurrent;
17+
using System.Formats.Cbor;
18+
using System.Threading;
19+
20+
namespace AWSSDK.Extensions.CborProtocol.Internal
21+
{
22+
public static class CborWriterPool
23+
{
24+
// Internal pool storage using thread-safe collection
25+
private static readonly ConcurrentBag<CborWriter> _pool = new ConcurrentBag<CborWriter>();
26+
27+
// Maximum number of CborWriter instances the pool can hold
28+
private static int _maxPoolSize = 16;
29+
30+
/// <summary>
31+
/// Gets or sets the maximum size of the writer pool.
32+
/// Minimum value is 1.
33+
/// </summary>
34+
public static int MaxPoolSize
35+
{
36+
get => Volatile.Read(ref _maxPoolSize);
37+
set => Volatile.Write(ref _maxPoolSize, Math.Max(1, value));
38+
}
39+
40+
/// <summary>
41+
/// Retrieves a CborWriter from the pool, or creates a new one if the pool is empty.
42+
/// </summary>
43+
public static CborWriter Rent()
44+
{
45+
if (_pool.TryTake(out var writer))
46+
{
47+
return writer;
48+
}
49+
// Create a new CborWriter if the pool is empty
50+
return new CborWriter(CborConformanceMode.Canonical, true);
51+
}
52+
53+
/// <summary>
54+
/// Returns a CborWriter to the pool for reuse.
55+
/// If the pool is already full then the writer will be discard.
56+
/// </summary>
57+
public static void Return(CborWriter writer)
58+
{
59+
if (_pool.Count >= Volatile.Read(ref _maxPoolSize))
60+
return;
61+
62+
writer.Reset(); // Ensure the writer is in a clean state before pooling
63+
_pool.Add(writer);
64+
}
65+
}
66+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
[assembly: AssemblyTitle("AWSSDK.Extensions.CborProtocol")]
6+
[assembly: AssemblyConfiguration("")]
7+
[assembly: AssemblyCompany("Amazon.com, Inc")]
8+
[assembly: AssemblyProduct("AWS SDK for .NET extensions for Cbor protocol support")]
9+
[assembly: AssemblyDescription("AWS SDK for .NET extensions for Cbor protocol support")]
10+
[assembly: AssemblyCopyright("Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.")]
11+
[assembly: AssemblyTrademark("")]
12+
13+
// Setting ComVisible to false makes the types in this assembly not visible
14+
// to COM components. If you need to access a type in this assembly from
15+
// COM, set the ComVisible attribute to true on that type.
16+
[assembly: ComVisible(false)]
17+
18+
[assembly: AssemblyVersion("4.0")]
19+
[assembly: AssemblyFileVersion("4.0.0.0")]

generator/ServiceClientGeneratorLib/GeneratorEnumerations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace ServiceClientGenerator
99
/// <summary>
1010
/// The set of data protocols used by AWS services
1111
/// </summary>
12-
public enum ServiceType { Json, Query, Rest_Xml, Rest_Json };
12+
public enum ServiceType { Json, Query, Rest_Xml, Rest_Json, Cbor };
1313

1414
/// <summary>
1515
/// Where the properties of the request should be placed

0 commit comments

Comments
 (0)