Skip to content

Instantly share code, notes, and snippets.

View StephenCleary's full-sized avatar

Stephen Cleary StephenCleary

View GitHub Profile
@StephenCleary
StephenCleary / ProducerConsumerStream.cs
Created August 24, 2022 21:11
Producer/Consumer Stream
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;
@StephenCleary
StephenCleary / JsonExtensions.cs
Last active July 29, 2022 13:25
Simple descendant axis support for System.Text.Json. Optionally calculates the relative path of each element, producing RFC (draft 5)-compatible simple JSON paths.
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)
@StephenCleary
StephenCleary / MemoryOwnerSliceExtensions.cs
Created March 31, 2022 16:54
Buffer sequence helpers
// 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);
}
@StephenCleary
StephenCleary / chat.lua
Last active March 7, 2022 14:16
Starting point for a custom TCP Wireshark dissector in Lua
-- 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.
@StephenCleary
StephenCleary / FindLocalMtu.cs
Last active February 17, 2022 14:46
Find the local MTU for a hostname/IPv4/IPv6 address
// 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;
@StephenCleary
StephenCleary / AsyncCache.cs
Last active April 15, 2024 10:22
Asynchronous cache
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Primitives;
using Nito.Logging;
@StephenCleary
StephenCleary / FnvHash.cs
Created October 6, 2019 14:15
FNV-1a hash in C#
/// <summary>
/// A hash combiner that is implemented with the Fowler/Noll/Vo algorithm (FNV-1a). This is a mutable struct for performance reasons.
/// </summary>
public struct FnvHash
{
/// <summary>
/// The starting point of the FNV hash.
/// </summary>
public const int Offset = unchecked((int)2166136261);
@StephenCleary
StephenCleary / Try.cs
Last active March 16, 2019 02:32
Try (error) monad in C#
public static class Try
{
public static Try<T> FromException<T>(Exception exception) => Try<T>.FromException(exception);
public static Try<T> FromValue<T>(T value) => Try<T>.FromValue(value);
public static Try<T> Create<T>(Func<T> func) => Try<T>.Create(func);
}
public sealed class Try<T>
{
public static Try<T> FromException(Exception exception) => new Try<T>(exception, default, true);
@StephenCleary
StephenCleary / InMemoryConfiguration.cs
Created September 6, 2017 20:53
AddJson and AddJsonObject extension methods for IConfigurationBuilder
public sealed class InMemoryFileProvider : IFileProvider
{
private readonly string _json;
public InMemoryFileProvider(string json)
{
_json = json;
}
public IFileInfo GetFileInfo(string subpath) => new FileInfo(_json);
@StephenCleary
StephenCleary / Auth0Authenticator.cs
Created September 6, 2017 18:09
Auth0 authenticator for use with Azure Functions
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Protocols;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.IdentityModel.Tokens;