Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ publish/
# Note: Comment the next line if you want to checkin your web deploy settings,
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
!Snaffler/Properties/PublishProfiles/*.pubxml
*.publishproj

# Microsoft Azure Web App publish settings. Comment the next line if you want to
Expand Down
16 changes: 15 additions & 1 deletion SnaffCore/Classifiers/ClassifierRule.cs
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@

using System.Collections.Generic;
using System.Text.RegularExpressions;
using CsToml;

namespace SnaffCore.Classifiers
{
public class ClassifierRule
[TomlSerializedObject]
public partial class ClassifierRule
{
// define in what phase this rule is applied
[TomlValueOnSerialized]
public EnumerationScope EnumerationScope { get; set; } = EnumerationScope.FileEnumeration;

// define a way to chain rules together
[TomlValueOnSerialized]
public string RuleName { get; set; } = "Default";
[TomlValueOnSerialized]
public MatchAction MatchAction { get; set; } = MatchAction.Snaffle;
[TomlValueOnSerialized]
public List<string> RelayTargets { get; set; } = null;

[TomlValueOnSerialized]
public string Description { get; set; } = "A description of what a rule does.";

// define the behaviour of this rule
[TomlValueOnSerialized]
public MatchLoc MatchLocation { get; set; } = MatchLoc.FileName;
[TomlValueOnSerialized]
public MatchListType WordListType { get; set; } = MatchListType.Contains;
[TomlValueOnSerialized]
public int MatchLength { get; set; } = 0;
[TomlValueOnSerialized]
public string MatchMD5 { get; set; }
[TomlValueOnSerialized]
public List<string> WordList { get; set; } = new List<string>();
[TomlValueOnSerialized]
public List<Regex> Regexes { get; set; }

// define the severity of any matches
[TomlValueOnSerialized]
public Triage Triage { get; set; } = Triage.Green;
}

Expand Down
58 changes: 45 additions & 13 deletions SnaffCore/Concurrency/BlockingMq.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
using SnaffCore.Classifiers;
using System;
using System.Collections.Concurrent;
#if NETFRAMEWORK
#else
using System.Threading.Channels;
#endif

namespace SnaffCore.Concurrency
{
Expand All @@ -19,29 +23,52 @@ public static BlockingMq GetMq()
}

// Message Queue
public BlockingCollection<SnafflerMessage> Q { get; private set; }
#if NETFRAMEWORK
private BlockingCollection<SnafflerMessage> _collection;
public BlockingCollection<SnafflerMessage> Collection => _collection;
#else
private Channel<SnafflerMessage> _channel;
public ChannelReader<SnafflerMessage> Reader => _channel.Reader;
#endif

private BlockingMq()
{
Q = new BlockingCollection<SnafflerMessage>();
#if NETFRAMEWORK
_collection = new BlockingCollection<SnafflerMessage>();
#else
_channel = Channel.CreateUnbounded<SnafflerMessage>();
#endif
}

private void Enqueue(SnafflerMessage message)
{
#if NETFRAMEWORK
_collection.Add(message);
#else
_channel.Writer.TryWrite(message);
#endif
}

public void Terminate()
{
// say we did a thing
Q.Add(new SnafflerMessage
Enqueue(new SnafflerMessage
{
DateTime = DateTime.Now,
Type = SnafflerMessageType.Fatal,
Message = "Terminate was called"
});
//this.Q.CompleteAdding();
#if NETFRAMEWORK
_collection.CompleteAdding();
#else
_channel.Writer.TryComplete();
#endif
}

public void Trace(string message)
{
// say we did a thing
Q.Add(new SnafflerMessage
Enqueue(new SnafflerMessage
{
DateTime = DateTime.Now,
Type = SnafflerMessageType.Trace,
Expand All @@ -52,7 +79,7 @@ public void Trace(string message)
public void Degub(string message)
{
// say we did a thing
Q.Add(new SnafflerMessage
Enqueue(new SnafflerMessage
{
DateTime = DateTime.Now,
Type = SnafflerMessageType.Degub,
Expand All @@ -63,7 +90,7 @@ public void Degub(string message)
public void Info(string message)
{
// say we did a thing
Q.Add(new SnafflerMessage
Enqueue(new SnafflerMessage
{
DateTime = DateTime.Now,
Type = SnafflerMessageType.Info,
Expand All @@ -73,7 +100,7 @@ public void Info(string message)

public void Error(string message)
{
Q.Add(new SnafflerMessage
Enqueue(new SnafflerMessage
{
DateTime = DateTime.Now,
Type = SnafflerMessageType.Error,
Expand All @@ -84,7 +111,7 @@ public void Error(string message)
public void FileResult(FileResult fileResult)
{
// say we did a thing
Q.Add(new SnafflerMessage
Enqueue(new SnafflerMessage
{
DateTime = DateTime.Now,
Type = SnafflerMessageType.FileResult,
Expand All @@ -94,7 +121,7 @@ public void FileResult(FileResult fileResult)
public void DirResult(DirResult dirResult)
{
// say we did a thing
Q.Add(new SnafflerMessage
Enqueue(new SnafflerMessage
{
DateTime = DateTime.Now,
DirResult = dirResult,
Expand All @@ -104,7 +131,7 @@ public void DirResult(DirResult dirResult)
public void ShareResult(ShareResult shareResult)
{
// say we did a thing
Q.Add(new SnafflerMessage
Enqueue(new SnafflerMessage
{
DateTime = DateTime.Now,
ShareResult = shareResult,
Expand All @@ -114,11 +141,16 @@ public void ShareResult(ShareResult shareResult)

public void Finish()
{
Q.Add(new SnafflerMessage()
Enqueue(new SnafflerMessage()
{
DateTime = DateTime.Now,
Type = SnafflerMessageType.Finish
});
#if NETFRAMEWORK
_collection.CompleteAdding();
#else
_channel.Writer.TryComplete();
#endif
}
}
}
}
20 changes: 20 additions & 0 deletions SnaffCore/Concurrency/BlockingTaskScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ public void New(Action action)
}
}
}

public void New(Func<Task> asyncAction)
{
bool proceed = false;

while (proceed == false)
{
lock (syncLock)
{
if (_maxBacklog != 0)
{
if (Scheduler.GetTaskCounters().CurrentTasksQueued >= _maxBacklog)
continue;
}

proceed = true;
_taskFactory.StartNew(asyncAction, _cancellationSource.Token).Unwrap();
}
}
}
}

public class TaskCounters
Expand Down
Loading