aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-03-15 01:05:27 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-03-15 01:05:27 -0400
commit00d182088cecefc08ca80b1faee9bed3f215f40b (patch)
treed3f322d5b60bb2dee6c28011af92fca13d74a231
parentd513c10d9895c6693519ef1d459c6a5a76929436 (diff)
chore: #6 Use utils filewatcher instead of built-in
-rw-r--r--lib/Plugins.Runtime/src/AssemblyWatcher.cs91
-rw-r--r--lib/Plugins.Runtime/src/PluginStackBuilder.cs6
-rw-r--r--lib/Plugins.Runtime/src/RuntimePluginLoader.cs17
-rw-r--r--lib/Utils.Cryptography/monocypher/CMakeLists.txt1
-rw-r--r--lib/Utils.Cryptography/monocypher/argon2.c6
-rw-r--r--lib/Utils.Cryptography/monocypher/blake2b.h3
-rw-r--r--lib/Utils.Cryptography/monocypher/util.h71
-rw-r--r--lib/Utils.Cryptography/monocypher/vnlib_monocypher.h4
8 files changed, 75 insertions, 124 deletions
diff --git a/lib/Plugins.Runtime/src/AssemblyWatcher.cs b/lib/Plugins.Runtime/src/AssemblyWatcher.cs
index 09b49dc..69c53ed 100644
--- a/lib/Plugins.Runtime/src/AssemblyWatcher.cs
+++ b/lib/Plugins.Runtime/src/AssemblyWatcher.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Runtime
@@ -22,95 +22,52 @@
* along with VNLib.Plugins.Runtime. If not, see http://www.gnu.org/licenses/.
*/
+using System;
using System.IO;
using System.Threading;
-using System.Collections.Generic;
using VNLib.Utils;
+using VNLib.Utils.IO;
using VNLib.Utils.Extensions;
-namespace VNLib.Plugins.Runtime
-{
- internal sealed class AssemblyWatcher : IPluginAssemblyWatcher
- {
- private readonly object _lock = new ();
- private readonly Dictionary<IPluginReloadEventHandler, AsmFileWatcher> _watchers;
- public AssemblyWatcher()
- {
- _watchers = new();
- }
+namespace VNLib.Plugins.Runtime
+{
- ///<inheritdoc/>
- public void StopWatching(IPluginReloadEventHandler handler)
- {
- lock (_lock)
- {
- //Find old watcher by its handler, then dispose it
- if (_watchers.Remove(handler, out AsmFileWatcher? watcher))
- {
- //dispose the watcher
- watcher.Dispose();
- }
- }
- }
+ internal static class AssemblyWatcher
+ {
- ///<inheritdoc/>
- public void WatchAssembly(IPluginReloadEventHandler handler, IPluginAssemblyLoader loader)
+ internal static IDisposable WatchAssembly(IPluginReloadEventHandler handler, IPluginAssemblyLoader loader)
{
- lock(_lock)
- {
- if(_watchers.Remove(handler, out AsmFileWatcher? watcher))
- {
- //dispose the watcher
- watcher.Dispose();
- }
+ ArgumentNullException.ThrowIfNull(handler);
+ ArgumentNullException.ThrowIfNull(loader);
- //Queue up a new watcher
- watcher = new(loader, handler);
+ DebouncedFSEventHandler dbh = new(loader, handler);
+ FileWatcher.Subscribe(loader.Config.AssemblyFile, dbh);
- //Store watcher
- _watchers.Add(handler, watcher);
- }
+ return dbh;
}
- private sealed class AsmFileWatcher : VnDisposeable
+ internal sealed class DebouncedFSEventHandler : VnDisposeable, IFSChangeHandler
{
- public IPluginReloadEventHandler Handler { get; }
+ private readonly IPluginReloadEventHandler _handler;
private readonly IPluginAssemblyLoader _loaderSource;
private readonly Timer _delayTimer;
- private readonly FileSystemWatcher _watcher;
private bool _pause;
- public AsmFileWatcher(IPluginAssemblyLoader LoaderSource, IPluginReloadEventHandler handler)
+ public DebouncedFSEventHandler(IPluginAssemblyLoader loader, IPluginReloadEventHandler handler)
{
- Handler = handler;
- _loaderSource = LoaderSource;
-
- string dir = Path.GetDirectoryName(LoaderSource.Config.AssemblyFile)!;
-
- //Configure watcher to notify only when the assembly file changes
- _watcher = new FileSystemWatcher(dir)
- {
- Filter = "*.dll",
- EnableRaisingEvents = false,
- IncludeSubdirectories = true,
- NotifyFilter = NotifyFilters.LastWrite,
- };
-
- //Configure listener
- _watcher.Changed += OnFileChanged;
- _watcher.Created += OnFileChanged;
-
- _watcher.EnableRaisingEvents = true;
+ _handler = handler;
+ _loaderSource = loader;
//setup delay timer to wait on the config
- _delayTimer = new(OnTimeout, this, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
+ _delayTimer = new(OnTimeout, null, Timeout.InfiniteTimeSpan, Timeout.InfiniteTimeSpan);
}
- void OnFileChanged(object sender, FileSystemEventArgs e)
+ ///<inheritdoc/>
+ void IFSChangeHandler.OnFileChanged(FileSystemEventArgs e)
{
//if were already waiting to process an event, we dont need to stage another
if (_pause)
@@ -130,7 +87,7 @@ namespace VNLib.Plugins.Runtime
_delayTimer.Stop();
//Fire event, let exception crash app
- Handler.OnPluginUnloaded(_loaderSource);
+ _handler.OnPluginUnloaded(_loaderSource);
//Clear pause flag
_pause = false;
@@ -140,9 +97,7 @@ namespace VNLib.Plugins.Runtime
{
_delayTimer.Dispose();
- //Detach event handler and dispose watcher
- _watcher.Changed -= OnFileChanged;
- _watcher.Dispose();
+ FileWatcher.Unsubscribe(_loaderSource.Config.AssemblyFile, this);
}
}
}
diff --git a/lib/Plugins.Runtime/src/PluginStackBuilder.cs b/lib/Plugins.Runtime/src/PluginStackBuilder.cs
index eed08e2..ef3ffc9 100644
--- a/lib/Plugins.Runtime/src/PluginStackBuilder.cs
+++ b/lib/Plugins.Runtime/src/PluginStackBuilder.cs
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Runtime
@@ -83,8 +83,10 @@ namespace VNLib.Plugins.Runtime
/// <returns>The current builder instance for chaining</returns>
public PluginStackBuilder WithConfigurationReader(IPluginConfigReader pluginConfig)
{
+ ArgumentNullException.ThrowIfNull(pluginConfig);
+
//Store binary copy
- PluginConfig = pluginConfig ?? throw new ArgumentNullException(nameof(pluginConfig));
+ PluginConfig = pluginConfig;
return this;
}
diff --git a/lib/Plugins.Runtime/src/RuntimePluginLoader.cs b/lib/Plugins.Runtime/src/RuntimePluginLoader.cs
index 60edf55..23bbcab 100644
--- a/lib/Plugins.Runtime/src/RuntimePluginLoader.cs
+++ b/lib/Plugins.Runtime/src/RuntimePluginLoader.cs
@@ -38,10 +38,9 @@ namespace VNLib.Plugins.Runtime
/// </summary>
public sealed class RuntimePluginLoader : VnDisposeable, IPluginReloadEventHandler
{
- private static readonly IPluginAssemblyWatcher Watcher = new AssemblyWatcher();
-
- private readonly IPluginAssemblyLoader Loader;
+ private readonly IPluginAssemblyLoader Loader;
private readonly ILogProvider? Log;
+ private readonly IDisposable? Watcher;
/// <summary>
/// Gets the plugin assembly loader configuration information
@@ -61,13 +60,15 @@ namespace VNLib.Plugins.Runtime
/// <exception cref="ArgumentNullException"></exception>
public RuntimePluginLoader(IPluginAssemblyLoader loader, ILogProvider? log)
{
- Log = log;
- Loader = loader ?? throw new ArgumentNullException(nameof(loader));
+ ArgumentNullException.ThrowIfNull(loader);
+
+ Log = log;
+ Loader = loader;
//Configure watcher if requested
if (loader.Config.WatchForReload)
{
- Watcher.WatchAssembly(this, loader);
+ Watcher = AssemblyWatcher.WatchAssembly(this, loader);
}
//Init container
@@ -200,10 +201,8 @@ namespace VNLib.Plugins.Runtime
///<inheritdoc/>
protected override void Free()
{
- //Stop watching for events
- Watcher.StopWatching(this);
-
//Cleanup
+ Watcher?.Dispose();
Controller.Dispose();
Loader.Dispose();
}
diff --git a/lib/Utils.Cryptography/monocypher/CMakeLists.txt b/lib/Utils.Cryptography/monocypher/CMakeLists.txt
index 9b17ea7..8cec60b 100644
--- a/lib/Utils.Cryptography/monocypher/CMakeLists.txt
+++ b/lib/Utils.Cryptography/monocypher/CMakeLists.txt
@@ -39,6 +39,7 @@ message(STATUS "Build type is '${CMAKE_BUILD_TYPE}'")
#if debug
add_compile_definitions($<$<CONFIG:Debug>:DEBUG>)
+add_compile_definitions(VNLIB_EXPORTING)
#setup flags for windows compilation
diff --git a/lib/Utils.Cryptography/monocypher/argon2.c b/lib/Utils.Cryptography/monocypher/argon2.c
index 9b13ce0..2606e73 100644
--- a/lib/Utils.Cryptography/monocypher/argon2.c
+++ b/lib/Utils.Cryptography/monocypher/argon2.c
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* vnlib_monocypher is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
@@ -18,9 +18,11 @@
#include "argon2.h"
#include <monocypher.h>
+#define ARGON2_WORK_AREA_MULTIPLIER 1024
+
VNLIB_EXPORT uint32_t VNLIB_CC Argon2CalcWorkAreaSize(const argon2Ctx* context)
{
- return context->m_cost * 1024;
+ return context->m_cost * ARGON2_WORK_AREA_MULTIPLIER;
}
/*
diff --git a/lib/Utils.Cryptography/monocypher/blake2b.h b/lib/Utils.Cryptography/monocypher/blake2b.h
index 4167aca..0a0b4f8 100644
--- a/lib/Utils.Cryptography/monocypher/blake2b.h
+++ b/lib/Utils.Cryptography/monocypher/blake2b.h
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* vnlib_monocypher is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
@@ -17,6 +17,7 @@
#pragma once
#ifndef VN_MONOCYPHER_BLAKE2_H
+#define VN_MONOCYPHER_BLAKE2_H
#include <stdint.h>
#include "util.h"
diff --git a/lib/Utils.Cryptography/monocypher/util.h b/lib/Utils.Cryptography/monocypher/util.h
index 68b3e51..7a9f638 100644
--- a/lib/Utils.Cryptography/monocypher/util.h
+++ b/lib/Utils.Cryptography/monocypher/util.h
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* vnlib_monocypher is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
@@ -16,47 +16,38 @@
*/
#pragma once
-#ifndef VN_MONOCYPHER_UTIL_H
+#ifndef VN_MONOCYPHER_UTIL_H
+#define VN_MONOCYPHER_UTIL_H
-#if defined(__GNUC__)
- #define inline __inline__
- #define VNLIB_EXPORT __attribute__((visibility("default")))
- #define VNLIB_CC
-#elif defined(_MSC_VER)
- #define VNLIB_EXPORT __declspec(dllexport)
- #define VNLIB_CC __cdecl
-#endif /* WIN32 */
-
-#ifdef USE_MEM_UTIL
-
- /* Include stdlib for malloc */
- #include <stdlib.h>
-
- /* If a custom allocator is not defined, set macros for built-in function */
- #ifndef CUSTOM_ALLOCATOR
-
- /* malloc and friends fallback if not defined */
- #define vnmalloc(size) malloc(size)
- #define vncalloc(count, size) calloc(count, size)
- #define vnrealloc(ptr, size) realloc(ptr, size)
- #define vnfree(ptr) free(ptr)
-
- #endif /* !CUSTOM_ALLOCATOR */
-
- #ifdef WIN32
-
- /* required for memove on windows */
- #include <memory.h>
-
- #define _memmove(dst, src, size) memmove_s(dst, size, src, size)
- #else
- /* use string.h posix on non-win platforms */
- #include <string.h>
-
- #define _memmove memmove
- #endif /* WIN32 */
+#if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
+ #define _P_IS_WINDOWS
+#endif
-#endif // USE_MEM_UTIL
+//Set api export calling convention (allow used to override)
+#ifndef VNLIB_CC
+ #ifdef _P_IS_WINDOWS
+ //STD for importing to other languages such as .NET
+ #define VNLIB_CC __stdcall
+ #else
+ #define VNLIB_CC
+ #endif
+#endif // !NC_CC
+
+#ifndef VNLIB_EXPORT //Allow users to disable the export/impoty macro if using source code directly
+ #ifdef VNLIB_EXPORTING
+ #ifdef _P_IS_WINDOWS
+ #define VNLIB_EXPORT __declspec(dllexport)
+ #else
+ #define VNLIB_EXPORT __attribute__((visibility("default")))
+ #endif // _NC_IS_WINDOWS
+ #else
+ #ifdef _P_IS_WINDOWS
+ #define VNLIB_EXPORT __declspec(dllimport)
+ #else
+ #define VNLIB_EXPORT
+ #endif // _P_IS_WINDOWS
+ #endif // !VNLIB_EXPORTING
+#endif // !VNLIB_EXPORT
#ifndef _In_
#define _In_
diff --git a/lib/Utils.Cryptography/monocypher/vnlib_monocypher.h b/lib/Utils.Cryptography/monocypher/vnlib_monocypher.h
index 920def9..943431a 100644
--- a/lib/Utils.Cryptography/monocypher/vnlib_monocypher.h
+++ b/lib/Utils.Cryptography/monocypher/vnlib_monocypher.h
@@ -1,5 +1,5 @@
/*
-* Copyright (c) 2023 Vaughn Nugent
+* Copyright (c) 2024 Vaughn Nugent
*
* vnlib_monocypher is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
@@ -16,7 +16,7 @@
*/
#pragma once
-#ifndef VNLIB_MONOCYPHER_H
+#ifndef VNLIB_MONOCYPHER_H
#define VNLIB_MONOCYPHER_H
#include "util.h"