mirror of
https://github.com/sourcegit-scm/sourcegit
synced 2025-05-20 19:55:00 +00:00
refactor: simpfy IPC code
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
parent
1e0fd63543
commit
e5dc211c35
1 changed files with 8 additions and 21 deletions
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.IO.Pipes;
|
using System.IO.Pipes;
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -41,7 +40,7 @@ namespace SourceGit.Models
|
||||||
{
|
{
|
||||||
using (var writer = new StreamWriter(client))
|
using (var writer = new StreamWriter(client))
|
||||||
{
|
{
|
||||||
writer.Write(Encoding.UTF8.GetBytes(cmd));
|
writer.WriteLine(cmd);
|
||||||
writer.Flush();
|
writer.Flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -55,35 +54,23 @@ namespace SourceGit.Models
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_server?.Close();
|
_cancellationTokenSource?.Cancel();
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void StartServer()
|
private async void StartServer()
|
||||||
{
|
{
|
||||||
var buffer = new byte[1024];
|
using var reader = new StreamReader(_server);
|
||||||
|
|
||||||
while (true)
|
while (!_cancellationTokenSource.IsCancellationRequested)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
await _server.WaitForConnectionAsync(_cancellationTokenSource.Token);
|
await _server.WaitForConnectionAsync(_cancellationTokenSource.Token);
|
||||||
|
|
||||||
using (var stream = new MemoryStream())
|
var line = (await reader.ReadLineAsync(_cancellationTokenSource.Token))?.Trim();
|
||||||
{
|
MessageReceived?.Invoke(line);
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
var readed = await _server.ReadAsync(buffer.AsMemory(0, 1024), _cancellationTokenSource.Token);
|
|
||||||
if (readed == 0)
|
|
||||||
break;
|
|
||||||
|
|
||||||
stream.Write(buffer, 0, readed);
|
|
||||||
}
|
|
||||||
|
|
||||||
stream.Seek(0, SeekOrigin.Begin);
|
|
||||||
MessageReceived?.Invoke(Encoding.UTF8.GetString(stream.ToArray()).Trim());
|
|
||||||
_server.Disconnect();
|
_server.Disconnect();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch
|
catch
|
||||||
{
|
{
|
||||||
// IGNORE
|
// IGNORE
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue