This gist kept getting bigger and bigger, so I moved it to a proper repository, complete with a proper NuGet package.
View DynamicTaskWhenAll.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Developed as part of https://github.com/StephenCleary/StructuredConcurrency but wasn't necessary for that project. | |
/// <summary> | |
/// Similar to <see cref="Task.WhenAll{TResult}(Task{TResult}[])"/>, but allowing any number of tasks to be added, even after waiting has begun. | |
/// At least one task must be added, or else the <see cref="Task"/> will never complete. | |
/// </summary> | |
/// <typeparam name="TResult">The type of the result of the tasks.</typeparam> | |
public sealed class DynamicTaskWhenAll<TResult> | |
{ | |
private readonly TaskCompletionSource<IReadOnlyList<TResult>> _taskCompletionSource = new(); |
View TaskDictionary.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class TaskDictionary<TRequest, TResult> | |
where TRequest : notnull | |
{ | |
public TaskDictionary(IEqualityComparer<TRequest>? comparer = null) | |
{ | |
_dictionary = new(comparer); | |
} | |
public Task<TResult> GetOrAdd(TRequest request) | |
{ |
View CancellationTokenSourceCache.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Concurrent; | |
using System.Diagnostics; | |
using Nito.Disposables; | |
/// <summary> | |
/// A cache of uncanceled <see cref="CancellationTokenSource"/> instances. | |
/// </summary> | |
public sealed class CancellationTokenSourceCache | |
{ | |
private readonly ConcurrentBag<CancellationTokenSource> _sources = new(); |
View AlignedMemoryManager.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class AlignedMemoryManager : MemoryManager<byte> | |
{ | |
// Note: No finalizer; this will deliberately leak memory if never disposed. | |
// https://gist.github.com/GrabYourPitchforks/8efb15abbd90bc5b128f64981766e834#implementation-notes-when-subclassing-memorymanagert | |
private IntPtr _pointer; | |
protected static unsafe IntPtr Allocate(nuint byteCount, nuint alignment) => (IntPtr)NativeMemory.AlignedAlloc(byteCount, alignment); | |
public override unsafe Span<byte> GetSpan() => new((void*)_pointer, ByteCount); |
View ProducerConsumerStream.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public sealed class ProducerConsumerStream | |
{ | |
public static Stream Create(Func<Stream, Task> producer, PipeOptions? options = null) | |
{ | |
var pipe = new Pipe(options ?? PipeOptions.Default); | |
var readStream = pipe.Reader.AsStream(); | |
var writeStream = pipe.Writer.AsStream(); | |
Run(); | |
return readStream; |
View JsonExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class JsonExtensions | |
{ | |
public static IEnumerable<JsonElement> SelfAndDescendants(this JsonElement element) | |
{ | |
if (element.ValueKind == JsonValueKind.Undefined) | |
yield break; | |
yield return element; | |
if (element.ValueKind == JsonValueKind.Object) |
View MemoryOwnerSliceExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/dotnet/runtime/issues/27869#issuecomment-475356398 | |
public static class MemoryOwnerSliceExtensions | |
{ | |
public static IMemoryOwner<T> Slice<T>(this IMemoryOwner<T> owner, int start, int length) | |
{ | |
if (start == 0 && length == owner.Memory.Length) | |
return owner; | |
return new SliceOwner<T>(owner, start, length); | |
} |
View chat.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- authors: Hadriel Kaplan <hadriel@128technology.com>, Stephen Cleary | |
-- Copyright (c) 2015-2022, Hadriel Kaplan, Stephen Cleary | |
-- This code is in the Public Domain, or the BSD (3 clause) license if Public Domain does not apply in your country. | |
-- Thanks to Hadriel Kaplan, who wrote the original FPM Lua script. | |
-- This is a starting point for defining a dissector for a custom TCP protocol. | |
-- This approach assumes that each message has a header that indicates the message length. | |
-- The code in this example uses a 4-byte header which is just a 4-byte big-endian message length, | |
-- and this length does not include the length of the header. | |
-- Modify the sections marked TODO to adjust these assumptions. |
View FindLocalMtu.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// LINQPad script | |
async Task Main() | |
{ | |
byte[] LogicalAnd(byte[] x, byte[] y) | |
{ | |
var result = new byte[x.Length]; | |
for (int i = 0; i != x.Length; ++i) | |
result[i] = (byte) (x[i] & y[i]); | |
return result; |
NewerOlder