1
- using System ;
2
1
using System . Collections . Concurrent ;
3
- using System . Linq ;
4
2
using System . Net . WebSockets ;
5
- using System . Text . Json ;
6
3
using System . Threading ;
7
4
using System . Threading . Tasks ;
8
5
using Microsoft . Extensions . Hosting ;
9
6
using Microsoft . Extensions . Logging ;
10
- using Microsoft . Extensions . Options ;
11
- using NNostr . Client ;
12
7
13
8
namespace Relay
14
9
{
@@ -17,9 +12,7 @@ public class ConnectionManager : IHostedService
17
12
private readonly StateManager _stateManager ;
18
13
private readonly ILogger < ConnectionManager > _logger ;
19
14
private readonly NostrEventService _nostrEventService ;
20
- private Task _processingSendMessages = Task . CompletedTask ;
21
- private CancellationTokenSource _cts ;
22
- public ConcurrentDictionary < string , WebSocket > Connections { get ; set ; } = new ( ) ;
15
+
23
16
24
17
25
18
public ConnectionManager ( StateManager stateManager , ILogger < ConnectionManager > logger ,
@@ -32,74 +25,29 @@ public ConnectionManager(StateManager stateManager, ILogger<ConnectionManager> l
32
25
33
26
public virtual Task StartAsync ( CancellationToken cancellationToken )
34
27
{
35
- _cts = new CancellationTokenSource ( ) ;
36
- _processingSendMessages = ProcessSendMessages ( _cts . Token ) ;
37
28
_nostrEventService . EventsMatched += ( sender , matched ) => { _ = NostrEventServiceOnEventsMatched ( matched ) ; } ;
38
29
return Task . CompletedTask ;
39
30
}
40
31
41
32
private async Task NostrEventServiceOnEventsMatched ( NostrEventsMatched e )
42
33
{
43
- if ( ! Connections . ContainsKey ( e . ConnectionId ) )
34
+ if ( ! _stateManager . Connections . ContainsKey ( e . ConnectionId ) )
44
35
{
45
36
_stateManager . RemoveConnection ( e . ConnectionId ) ;
46
37
return ;
47
38
}
48
39
49
40
foreach ( var nostrEvent in e . Events )
50
41
{
51
- await _stateManager . PendingMessages . Writer . WriteAsync ( ( e . ConnectionId ,
52
- JsonSerializer . Serialize ( new object [ ]
53
- {
54
- "EVENT" ,
55
- e . SubscriptionId , nostrEvent
56
- } ) ) ) ;
42
+ await _stateManager . SendEvent ( e . ConnectionId , e . SubscriptionId , nostrEvent ) ;
57
43
}
58
- e . OnSent . SetResult ( ) ;
59
44
_logger . LogInformation ( $ "Sent { e . Events . Length } events to { e . ConnectionId } for subscription { e . SubscriptionId } ") ;
60
45
}
61
46
62
- private async Task ProcessSendMessages ( CancellationToken cancellationToken )
63
- {
64
- while ( await _stateManager . PendingMessages . Reader . WaitToReadAsync ( cancellationToken ) )
65
- {
66
- if ( _stateManager . PendingMessages . Reader . TryRead ( out var evt ) )
67
- {
68
- try
69
- {
70
- if ( Connections . TryGetValue ( evt . Item1 , out var conn ) )
71
- {
72
- _logger . LogTrace ( $ "sending message to connection { evt . connectionId } \n { evt . message } ") ;
73
- await conn . SendMessageAsync ( evt . Item2 , cancellationToken ) ;
74
- }
75
- else
76
- {
77
- _logger . LogWarning (
78
- $ "Had to send a message to a connection that no longer exists { evt . Item1 } ") ;
79
- }
80
- }
81
- catch when ( cancellationToken . IsCancellationRequested )
82
- {
83
- throw ;
84
- }
85
- catch ( Exception ex )
86
- {
87
- _logger . LogWarning ( ex , $ "Unhandled exception in { this . GetType ( ) . Name } ") ;
88
- }
89
- }
90
- }
91
- }
47
+
92
48
93
49
public virtual async Task StopAsync ( CancellationToken cancellationToken )
94
50
{
95
- _cts ? . Cancel ( ) ;
96
- try
97
- {
98
- await _processingSendMessages ;
99
- }
100
- catch ( OperationCanceledException )
101
- {
102
- }
103
51
}
104
52
}
105
53
}
0 commit comments