Skip to content

Commit

Permalink
Major Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Jan 31, 2025
1 parent 67baefd commit a826a1c
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 75 deletions.
16 changes: 1 addition & 15 deletions src/Library/Sucrose.Pipe/Helper/PipeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ internal class PipeClient : IDisposable
private bool _isConnected;
private StreamWriter _writer;
private NamedPipeClientStream _pipeClient;
private readonly CancellationTokenSource _cancellationTokenSource = new();

public bool IsConnected => _pipeClient?.IsConnected ?? false;

public async Task Start(string pipeName)
{
_pipeClient = new(SMMRG.PipeServerName, pipeName, PipeDirection.Out, PipeOptions.Asynchronous);

await _pipeClient.ConnectAsync(_cancellationTokenSource.Token);
await _pipeClient.ConnectAsync();
_isConnected = true;

_writer = new(_pipeClient)
Expand All @@ -29,16 +28,8 @@ public async Task Stop()
{
_isConnected = false;

#if NET8_0_OR_GREATER
await _cancellationTokenSource.CancelAsync();
#else
_cancellationTokenSource.Cancel();
#endif

if (_writer != null)
{
await _writer.FlushAsync();

#if NET8_0_OR_GREATER
await _writer.DisposeAsync();
#else
Expand Down Expand Up @@ -75,17 +66,12 @@ public async Task SendMessage(string message)
if (!string.IsNullOrWhiteSpace(message))
{
await _writer.WriteLineAsync(message);
await _writer.FlushAsync();
}
}

public void Dispose()
{
_cancellationTokenSource.Cancel();

_ = Stop();

_cancellationTokenSource.Dispose();
}
}
}
17 changes: 3 additions & 14 deletions src/Library/Sucrose.Pipe/Helper/PipeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ internal class PipeServer : IDisposable
private bool _isRunning;
private StreamReader _reader;
private NamedPipeServerStream _pipeServer;
private readonly CancellationTokenSource _cancellationTokenSource = new();

public bool IsConnected => _pipeServer?.IsConnected ?? false;

Expand All @@ -17,14 +16,14 @@ public async Task Start(string pipeName, EventHandler<SPEMREA> eventHandler)
_isRunning = true;
_pipeServer = new(pipeName, PipeDirection.In, 10, PipeTransmissionMode.Byte, PipeOptions.Asynchronous);

while (_isRunning && !_cancellationTokenSource.Token.IsCancellationRequested)
while (_isRunning)
{
try
{
await _pipeServer.WaitForConnectionAsync(_cancellationTokenSource.Token);
await _pipeServer.WaitForConnectionAsync();
_reader = new(_pipeServer);

while (IsConnected && !_cancellationTokenSource.Token.IsCancellationRequested)
while (IsConnected)
{
string message = await _reader.ReadLineAsync();

Expand Down Expand Up @@ -58,12 +57,6 @@ public async Task Stop()
{
_isRunning = false;

#if NET8_0_OR_GREATER
await _cancellationTokenSource.CancelAsync();
#else
_cancellationTokenSource.Cancel();
#endif

if (_reader != null)
{
_reader.Dispose();
Expand All @@ -89,11 +82,7 @@ public async Task Stop()

public void Dispose()
{
_cancellationTokenSource.Cancel();

_ = Stop();

_cancellationTokenSource.Dispose();
}
}
}
59 changes: 15 additions & 44 deletions src/Library/Sucrose.Pipe/PipeT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,14 @@ namespace Sucrose.Pipe
{
public class PipeT(string PipeName)
{
private bool ClientStarted;
private bool ServerStarted;

private bool ClientStopped;
private bool ServerStopped;

private readonly SPHPC PC = new();
private readonly SPHPS PS = new();

public event EventHandler<SPEMREA> MessageReceived;

public async Task StartClient()
{
if (!ClientStarted)
{
await PC.Start(PipeName);
ClientStarted = true;
}

if (!ClientStopped && !PC.IsConnected)
if (!PC.IsConnected)
{
try
{
Expand All @@ -39,70 +27,53 @@ public async Task StartClient()

public async Task StartClient(string message)
{
if (!ClientStarted)
if (!PC.IsConnected)
{
await PC.Start(PipeName);
ClientStarted = true;
}

if (!ClientStopped)
{
if (!PC.IsConnected)
try
{
try
{
await PC.Stop();
}
catch { }
await PC.Start(PipeName);
await PC.Stop();
}
catch { }

await PC.SendMessage(message);
await PC.Start(PipeName);
}

await PC.SendMessage(message);
}

public async Task StartServer()
{
if (!ServerStarted || ServerStopped)
if (!PS.IsConnected)
{
await PS.Start(PipeName, MessageReceived);
try
{
await PS.Stop();
}
catch { }

ServerStopped = false;
ServerStarted = true;
await PS.Start(PipeName, MessageReceived);
}
}

public async Task StopClient()
{
await PC.Stop();

ClientStopped = true;
ClientStarted = false;
}

public async Task StopServer()
{
await PS.Stop();

ServerStopped = true;
ServerStarted = false;
}

public async Task DisposeClient()
{
ClientStarted = false;
ClientStopped = true;

await PC.Stop();

PC.Dispose();
}

public async Task DisposeServer()
{
ServerStarted = false;
ServerStopped = true;

await PS.Stop();

PS.Dispose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ internal static class Backgroundog

public static SSDEPT MemoryPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMMCB.MemoryPerformance, SSDEPT.Resume);

public static SSDEPT FocusPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMMCB.FocusPerformance, SSDEPT.Resume);
public static SSDECT CommunicationType => SMMI.BackgroundogSettingManager.GetSetting(SMMCB.CommunicationType, SSDECT.Signal);

public static SSDECT CommunicationType => SMMI.BackgroundogSettingManager.GetSetting(SMMCB.CommunicationType, SSDECT.Pipe);
public static SSDEPT FocusPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMMCB.FocusPerformance, SSDEPT.Resume);

public static SSDEPT SleepPerformance => SMMI.BackgroundogSettingManager.GetSetting(SMMCB.SleepPerformance, SSDEPT.Close);

Expand Down

0 comments on commit a826a1c

Please sign in to comment.