Skip to content

Commit

Permalink
🎨 SourceGenerator
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Aug 9, 2024
1 parent bade22e commit a6b7e8c
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace System.CodeDom.Compiler;
/// <summary>
/// 用于标注 BD.Common8.SourceGenerator.ResX 源生成器的 <see cref="Attribute"/>,替代 Resx 嵌入二进制资源实现零反射
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public sealed class BinaryResourceAttribute(string args, string? appendTemplate = null) : Attribute
{
public string Arguments { get; init; } = args;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace System.CodeDom.Compiler;
/// <summary>
/// 用于标注 BD.Common8.SourceGenerator.ResX 源生成器的 <see cref="Attribute"/>
/// </summary>
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public sealed class ResXGeneratedCodeAttribute(string relativeFilePath, byte version = 0) : Attribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public static IpcAppConnDelegatingHandler GetHttpMessageHandler(IpcAppConnection
};
IpcAppConnDelegatingHandler delegatingHandler = new(connectionString, innerHandler);
if (ignoreRemoteCertificateValidation)
innerHandler.SslOptions.RemoteCertificateValidationCallback = (_, _, _, _) => true;
innerHandler.SslOptions.RemoteCertificateValidationCallback =
static (_, _, _, _) => true;
return delegatingHandler;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace BD.Common8.Ipc.Services.Implementation;
/// Ipc 客户端连接服务实现
/// </summary>
/// <param name="connectionString"></param>
public partial class IpcClientService(IpcAppConnectionString connectionString) :
public abstract partial class IpcClientService(IpcAppConnectionString connectionString) :
WebApiClientService(Log.Factory.CreateLogger<IpcClientService>(), null!),
IIpcClientService, IAsyncDisposable
{
Expand Down Expand Up @@ -45,15 +45,11 @@ protected sealed override HttpClient CreateClient()
/// <para>https://learn.microsoft.com/zh-cn/aspnet/core/signalr/authn-and-authz?view=aspnetcore-8.0#bearer-token-authentication</para>
/// </summary>
/// <returns></returns>
protected virtual string GetAccessToken()
{
_AccessToken ??= connectionString.GetAccessToken();
return _AccessToken;
}
protected abstract string GetAccessToken();

Task<string?> GetAccessTokenAsync()
{
var accessToken = GetAccessToken();
var accessToken = _AccessToken ??= GetAccessToken();
return Task.FromResult(accessToken)!;
}

Expand Down
7 changes: 0 additions & 7 deletions src/BD.Common8.Ipc.Server/Services/IIpcServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,4 @@ public partial interface IIpcServerService
/// </summary>
/// <returns></returns>
ValueTask RunAsync();

/// <summary>
/// 获取用于客户端的连接字符串
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
IpcAppConnectionString GetConnectionString(IpcAppConnectionStringType type);
}
109 changes: 53 additions & 56 deletions src/BD.Common8.Ipc.Server/Services/Implementation/IpcServerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public abstract class IpcServerService(X509Certificate2 serverCertificate) : IIp

protected static void OnMapGroup(IEndpointRouteBuilder builder) => OnMapGroupEvent?.Invoke(builder);

static readonly long tickCount64 = long.MaxValue / 2; // 不可修改!!!
//static readonly long tickCount64 = long.MaxValue / 2; // 不可修改!!!

WebApplication? app;

Expand Down Expand Up @@ -204,53 +204,53 @@ static bool IsUsePort(IPAddress address, int port)
// return IpcAppConnectionStringType.UnixSocket;
//}

/// <inheritdoc/>
public IpcAppConnectionString GetConnectionString(IpcAppConnectionStringType/*?*/ type/* = null*/)
{
if (app == null)
throw new InvalidOperationException("The service has not been started yet.");
///// <inheritdoc/>
//public IpcAppConnectionString GetConnectionString(IpcAppConnectionStringType/*?*/ type/* = null*/)
//{
// if (app == null)
// throw new InvalidOperationException("The service has not been started yet.");

//type ??= GetFirstIpcAppConnectionStringType();
// //type ??= GetFirstIpcAppConnectionStringType();

switch (type/*.Value*/)
{
case IpcAppConnectionStringType.Https:
if (!ListenLocalhost)
throw new NotSupportedException(
"The current service does not support listening localhost.");
return new()
{
Type = IpcAppConnectionStringType.Https,
Int32Value = Http2Port,
TickCount64 = tickCount64,
ProcessId = Environment.ProcessId,
};
case IpcAppConnectionStringType.UnixSocket:
if (!ListenUnixSocket)
throw new NotSupportedException(
"The current service does not support listening unix socket.");
return new()
{
Type = IpcAppConnectionStringType.UnixSocket,
StringValue = UnixSocketPath,
TickCount64 = tickCount64,
ProcessId = Environment.ProcessId,
};
case IpcAppConnectionStringType.NamedPipe:
if (!ListenNamedPipe)
throw new NotSupportedException(
"The current service does not support listening named pipe.");
return new()
{
Type = IpcAppConnectionStringType.NamedPipe,
StringValue = PipeName,
TickCount64 = tickCount64,
ProcessId = Environment.ProcessId,
};
default:
throw ThrowHelper.GetArgumentOutOfRangeException(type);
}
}
// switch (type/*.Value*/)
// {
// case IpcAppConnectionStringType.Https:
// if (!ListenLocalhost)
// throw new NotSupportedException(
// "The current service does not support listening localhost.");
// return new()
// {
// Type = IpcAppConnectionStringType.Https,
// Int32Value = Http2Port,
// TickCount64 = tickCount64,
// ProcessId = Environment.ProcessId,
// };
// case IpcAppConnectionStringType.UnixSocket:
// if (!ListenUnixSocket)
// throw new NotSupportedException(
// "The current service does not support listening unix socket.");
// return new()
// {
// Type = IpcAppConnectionStringType.UnixSocket,
// StringValue = UnixSocketPath,
// TickCount64 = tickCount64,
// ProcessId = Environment.ProcessId,
// };
// case IpcAppConnectionStringType.NamedPipe:
// if (!ListenNamedPipe)
// throw new NotSupportedException(
// "The current service does not support listening named pipe.");
// return new()
// {
// Type = IpcAppConnectionStringType.NamedPipe,
// StringValue = PipeName,
// TickCount64 = tickCount64,
// ProcessId = Environment.ProcessId,
// };
// default:
// throw ThrowHelper.GetArgumentOutOfRangeException(type);
// }
//}

/// <summary>
/// 走 Http2 传输协议默认端口号,如果端口占用将随机一个新的
Expand Down Expand Up @@ -465,21 +465,18 @@ protected virtual void OnError(IExceptionHandlerFeature exceptionHandlerPathFeat
/// <para>https://learn.microsoft.com/zh-cn/aspnet/core/signalr/authn-and-authz?view=aspnetcore-8.0#bearer-token-authentication</para>
/// </summary>
/// <returns></returns>
protected virtual byte[] GetAccessToken()
protected abstract byte[] GetAccessToken();

/// <inheritdoc cref="GetAccessToken"/>
internal byte[] AccessToken
{
if (_AccessToken == null)
get
{
using var stream = new MemoryStream();
IpcAppConnectionString.WriteAccessToken(stream, tickCount64, Environment.ProcessId);
_AccessToken = Hashs.ByteArray.SHA256(stream);
Console.WriteLine($"Server,GetAccessToken:{_AccessToken.ToHexString()}, tickCount64: {tickCount64}, pid: {Environment.ProcessId}");
_AccessToken ??= GetAccessToken();
return _AccessToken;
}
return _AccessToken;
}

/// <inheritdoc cref="GetAccessToken"/>
internal byte[] AccessToken => GetAccessToken();

#region 同时实现释放模式和异步释放模式 https://learn.microsoft.com/zh-cn/dotnet/standard/garbage-collection/implementing-disposeasync#implement-both-dispose-and-async-dispose-patterns

/// <inheritdoc/>
Expand Down
Loading

0 comments on commit a6b7e8c

Please sign in to comment.