- Notifications
You must be signed in to change notification settings - Fork8
chore: add MutagenSdk project#48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
716b98b
de4c1ce
776aff7
ee10e7e
f8807ee
00b522a
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
MutagenSdk/Proto/**/*.proto linguist-generated=true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using Coder.Desktop.MutagenSdk.Proto.Service.Synchronization; | ||
using Grpc.Core; | ||
using Grpc.Net.Client; | ||
namespace Coder.Desktop.MutagenSdk; | ||
public class MutagenClient : IDisposable | ||
{ | ||
private readonly GrpcChannel _channel; | ||
public readonly Synchronization.SynchronizationClient Synchronization; | ||
public MutagenClient(string dataDir) | ||
{ | ||
// Check for the lock file first, since it should exist if it's running. | ||
var daemonLockFile = Path.Combine(dataDir, "daemon", "daemon.lock"); | ||
if (!File.Exists(daemonLockFile)) | ||
throw new FileNotFoundException( | ||
"Mutagen daemon lock file not found, did the mutagen daemon start successfully?", daemonLockFile); | ||
// Read the IPC named pipe address from the sock file. | ||
var daemonSockFile = Path.Combine(dataDir, "daemon", "daemon.sock"); | ||
if (!File.Exists(daemonSockFile)) | ||
throw new FileNotFoundException( | ||
"Mutagen daemon socket file not found, did the mutagen daemon start successfully?", daemonSockFile); | ||
var daemonSockAddress = File.ReadAllText(daemonSockFile).Trim(); | ||
if (string.IsNullOrWhiteSpace(daemonSockAddress)) | ||
throw new InvalidOperationException( | ||
"Mutagen daemon socket address is empty, did the mutagen daemon start successfully?"); | ||
const string namedPipePrefix = @"\\.\pipe\"; | ||
if (!daemonSockAddress.StartsWith(namedPipePrefix)) | ||
throw new InvalidOperationException("Mutagen daemon socket address is not a named pipe address"); | ||
var pipeName = daemonSockAddress[namedPipePrefix.Length..]; | ||
var connectionFactory = new NamedPipesConnectionFactory(pipeName); | ||
var socketsHttpHandler = new SocketsHttpHandler | ||
{ | ||
ConnectCallback = connectionFactory.ConnectAsync, | ||
}; | ||
_channel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions | ||
{ | ||
Credentials = ChannelCredentials.Insecure, | ||
HttpHandler = socketsHttpHandler, | ||
}); | ||
Synchronization = new Synchronization.SynchronizationClient(_channel); | ||
} | ||
public void Dispose() | ||
{ | ||
_channel.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<PropertyGroup> | ||
<AssemblyName>Coder.Desktop.MutagenSdk</AssemblyName> | ||
<RootNamespace>Coder.Desktop.MutagenSdk</RootNamespace> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Protobuf Include="Proto\**\*.proto" ProtoRoot="Proto" GrpcServices="Client" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Google.Protobuf" Version="3.29.3" /> | ||
<PackageReference Include="Grpc.Net.Client" Version="2.67.0" /> | ||
<PackageReference Include="Grpc.Tools" Version="2.69.0"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
</ItemGroup> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.IO.Pipes; | ||
using System.Security.Principal; | ||
namespace Coder.Desktop.MutagenSdk; | ||
public class NamedPipesConnectionFactory | ||
{ | ||
private readonly string _pipeName; | ||
public NamedPipesConnectionFactory(string pipeName) | ||
{ | ||
_pipeName = pipeName; | ||
} | ||
public async ValueTask<Stream> ConnectAsync(SocketsHttpConnectionContext _, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var client = new NamedPipeClientStream( | ||
".", | ||
_pipeName, | ||
PipeDirection.InOut, | ||
PipeOptions.WriteThrough | PipeOptions.Asynchronous, | ||
TokenImpersonationLevel.Anonymous); | ||
try | ||
{ | ||
await client.ConnectAsync(cancellationToken); | ||
return client; | ||
} | ||
catch | ||
{ | ||
await client.DisposeAsync(); | ||
throw; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Some generated files are not rendered by default. Learn more abouthow customized files appear on GitHub.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.