~ruther/NosSmooth.Comms

ref: edcd26a2f21092371c9169ae56ae85c144a8bfd7 NosSmooth.Comms/src/Core/NosSmooth.Comms.Core/Tcp/TcpServer.cs -rw-r--r-- 2.5 KiB
edcd26a2 — Rutherther feat: move to new injection supporting passing in data and returning integer 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
//  TcpServer.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.Net;
using System.Net.Sockets;
using NosSmooth.Comms.Data;
using Remora.Results;

namespace NosSmooth.Comms.Core.Tcp;

/// <summary>
/// A server using tcp.
/// </summary>
public class TcpServer : IServer, IDisposable
{
    private readonly TcpListener _listener;
    private readonly List<IConnection> _connections;

    /// <summary>
    /// Initializes a new instance of the <see cref="TcpServer"/> class.
    /// </summary>
    /// <param name="ip">The ip to bind to.</param>
    /// <param name="port">The port to bind to.</param>
    public TcpServer(IPAddress ip, int port)
    {
        _listener = new TcpListener(ip, port);
        _connections = new List<IConnection>();
    }

    /// <inheritdoc />
    public IReadOnlyList<IConnection> Clients => _connections.AsReadOnly();

    /// <inheritdoc />
    public async Task<Result<IConnection>> WaitForConnectionAsync(CancellationToken ct = default)
    {
        var tcpClient = await _listener.AcceptTcpClientAsync(ct);
        var connection = new TcpConnection(tcpClient);

        _connections.Add(connection);
        return connection;
    }

    /// <inheritdoc />
    public Task<Result> ListenAsync(CancellationToken stopToken = default)
    {
        try
        {
            _listener.Start();
            return Task.FromResult(Result.FromSuccess());
        }
        catch (Exception e)
        {
            return Task.FromResult<Result>(e);
        }
    }

    /// <inheritdoc />
    public void Close()
    {
        _listener.Stop();
    }

    private class TcpConnection : IConnection, IDisposable
    {
        private readonly System.Net.Sockets.TcpClient _client;

        public TcpConnection(System.Net.Sockets.TcpClient client)
        {
            _client = client;
        }

        public ConnectionState State => _client.Connected ? ConnectionState.Open : ConnectionState.Closed;

        public Stream ReadStream => _client.GetStream();

        public Stream WriteStream => _client.GetStream();

        public void Disconnect()
        {
            _client.Close();
        }

        public void Dispose()
        {
            _client.Dispose();
        }
    }

    /// <inheritdoc />
    public void Dispose()
    {
        foreach (var connection in _connections.Cast<IDisposable>())
        {
            connection.Dispose();
        }
    }
}
Do not follow this link