Skip to content

Commit

Permalink
修复线程顶层抛出异常
Browse files Browse the repository at this point in the history
  • Loading branch information
lindexi committed Oct 30, 2023
1 parent 776f7eb commit 72a310c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
26 changes: 17 additions & 9 deletions src/dotnetCampus.Ipc/Internals/PeerReConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
}
}

Expand Down
19 changes: 14 additions & 5 deletions src/dotnetCampus.Ipc/Pipes/PeerProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -212,14 +213,22 @@ internal void Update(IpcInternalPeerConnectedArgs ipcInternalPeerConnectedArgs)
/// </summary>
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)
Expand Down

0 comments on commit 72a310c

Please sign in to comment.