Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复线程顶层抛出异常 #138

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dotnetCampus.Ipc/Internals/PeerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void RemovePeerProxy(PeerProxy peerProxy)
{
if (!peerProxy.IsBroken)
{
throw new ArgumentException($"Must remove the Broken peer");
throw new ArgumentException($"Must remove the Broken peer. PeerName={peerProxy.PeerName}");
}

ConnectedServerManagerList.TryRemove(peerProxy.PeerName, out var value);
Expand Down
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
Loading