Fix NetMQ not stopping.
This commit is contained in:
@@ -12,15 +12,14 @@ namespace Backend.Handler;
|
||||
|
||||
public class Communication
|
||||
{
|
||||
private readonly NetMQPoller _poller;
|
||||
private readonly DbHandler _dbHandler;
|
||||
private readonly ThreadHandler _threadHandler;
|
||||
private bool _isRunning = true;
|
||||
|
||||
public Communication(DbHandler dbHandler, ThreadHandler threadHandler)
|
||||
{
|
||||
_dbHandler = dbHandler;
|
||||
_threadHandler = threadHandler;
|
||||
_poller = new();
|
||||
}
|
||||
|
||||
public WaitHandle[] Start()
|
||||
@@ -35,7 +34,7 @@ public class Communication
|
||||
return waitHandles;
|
||||
}
|
||||
|
||||
private void Server(object obj)
|
||||
/*private void Server(object obj)
|
||||
{
|
||||
using ResponseSocket server = new();
|
||||
server.Bind("tcp://*:5556");
|
||||
@@ -50,17 +49,31 @@ public class Communication
|
||||
|
||||
Console.WriteLine("Communication stopped.");
|
||||
|
||||
((EventWaitHandle) obj).Set();
|
||||
}*/
|
||||
|
||||
private void Server(object obj)
|
||||
{
|
||||
using ResponseSocket rep = new();
|
||||
|
||||
rep.Bind("tcp://127.0.0.1:5556");
|
||||
|
||||
while (_isRunning)
|
||||
{
|
||||
byte[] message = rep.ReceiveFrameBytes();
|
||||
|
||||
CommunicationObject communicationObject = MessagePackSerializer.Deserialize<CommunicationObject>(message);
|
||||
|
||||
OnServerOnReceiveReady(communicationObject, rep);
|
||||
}
|
||||
|
||||
((EventWaitHandle) obj).Set();
|
||||
}
|
||||
|
||||
[RequiresDynamicCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
|
||||
[RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")]
|
||||
private void OnServerOnReceiveReady(object? _, NetMQSocketEventArgs e)
|
||||
private void OnServerOnReceiveReady(CommunicationObject communicationObject, ResponseSocket rep)
|
||||
{
|
||||
byte[] message = e.Socket.ReceiveFrameBytes();
|
||||
|
||||
CommunicationObject communicationObject = MessagePackSerializer.Deserialize<CommunicationObject>(message);
|
||||
|
||||
switch (communicationObject.Command)
|
||||
{
|
||||
case CommunicationCommand.GetScanningProgress:
|
||||
@@ -68,10 +81,10 @@ public class Communication
|
||||
DatabaseSizes databaseSizes = FilesystemHelper.GetDatabaseSizes();
|
||||
|
||||
long discardedIndexes = _dbHandler.GetDiscardedIndexes();
|
||||
|
||||
|
||||
ScanningStatus status = new();
|
||||
// 4294967296 is all Ipv4 addresses.
|
||||
|
||||
|
||||
if (discardedIndexes != 0)
|
||||
{
|
||||
status.PercentageOfIpv4Scanned = (float)discardedIndexes / 4294967296 * 100;
|
||||
@@ -80,67 +93,64 @@ public class Communication
|
||||
{
|
||||
status.PercentageOfIpv4Scanned = 0.0000000001f;
|
||||
}
|
||||
|
||||
|
||||
status.AmountOfIpv4Left = 4294967296 - discardedIndexes;
|
||||
status.TotalFiltered = DbHandler.GetFilteredIndexes();
|
||||
status.TotalDiscarded = discardedIndexes;
|
||||
status.MyDbSize = databaseSizes.MyDbSize;
|
||||
status.FilteredDbSize = databaseSizes.FilteredDbSize;
|
||||
status.DiscardedDbSize = databaseSizes.DiscardedDbSize;
|
||||
|
||||
|
||||
|
||||
byte[] serializedResult = MessagePackSerializer.Serialize(status, MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray));
|
||||
|
||||
e.Socket.SendFrame(serializedResult);
|
||||
|
||||
rep.SendFrame(serializedResult);
|
||||
|
||||
break;
|
||||
}
|
||||
case CommunicationCommand.StopScanning:
|
||||
SendStringResponse(e, "Server is stopping.");
|
||||
|
||||
_threadHandler.Stop();
|
||||
break;
|
||||
|
||||
case CommunicationCommand.GarbageCollect:
|
||||
ThreadHandler.ManualGc();
|
||||
|
||||
SendStringResponse(e, "Server has garbage collected.");
|
||||
|
||||
_threadHandler.Stop();
|
||||
break;
|
||||
|
||||
case CommunicationCommand.DbReindex:
|
||||
{
|
||||
_dbHandler.ReIndex();
|
||||
|
||||
SendStringResponse(e, "All Dbs have been reindexed.");
|
||||
SendStringResponse(rep, "All Dbs have been reindexed.");
|
||||
break;
|
||||
}
|
||||
|
||||
case CommunicationCommand.DbVacuum:
|
||||
{
|
||||
_dbHandler.Vacuum();
|
||||
|
||||
SendStringResponse(e, "All Dbs have been vacuumed.");
|
||||
SendStringResponse(rep, "All Dbs have been vacuumed.");
|
||||
break;
|
||||
}
|
||||
|
||||
case CommunicationCommand.GetSearches:
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(communicationObject.SearchTerm))
|
||||
{
|
||||
SendSearchResponse(e, communicationObject.SearchTerm);
|
||||
SendSearchResponse(rep, communicationObject.SearchTerm);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case CommunicationCommand.StopScanning:
|
||||
{
|
||||
_isRunning = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void SendStringResponse(NetMQSocketEventArgs e, string response)
|
||||
private static void SendStringResponse(ResponseSocket rep, string response)
|
||||
{
|
||||
MessagePackSerializerOptions withCompression = MessagePackSerializerOptions.Standard.WithCompression(MessagePackCompression.Lz4BlockArray);
|
||||
byte[] serializedResult = MessagePackSerializer.Serialize(response, withCompression);
|
||||
|
||||
e.Socket.SendFrame(serializedResult);
|
||||
rep.SendFrame(serializedResult);
|
||||
}
|
||||
|
||||
private static void SendSearchResponse(NetMQSocketEventArgs e, string searchTerm)
|
||||
private static void SendSearchResponse(ResponseSocket rep, string searchTerm)
|
||||
{
|
||||
//SearchResults result = SearchHelper.Search(communicationObject.SearchTerm!, _dbHandler);
|
||||
SearchResults result = new()
|
||||
@@ -157,11 +167,6 @@ public class Communication
|
||||
|
||||
string serializedResult = JsonSerializer.Serialize(result);
|
||||
|
||||
e.Socket.SendFrame(serializedResult);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_poller.Stop();
|
||||
rep.SendFrame(serializedResult);
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,8 @@ public class ContentFilter
|
||||
{
|
||||
private readonly ConcurrentQueue<QueueItem> _queue;
|
||||
private readonly DbHandler _dbHandler;
|
||||
private const string GetDomainPort80 = "/home/skingging/Documents/Projects/CSharp/RSE/Backend/GetDomainNamePort80.sh";
|
||||
private const string GetDomainPort443 = "/home/skingging/Documents/Projects/CSharp/RSE/Backend/GetDomainNamePort443.sh";
|
||||
private const string GetDomainPort80 = "/home/skingging/Documents/Projects/CSharp/RSE/Backend/Scripts/GetDomainNamePort80.sh";
|
||||
private const string GetDomainPort443 = "/home/skingging/Documents/Projects/CSharp/RSE/Backend/Scripts/GetDomainNamePort443.sh";
|
||||
private bool _stop;
|
||||
|
||||
public ContentFilter(ConcurrentQueue<QueueItem> queue, DbHandler dbHandler)
|
||||
@@ -54,6 +54,11 @@ public class ContentFilter
|
||||
|
||||
_queue.Enqueue(superUnfilteredObject);
|
||||
|
||||
if (_dbHandler.GetFilteredIp(unfiltered.Ip))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Filtered filtered = GetSiteData(unfiltered.Ip);
|
||||
|
||||
filtered.Port1 = unfiltered.Port1;
|
||||
@@ -230,7 +235,7 @@ public class ContentFilter
|
||||
proc.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
_stop = true;
|
||||
|
||||
@@ -29,20 +29,20 @@ public class ThreadHandler
|
||||
|
||||
public void Start()
|
||||
{
|
||||
//Thread scanner = new(StartScanner);
|
||||
//Thread indexer = new(StartIndexer);
|
||||
Thread scanner = new(StartScanner);
|
||||
Thread indexer = new(StartIndexer);
|
||||
Thread database = new(StartDbHandler);
|
||||
Thread discarded = new(StartDiscardedDbHandler);
|
||||
Thread communication = new(StartCommunicationHandler);
|
||||
|
||||
//scanner.Start();
|
||||
//indexer.Start();
|
||||
scanner.Start();
|
||||
indexer.Start();
|
||||
database.Start();
|
||||
discarded.Start();
|
||||
communication.Start();
|
||||
|
||||
//scanner.Join();
|
||||
//indexer.Join();
|
||||
scanner.Join();
|
||||
indexer.Join();
|
||||
database.Join();
|
||||
discarded.Join();
|
||||
communication.Join();
|
||||
@@ -107,21 +107,15 @@ public class ThreadHandler
|
||||
Console.WriteLine("Communicator finished");
|
||||
|
||||
_communicationStopped = true;
|
||||
|
||||
Stop();
|
||||
}
|
||||
|
||||
private void StopCommunicator()
|
||||
{
|
||||
Thread t = new(_communication.Stop);
|
||||
t.Start();
|
||||
t.Join();
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
|
||||
private void Stop()
|
||||
{
|
||||
_stopSignal = true;
|
||||
_ipScanner.Stop();
|
||||
_contentFilter.Stop();
|
||||
StopCommunicator();
|
||||
|
||||
bool stopping = true;
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Backend")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Backend")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Backend")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@@ -1 +1 @@
|
||||
9371234864fbc6630f90d88b86a9165d6bf6b07556d735c0a3bcf7b4cfc5fd84
|
||||
5e46353062a73d293516ee43511f79696b25ea1a0227ab06137d80abd08b8286
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -13,7 +13,7 @@ using System.Reflection;
|
||||
[assembly: System.Reflection.AssemblyCompanyAttribute("Backend")]
|
||||
[assembly: System.Reflection.AssemblyConfigurationAttribute("Release")]
|
||||
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+981960411028790aaa0b551986f102c57a5995a2")]
|
||||
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+f3c6338a6ae57d4e839090fca125bc38f764d016")]
|
||||
[assembly: System.Reflection.AssemblyProductAttribute("Backend")]
|
||||
[assembly: System.Reflection.AssemblyTitleAttribute("Backend")]
|
||||
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
|
||||
|
||||
@@ -1 +1 @@
|
||||
82adc2dfb6f85265418a27693e5a67940d85f327127a5bc478825c0f31a766c5
|
||||
ffe56baf943e027152af8f421cec2146e64a2c69fab0778620c627d36d497988
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user