Skip to content

Commit

Permalink
Improve exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gerrit-amagno committed Sep 17, 2024
1 parent 9329773 commit cb22cf5
Showing 1 changed file with 37 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,41 +92,56 @@ private void StartFileWatcher([NotNull] string dir)

private void HandleClient([NotNull]IAsyncResult ar)
{
const string printer = Defaults.PrinterName;
IJob job;

var socket = (TcpListener) ar.AsyncState;
using (var client = socket.EndAcceptTcpClient(ar))
try
{
var local = client.Client.LocalEndPoint;
var remote = client.Client.RemoteEndPoint;
const string printer = Defaults.PrinterName;
IJob job;

LogDebug($"{remote} --> {local}");
job = _jobFactory.Create(printer, client.GetStream());
}
var socket = (TcpListener) ar.AsyncState;
using (var client = socket.EndAcceptTcpClient(ar))
{
var local = client.Client.LocalEndPoint;
var remote = client.Client.RemoteEndPoint;

LogDebug($"{remote} --> {local}");
job = _jobFactory.Create(printer, client.GetStream());
}

socket.BeginAcceptTcpClient(HandleClient, ar.AsyncState);
socket.BeginAcceptTcpClient(HandleClient, ar.AsyncState);

if (job == null)
{
LogError("Job could not be created. Check your Printer Settings.");
if (job == null)
{
LogError("Job could not be created. Check your Printer Settings.");
}
else
{
LogDebug($"Temporarily printed '{job.RawDataPath}'!");
_jobService.Start(job);
RestartFileWatcherIfNeeded(job.SessionInfo.Sid);
}
}
else
catch (Exception e)
{
LogDebug($"Temporarily printed '{job.RawDataPath}'!");
_jobService.Start(job);
RestartFileWatcherIfNeeded(job.SessionInfo.Sid);
LogError($"Exception thrown in {nameof(HandleClient)}", e);
throw;
}
}

private void RestartFileWatcherIfNeeded(string sid)
{
var config = GetUserRegistryConfig(sid);
_outputDir = _directoryHelper.GetOutputDirectory(config);
try
{
var config = GetUserRegistryConfig(sid);
_outputDir = _directoryHelper.GetOutputDirectory(config);

if (_watcher == null || _watcher.Path != _outputDir)
if (_watcher == null || _watcher.Path != _outputDir)
{
StartFileWatcher(_outputDir);
}
}
catch (Exception e)
{
StartFileWatcher(_outputDir);
LogError($"Thrown exception in {nameof(RestartFileWatcherIfNeeded)}", e);
}
}

Expand Down

0 comments on commit cb22cf5

Please sign in to comment.