wangxinhui
2026-02-05 13ca4d6e7ca157092dac6e369cd52654a34b9465
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
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
 
namespace WIDESEAWCS_Tasks.SocketServer
{
    public partial class TcpSocketServer
    {
        public IReadOnlyList<string> GetClientIds()
        {
            lock (_syncRoot)
            {
                return _clients.Keys.ToList();
            }
        }
 
        public string? GetClientIdByDevice(string deviceId)
        {
            lock (_syncRoot)
            {
                return _deviceBindings.TryGetValue(deviceId, out var cid) ? cid : null;
            }
        }
 
        public Task<bool> SendToDeviceAsync(string deviceId, string message)
        {
            var clientId = GetClientIdByDevice(deviceId);
            if (clientId == null) return Task.FromResult(false);
            return SendToClientAsync(clientId, message);
        }
 
        public async Task<bool> SendToClientAsync(string clientId, string message)
        {
            TcpClient? client;
            SemaphoreSlim? sem = null;
            Encoding? enc = null;
            lock (_syncRoot)
            {
                _clients.TryGetValue(clientId, out client);
                _clientLocks.TryGetValue(clientId, out sem);
                _clientEncodings.TryGetValue(clientId, out enc);
            }
 
            if (client == null || !client.Connected)
            {
                return false;
            }
 
            enc ??= _textEncoding;
 
            if (sem != null) await sem.WaitAsync();
            try
            {
                var ns = client.GetStream();
                var data = enc.GetBytes((message ?? string.Empty) + "\n");
                await ns.WriteAsync(data, 0, data.Length);
            }
            finally
            {
                if (sem != null) sem.Release();
            }
            return true;
        }
 
        public async Task BroadcastAsync(string message)
        {
            List<TcpClient> clients;
            lock (_syncRoot)
            {
                clients = _clients.Values.ToList();
            }
 
            await Task.WhenAll(clients.Select(c => Task.Run(async () =>
            {
                try { await SendAsync(c, message); } catch { }
            })));
        }
 
        public static async Task SendAsync(TcpClient client, string message)
        {
            if (client == null || !client.Connected)
            {
                return;
            }
 
            NetworkStream stream = client.GetStream();
            var data = Encoding.UTF8.GetBytes((message ?? string.Empty) + "\n");
            await stream.WriteAsync(data, 0, data.Length);
        }
    }
}