Skip to content

Commit

Permalink
Add UdpClient Send for ReadOnlySpan (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ngraziano authored Feb 28, 2024
1 parent fd3da16 commit c27d4c5
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Net;

static partial class PolyfillExtensions
{
public static int Send(this System.Net.Sockets.UdpClient client, ReadOnlySpan<byte> datagram, IPEndPoint? endPoint)
{
return client.Send(datagram.ToArray(), datagram.Length, endPoint);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

static partial class PolyfillExtensions
{
public static int Send(this System.Net.Sockets.UdpClient client, ReadOnlySpan<byte> datagram, string? hostname, int port)
{
return client.Send(datagram.ToArray(), datagram.Length, hostname, port);
}
}
36 changes: 35 additions & 1 deletion Meziantou.Polyfill.Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
Expand Down Expand Up @@ -339,7 +341,7 @@ public void Enumerable_AggregateBy_Seed()
};
Assert.Equal(expected, collection.AggregateBy(item => item, seed: 0, (acc, item) => acc + item));
}

[Fact]
public void Enumerable_AggregateBy_SeedSelector()
{
Expand Down Expand Up @@ -502,6 +504,38 @@ public void HttpContent_ReadAsStream()
Assert.Equal([1, 2], streamContent.ToArray());
}

[Fact]
public void UdpClient()
{
int port = 1024;

UdpClient CreateUdpClient()
{
while (true)
{
try
{
return new UdpClient(port);
}
catch
{
port++;
if (port >= ushort.MaxValue)
throw;
}
}
}

using UdpClient client = CreateUdpClient();
using UdpClient server = new();

ReadOnlySpan<byte> data = [1, 2, 3];
server.Send(data, "localhost", port);
IPEndPoint endpoint = new(IPAddress.Any, 0);
var result = client.Receive(ref endpoint);
Assert.Equal(data.ToArray(), result);
}

[Fact]
public void Encoding_GetString()
{
Expand Down
2 changes: 1 addition & 1 deletion Meziantou.Polyfill/Meziantou.Polyfill.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<Version>1.0.35</Version>
<Version>1.0.36</Version>
<TransformOnBuild>true</TransformOnBuild>

<LangVersion>preview</LangVersion>
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ By default, all needed polyfills are generated. You can configure which polyfill
- `System.Net.Http.HttpContent.CopyToAsync(System.IO.Stream stream, System.Threading.CancellationToken cancellationToken)`
- `System.Net.Http.HttpContent.ReadAsStream(System.Threading.CancellationToken cancellationToken)`
- `System.Net.Http.HttpContent.ReadAsStream()`
- `System.Net.Sockets.UdpClient.Send(System.ReadOnlySpan<System.Byte> datagram, System.Net.IPEndPoint? endPoint)`
- `System.Net.Sockets.UdpClient.Send(System.ReadOnlySpan<System.Byte> datagram, System.String? hostname, System.Int32 port)`
- `System.String.Contains(System.Char value)`
- `System.String.Contains(System.Char value, System.StringComparison comparisonType)`
- `System.String.Contains(System.String value, System.StringComparison comparisonType)`
Expand Down

0 comments on commit c27d4c5

Please sign in to comment.