Skip to content

Commit a020570

Browse files
committed
feat: Add UpdClient and UpdServer
1 parent ec208c5 commit a020570

17 files changed

+836
-112
lines changed

Atc.Network.sln

+8-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Atc.Network.Test", "test\At
1818
EndProject
1919
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sample", "sample", "{0514CBF8-F8F9-46B7-B8F6-0900D0F71C41}"
2020
EndProject
21-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Atc.Network.Console.Tcp", "sample\Atc.Network.Console.Tcp\Atc.Network.Console.Tcp.csproj", "{5A4A2683-7F94-4C9F-8DBF-4F67C5014BF6}"
21+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Atc.Network.Console.Tcp", "sample\Atc.Network.Console.Tcp\Atc.Network.Console.Tcp.csproj", "{5A4A2683-7F94-4C9F-8DBF-4F67C5014BF6}"
22+
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Atc.Network.Console.Udp", "sample\Atc.Network.Console.Udp\Atc.Network.Console.Udp.csproj", "{AEFFA50B-9DE9-4CD5-BF72-D6C65788C7BB}"
2224
EndProject
2325
Global
2426
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -38,6 +40,10 @@ Global
3840
{5A4A2683-7F94-4C9F-8DBF-4F67C5014BF6}.Debug|Any CPU.Build.0 = Debug|Any CPU
3941
{5A4A2683-7F94-4C9F-8DBF-4F67C5014BF6}.Release|Any CPU.ActiveCfg = Release|Any CPU
4042
{5A4A2683-7F94-4C9F-8DBF-4F67C5014BF6}.Release|Any CPU.Build.0 = Release|Any CPU
43+
{AEFFA50B-9DE9-4CD5-BF72-D6C65788C7BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
44+
{AEFFA50B-9DE9-4CD5-BF72-D6C65788C7BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
45+
{AEFFA50B-9DE9-4CD5-BF72-D6C65788C7BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
46+
{AEFFA50B-9DE9-4CD5-BF72-D6C65788C7BB}.Release|Any CPU.Build.0 = Release|Any CPU
4147
EndGlobalSection
4248
GlobalSection(SolutionProperties) = preSolution
4349
HideSolutionNode = FALSE
@@ -46,6 +52,7 @@ Global
4652
{24632154-8E16-49BD-B8E3-BA5F486F45A5} = {69C84246-AA75-43E8-94B2-66FD555B18B0}
4753
{F50A2350-B9D4-4A11-9933-CD3B7D75E151} = {53B6038B-38A2-4891-BB4F-8449721E096D}
4854
{5A4A2683-7F94-4C9F-8DBF-4F67C5014BF6} = {0514CBF8-F8F9-46B7-B8F6-0900D0F71C41}
55+
{AEFFA50B-9DE9-4CD5-BF72-D6C65788C7BB} = {0514CBF8-F8F9-46B7-B8F6-0900D0F71C41}
4956
EndGlobalSection
5057
GlobalSection(ExtensibilityGlobals) = postSolution
5158
SolutionGuid = {04120463-05C5-417B-8D7A-2D7D35B71A07}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<IsPackable>false</IsPackable>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Atc" Version="2.0.200" />
10+
<PackageReference Include="Microsoft.Extensions.Logging" Version="6.0.0" />
11+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\..\src\Atc.Network\Atc.Network.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
3+
[assembly: SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "OK.")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
global using System.Net;
2+
global using System.Text;
3+
4+
global using Atc.Network.Udp;
5+
6+
global using Microsoft.Extensions.Logging;
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
void OnServerDataReceived(
2+
byte[] data)
3+
{
4+
Console.WriteLine($"Server received Data Length: {data.Length}");
5+
var dataStr = Encoding.ASCII
6+
.GetString(data)
7+
.RemoveNonPrintableCharacter();
8+
Console.WriteLine($"Server received Data: {dataStr}");
9+
}
10+
11+
void OnClientDataReceived(
12+
byte[] data)
13+
{
14+
Console.WriteLine($"Client received Data Length: {data.Length}");
15+
var dataStr = Encoding.ASCII
16+
.GetString(data)
17+
.RemoveNonPrintableCharacter();
18+
Console.WriteLine($"Client received Data: {dataStr}");
19+
}
20+
21+
using var loggerFactory = LoggerFactory.Create(builder =>
22+
{
23+
builder.SetMinimumLevel(LogLevel.Trace);
24+
builder.AddConsole();
25+
});
26+
27+
var loggerServer = loggerFactory.CreateLogger<UdpServer>();
28+
var loggerClient = loggerFactory.CreateLogger<UdpClient>();
29+
30+
var udpServer = new UdpServer(
31+
loggerServer,
32+
new IPEndPoint(IPAddress.Parse("127.0.0.1"), 27001),
33+
new UdpServerConfig
34+
{
35+
EchoOnReceivedData = true,
36+
});
37+
38+
udpServer.DataReceived += OnServerDataReceived;
39+
40+
await udpServer.StartAsync(CancellationToken.None);
41+
42+
var udpClient = new UdpClient(
43+
loggerClient,
44+
new IPEndPoint(IPAddress.Parse("127.0.0.1"), 27001),
45+
27002);
46+
47+
udpClient.Connected += () => Console.WriteLine("Connected");
48+
udpClient.Disconnected += () => Console.WriteLine("Disconnected");
49+
udpClient.ConnectionStateChanged += (_, args) => Console.WriteLine($"Connection state: {args.State}");
50+
udpClient.DataReceived += OnClientDataReceived;
51+
if (!await udpClient.Connect())
52+
{
53+
Console.WriteLine("Cannot connect");
54+
udpClient.Dispose();
55+
return;
56+
}
57+
58+
await udpClient.Send("Hallo", CancellationToken.None);
59+
await udpClient.Send("World..", CancellationToken.None);
60+
await udpClient.Send("ping", CancellationToken.None);
61+
await udpServer.Send(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 27002), "The UdpServer say hallo", CancellationToken.None);
62+
63+
await Task.Delay(TimeSpan.FromMilliseconds(500));
64+
65+
await udpClient.Disconnect();
66+
udpClient.Dispose();
67+
68+
await udpServer.StopAsync(CancellationToken.None);
69+
udpServer.Dispose();
70+
71+
Console.WriteLine("Press any key for quit");
72+
Console.ReadLine();

src/Atc.Network/GlobalUsings.cs

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
global using Atc.Network.Resource;
1717
global using Atc.Network.Tcp;
1818

19+
global using Microsoft.Extensions.Hosting;
1920
global using Microsoft.Extensions.Logging;
2021
global using Microsoft.Extensions.Logging.Abstractions;

src/Atc.Network/LoggingEventIdConstants.cs

+2
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,6 @@ public static class LoggingEventIdConstants
1616
public const int DataReceiveTimeout = 10012;
1717
public const int DataReceiveNoData = 10013;
1818
public const int DataReceiveError = 10014;
19+
20+
public const int ServiceNotRunning = 10020;
1921
}

src/Atc.Network/Udp/IUdpClient.cs

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
namespace Atc.Network.Udp;
2+
3+
public interface IUdpClient : IDisposable
4+
{
5+
/// <summary>
6+
/// Event to raise when connection is established.
7+
/// </summary>
8+
event Action? Connected;
9+
10+
/// <summary>
11+
/// Event to raise when connection is destroyed.
12+
/// </summary>
13+
event Action? Disconnected;
14+
15+
/// <summary>
16+
/// Event to raise when connection state is changed.
17+
/// </summary>
18+
event EventHandler<ConnectionStateEventArgs>? ConnectionStateChanged;
19+
20+
/// <summary>
21+
/// Event to raise when data has become available from the server.
22+
/// </summary>
23+
event Action<byte[]>? DataReceived;
24+
25+
/// <summary>
26+
/// Is client connected.
27+
/// </summary>
28+
bool IsConnected { get; }
29+
30+
/// <summary>
31+
/// Connect.
32+
/// </summary>
33+
/// <param name="cancellationToken">The cancellationToken.</param>
34+
Task<bool> Connect(
35+
CancellationToken cancellationToken = default);
36+
37+
/// <summary>
38+
/// Disconnect.
39+
/// </summary>
40+
Task Disconnect();
41+
42+
/// <summary>
43+
/// Send data.
44+
/// </summary>
45+
/// <param name="data">The data to send.</param>
46+
/// <param name="cancellationToken">The cancellationToken.</param>
47+
Task Send(
48+
string data,
49+
CancellationToken cancellationToken);
50+
51+
/// <summary>
52+
/// Send data.
53+
/// </summary>
54+
/// <param name="encoding">The encoding.</param>
55+
/// <param name="data">The data to send.</param>
56+
/// <param name="cancellationToken">The cancellationToken.</param>
57+
Task Send(
58+
Encoding encoding,
59+
string data,
60+
CancellationToken cancellationToken);
61+
62+
/// <summary>
63+
/// Send data.
64+
/// </summary>
65+
/// <param name="data">The data to send.</param>
66+
/// <param name="cancellationToken">The cancellationToken.</param>
67+
Task Send(
68+
byte[] data,
69+
CancellationToken cancellationToken);
70+
71+
/// <summary>
72+
/// Send data.
73+
/// </summary>
74+
/// <param name="data">The data to send.</param>
75+
/// <param name="terminationType">The terminationType.</param>
76+
/// <param name="cancellationToken">The cancellationToken.</param>
77+
Task Send(
78+
byte[] data,
79+
TerminationType terminationType,
80+
CancellationToken cancellationToken);
81+
}

src/Atc.Network/Udp/IUdpServer.cs

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
namespace Atc.Network.Udp;
2+
3+
public interface IUdpServer : IHostedService, IDisposable
4+
{
5+
/// <summary>
6+
/// Event to raise when data has become available from the server.
7+
/// </summary>
8+
event Action<byte[]>? DataReceived;
9+
10+
/// <summary>
11+
/// Is running.
12+
/// </summary>
13+
bool IsRunning { get; }
14+
15+
/// <summary>
16+
/// Send data.
17+
/// </summary>
18+
/// <param name="recipient">The recipient endpoint.</param>
19+
/// <param name="data">The data to send.</param>
20+
/// <param name="cancellationToken">The cancellationToken.</param>
21+
Task Send(
22+
EndPoint recipient,
23+
string data,
24+
CancellationToken cancellationToken);
25+
26+
/// <summary>
27+
/// Send data.
28+
/// </summary>
29+
/// <param name="recipient">The recipient endpoint.</param>
30+
/// <param name="encoding">The encoding.</param>
31+
/// <param name="data">The data to send.</param>
32+
/// <param name="cancellationToken">The cancellationToken.</param>
33+
Task Send(
34+
EndPoint recipient,
35+
Encoding encoding,
36+
string data,
37+
CancellationToken cancellationToken);
38+
39+
/// <summary>
40+
/// Send data.
41+
/// </summary>
42+
/// <param name="recipient">The recipient endpoint.</param>
43+
/// <param name="data">The data to send.</param>
44+
/// <param name="cancellationToken">The cancellationToken.</param>
45+
Task Send(
46+
EndPoint recipient,
47+
byte[] data,
48+
CancellationToken cancellationToken);
49+
50+
/// <summary>
51+
/// Send data.
52+
/// </summary>
53+
/// <param name="recipient">The recipient endpoint.</param>
54+
/// <param name="data">The data to send.</param>
55+
/// <param name="terminationType">The terminationType.</param>
56+
/// <param name="cancellationToken">The cancellationToken.</param>
57+
Task Send(
58+
EndPoint recipient,
59+
byte[] data,
60+
TerminationType terminationType,
61+
CancellationToken cancellationToken);
62+
}

0 commit comments

Comments
 (0)