diff options
author | vnugent <public@vaughnnugent.com> | 2024-02-14 14:10:27 -0500 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-02-14 14:10:27 -0500 |
commit | 2b1314c1475e7e1831c691cf349cb89c66fa320c (patch) | |
tree | 091fc132a2bee2e79a68d8c6d5eb20f1d989a3d2 /lib/Net.Messaging.FBM/src | |
parent | f4e4db7c5320976406feb252ae8f8bdbe9b3e351 (diff) |
Squashed commit of the following:
commit ddd8a651b6eb43cfdd49d84056f8b9c34b543992
Author: vnugent <public@vaughnnugent.com>
Date: Wed Feb 14 00:15:50 2024 -0500
ci: reduce output noise and update Argon2 build
commit cf942959ff2feea03d3eda2ff2a263bdac4d6bc6
Author: vnugent <public@vaughnnugent.com>
Date: Mon Feb 12 18:39:18 2024 -0500
chore: update packages and minor fixes
commit ab506af9e2de2876b11bb45b3c7e787616c80155
Author: vnugent <public@vaughnnugent.com>
Date: Fri Feb 9 21:27:24 2024 -0500
fix: patch and update core runtime service injection
commit 7ed5e8b19164c28d3a238bd56878d2161fbea2e4
Author: vnugent <public@vaughnnugent.com>
Date: Thu Feb 8 18:26:11 2024 -0500
fork dotnetplugins and make some intial updates/upgrades
commit f4cab88d67be5da0953b14bd46fc972d4acc8606
Author: vnugent <public@vaughnnugent.com>
Date: Thu Feb 8 12:16:13 2024 -0500
update some heap api functions
commit 6035bf7ed8412f1da361cc5feddd860abfaf4fc1
Author: vnugent <public@vaughnnugent.com>
Date: Wed Feb 7 22:09:11 2024 -0500
working file-watcher notifications/rework
commit 698f8edf694ad9700ee2ce2220e692b496448ff9
Author: vnugent <public@vaughnnugent.com>
Date: Wed Feb 7 20:37:28 2024 -0500
remove mem-template and add file-watcher utility
commit b17591e0fb363222fcd7d93c2bad4ab1b102385f
Author: vnugent <public@vaughnnugent.com>
Date: Wed Feb 7 18:28:21 2024 -0500
add small memmove support for known small blocks
commit 631be4d4b27fdbcd4b0526e17a128bb0d86911eb
Author: vnugent <public@vaughnnugent.com>
Date: Wed Feb 7 18:08:02 2024 -0500
setup some readonly ref arguments and convert copy apis to readonly refs
commit 2ba8dec68d5cb192e61ad0141d4b460076d3f90a
Author: vnugent <public@vaughnnugent.com>
Date: Mon Feb 5 18:30:38 2024 -0500
restructure internal memmove strategies
commit 25cf02872da980893ad7fb51d4eccc932380582b
Author: vnugent <public@vaughnnugent.com>
Date: Sun Feb 4 01:29:18 2024 -0500
add http stream interface, profiling -> file read updates
commit 757668c44e78864dc69d5713a2cfba6db2ed9a2a
Author: vnugent <public@vaughnnugent.com>
Date: Fri Feb 2 14:27:04 2024 -0500
streamline data-copy api with proper large block support and net8 feature updates
commit f22c1765fd72ab40a10d8ec92a8cb6d9ec1b1a04
Author: vnugent <public@vaughnnugent.com>
Date: Mon Jan 29 16:16:23 2024 -0500
check for compression lib updates to close #2 and fix some ci build stuff
commit f974bfdef6a795b4a1c04602502ef506ef2587a9
Author: vnugent <public@vaughnnugent.com>
Date: Tue Jan 23 17:36:17 2024 -0500
switch allocator libs to lgpl2.1
commit 1fe5e01b329cd27b675000f1a557b784d3c88b56
Author: vnugent <public@vaughnnugent.com>
Date: Tue Jan 23 17:05:59 2024 -0500
consolidate allocator packages and close #1
commit 74e1107e522f00b670526193396217f40a6bade7
Author: vnugent <public@vaughnnugent.com>
Date: Tue Jan 23 15:43:40 2024 -0500
cache extension api tweaks
commit 96ca2b0388a6326b9bb74f3ab2f62eaede6681e0
Author: vnugent <public@vaughnnugent.com>
Date: Mon Jan 22 17:54:23 2024 -0500
explicit tcp server args reuse
Diffstat (limited to 'lib/Net.Messaging.FBM/src')
-rw-r--r-- | lib/Net.Messaging.FBM/src/Client/FBMBuffer.cs | 21 | ||||
-rw-r--r-- | lib/Net.Messaging.FBM/src/Client/FBMClient.cs | 18 | ||||
-rw-r--r-- | lib/Net.Messaging.FBM/src/Client/FBMRequest.cs | 26 | ||||
-rw-r--r-- | lib/Net.Messaging.FBM/src/Client/FBMResponse.cs | 4 | ||||
-rw-r--r-- | lib/Net.Messaging.FBM/src/FBMMessageHeader.cs | 13 | ||||
-rw-r--r-- | lib/Net.Messaging.FBM/src/Server/FBMListener.cs | 47 |
6 files changed, 64 insertions, 65 deletions
diff --git a/lib/Net.Messaging.FBM/src/Client/FBMBuffer.cs b/lib/Net.Messaging.FBM/src/Client/FBMBuffer.cs index 698a98b..0b5d9c1 100644 --- a/lib/Net.Messaging.FBM/src/Client/FBMBuffer.cs +++ b/lib/Net.Messaging.FBM/src/Client/FBMBuffer.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Messaging.FBM @@ -123,17 +123,8 @@ namespace VNLib.Net.Messaging.FBM.Client Span<char> IFBMHeaderBuffer.GetSpan() => MemoryMarshal.Cast<byte, char>(Handle.GetSpan()); - private sealed class BinaryRequestAccumulator : IDataAccumulator<byte> + private sealed class BinaryRequestAccumulator(FBMBuffer Buffer, int Size) : IDataAccumulator<byte> { - private readonly int Size; - private readonly FBMBuffer Buffer; - - internal BinaryRequestAccumulator(FBMBuffer buffer, int size) - { - Buffer = buffer; - Size = size; - } - ///<inheritdoc/> public int AccumulatedSize { get; private set; } @@ -168,12 +159,8 @@ namespace VNLib.Net.Messaging.FBM.Client * a finite amount of data to be written to the accumulator since * it uses a fixed size internal buffer. */ - private sealed class BufferWriter : IBufferWriter<byte> - { - private readonly FBMBuffer Buffer; - - public BufferWriter(FBMBuffer buffer) => Buffer = buffer; - + private sealed class BufferWriter(FBMBuffer Buffer) : IBufferWriter<byte> + { ///<inheritdoc/> public void Advance(int count) => Buffer._requestAccumulator.Advance(count); diff --git a/lib/Net.Messaging.FBM/src/Client/FBMClient.cs b/lib/Net.Messaging.FBM/src/Client/FBMClient.cs index 9628313..d3b6b55 100644 --- a/lib/Net.Messaging.FBM/src/Client/FBMClient.cs +++ b/lib/Net.Messaging.FBM/src/Client/FBMClient.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Messaging.FBM @@ -108,15 +108,15 @@ namespace VNLib.Net.Messaging.FBM.Client /// <param name="websocket">The websocket instance used to comunicate with an FBMServer</param> public FBMClient(in FBMClientConfig config, IFbmClientWebsocket websocket) :this(in config, websocket, null) - { - } + { } internal FBMClient(in FBMClientConfig config, IFbmClientWebsocket websocket, IObjectRental<FBMRequest>? requestRental) { + ArgumentNullException.ThrowIfNull(websocket); + ArgumentNullException.ThrowIfNull(config.MemoryManager, nameof(config.MemoryManager)); + _config = config; - _socket = websocket ?? throw new ArgumentNullException(nameof(websocket)); - - _ = config.MemoryManager ?? throw new ArgumentException("FBM memory manager is required", nameof(config)); + _socket = websocket; //Create new request rental if none supplied if(requestRental is null) @@ -218,7 +218,8 @@ namespace VNLib.Net.Messaging.FBM.Client /// <exception cref="ObjectDisposedException"></exception> /// <exception cref="InvalidOperationException"></exception> /// <exception cref="FBMInvalidRequestException"></exception> - public Task<FBMResponse> SendAsync(FBMRequest request, CancellationToken cancellationToken = default) => SendAsync(request, Config.RequestTimeout, cancellationToken); + public Task<FBMResponse> SendAsync(FBMRequest request, CancellationToken cancellationToken = default) + => SendAsync(request, Config.RequestTimeout, cancellationToken); /// <summary> /// Sends a <see cref="FBMRequest"/> to the connected server @@ -311,7 +312,8 @@ namespace VNLib.Net.Messaging.FBM.Client /// <exception cref="InvalidOperationException"></exception> public async Task<FBMResponse> StreamDataAsync(FBMRequest request, Stream payload, ContentType contentType, TimeSpan timeout, CancellationToken cancellationToken = default) { - _ = payload ?? throw new ArgumentNullException(nameof(payload)); + ArgumentNullException.ThrowIfNull(request); + ArgumentNullException.ThrowIfNull(payload); Check(); diff --git a/lib/Net.Messaging.FBM/src/Client/FBMRequest.cs b/lib/Net.Messaging.FBM/src/Client/FBMRequest.cs index 8432d27..1787941 100644 --- a/lib/Net.Messaging.FBM/src/Client/FBMRequest.cs +++ b/lib/Net.Messaging.FBM/src/Client/FBMRequest.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Messaging.FBM @@ -97,7 +97,7 @@ namespace VNLib.Net.Messaging.FBM.Client /// <param name="messageId">The custom message id</param> /// <param name="config">The fbm client config storing required config variables</param> public FBMRequest(int messageId, in FBMClientConfig config) - :this(messageId, config.MemoryManager, config.MessageBufferSize, config.HeaderEncoding) + :this(messageId, config.MemoryManager, config.MessageBufferSize, config.HeaderEncoding) { } /// <summary> @@ -110,8 +110,10 @@ namespace VNLib.Net.Messaging.FBM.Client public FBMRequest(int messageId, IFBMMemoryManager manager, int bufferSize, Encoding headerEncoding) { MessageId = messageId; - HeaderEncoding = headerEncoding ?? throw new ArgumentNullException(nameof(headerEncoding)); - _ = manager ?? throw new ArgumentNullException(nameof(manager)); + ArgumentNullException.ThrowIfNull(manager); + ArgumentNullException.ThrowIfNull(headerEncoding); + + HeaderEncoding = headerEncoding; //Configure waiter Waiter = new FBMMessageWaiter(this); @@ -283,15 +285,11 @@ namespace VNLib.Net.Messaging.FBM.Client _request = request; //Configure timer - _timer = new(OnCancelled, this, Timeout.Infinite, Timeout.Infinite); + _timer = new(OnTimeout, this, Timeout.Infinite, Timeout.Infinite); } ///<inheritdoc/> - public void OnBeginRequest() - { - //Configure new tcs - _tcs = new(TaskCreationOptions.None); - } + public void OnBeginRequest() => _tcs = new(TaskCreationOptions.None); ///<inheritdoc/> public void OnEndRequest() @@ -360,7 +358,7 @@ namespace VNLib.Net.Messaging.FBM.Client if (cancellation.CanBeCanceled) { //Register cancellation - _token = cancellation.Register(OnCancelled, this); + _token = cancellation.Register(OnCancelled, this, false); } } @@ -373,6 +371,12 @@ namespace VNLib.Net.Messaging.FBM.Client //Set cancelled state if exists, the task may have already completed private void OnCancelled(object? state) => _tcs?.TrySetCanceled(); + private void OnTimeout(object? state) + { + TimeoutException to = new("A response was not received in the desired timeout period. Operation aborted"); + _tcs?.TrySetException(to); + } + ///<inheritdoc/> public void Dispose() { diff --git a/lib/Net.Messaging.FBM/src/Client/FBMResponse.cs b/lib/Net.Messaging.FBM/src/Client/FBMResponse.cs index f1148f1..ac75394 100644 --- a/lib/Net.Messaging.FBM/src/Client/FBMResponse.cs +++ b/lib/Net.Messaging.FBM/src/Client/FBMResponse.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Messaging.FBM @@ -56,7 +56,7 @@ namespace VNLib.Net.Messaging.FBM.Client /// <summary> /// The body segment of the response message /// </summary> - public readonly ReadOnlySpan<byte> ResponseBody => IsSet ? Helpers.GetRemainingData(MessagePacket!) : ReadOnlySpan<byte>.Empty; + public readonly ReadOnlySpan<byte> ResponseBody => IsSet ? Helpers.GetRemainingData(MessagePacket!) : []; /// <summary> /// Initailzies a response message structure and parses response diff --git a/lib/Net.Messaging.FBM/src/FBMMessageHeader.cs b/lib/Net.Messaging.FBM/src/FBMMessageHeader.cs index 180ce7d..bb89e12 100644 --- a/lib/Net.Messaging.FBM/src/FBMMessageHeader.cs +++ b/lib/Net.Messaging.FBM/src/FBMMessageHeader.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Messaging.FBM @@ -24,7 +24,6 @@ using System; - namespace VNLib.Net.Messaging.FBM { /// <summary> @@ -97,7 +96,15 @@ namespace VNLib.Net.Messaging.FBM /// </summary> /// <param name="other">The other header to compare</param> /// <returns>True if both headers have the same commad and value sequence</returns> - public bool Equals(FBMMessageHeader other) => Header == other.Header && Value.SequenceEqual(other.Value); + public readonly bool Equals(FBMMessageHeader other) => Header == other.Header && Value.SequenceEqual(other.Value); + + /// <summary> + /// Determines if a string value is equal to the current header value + /// </summary> + /// <param name="other">Value to test</param> + /// <param name="comparison">The string comparison type</param> + /// <returns>True if the values are equal, false otherwise</returns> + public readonly bool ValueEquals(ReadOnlySpan<char> other, StringComparison comparison) => Value.Equals(other, comparison); /// <summary> /// Gets a concatinated string of the current instance for debugging purposes diff --git a/lib/Net.Messaging.FBM/src/Server/FBMListener.cs b/lib/Net.Messaging.FBM/src/Server/FBMListener.cs index 30fa1ac..c248f9b 100644 --- a/lib/Net.Messaging.FBM/src/Server/FBMListener.cs +++ b/lib/Net.Messaging.FBM/src/Server/FBMListener.cs @@ -1,5 +1,5 @@ /* -* Copyright (c) 2023 Vaughn Nugent +* Copyright (c) 2024 Vaughn Nugent * * Library: VNLib * Package: VNLib.Net.Messaging.FBM @@ -41,20 +41,18 @@ namespace VNLib.Net.Messaging.FBM.Server /// A FBM protocol listener. Listens for messages on a <see cref="WebSocketSession"/> /// and raises events on requests. /// </summary> - public class FBMListener + /// <remarks> + /// Creates a new <see cref="FBMListener"/> instance ready for + /// processing connections + /// </remarks> + /// <param name="heap">The heap to alloc buffers from</param> + /// <exception cref="ArgumentNullException"></exception> + public class FBMListener(IFBMMemoryManager heap) { public const int SEND_SEMAPHORE_TIMEOUT_MS = 10 * 1000; - private readonly IFBMMemoryManager MemoryManger; - - /// <summary> - /// Creates a new <see cref="FBMListener"/> instance ready for - /// processing connections - /// </summary> - /// <param name="heap">The heap to alloc buffers from</param> - /// <exception cref="ArgumentNullException"></exception> - public FBMListener(IFBMMemoryManager heap) => MemoryManger = heap ?? throw new ArgumentNullException(nameof(heap)); + private readonly IFBMMemoryManager MemoryManger = heap ?? throw new ArgumentNullException(nameof(heap)); #pragma warning disable CA2007 // Consider calling ConfigureAwait on the awaited task @@ -68,8 +66,8 @@ namespace VNLib.Net.Messaging.FBM.Server /// <returns>A <see cref="Task"/> that completes when the connection closes</returns> public async Task ListenAsync(WebSocketSession wss, IFBMServerMessageHandler handler, FBMListenerSessionParams args) { - _ = wss ?? throw new ArgumentNullException(nameof(wss)); - _ = handler ?? throw new ArgumentNullException(nameof(handler)); + ArgumentNullException.ThrowIfNull(wss); + ArgumentNullException.ThrowIfNull(handler); ListeningSession session = new(wss, handler, in args, MemoryManger); @@ -79,7 +77,9 @@ namespace VNLib.Net.Messaging.FBM.Server //Start a task to process the queue Task queueWorker = QueueWorkerDoWork(workQueue, session); - //Alloc buffer + /* + * Alloc a top level receive buffer directly from the memory manager + */ IFBMMemoryHandle memHandle = MemoryManger.InitHandle(); MemoryManger.AllocBuffer(memHandle, args.RecvBufferSize); @@ -232,7 +232,7 @@ namespace VNLib.Net.Messaging.FBM.Server * WARNING! * this code relies on the managed websocket impl that the websocket will read * the entire buffer before returning. If this is not the case, this code will - * overwrite the memory buffer on the next call to move next. + * overwrite the memory buffer on the next call to MoveNext(). */ //Move to next segment @@ -316,10 +316,12 @@ namespace VNLib.Net.Messaging.FBM.Server Cancellation.Cancel(); //If dispose happens without any outstanding requests, we can dispose the session - if (_counter == 0) + if (_pendingRequests == 0) { CleanupInternal(); } + + //Internal data cannot be cleaned, because requests are still outstanding, fallback to reference counting } private void CleanupInternal() @@ -331,7 +333,7 @@ namespace VNLib.Net.Messaging.FBM.Server } - private uint _counter; + private uint _pendingRequests; /// <summary> /// Rents a new <see cref="FBMContext"/> instance from the pool @@ -341,15 +343,12 @@ namespace VNLib.Net.Messaging.FBM.Server /// <exception cref="ObjectDisposedException"></exception> public FBMContext RentContext() { - if (Cancellation.IsCancellationRequested) - { - throw new ObjectDisposedException("The instance has been disposed"); - } + ObjectDisposedException.ThrowIf(Cancellation.IsCancellationRequested, this); //Rent context FBMContext ctx = CtxStore.Rent(); - //Increment counter - Interlocked.Increment(ref _counter); + //Increment reference count counter + Interlocked.Increment(ref _pendingRequests); return ctx; } @@ -365,7 +364,7 @@ namespace VNLib.Net.Messaging.FBM.Server //Return the context CtxStore.Return(ctx); - uint current = Interlocked.Decrement(ref _counter); + uint current = Interlocked.Decrement(ref _pendingRequests); //No more contexts in use, dispose internals if (Cancellation.IsCancellationRequested && current == 0) |