summary refs log tree commit diff
path: root/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket
diff options
context:
space:
mode:
authorRory& <root@rory.gay>2026-01-17 14:59:13 +0100
committerRory& <root@rory.gay>2026-01-17 14:59:13 +0100
commita46ae853223eb1de45b21a19bf064405c5ccb9a2 (patch)
tree2e8362f72e61829e642690826a5902ecd5cacf06 /extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket
parentAdd stub POSt report endpoint (diff)
downloadserver-ts-a46ae853223eb1de45b21a19bf064405c5ccb9a2.tar.xz
Setting myself up for failure
Diffstat (limited to 'extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket')
-rw-r--r--extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/Spacebar.Interop.Replication.UnixSocket.csproj17
-rw-r--r--extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/UnixSocketSpacebarReplication.cs41
-rw-r--r--extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/deps.json0
3 files changed, 58 insertions, 0 deletions
diff --git a/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/Spacebar.Interop.Replication.UnixSocket.csproj b/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/Spacebar.Interop.Replication.UnixSocket.csproj
new file mode 100644

index 00000000..81f042ed --- /dev/null +++ b/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/Spacebar.Interop.Replication.UnixSocket.csproj
@@ -0,0 +1,17 @@ +<Project Sdk="Microsoft.NET.Sdk"> + + <PropertyGroup> + <TargetFramework>net10.0</TargetFramework> + <ImplicitUsings>enable</ImplicitUsings> + <Nullable>enable</Nullable> + </PropertyGroup> + + <ItemGroup> + <ProjectReference Include="..\Spacebar.Interop.Replication.Abstractions\Spacebar.Interop.Replication.Abstractions.csproj" /> + </ItemGroup> + + <ItemGroup> + <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="10.0.2" /> + </ItemGroup> + +</Project> diff --git a/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/UnixSocketSpacebarReplication.cs b/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/UnixSocketSpacebarReplication.cs new file mode 100644
index 00000000..a817335d --- /dev/null +++ b/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/UnixSocketSpacebarReplication.cs
@@ -0,0 +1,41 @@ +using System.Net.Sockets; +using System.Text.Json; +using Microsoft.Extensions.Configuration; +using Spacebar.Interop.Replication.Abstractions; + +namespace Spacebar.Interop.Replication.UnixSocket; + +public class UnixSocketSpacebarReplication(UnixSocketConfiguration conf) : ISpacebarReplication { + private readonly Dictionary<string, Socket> _sockets = new(); + + public async Task InitializeAsync() { + var fsw = new FileSystemWatcher(conf.SocketDir); + fsw.EnableRaisingEvents = true; + fsw.Created += (s, e) => { + Console.WriteLine($"Socket created: {e.FullPath}"); + var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.Unspecified); + var ep = new UnixDomainSocketEndPoint(e.FullPath); + socket.Connect(ep); + _sockets[e.Name] = socket; + }; + } + + public async Task SendAsync(ReplicationMessage message) { + // message format: [uint32be length][payload] + var payload = JsonSerializer.SerializeToUtf8Bytes(message); + byte[] formattedPayload = [..BitConverter.GetBytes(System.Net.IPAddress.HostToNetworkOrder(payload.Length)), ..payload]; + + Parallel.ForEach(_sockets, skv => { + lock (skv.Value) + skv.Value.SendAsync(formattedPayload); + }); + } +} + +public class UnixSocketConfiguration { + public UnixSocketConfiguration(IConfiguration config) { + config.GetRequiredSection("UnixSocketReplication").Bind(this); + } + + public string SocketDir { get; set; } = null!; +} \ No newline at end of file diff --git a/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/deps.json b/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/deps.json new file mode 100644
index 00000000..e69de29b --- /dev/null +++ b/extra/admin-api/Interop/Spacebar.Interop.Replication.UnixSocket/deps.json