Skip to content

Commit

Permalink
Switch NetMQ poller to RunAsync (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
Spiritreader committed Oct 12, 2021
1 parent 8a7f1db commit 649e2fa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 61 deletions.
2 changes: 1 addition & 1 deletion AutoDarkModeSvc/AutoDarkModeSvc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<TargetFramework>net5.0-windows10.0.19041.0</TargetFramework>
<TargetPlatformMinVersion>10.0.18362.0</TargetPlatformMinVersion>
<UseWindowsForms>true</UseWindowsForms>
<Version>10.0.1.34</Version>
<Version>10.0.1.35</Version>
<AssemblyName>AutoDarkModeSvc</AssemblyName>
<ApplicationIcon>..\adm_tray_new.ico</ApplicationIcon>
<StartupObject>AutoDarkModeSvc.Program</StartupObject>
Expand Down
117 changes: 57 additions & 60 deletions AutoDarkModeSvc/Communication/ZeroMQServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,65 +65,8 @@ public void Start()
}

Poller = new NetMQPoller { Server };
Server.ReceiveReady += (s, a) =>
{
string msg = "";
try
{
msg = a.Socket.ReceiveFrameString();
Logger.Debug("received message: {0}", msg);
}
catch (Exception ex)
{
Logger.Error(ex, "error while receiving message:");
}

try
{

MessageParser.Parse(new List<string>() { msg }, (message) =>
{
bool sent = a.Socket.TrySendFrame(new TimeSpan(10000000), message);
if (!sent)
{
Logger.Error("could not send response: timeout");
}
}, Service);
}
catch (Exception ex)
{
Logger.Error(ex, $"exception while processing command {msg}");
try
{
bool sent = a.Socket.TrySendFrame(new TimeSpan(10000000), new ApiResponse()
{
StatusCode = StatusCode.Err,
Message = ex.Message
}.ToString());
if (!sent)
{
Logger.Error("could not send response: timeout");
}
}
catch (Exception exErr)
{
Logger.Error(exErr, "could not send error response:");
}

}
};
PollTask = Task.Run(() =>
{
try
{
Poller.Run();
}
catch (Exception ex)
{
Logger.Error(ex, "ZMQ Poller died");
Environment.Exit(-1);
}
});
Server.ReceiveReady += ReceiveEvent;
Poller.RunAsync();
Logger.Info("started server (polling)");
}

Expand All @@ -134,7 +77,6 @@ public void Stop()
{
Logger.Info("stopping server");
Poller.Stop();
PollTask.Wait();
try
{
Poller.Dispose();
Expand All @@ -147,5 +89,60 @@ public void Stop()
_portshare.Dispose();
NetMQConfig.Cleanup();
}

public void ReceiveEvent(object sender, NetMQSocketEventArgs a)
{
string msg = "";
try
{
bool recv = a.Socket.TryReceiveFrameString(new TimeSpan(10000000), out msg);
if (recv)
{
Logger.Debug("received message: {0}", msg);
}
else
{
Logger.Error("server receive ready called, but no message available:");
}
}
catch (Exception ex)
{
Logger.Error(ex, "error while receiving message:");
}

try
{

MessageParser.Parse(new List<string>() { msg }, (message) =>
{
bool sent = a.Socket.TrySendFrame(new TimeSpan(10000000), message);
if (!sent)
{
Logger.Error("could not send response: timeout");
}
}, Service);
}
catch (Exception ex)
{
Logger.Error(ex, $"exception while processing command {msg}");
try
{
bool sent = a.Socket.TrySendFrame(new TimeSpan(10000000), new ApiResponse()
{
StatusCode = StatusCode.Err,
Message = ex.Message
}.ToString());
if (!sent)
{
Logger.Error("could not send response: timeout");
}
}
catch (Exception exErr)
{
Logger.Error(exErr, "could not send error response:");
}

}
}
}
}

0 comments on commit 649e2fa

Please sign in to comment.