@@ -107,7 +107,7 @@ public NamedPipeClientStreamResult(Exception exception)
107107 /// <returns></returns>
108108 public async Task Start ( bool shouldRegisterToPeer = true )
109109 {
110- var result = await StartInternalAsync ( isReConnect : false , shouldRegisterToPeer , onlyConnectExistsPeer : false /*不是只连接存在的对方,如果对方还不存在,则进行等待*/ ) ;
110+ var result = await StartInternalAsync ( isReConnect : false , shouldRegisterToPeer , onlyConnectToExistingPeer : false /*不是只连接存在的对方,如果对方还不存在,则进行等待*/ ) ;
111111
112112 if ( ! result )
113113 {
@@ -121,22 +121,22 @@ public async Task Start(bool shouldRegisterToPeer = true)
121121 /// <returns></returns>
122122 internal Task < bool > TryConnectToExistingPeerAsync ( )
123123 {
124- return StartInternalAsync ( isReConnect : false , shouldRegisterToPeer : true , onlyConnectExistsPeer : true ) ;
124+ return StartInternalAsync ( isReConnect : false , shouldRegisterToPeer : false , onlyConnectToExistingPeer : true ) ;
125125 }
126126
127127 /// <inheritdoc cref="Start"/>
128128 /// <param name="isReConnect">是否属于重新连接</param>
129129 /// <param name="shouldRegisterToPeer">是否需要向对方注册</param>
130- /// <param name="onlyConnectExistsPeer ">只连接存在的对方</param>
130+ /// <param name="onlyConnectToExistingPeer ">只连接存在的对方</param>
131131 /// <returns>True:启动成功</returns>
132- internal async Task < bool > StartInternalAsync ( bool isReConnect , bool shouldRegisterToPeer , bool onlyConnectExistsPeer )
132+ internal async Task < bool > StartInternalAsync ( bool isReConnect , bool shouldRegisterToPeer , bool onlyConnectToExistingPeer )
133133 {
134134 var localClient = IpcContext . PipeName ;
135135 var remoteServer = PeerName ;
136136
137137 Logger . Trace ( $ "StartInternalAsync Connecting NamedPipe. LocalClient:'{ localClient } ';RemoteServer:'{ remoteServer } '") ;
138138
139- if ( onlyConnectExistsPeer )
139+ if ( onlyConnectToExistingPeer )
140140 {
141141 if ( ! PipeHelper . IsPipeExists ( remoteServer ) )
142142 {
@@ -151,7 +151,7 @@ internal async Task<bool> StartInternalAsync(bool isReConnect, bool shouldRegist
151151
152152 try
153153 {
154- var result = await ConnectNamedPipeAsync ( isReConnect , namedPipeClientStream , onlyConnectExistsPeer ) ;
154+ var result = await ConnectNamedPipeAsync ( isReConnect , namedPipeClientStream , onlyConnectToExistingPeer ) ;
155155 if ( ! result )
156156 {
157157 _namedPipeClientStreamTaskCompletionSource . TrySetResult ( new NamedPipeClientStreamResult ( namedPipeClientStream : null ) ) ;
@@ -175,7 +175,7 @@ internal async Task<bool> StartInternalAsync(bool isReConnect, bool shouldRegist
175175 if ( shouldRegisterToPeer )
176176 {
177177 // 启动之后,向对方注册,此时对方是服务器
178- await RegisterToPeer ( ) ;
178+ await RegisterToPeerAsync ( ) ;
179179 }
180180
181181 return true ;
@@ -186,17 +186,17 @@ internal async Task<bool> StartInternalAsync(bool isReConnect, bool shouldRegist
186186 /// </summary>
187187 /// <param name="isReConnect">是否属于重新连接</param>
188188 /// <param name="namedPipeClientStream"></param>
189- /// <param name="onlyConnectExistsPeer ">只连接存在的对方</param>
189+ /// <param name="onlyConnectToExistingPeer ">只连接存在的对方</param>
190190 /// <returns>True 连接成功</returns>
191191 /// 独立方法,方便 dnspy 调试
192- private async Task < bool > ConnectNamedPipeAsync ( bool isReConnect , NamedPipeClientStream namedPipeClientStream , bool onlyConnectExistsPeer )
192+ private async Task < bool > ConnectNamedPipeAsync ( bool isReConnect , NamedPipeClientStream namedPipeClientStream , bool onlyConnectToExistingPeer )
193193 {
194194 var connector = IpcContext . IpcClientPipeConnector ;
195195
196196 if ( connector == null )
197197 {
198198 var timeout = Timeout . Infinite ;
199- if ( onlyConnectExistsPeer )
199+ if ( onlyConnectToExistingPeer )
200200 {
201201 // 如果是只连接存在的对方的情况,即使对方存在,也需要设置一个短暂的超时时间
202202 // 为什么这里还需要设置超时时间,这是因为可能上一步判断 IsPipeExists 时,对方还是存在的,然而当前准备连接的时候,对方已经挂了,因此不能无限等待,需要设置一个短暂的时间
@@ -209,10 +209,11 @@ private async Task<bool> ConnectNamedPipeAsync(bool isReConnect, NamedPipeClient
209209 }
210210 catch ( Exception e )
211211 {
212- if ( onlyConnectExistsPeer && e is IpcPipeConnectionException ipcPipeConnectionException )
212+ if ( onlyConnectToExistingPeer && e is IpcPipeConnectionException ipcPipeConnectionException )
213213 {
214214 if ( ipcPipeConnectionException . InnerException is TimeoutException )
215215 {
216+ // 如果是只连接存在的对方,且连接超时了,则直接返回 false 证明对方现在无法被连接上,正常就是对方不存在
216217 return false ;
217218 }
218219 }
@@ -223,7 +224,7 @@ private async Task<bool> ConnectNamedPipeAsync(bool isReConnect, NamedPipeClient
223224 }
224225 else
225226 {
226- return await CustomConnectNamedPipeAsync ( connector , isReConnect , namedPipeClientStream , onlyConnectExistsPeer ) ;
227+ return await CustomConnectNamedPipeAsync ( connector , isReConnect , namedPipeClientStream , onlyConnectToExistingPeer ) ;
227228 }
228229 }
229230
@@ -233,16 +234,16 @@ private async Task<bool> ConnectNamedPipeAsync(bool isReConnect, NamedPipeClient
233234 /// <param name="ipcClientPipeConnector"></param>
234235 /// <param name="isReConnect">是否属于重新连接</param>
235236 /// <param name="namedPipeClientStream"></param>
236- /// <param name="onlyConnectExistsPeer "></param>
237+ /// <param name="onlyConnectToExistingPeer "></param>
237238 /// <returns></returns>
238239 private async Task < bool > CustomConnectNamedPipeAsync ( IIpcClientPipeConnector ipcClientPipeConnector ,
239240 bool isReConnect ,
240- NamedPipeClientStream namedPipeClientStream , bool onlyConnectExistsPeer )
241+ NamedPipeClientStream namedPipeClientStream , bool onlyConnectToExistingPeer )
241242 {
242243 Logger . Trace ( $ "Connecting NamedPipe by { nameof ( CustomConnectNamedPipeAsync ) } . LocalClient:'{ IpcContext . PipeName } ';RemoteServer:'{ PeerName } '") ;
243244 var cancellationToken = CancellationToken . None ;
244245
245- if ( onlyConnectExistsPeer )
246+ if ( onlyConnectToExistingPeer )
246247 {
247248 cancellationToken = new CancellationTokenSource ( TimeSpan . FromMilliseconds ( 10 ) ) . Token ;
248249 }
@@ -292,7 +293,7 @@ void ConnectNamedPipe()
292293 }
293294 }
294295
295- private async Task RegisterToPeer ( )
296+ internal async Task RegisterToPeerAsync ( )
296297 {
297298 Logger . Trace ( $ "[{ nameof ( IpcClientService ) } ] StartRegisterToPeer PipeName={ IpcContext . PipeName } ") ;
298299
0 commit comments