Skip to content

Commit

Permalink
抛出更加明确的异常
Browse files Browse the repository at this point in the history
以下是收到的埋点信息,不知道是哪个炸了

```
Name: System.UnauthorizedAccessException;
Message: Access to the path is denied.;
StackTrace:    at System.IO.Pipes.NamedPipeClientStream.TryConnect(Int32 timeout, CancellationToken cancellationToken)
   at System.IO.Pipes.NamedPipeClientStream.ConnectInternal(Int32 timeout, CancellationToken cancellationToken, Int32 startTime)
   at System.IO.Pipes.NamedPipeClientStream.Connect(Int32 timeout)
   at System.IO.Pipes.NamedPipeClientStream.Connect()
   at dotnetCampus.Ipc.Pipes.IpcClientService.<>c__DisplayClass22_0.<DefaultConnectNamedPipeAsync>g__ConnectNamedPipe|0()
   at System.Threading.Tasks.Task.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__272_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at dotnetCampus.Ipc.Pipes.IpcClientService.DefaultConnectNamedPipeAsync(NamedPipeClientStream namedPipeClientStream)
   at dotnetCampus.Ipc.Pipes.IpcClientService.ConnectNamedPipeAsync(NamedPipeClientStream namedPipeClientStream)
   at dotnetCampus.Ipc.Pipes.IpcClientService.Start(Boolean shouldRegisterToPeer)
   at dotnetCampus.Ipc.Pipes.IpcProvider.ConnectBackToPeer(IpcInternalPeerConnectedArgs e)
   at dotnetCampus.Ipc.Pipes.IpcProvider.NamedPipeServerStreamPoolPeerConnected(Object sender, IpcInternalPeerConnectedArgs e)
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.Execute()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
   at System.Threading.Thread.StartCallback();
```
  • Loading branch information
lindexi committed Oct 17, 2023
1 parent a6f6299 commit ab42667
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
32 changes: 32 additions & 0 deletions src/dotnetCampus.Ipc/Exceptions/IpcPipeConnectionException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;

namespace dotnetCampus.Ipc.Exceptions
{
/// <summary>
/// 进行管道连接时的异常
/// </summary>
public class IpcPipeConnectionException : IpcLocalException
{
internal IpcPipeConnectionException(string connectingPipeName, string localClientName, string remoteServerName, string message, Exception innerException) : base(message, innerException)
{
ConnectingPipeName = connectingPipeName;
LocalPeerName = localClientName;
RemotePeerName = remoteServerName;
}

/// <summary>
/// 正在连接中的管道名。按照当前的设计,应该和 <see cref="RemotePeerName"/> 是相同的值
/// </summary>
public string ConnectingPipeName { get; }

/// <summary>
/// 本地当前的 Peer 名
/// </summary>
public string LocalPeerName { get; }

/// <summary>
/// 远端对方的 Peer 名
/// </summary>
public string RemotePeerName { get; }
}
}
19 changes: 16 additions & 3 deletions src/dotnetCampus.Ipc/Pipes/IpcClientService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,23 @@ private async Task DefaultConnectNamedPipeAsync(NamedPipeClientStream namedPipeC

void ConnectNamedPipe()
{
namedPipeClientStream.Connect();
try
{
namedPipeClientStream.Connect();

// 强行捕获变量,方便调试是在等待哪个连接
Logger.Trace($"Connected NamedPipe by {nameof(DefaultConnectNamedPipeAsync)}. LocalClient:'{localClient}';RemoteServer:'{remoteServer}'");
}
catch (Exception e)
{
// 管道连接失败了,比如抛出的是 UnauthorizedAccessException 异常
var message =
$"IPC Pipe connect to `{remoteServer}` exception by {nameof(DefaultConnectNamedPipeAsync)}. LocalClient:`{localClient}`;RemoteServer:`{remoteServer}`. Exception:{e.Message}";

// 强行捕获变量,方便调试是在等待哪个连接
Logger.Trace($"Connected NamedPipe by {nameof(DefaultConnectNamedPipeAsync)}. LocalClient:'{localClient}';RemoteServer:'{remoteServer}'");
Logger.Trace(message);

throw new IpcPipeConnectionException(remoteServer, localClient, remoteServer, message, e);
}
}
}

Expand Down

0 comments on commit ab42667

Please sign in to comment.