~ruther/NosSmooth.Comms

ref: 6200e9cc894ce951c55245d69b86fa942111341f NosSmooth.Comms/src/Core/NosSmooth.Comms.Core/NamedPipes/NamedPipeClient.cs -rw-r--r-- 1.6 KiB
6200e9cc — Rutherther ci: add build and nuget push ci 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
//  NamedPipeClient.cs
//
//  Copyright (c) František Boháček. All rights reserved.
//  Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Data;
using System.IO.Pipes;
using NosSmooth.Comms.Data;
using Remora.Results;

namespace NosSmooth.Comms.Core.NamedPipes;

/// <summary>
/// A client using named pipes.
/// </summary>
public class NamedPipeClient : IClient
{
    private readonly NamedPipeClientStream _stream;

    /// <summary>
    /// Initializes a new instance of the <see cref="NamedPipeClient"/> class.
    /// </summary>
    /// <param name="pipeName">The name of the pipe.</param>
    public NamedPipeClient(string pipeName)
    {
        _stream = new NamedPipeClientStream(".", pipeName, PipeDirection.InOut, PipeOptions.Asynchronous);
    }

    /// <inheritdoc />
    public ConnectionState State => _stream.IsConnected ? ConnectionState.Open : ConnectionState.Closed;

    /// <inheritdoc />
    public Stream ReadStream => _stream;

    /// <inheritdoc />
    public Stream WriteStream => _stream;

    /// <inheritdoc />
    public void Disconnect()
    {
        _stream.Close();
        _stream.Dispose();
    }

    /// <inheritdoc />
    public async Task<Result> ConnectAsync(CancellationToken ct)
    {
        try
        {
            await _stream.ConnectAsync(ct);
            return Result.FromSuccess();
        }
        catch (OperationCanceledException)
        {
            // ignored
            return Result.FromSuccess();
        }
        catch (Exception e)
        {
            return e;
        }
    }
}
Do not follow this link