-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Named pipes for. custom uri handling
- Loading branch information
Showing
11 changed files
with
226 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace OpenShock.ShockOsc.Cli.Uri; | ||
|
||
public class UriParameter | ||
{ | ||
public required UriParameterType Type { get; set; } | ||
public IReadOnlyCollection<string> Arguments { get; set; } = Array.Empty<string>(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace OpenShock.ShockOsc.Cli.Uri; | ||
|
||
public enum UriParameterType | ||
{ | ||
Token | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace OpenShock.ShockOsc.Cli.Uri; | ||
|
||
public static class UriParser | ||
{ | ||
public static UriParameter Parse(string uri) | ||
{ | ||
ReadOnlySpan<char> uriSpan = uri; | ||
var dePrefixed = uriSpan[9..]; | ||
var type = dePrefixed[..dePrefixed.IndexOf('/')]; | ||
|
||
return new UriParameter | ||
{ | ||
Type = Enum.Parse<UriParameterType>(type, true), | ||
Arguments = dePrefixed[(type.Length + 1)..].ToString().Split('/') | ||
}; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Collections; | ||
|
||
namespace OpenShock.ShockOsc; | ||
|
||
public static class PipeHelper | ||
{ | ||
public static IEnumerable<string> EnumeratePipes() { | ||
bool MoveNextSafe(IEnumerator enumerator) { | ||
|
||
// Pipes might have illegal characters in path. Seen one from IAR containing < and >. | ||
// The FileSystemEnumerable.MoveNext source code indicates that another call to MoveNext will return | ||
// the next entry. | ||
// Pose a limit in case the underlying implementation changes somehow. This also means that no more than 10 | ||
// pipes with bad names may occur in sequence. | ||
const int retries = 10; | ||
for (int i = 0; i < retries; i++) { | ||
try { | ||
return enumerator.MoveNext(); | ||
} catch (ArgumentException) { | ||
} | ||
} | ||
Console.WriteLine("Pipe enumeration: Retry limit due to bad names reached."); | ||
return false; | ||
} | ||
|
||
using (var enumerator = Directory.EnumerateFiles(@"\\.\pipe\").GetEnumerator()) { | ||
while (MoveNextSafe(enumerator)) { | ||
yield return enumerator.Current; | ||
} | ||
} | ||
} | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace OpenShock.ShockOsc.Services.Pipes; | ||
|
||
public sealed class PipeMessage | ||
{ | ||
public required PipeMessageType Type { get; set; } | ||
public object? Data { get; set; } | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace OpenShock.ShockOsc.Services.Pipes; | ||
|
||
public enum PipeMessageType | ||
{ | ||
Token | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System.Collections.Concurrent; | ||
using System.IO.Pipes; | ||
using System.Text.Json; | ||
using Microsoft.Extensions.Logging; | ||
using OpenShock.SDK.CSharp.Utils; | ||
using OpenShock.ShockOsc.Utils; | ||
|
||
namespace OpenShock.ShockOsc.Services.Pipes; | ||
|
||
public sealed class PipeServerService | ||
{ | ||
private readonly ILogger<PipeServerService> _logger; | ||
private uint _clientCount = 0; | ||
|
||
public PipeServerService(ILogger<PipeServerService> logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public ConcurrentQueue<PipeMessage> MessageQueue { get; } = new(); | ||
public event Func<Task>? OnMessageReceived; | ||
|
||
public void StartServer() | ||
{ | ||
OsTask.Run(ServerLoop); | ||
} | ||
|
||
private async Task ServerLoop() | ||
{ | ||
var id = _clientCount++; | ||
|
||
await using var pipeServerStream = new NamedPipeServerStream("OpenShock.ShockOsc", PipeDirection.In, 20, | ||
PipeTransmissionMode.Byte, PipeOptions.Asynchronous); | ||
|
||
|
||
_logger.LogInformation("[{Id}] Starting new server loop", id); | ||
|
||
await pipeServerStream.WaitForConnectionAsync(); | ||
#pragma warning disable CS4014 | ||
OsTask.Run(ServerLoop); | ||
#pragma warning restore CS4014 | ||
|
||
_logger.LogInformation("[{Id}] Pipe connected!", id); | ||
|
||
using var reader = new StreamReader(pipeServerStream); | ||
while (pipeServerStream.IsConnected && !reader.EndOfStream) | ||
{ | ||
var line = await reader.ReadLineAsync(); | ||
if (string.IsNullOrEmpty(line)) | ||
{ | ||
_logger.LogWarning("[{Id}] Received empty pipe message. Skipping...", id); | ||
continue; | ||
} | ||
|
||
try | ||
{ | ||
var jsonObj = JsonSerializer.Deserialize<PipeMessage>(line); | ||
if (jsonObj is null) | ||
{ | ||
_logger.LogWarning("[{Id}] Failed to deserialize pipe message. Skipping...", id); | ||
continue; | ||
} | ||
|
||
MessageQueue.Enqueue(jsonObj); | ||
await OnMessageReceived.Raise(); | ||
_logger.LogInformation("[{Id}], Received pipe message of type: {Type}", id, jsonObj.Type); | ||
} | ||
catch (JsonException ex) | ||
{ | ||
_logger.LogError(ex, "[{Id}] Failed to deserialize pipe message. Skipping...", id); | ||
} | ||
} | ||
|
||
_logger.LogInformation("[{Id}] Pipe disconnected. Stopping server loop...", id); | ||
} | ||
} |
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