Skip to content

Commit 85436cd

Browse files
committed
added comments using AI - 2
1 parent b309350 commit 85436cd

File tree

146 files changed

+3429
-151
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+3429
-151
lines changed

src/SuperSocket.Kestrel/KestrelPipeConnection.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ namespace SuperSocket.Kestrel;
1212
using SuperSocket.Connection;
1313
using SuperSocket.ProtoBase;
1414

15+
/// <summary>
16+
/// Represents a pipe connection that integrates with Kestrel's <see cref="ConnectionContext"/>.
17+
/// </summary>
1518
public class KestrelPipeConnection : PipeConnectionBase
1619
{
1720
private ConnectionContext _context;
1821

22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="KestrelPipeConnection"/> class with the specified connection context and options.
24+
/// </summary>
25+
/// <param name="context">The Kestrel connection context.</param>
26+
/// <param name="options">The connection options.</param>
1927
public KestrelPipeConnection(ConnectionContext context, ConnectionOptions options)
2028
: base(context.Transport.Input, context.Transport.Output, options)
2129
{
@@ -30,6 +38,11 @@ public KestrelPipeConnection(ConnectionContext context, ConnectionOptions option
3038
}
3139
}
3240

41+
/// <summary>
42+
/// Completes the reader asynchronously.
43+
/// </summary>
44+
/// <param name="reader">The pipe reader to complete.</param>
45+
/// <param name="isDetaching">Indicates whether the connection is detaching.</param>
3346
protected override async ValueTask CompleteReaderAsync(PipeReader reader, bool isDetaching)
3447
{
3548
if (!isDetaching)
@@ -38,6 +51,11 @@ protected override async ValueTask CompleteReaderAsync(PipeReader reader, bool i
3851
}
3952
}
4053

54+
/// <summary>
55+
/// Completes the writer asynchronously.
56+
/// </summary>
57+
/// <param name="writer">The pipe writer to complete.</param>
58+
/// <param name="isDetaching">Indicates whether the connection is detaching.</param>
4159
protected override async ValueTask CompleteWriterAsync(PipeWriter writer, bool isDetaching)
4260
{
4361
if (!isDetaching)
@@ -46,6 +64,9 @@ protected override async ValueTask CompleteWriterAsync(PipeWriter writer, bool i
4664
}
4765
}
4866

67+
/// <summary>
68+
/// Handles the closure of the connection.
69+
/// </summary>
4970
protected override void OnClosed()
5071
{
5172
if (!CloseReason.HasValue)
@@ -54,6 +75,9 @@ protected override void OnClosed()
5475
base.OnClosed();
5576
}
5677

78+
/// <summary>
79+
/// Closes the connection and disposes the context.
80+
/// </summary>
5781
protected override async void Close()
5882
{
5983
var context = _context;
@@ -67,31 +91,57 @@ protected override async void Close()
6791
}
6892
}
6993

94+
/// <summary>
95+
/// Updates the last active time when input pipe data is read.
96+
/// </summary>
97+
/// <param name="result">The result of the pipe read operation.</param>
7098
protected override void OnInputPipeRead(ReadResult result)
7199
{
72100
if (result is { IsCanceled: false, IsCompleted: false })
73101
UpdateLastActiveTime();
74102
}
75103

104+
/// <summary>
105+
/// Sends data over the connection asynchronously using a custom write action.
106+
/// </summary>
107+
/// <param name="write">The action to write data to the pipe writer.</param>
108+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
76109
public override async ValueTask SendAsync(Action<PipeWriter> write, CancellationToken cancellationToken)
77110
{
78111
await base.SendAsync(write, cancellationToken);
79112
UpdateLastActiveTime();
80113
}
81114

115+
/// <summary>
116+
/// Sends data over the connection asynchronously.
117+
/// </summary>
118+
/// <param name="buffer">The data to send.</param>
119+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
82120
public override async ValueTask SendAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken)
83121
{
84122
await base.SendAsync(buffer, cancellationToken);
85123
UpdateLastActiveTime();
86124
}
87125

126+
/// <summary>
127+
/// Sends a package over the connection asynchronously using the specified encoder.
128+
/// </summary>
129+
/// <typeparam name="TPackage">The type of the package to send.</typeparam>
130+
/// <param name="packageEncoder">The encoder to use for the package.</param>
131+
/// <param name="package">The package to send.</param>
132+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
88133
public override async ValueTask SendAsync<TPackage>(IPackageEncoder<TPackage> packageEncoder, TPackage package,
89134
CancellationToken cancellationToken)
90135
{
91136
await base.SendAsync(packageEncoder, package, cancellationToken);
92137
UpdateLastActiveTime();
93138
}
94139

