Skip to content

Commit fbe9c65

Browse files
committed
improve LocalNode to use the new APIs
1 parent ba414ac commit fbe9c65

File tree

3 files changed

+71
-33
lines changed

3 files changed

+71
-33
lines changed

src/Smdn.Net.MuninNode/Smdn.Net.MuninNode/LocalNode.Create.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
using System;
44
using System.Collections.Generic;
55
using System.Net;
6+
using System.Threading;
7+
using System.Threading.Tasks;
68

79
using Microsoft.Extensions.DependencyInjection;
810
using Microsoft.Extensions.Logging;
911

1012
using Smdn.Net.MuninNode.AccessRules;
13+
using Smdn.Net.MuninNode.Transport;
1114
using Smdn.Net.MuninPlugin;
1215

1316
namespace Smdn.Net.MuninNode;
@@ -39,6 +42,7 @@ public ConcreteLocalNode(
3942
IServiceProvider? serviceProvider = null
4043
)
4144
: base(
45+
serverFactory: new ServerFactory(serviceProvider?.GetService<ILoggerFactory>()?.CreateLogger<LocalNode>()),
4246
accessRule: accessRule ?? serviceProvider?.GetService<IAccessRule>(),
4347
logger: serviceProvider?.GetService<ILoggerFactory>()?.CreateLogger<LocalNode>()
4448
)
@@ -53,6 +57,23 @@ protected override EndPoint GetLocalEndPointToBind()
5357
address: ((IPEndPoint)base.GetLocalEndPointToBind()).Address,
5458
port: port
5559
);
60+
61+
private new sealed class ServerFactory(ILogger? logger) : IMuninNodeServerFactory {
62+
public ValueTask<IMuninNodeServer> CreateAsync(
63+
EndPoint endPoint,
64+
IMuninNode node,
65+
CancellationToken cancellationToken
66+
)
67+
#pragma warning disable CA2000
68+
=> new(
69+
new MuninNodeServer(
70+
endPoint: endPoint,
71+
logger: logger,
72+
serviceProvider: null
73+
)
74+
);
75+
#pragma warning restore CA2000
76+
}
5677
}
5778

5879
/// <summary>

src/Smdn.Net.MuninNode/Smdn.Net.MuninNode/LocalNode.Obsolete.cs

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

66
using Microsoft.Extensions.Logging;
77

8+
using Smdn.Net.MuninNode.Transport;
9+
810
namespace Smdn.Net.MuninNode;
911

1012
#pragma warning disable IDE0040
@@ -19,7 +21,7 @@ partial class LocalNode {
1921
/// <param name="logger">
2022
/// The <see cref="ILogger"/> to report the situation.
2123
/// </param>
22-
[Obsolete("This constructor will be deprecated in the future.")]
24+
[Obsolete($"Use a constructor overload that takes {nameof(IMuninNodeServerFactory)} as an argument.")]
2325
protected LocalNode(
2426
IAccessRule? accessRule,
2527
ILogger? logger = null
@@ -31,34 +33,7 @@ protected LocalNode(
3133
{
3234
}
3335

34-
[Obsolete("This method will be deprecated in the future.")]
36+
[Obsolete($"Use {nameof(IMuninNodeServerFactory)} and {nameof(StartAsync)} instead.")]
3537
protected override Socket CreateServerSocket()
36-
{
37-
const int MaxClients = 1;
38-
39-
Socket? server = null;
40-
41-
try {
42-
var endPoint = GetLocalEndPointToBind();
43-
44-
server = new Socket(
45-
endPoint.AddressFamily,
46-
SocketType.Stream,
47-
ProtocolType.Tcp
48-
);
49-
50-
if (endPoint.AddressFamily == AddressFamily.InterNetworkV6 && Socket.OSSupportsIPv4)
51-
server.DualMode = true;
52-
53-
server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
54-
server.Bind(endPoint);
55-
server.Listen(MaxClients);
56-
57-
return server;
58-
}
59-
catch {
60-
server?.Dispose();
61-
throw;
62-
}
63-
}
38+
=> MuninNodeServer.CreateServerSocket(endPoint: GetLocalEndPointToBind());
6439
}

src/Smdn.Net.MuninNode/Smdn.Net.MuninNode/LocalNode.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
// SPDX-FileCopyrightText: 2023 smdn <smdn@smdn.jp>
22
// SPDX-License-Identifier: MIT
3+
using System.Net;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
using Microsoft.Extensions.Logging;
8+
9+
using Smdn.Net.MuninNode.Transport;
10+
311
namespace Smdn.Net.MuninNode;
412

513
/// <summary>
@@ -11,11 +19,45 @@ public abstract partial class LocalNode : NodeBase {
1119
/// <summary>
1220
/// Initializes a new instance of the <see cref="LocalNode"/> class.
1321
/// </summary>
14-
private LocalNode()
22+
/// <param name="serverFactory">
23+
/// The <see cref="IMuninNodeServerFactory"/> factory to create an <see cref="IMuninNodeServer"/> to be used in this instance.
24+
/// If <see langword="null"/>, the default <see cref="IMuninNodeServerFactory"/> implementation is used.
25+
/// </param>
26+
/// <param name="accessRule">
27+
/// The <see cref="IAccessRule"/> to determine whether to accept or reject a remote host that connects to <see cref="LocalNode"/>.
28+
/// </param>
29+
/// <param name="logger">
30+
/// The <see cref="ILogger"/> to report the situation.
31+
/// </param>
32+
protected LocalNode(
33+
IMuninNodeServerFactory? serverFactory,
34+
IAccessRule? accessRule,
35+
ILogger? logger
36+
)
1537
: base(
16-
accessRule: null,
17-
logger: null
38+
serverFactory: serverFactory ?? ServerFactory.Instance,
39+
accessRule: accessRule,
40+
logger: logger
1841
)
1942
{
2043
}
44+
45+
private sealed class ServerFactory : IMuninNodeServerFactory {
46+
public static readonly ServerFactory Instance = new();
47+
48+
public ValueTask<IMuninNodeServer> CreateAsync(
49+
EndPoint endPoint,
50+
IMuninNode node,
51+
CancellationToken cancellationToken
52+
)
53+
#pragma warning disable CA2000
54+
=> new(
55+
new MuninNodeServer(
56+
endPoint: endPoint,
57+
logger: null,
58+
serviceProvider: null
59+
)
60+
);
61+
#pragma warning restore CA2000
62+
}
2163
}

0 commit comments

Comments
 (0)