Skip to content

Commit e9b80c9

Browse files
authored
Merge pull request #484 from nblair2/rdp-wait
RDP Wait exponential back off
2 parents b8e2b9e + 3ca35c8 commit e9b80c9

File tree

1 file changed

+11
-10
lines changed
  • src/Ghosts.Client/Handlers

1 file changed

+11
-10
lines changed

Diff for: src/Ghosts.Client/Handlers/Rdp.cs

+11-10
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,15 @@ public void RdpEx(TimelineHandler handler, TimelineEvent timelineEvent, string c
242242
process.StandardInput.Close(); // line added to stop process from hanging on ReadToEnd()
243243
Thread.Sleep(2000); //give time for response
244244
checkPasswordPrompt(au, password, username, usePasswordOnly);
245-
//wait 3 minutes for connection window
246-
if (!findRdpWindow(target, 3))
245+
//wait 15 seconds for connection window
246+
if (!findRdpWindow(target, 15))
247247
{
248248
//may have a certificate problem
249+
Log.Trace("RDP:: Unable to find a RDP window, attempting to accept an untrusted certificate");
249250
checkGenericPrompt(au, "{TAB}{TAB}{TAB}{ENTER}"); //this is for a certificate prompt
250251
}
251-
//try again for another 3 minutes to find
252-
if (findRdpWindow(target, 3))
252+
//try again for another 4.25 minutes to find
253+
if (findRdpWindow(target, 255))
253254
{
254255
doMouseLoop(caption, target, au, timelineEvent);
255256
}
@@ -278,18 +279,18 @@ public void RdpEx(TimelineHandler handler, TimelineEvent timelineEvent, string c
278279
}
279280
}
280281

281-
public bool findRdpWindow(string target,int minutes)
282+
public bool findRdpWindow(string target,int timeout)
282283
{
283284
var caption = $"{target} - Remote Desktop Connection";
284285
var winHandle = Winuser.FindWindow("TscShellContainerClass", caption);
285-
int count = 0;
286+
int wait = 1; //time to wait for this loop (exponential back off: 2^n starting with n=0)
286287
//wait for window to appear
287288
while (winHandle == IntPtr.Zero)
288289
{
289-
Log.Trace($"RDP:: Unable to find desktop window for {target}, sleeping 1 minute");
290-
Thread.Sleep(60 * 1000); //sleep 1 minute, then try again
291-
count++;
292-
if (count >= minutes) break;
290+
Log.Trace($"RDP:: Unable to find desktop window for {target}, sleeping {wait} seconds");
291+
Thread.Sleep(wait * 1000); //sleep for exponential back off amount of seconds
292+
if (2 * wait - 1 >= timeout) break; // sum(2^n) from n=0 -> n=x is 2 * 2^x - 1
293+
wait = 2 * wait; //double the time to wait next loop (2^n)
293294
winHandle = Winuser.FindWindow("TscShellContainerClass", caption);
294295
}
295296
return winHandle != IntPtr.Zero;

0 commit comments

Comments
 (0)