88
99namespace dotnetCampus . Ipc . Internals
1010{
11- class PeerManager : IDisposable
11+ internal interface IPeerManager
12+ {
13+ /// <summary>
14+ /// 和 <see cref="IpcProvider"/> 不同的是,无论是主动连接还是被动连过来的,都会触发此事件
15+ /// </summary>
16+ event EventHandler < PeerConnectedArgs > ? PeerConnected ;
17+
18+ /// <summary>
19+ /// 当前连接的数量。此属性仅有记日志的作用。由于 IPC 将会不断多进程连接和断开,所以这个数量是不断变化的。可能获取的一刻,实际情况就和此不相同
20+ /// </summary>
21+ int CurrentConnectedPeerProxyCount { get ; }
22+
23+ /// <summary>
24+ /// 获取当前连接到的 <see cref="PeerProxy"/> 列表。仅表示获取时的状态,由于 IPC 将会不断多进程连接和断开,所以这个列表是不断变化的。可能获取的一刻,实际情况就和此不相同
25+ /// </summary>
26+ /// <returns></returns>
27+ IReadOnlyList < PeerProxy > GetCurrentConnectedPeerProxyList ( ) ;
28+ }
29+
30+ class PeerManager : IPeerManager , IDisposable
1231 {
1332 public PeerManager ( IpcProvider ipcProvider )
1433 {
@@ -19,12 +38,12 @@ public bool TryAdd(PeerProxy peerProxy)
1938 {
2039 peerProxy . PeerConnectionBroken += PeerProxy_PeerConnectionBroken ;
2140 OnAdd ( peerProxy ) ;
22- return ConnectedServerManagerDictionary . TryAdd ( peerProxy . PeerName , peerProxy ) ;
41+ return ConnectedPeerProxyDictionary . TryAdd ( peerProxy . PeerName , peerProxy ) ;
2342 }
2443
2544 public bool TryGetValue ( string key , [ NotNullWhen ( true ) ] out PeerProxy ? peer )
2645 {
27- return ConnectedServerManagerDictionary . TryGetValue ( key , out peer ) ;
46+ return ConnectedPeerProxyDictionary . TryGetValue ( key , out peer ) ;
2847 }
2948
3049 /// <summary>
@@ -38,7 +57,7 @@ public void RemovePeerProxy(PeerProxy peerProxy)
3857 throw new ArgumentException ( $ "Must remove the Broken peer. PeerName={ peerProxy . PeerName } ") ;
3958 }
4059
41- ConnectedServerManagerDictionary . TryRemove ( peerProxy . PeerName , out var value ) ;
60+ ConnectedPeerProxyDictionary . TryRemove ( peerProxy . PeerName , out var value ) ;
4261
4362 if ( ReferenceEquals ( peerProxy , value ) || value is null )
4463 {
@@ -55,7 +74,7 @@ public void RemovePeerProxy(PeerProxy peerProxy)
5574 $ "Peer 断开之后,从已有列表删除时发现列表里面记录的 Peer 和当前的不是相同的一个。仅调试下抛出。PeerName={ peerProxy . PeerName } ") ;
5675 }
5776
58- ConnectedServerManagerDictionary . TryAdd ( value . PeerName , value ) ;
77+ ConnectedPeerProxyDictionary . TryAdd ( value . PeerName , value ) ;
5978 }
6079 }
6180
@@ -70,7 +89,7 @@ public async Task WaitForPeerConnectFinishedAsync(PeerProxy peerProxy)
7089
7190 OnAdd ( peerProxy ) ;
7291 // 更新或注册,用于解决之前注册的实际上是断开的连接
73- ConnectedServerManagerDictionary . AddOrUpdate ( peerProxy . PeerName , peerProxy , ( s , proxy ) => proxy ) ;
92+ ConnectedPeerProxyDictionary . AddOrUpdate ( peerProxy . PeerName , peerProxy , ( s , proxy ) => proxy ) ;
7493 }
7594
7695 private void OnAdd ( PeerProxy peerProxy )
@@ -82,6 +101,25 @@ private void OnAdd(PeerProxy peerProxy)
82101 peerProxy . PeerReConnector . ReconnectFail -= PeerReConnector_ReconnectFail ;
83102 peerProxy . PeerReConnector . ReconnectFail += PeerReConnector_ReconnectFail ;
84103 }
104+
105+ if ( ! ConnectedPeerProxyDictionary . ContainsKey ( peerProxy . PeerName ) )
106+ {
107+ // 没有从字典找到,证明是首次连接到的。或曾经断开过的,此后再连接的
108+ PeerConnected ? . Invoke ( this , new PeerConnectedArgs ( peerProxy ) ) ;
109+ }
110+ }
111+
112+ /// <inheritdoc />
113+ public event EventHandler < PeerConnectedArgs > ? PeerConnected ;
114+
115+ /// <inheritdoc />
116+ public int CurrentConnectedPeerProxyCount => ConnectedPeerProxyDictionary . Count ;
117+
118+ /// <inheritdoc />
119+ public IReadOnlyList < PeerProxy > GetCurrentConnectedPeerProxyList ( )
120+ {
121+ // 这里是线程安全的,但只会返回当前的状态
122+ return ConnectedPeerProxyDictionary . Values . ToList ( ) ;
85123 }
86124
87125 private void PeerReConnector_ReconnectFail ( object ? sender , ReconnectFailEventArgs e )
@@ -95,7 +133,7 @@ private void PeerReConnector_ReconnectFail(object? sender, ReconnectFailEventArg
95133
96134 public void Dispose ( )
97135 {
98- foreach ( var pair in ConnectedServerManagerDictionary )
136+ foreach ( var pair in ConnectedPeerProxyDictionary )
99137 {
100138 var peer = pair . Value ;
101139 // 为什么 PeerProxy 不加上 IDisposable 方法
@@ -104,7 +142,7 @@ public void Dispose()
104142 }
105143 }
106144
107- private ConcurrentDictionary < string /*PeerName*/ , PeerProxy > ConnectedServerManagerDictionary { get ; } =
145+ private ConcurrentDictionary < string /*PeerName*/ , PeerProxy > ConnectedPeerProxyDictionary { get ; } =
108146 new ConcurrentDictionary < string , PeerProxy > ( ) ;
109147 private readonly IpcProvider _ipcProvider ;
110148
0 commit comments