diff --git a/src/dotnetCampus.Ipc/Internals/PeerReConnector.cs b/src/dotnetCampus.Ipc/Internals/PeerReConnector.cs index 96b3def..4f58c44 100644 --- a/src/dotnetCampus.Ipc/Internals/PeerReConnector.cs +++ b/src/dotnetCampus.Ipc/Internals/PeerReConnector.cs @@ -34,18 +34,26 @@ private void PeerProxy_PeerConnectionBroken(object? sender, IPeerConnectionBroke private async void Reconnect() { - var ipcClientService = _ipcProvider.CreateIpcClientService(_peerProxy.PeerName); - var success = await TryReconnectAsync(ipcClientService); - - if (success) + try { - _peerProxy.Reconnect(ipcClientService); + var ipcClientService = _ipcProvider.CreateIpcClientService(_peerProxy.PeerName); + var success = await TryReconnectAsync(ipcClientService); + + if (success) + { + _peerProxy.Reconnect(ipcClientService); + } + else + { + _ipcProvider.IpcContext.Logger.Error($"[PeerReConnector][Reconnect] Fail. PeerName={_peerProxy.PeerName}"); + + ReconnectFail?.Invoke(this, new ReconnectFailEventArgs(_peerProxy, _ipcProvider)); + } } - else + catch (Exception e) { - _ipcProvider.IpcContext.Logger.Error($"[PeerReConnector][Reconnect] Fail. PeerName={_peerProxy.PeerName}"); - - ReconnectFail?.Invoke(this, new ReconnectFailEventArgs(_peerProxy, _ipcProvider)); + // 线程顶层,吃掉所有的异常 + _ipcProvider.IpcContext.Logger.Error(e, $"[PeerReConnector][Reconnect] Reconnect Peer Fail. PeerName={_peerProxy.PeerName}"); } } diff --git a/src/dotnetCampus.Ipc/Pipes/PeerProxy.cs b/src/dotnetCampus.Ipc/Pipes/PeerProxy.cs index 39e440f..41383ec 100644 --- a/src/dotnetCampus.Ipc/Pipes/PeerProxy.cs +++ b/src/dotnetCampus.Ipc/Pipes/PeerProxy.cs @@ -7,6 +7,7 @@ using dotnetCampus.Ipc.Exceptions; using dotnetCampus.Ipc.Internals; using dotnetCampus.Ipc.Messages; +using dotnetCampus.Ipc.Utils.Extensions; using Newtonsoft.Json.Schema; namespace dotnetCampus.Ipc.Pipes @@ -212,14 +213,22 @@ internal void Update(IpcInternalPeerConnectedArgs ipcInternalPeerConnectedArgs) /// internal async void Reconnect(IpcClientService ipcClientService) { - Debug.Assert(ipcClientService.PeerName == PeerName); + try + { + Debug.Assert(ipcClientService.PeerName == PeerName); - IpcClientService = ipcClientService; + IpcClientService = ipcClientService; - // 等待完成更新之后,再进行通知,否则将会在收到事件时,还在准备完成所有逻辑 - await WaitForFinishedTaskCompletionSource.Task; + // 等待完成更新之后,再进行通知,否则将会在收到事件时,还在准备完成所有逻辑 + await WaitForFinishedTaskCompletionSource.Task; - PeerReconnected?.Invoke(this, new PeerReconnectedArgs()); + PeerReconnected?.Invoke(this, new PeerReconnectedArgs()); + } + catch (Exception e) + { + // 线程顶层,不能再抛出异常 + IpcContext.Logger.Error(e, $"[PeerProxy] Reconnect Fail. PeerName={PeerName}"); + } } private void ServerStreamMessageReader_PeerConnectBroke(object? sender, PeerConnectionBrokenArgs e)