140+
/// <summary>
141+
/// Determines whether the specified exception is ignorable.
142+
/// </summary>
143+
/// <param name="e">The exception to check.</param>
144+
/// <returns><c>true</c> if the exception is ignorable; otherwise, <c>false</c>.</returns>
95145
protected override bool IsIgnorableException(Exception e)
96146
{
97147
if (e is IOException ioe && ioe.InnerException != null)

src/SuperSocket.Kestrel/KestrelPipeConnectionExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@
99
using Microsoft.Extensions.Configuration;
1010
using Microsoft.Extensions.Logging;
1111

12+
/// <summary>
13+
/// Provides extension methods for configuring SuperSocket to use Kestrel's pipe-based connection factory.
14+
/// </summary>
1215
public static class KestrelPipeConnectionExtensions
1316
{
17+
/// <summary>
18+
/// Configures the SuperSocket host to use Kestrel's pipe-based connection factory.
19+
/// </summary>
20+
/// <param name="hostBuilder">The SuperSocket host builder.</param>
21+
/// <param name="options">The options for the socket connection factory. If <c>null</c>, default options are used.</param>
22+
/// <returns>The updated SuperSocket host builder.</returns>
1423
public static ISuperSocketHostBuilder UseKestrelPipeConnection(this ISuperSocketHostBuilder hostBuilder, SocketConnectionFactoryOptions options = null)
1524
{
1625
hostBuilder.ConfigureServices((ctx, services) =>

src/SuperSocket.Kestrel/KestrelPipeConnectionFactory.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,32 @@
99
using SuperSocket.Server.Abstractions.Connections;
1010
using Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets;
1111

12+
/// <summary>
13+
/// Represents a connection factory for creating connections using Kestrel's <see cref="SocketConnectionContextFactory"/>.
14+
/// </summary>
1215
public class KestrelPipeConnectionFactory : TcpConnectionFactoryBase
1316
{
1417
private readonly SocketConnectionContextFactory _socketConnectionContextFactory;
1518

19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="KestrelPipeConnectionFactory"/> class with the specified context factory, listen options, connection options, and socket options setter.
21+
/// </summary>
22+
/// <param name="socketConnectionContextFactory">The factory for creating socket connection contexts.</param>
23+
/// <param name="listenOptions">The options for listening to incoming connections.</param>
24+
/// <param name="connectionOptions">The options for managing connections.</param>
25+
/// <param name="socketOptionsSetter">The setter for configuring socket options.</param>
1626
public KestrelPipeConnectionFactory(SocketConnectionContextFactory socketConnectionContextFactory, ListenOptions listenOptions, ConnectionOptions connectionOptions, Action<Socket> socketOptionsSetter)
1727
: base(listenOptions, connectionOptions, socketOptionsSetter, null)
1828
{
1929
_socketConnectionContextFactory = socketConnectionContextFactory;
2030
}
2131

32+
/// <summary>
33+
/// Creates a connection using the specified socket and cancellation token.
34+
/// </summary>
35+
/// <param name="connection">The socket representing the connection.</param>
36+
/// <param name="cancellationToken">A token to monitor for cancellation requests.</param>
37+
/// <returns>A task that represents the asynchronous operation. The task result contains the created connection.</returns>
2238
public override Task<IConnection> CreateConnection(object connection, CancellationToken cancellationToken)
2339
{
2440
var socket = connection as Socket;

src/SuperSocket.Kestrel/KestrelPipeConnectionFactoryBuilder.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,32 @@
77

88
namespace SuperSocket.Kestrel;
99

10+
/// <summary>
11+
/// Represents a builder for creating connection factories using Kestrel's <see cref="SocketConnectionContextFactory"/>.
12+
/// </summary>
1013
public class KestrelPipeConnectionFactoryBuilder : IConnectionFactoryBuilder
1114
{
1215
private readonly SocketConnectionContextFactory _socketConnectionContextFactory;
1316

1417
private readonly Action<Socket> _socketOptionsSetter;
1518

19+
/// <summary>
20+
/// Initializes a new instance of the <see cref="KestrelPipeConnectionFactoryBuilder"/> class with the specified context factory and socket options setter.
21+
/// </summary>
22+
/// <param name="socketConnectionContextFactory">The factory for creating socket connection contexts.</param>
23+
/// <param name="socketOptionsSetter">The setter for configuring socket options.</param>
1624
public KestrelPipeConnectionFactoryBuilder(SocketConnectionContextFactory socketConnectionContextFactory, SocketOptionsSetter socketOptionsSetter)
1725
{
1826
_socketConnectionContextFactory = socketConnectionContextFactory;
1927
_socketOptionsSetter = socketOptionsSetter.Setter;
2028
}
2129

30+
/// <summary>
31+
/// Builds a connection factory using the specified listen options and connection options.
32+
/// </summary>
33+
/// <param name="listenOptions">The options for listening to incoming connections.</param>
34+
/// <param name="connectionOptions">The options for managing connections.</param>
35+
/// <returns>A connection factory for creating connections.</returns>
2236
public IConnectionFactory Build(ListenOptions listenOptions, ConnectionOptions connectionOptions)
2337
{
2438
return new KestrelPipeConnectionFactory(_socketConnectionContextFactory, listenOptions, connectionOptions, _socketOptionsSetter);

src/SuperSocket.Primitives/AsyncParallel.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,19 @@
55

66
namespace SuperSocket
77
{
8+
/// <summary>
9+
/// Provides utility methods for executing asynchronous operations in parallel.
10+
/// </summary>
811
public static class AsyncParallel
912
{
13+
/// <summary>
14+
/// Executes the specified asynchronous operation for each item in the source collection in parallel.
15+
/// </summary>
16+
/// <typeparam name="TItem">The type of the items in the source collection.</typeparam>
17+
/// <param name="source">The source collection of items.</param>
18+
/// <param name="operation">The asynchronous operation to execute for each item.</param>
19+
/// <param name="maxDegreeOfParallelism">The maximum number of operations to execute in parallel. Default is 5.</param>
20+
/// <returns>A task that represents the asynchronous operation.</returns>
1021
public static async Task ForEach<TItem>(IEnumerable<TItem> source, Func<TItem, Task> operation, int maxDegreeOfParallelism = 5)
1122
{
1223
await ForEach(source, operation, new ParallelOptions
@@ -16,6 +27,14 @@ public static async Task ForEach<TItem>(IEnumerable<TItem> source, Func<TItem, T
1627
});
1728
}
1829

30+
/// <summary>
31+
/// Executes the specified asynchronous operation for each item in the source collection in parallel with the specified parallel options.
32+
/// </summary>
33+
/// <typeparam name="TItem">The type of the items in the source collection.</typeparam>
34+
/// <param name="source">The source collection of items.</param>
35+
/// <param name="operation">The asynchronous operation to execute for each item.</param>
36+
/// <param name="parallelOptions">The options for controlling the parallel execution.</param>
37+
/// <returns>A task that represents the asynchronous operation.</returns>
1938
public static async Task ForEach<TItem>(IEnumerable<TItem> source, Func<TItem, Task> operation, ParallelOptions parallelOptions)
2039
{
2140
var allTasks = new List<Task>();

src/SuperSocket.Primitives/CancellationTokenSourcePool.cs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,25 @@ namespace SuperSocket;
66

77
#if NET6_0_OR_GREATER
88

9+
/// <summary>
10+
/// Provides a pool for reusing <see cref="CancellationTokenSource"/> instances to reduce memory allocations.
11+
/// </summary>
912
public sealed class CancellationTokenSourcePool
1013
{
1114
private const int MaxQueueSize = 1024;
1215

1316
private readonly ConcurrentQueue<PooledCancellationTokenSource> _queue = new();
1417
private int _count;
1518

19+
/// <summary>
20+
/// Gets a shared instance of the <see cref="CancellationTokenSourcePool"/>.
21+
/// </summary>
1622
public static readonly CancellationTokenSourcePool Shared = new();
17-
23+
24+
/// <summary>
25+
/// Rents a <see cref="PooledCancellationTokenSource"/> from the pool.
26+
/// </summary>
27+
/// <returns>A pooled <see cref="PooledCancellationTokenSource"/>.</returns>
1828
public PooledCancellationTokenSource Rent()
1929
{
2030
if (_queue.TryDequeue(out var cts))
@@ -27,13 +37,18 @@ public PooledCancellationTokenSource Rent()
2737
return new PooledCancellationTokenSource(this);
2838
}
2939

40+
/// <summary>
41+
/// Rents a <see cref="PooledCancellationTokenSource"/> from the pool and sets a cancellation delay.
42+
/// </summary>
43+
/// <param name="delay">The delay after which the token will be canceled.</param>
44+
/// <returns>A pooled <see cref="PooledCancellationTokenSource"/>.</returns>
3045
public PooledCancellationTokenSource Rent(TimeSpan delay)
3146
{
3247
var token = Rent();
3348
token.CancelAfter(delay);
3449
return token;
3550
}
36-
51+
3752
private bool Return(PooledCancellationTokenSource cts)
3853
{
3954
if (Interlocked.Increment(ref _count) > MaxQueueSize || !cts.TryReset())
@@ -47,24 +62,31 @@ private bool Return(PooledCancellationTokenSource cts)
4762
}
4863

4964
/// <summary>
50-
/// A <see cref="CancellationTokenSource"/> with a back pointer to the pool it came from.
51-
/// Dispose will return it to the pool.
65+
/// Represents a <see cref="CancellationTokenSource"/> with a back pointer to the pool it came from.
5266
/// </summary>
53-
public sealed class PooledCancellationTokenSource
54-
: CancellationTokenSource
67+
public sealed class PooledCancellationTokenSource : CancellationTokenSource
5568
{
5669
private readonly CancellationTokenSourcePool _pool;
5770

71+
/// <summary>
72+
/// Initializes a new instance of the <see cref="PooledCancellationTokenSource"/> class with the specified pool.
73+
/// </summary>
74+
/// <param name="pool">The pool to which this instance belongs.</param>
5875
public PooledCancellationTokenSource(CancellationTokenSourcePool pool)
5976
{
6077
_pool = pool;
6178
}
6279

80+
/// <summary>
81+
/// Disposes the <see cref="PooledCancellationTokenSource"/> and returns it to the pool if possible.
82+
/// </summary>
83+
/// <param name="disposing">A value indicating whether the object is being disposed.</param>
6384
protected override void Dispose(bool disposing)
6485
{
6586
if (!disposing)
6687
return;
67-
// If we failed to return to the pool then dispose
88+
89+
// If we failed to return to the pool, then dispose
6890
if (!_pool.Return(this))
6991
{
7092
base.Dispose(disposing);

src/SuperSocket.Primitives/CertificateOptions.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,49 +5,49 @@
55

66
namespace SuperSocket.Server.Abstractions
77
{
8+
/// <summary>
9+
/// Represents configuration options for loading an X.509 certificate.
10+
/// </summary>
811
public class CertificateOptions
912
{
1013
/// <summary>
11-
/// Gets the certificate file path (pfx).
14+
/// Gets or sets the certificate file path (pfx).
1215
/// </summary>
1316
public string FilePath { get; set; }
1417

1518
/// <summary>
16-
/// Gets the password.
19+
/// Gets or sets the password for the certificate file.
1720
/// </summary>
1821
public string Password { get; set; }
1922

2023
/// <summary>
21-
/// Gets the the store where certificate locates.
24+
/// Gets or sets the name of the store where the certificate is located.
2225
/// </summary>
23-
/// <value>
24-
/// The name of the store.
25-
/// </value>
26-
public string StoreName { get; set; } = "My";//The X.509 certificate store for personal certificates.
26+
public string StoreName { get; set; } = "My"; // The X.509 certificate store for personal certificates.
2727

2828
/// <summary>
29-
/// Gets the thumbprint.
29+
/// Gets or sets the thumbprint of the certificate.
3030
/// </summary>
3131
public string Thumbprint { get; set; }
3232

33-
3433
/// <summary>
35-
/// Gets the store location of the certificate.
34+
/// Gets or sets the store location of the certificate.
3635
/// </summary>
37-
/// <value>
38-
/// The store location.
39-
/// </value>
40-
public StoreLocation StoreLocation { get; set; } = StoreLocation.CurrentUser;//The X.509 certificate store used by the current user.
41-
36+
public StoreLocation StoreLocation { get; set; } = StoreLocation.CurrentUser; // The X.509 certificate store used by the current user.
4237

4338
/// <summary>
44-
/// Gets a value that will be used to instantiate the X509Certificate2 object in the CertificateManager
39+
/// Gets or sets the key storage flags used to instantiate the X509Certificate2 object.
4540
/// </summary>
4641
public X509KeyStorageFlags KeyStorageFlags { get; set; }
4742

43+
/// <summary>
44+
/// Retrieves the X.509 certificate based on the specified options.
45+
/// </summary>
46+
/// <returns>The loaded <see cref="X509Certificate"/>.</returns>
47+
/// <exception cref="Exception">Thrown if neither <see cref="FilePath"/> nor <see cref="Thumbprint"/> is provided.</exception>
4848
public X509Certificate GetCertificate()
4949
{
50-
// load certificate from pfx file
50+
// Load certificate from pfx file
5151
if (!string.IsNullOrEmpty(FilePath))
5252
{
5353
string filePath = FilePath;
@@ -59,7 +59,7 @@ public X509Certificate GetCertificate()
5959

6060
return new X509Certificate2(filePath, Password, KeyStorageFlags);
6161
}
62-
else if (!string.IsNullOrEmpty(Thumbprint)) // load certificate from certificate store
62+
else if (!string.IsNullOrEmpty(Thumbprint)) // Load certificate from certificate store
6363
{
6464
using var store = new X509Store((StoreName)Enum.Parse(typeof(StoreName), StoreName), StoreLocation);
6565

0 commit comments

Comments
 (0)