aboutsummaryrefslogtreecommitdiff
path: root/Net.Messaging.FBM
diff options
context:
space:
mode:
Diffstat (limited to 'Net.Messaging.FBM')
-rw-r--r--Net.Messaging.FBM/LICENSE.txt195
-rw-r--r--Net.Messaging.FBM/README.md3
-rw-r--r--Net.Messaging.FBM/src/Client/ClientExtensions.cs69
-rw-r--r--Net.Messaging.FBM/src/Client/FBMClient.cs475
-rw-r--r--Net.Messaging.FBM/src/Client/FBMClientConfig.cs81
-rw-r--r--Net.Messaging.FBM/src/Client/FBMClientWorkerBase.cs125
-rw-r--r--Net.Messaging.FBM/src/Client/FBMRequest.cs302
-rw-r--r--Net.Messaging.FBM/src/Client/FBMResponse.cs106
-rw-r--r--Net.Messaging.FBM/src/Client/FMBClientErrorEventArgs.cs46
-rw-r--r--Net.Messaging.FBM/src/Client/HeaderCommand.cs57
-rw-r--r--Net.Messaging.FBM/src/Client/HeaderParseStatus.cs40
-rw-r--r--Net.Messaging.FBM/src/Client/Helpers.cs272
-rw-r--r--Net.Messaging.FBM/src/Client/IFBMMessage.cs62
-rw-r--r--Net.Messaging.FBM/src/Client/IStatefulConnection.cs54
-rw-r--r--Net.Messaging.FBM/src/Client/ManagedClientWebSocket.cs201
-rw-r--r--Net.Messaging.FBM/src/Client/README.md169
-rw-r--r--Net.Messaging.FBM/src/Exceptions/FBMException.cs52
-rw-r--r--Net.Messaging.FBM/src/Exceptions/FBMInvalidRequestException.cs47
-rw-r--r--Net.Messaging.FBM/src/Exceptions/InvalidResponseException.cs52
-rw-r--r--Net.Messaging.FBM/src/Server/FBMContext.cs85
-rw-r--r--Net.Messaging.FBM/src/Server/FBMListener.cs389
-rw-r--r--Net.Messaging.FBM/src/Server/FBMListenerBase.cs113
-rw-r--r--Net.Messaging.FBM/src/Server/FBMListenerSessionParams.cs62
-rw-r--r--Net.Messaging.FBM/src/Server/FBMRequestMessage.cs196
-rw-r--r--Net.Messaging.FBM/src/Server/FBMResponseMessage.cs226
-rw-r--r--Net.Messaging.FBM/src/Server/HeaderDataAccumulator.cs89
-rw-r--r--Net.Messaging.FBM/src/Server/IAsyncMessageBody.cs57
-rw-r--r--Net.Messaging.FBM/src/Server/IAsyncMessageReader.cs42
-rw-r--r--Net.Messaging.FBM/src/Server/readme.md35
-rw-r--r--Net.Messaging.FBM/src/VNLib.Net.Messaging.FBM.csproj33
30 files changed, 3735 insertions, 0 deletions
diff --git a/Net.Messaging.FBM/LICENSE.txt b/Net.Messaging.FBM/LICENSE.txt
new file mode 100644
index 0000000..147bcd6
--- /dev/null
+++ b/Net.Messaging.FBM/LICENSE.txt
@@ -0,0 +1,195 @@
+Copyright (c) 2022 Vaughn Nugent
+
+Contact information
+ Name: Vaughn Nugent
+ Email: public[at]vaughnnugent[dot]com
+ Website: https://www.vaughnnugent.com
+
+The software in this repository is licensed under the GNU Affero GPL version 3.0 (or any later version).
+
+GNU AFFERO GENERAL PUBLIC LICENSE
+
+Version 3, 19 November 2007
+
+Copyright © 2007 Free Software Foundation, Inc. <https://fsf.org/>
+Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
+Preamble
+
+The GNU Affero General Public License is a free, copyleft license for software and other kinds of works, specifically designed to ensure cooperation with the community in the case of network server software.
+
+The licenses for most software and other practical works are designed to take away your freedom to share and change the works. By contrast, our General Public Licenses are intended to guarantee your freedom to share and change all versions of a program--to make sure it remains free software for all its users.
+
+When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for them if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs, and that you know you can do these things.
+
+Developers that use our General Public Licenses protect your rights with two steps: (1) assert copyright on the software, and (2) offer you this License which gives you legal permission to copy, distribute and/or modify the software.
+
+A secondary benefit of defending all users' freedom is that improvements made in alternate versions of the program, if they receive widespread use, become available for other developers to incorporate. Many developers of free software are heartened and encouraged by the resulting cooperation. However, in the case of software used on network servers, this result may fail to come about. The GNU General Public License permits making a modified version and letting the public access it on a server without ever releasing its source code to the public.
+
+The GNU Affero General Public License is designed specifically to ensure that, in such cases, the modified source code becomes available to the community. It requires the operator of a network server to provide the source code of the modified version running there to the users of that server. Therefore, public use of a modified version, on a publicly accessible server, gives the public access to the source code of the modified version.
+
+An older license, called the Affero General Public License and published by Affero, was designed to accomplish similar goals. This is a different license, not a version of the Affero GPL, but Affero has released a new version of the Affero GPL which permits relicensing under this license.
+
+The precise terms and conditions for copying, distribution and modification follow.
+TERMS AND CONDITIONS
+0. Definitions.
+
+"This License" refers to version 3 of the GNU Affero General Public License.
+
+"Copyright" also means copyright-like laws that apply to other kinds of works, such as semiconductor masks.
+
+"The Program" refers to any copyrightable work licensed under this License. Each licensee is addressed as "you". "Licensees" and "recipients" may be individuals or organizations.
+
+To "modify" a work means to copy from or adapt all or part of the work in a fashion requiring copyright permission, other than the making of an exact copy. The resulting work is called a "modified version" of the earlier work or a work "based on" the earlier work.
+
+A "covered work" means either the unmodified Program or a work based on the Program.
+
+To "propagate" a work means to do anything with it that, without permission, would make you directly or secondarily liable for infringement under applicable copyright law, except executing it on a computer or modifying a private copy. Propagation includes copying, distribution (with or without modification), making available to the public, and in some countries other activities as well.
+
+To "convey" a work means any kind of propagation that enables other parties to make or receive copies. Mere interaction with a user through a computer network, with no transfer of a copy, is not conveying.
+
+An interactive user interface displays "Appropriate Legal Notices" to the extent that it includes a convenient and prominently visible feature that (1) displays an appropriate copyright notice, and (2) tells the user that there is no warranty for the work (except to the extent that warranties are provided), that licensees may convey the work under this License, and how to view a copy of this License. If the interface presents a list of user commands or options, such as a menu, a prominent item in the list meets this criterion.
+1. Source Code.
+
+The "source code" for a work means the preferred form of the work for making modifications to it. "Object code" means any non-source form of a work.
+
+A "Standard Interface" means an interface that either is an official standard defined by a recognized standards body, or, in the case of interfaces specified for a particular programming language, one that is widely used among developers working in that language.
+
+The "System Libraries" of an executable work include anything, other than the work as a whole, that (a) is included in the normal form of packaging a Major Component, but which is not part of that Major Component, and (b) serves only to enable use of the work with that Major Component, or to implement a Standard Interface for which an implementation is available to the public in source code form. A "Major Component", in this context, means a major essential component (kernel, window system, and so on) of the specific operating system (if any) on which the executable work runs, or a compiler used to produce the work, or an object code interpreter used to run it.
+
+The "Corresponding Source" for a work in object code form means all the source code needed to generate, install, and (for an executable work) run the object code and to modify the work, including scripts to control those activities. However, it does not include the work's System Libraries, or general-purpose tools or generally available free programs which are used unmodified in performing those activities but which are not part of the work. For example, Corresponding Source includes interface definition files associated with source files for the work, and the source code for shared libraries and dynamically linked subprograms that the work is specifically designed to require, such as by intimate data communication or control flow between those subprograms and other parts of the work.
+
+The Corresponding Source need not include anything that users can regenerate automatically from other parts of the Corresponding Source.
+
+The Corresponding Source for a work in source code form is that same work.
+2. Basic Permissions.
+
+All rights granted under this License are granted for the term of copyright on the Program, and are irrevocable provided the stated conditions are met. This License explicitly affirms your unlimited permission to run the unmodified Program. The output from running a covered work is covered by this License only if the output, given its content, constitutes a covered work. This License acknowledges your rights of fair use or other equivalent, as provided by copyright law.
+
+You may make, run and propagate covered works that you do not convey, without conditions so long as your license otherwise remains in force. You may convey covered works to others for the sole purpose of having them make modifications exclusively for you, or provide you with facilities for running those works, provided that you comply with the terms of this License in conveying all material for which you do not control copyright. Those thus making or running the covered works for you must do so exclusively on your behalf, under your direction and control, on terms that prohibit them from making any copies of your copyrighted material outside their relationship with you.
+
+Conveying under any other circumstances is permitted solely under the conditions stated below. Sublicensing is not allowed; section 10 makes it unnecessary.
+3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+No covered work shall be deemed part of an effective technological measure under any applicable law fulfilling obligations under article 11 of the WIPO copyright treaty adopted on 20 December 1996, or similar laws prohibiting or restricting circumvention of such measures.
+
+When you convey a covered work, you waive any legal power to forbid circumvention of technological measures to the extent such circumvention is effected by exercising rights under this License with respect to the covered work, and you disclaim any intention to limit operation or modification of the work as a means of enforcing, against the work's users, your or third parties' legal rights to forbid circumvention of technological measures.
+4. Conveying Verbatim Copies.
+
+You may convey verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice; keep intact all notices stating that this License and any non-permissive terms added in accord with section 7 apply to the code; keep intact all notices of the absence of any warranty; and give all recipients a copy of this License along with the Program.
+
+You may charge any price or no price for each copy that you convey, and you may offer support or warranty protection for a fee.
+5. Conveying Modified Source Versions.
+
+You may convey a work based on the Program, or the modifications to produce it from the Program, in the form of source code under the terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified it, and giving a relevant date.
+ b) The work must carry prominent notices stating that it is released under this License and any conditions added under section 7. This requirement modifies the requirement in section 4 to "keep intact all notices".
+ c) You must license the entire work, as a whole, under this License to anyone who comes into possession of a copy. This License will therefore apply, along with any applicable section 7 additional terms, to the whole of the work, and all its parts, regardless of how they are packaged. This License gives no permission to license the work in any other way, but it does not invalidate such permission if you have separately received it.
+ d) If the work has interactive user interfaces, each must display Appropriate Legal Notices; however, if the Program has interactive interfaces that do not display Appropriate Legal Notices, your work need not make them do so.
+
+A compilation of a covered work with other separate and independent works, which are not by their nature extensions of the covered work, and which are not combined with it such as to form a larger program, in or on a volume of a storage or distribution medium, is called an "aggregate" if the compilation and its resulting copyright are not used to limit the access or legal rights of the compilation's users beyond what the individual works permit. Inclusion of a covered work in an aggregate does not cause this License to apply to the other parts of the aggregate.
+6. Conveying Non-Source Forms.
+
+You may convey a covered work in object code form under the terms of sections 4 and 5, provided that you also convey the machine-readable Corresponding Source under the terms of this License, in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by the Corresponding Source fixed on a durable physical medium customarily used for software interchange.
+ b) Convey the object code in, or embodied in, a physical product (including a physical distribution medium), accompanied by a written offer, valid for at least three years and valid for as long as you offer spare parts or customer support for that product model, to give anyone who possesses the object code either (1) a copy of the Corresponding Source for all the software in the product that is covered by this License, on a durable physical medium customarily used for software interchange, for a price no more than your reasonable cost of physically performing this conveying of source, or (2) access to copy the Corresponding Source from a network server at no charge.
+ c) Convey individual copies of the object code with a copy of the written offer to provide the Corresponding Source. This alternative is allowed only occasionally and noncommercially, and only if you received the object code with such an offer, in accord with subsection 6b.
+ d) Convey the object code by offering access from a designated place (gratis or for a charge), and offer equivalent access to the Corresponding Source in the same way through the same place at no further charge. You need not require recipients to copy the Corresponding Source along with the object code. If the place to copy the object code is a network server, the Corresponding Source may be on a different server (operated by you or a third party) that supports equivalent copying facilities, provided you maintain clear directions next to the object code saying where to find the Corresponding Source. Regardless of what server hosts the Corresponding Source, you remain obligated to ensure that it is available for as long as needed to satisfy these requirements.
+ e) Convey the object code using peer-to-peer transmission, provided you inform other peers where the object code and Corresponding Source of the work are being offered to the general public at no charge under subsection 6d.
+
+A separable portion of the object code, whose source code is excluded from the Corresponding Source as a System Library, need not be included in conveying the object code work.
+
+A "User Product" is either (1) a "consumer product", which means any tangible personal property which is normally used for personal, family, or household purposes, or (2) anything designed or sold for incorporation into a dwelling. In determining whether a product is a consumer product, doubtful cases shall be resolved in favor of coverage. For a particular product received by a particular user, "normally used" refers to a typical or common use of that class of product, regardless of the status of the particular user or of the way in which the particular user actually uses, or expects or is expected to use, the product. A product is a consumer product regardless of whether the product has substantial commercial, industrial or non-consumer uses, unless such uses represent the only significant mode of use of the product.
+
+"Installation Information" for a User Product means any methods, procedures, authorization keys, or other information required to install and execute modified versions of a covered work in that User Product from a modified version of its Corresponding Source. The information must suffice to ensure that the continued functioning of the modified object code is in no case prevented or interfered with solely because modification has been made.
+
+If you convey an object code work under this section in, or with, or specifically for use in, a User Product, and the conveying occurs as part of a transaction in which the right of possession and use of the User Product is transferred to the recipient in perpetuity or for a fixed term (regardless of how the transaction is characterized), the Corresponding Source conveyed under this section must be accompanied by the Installation Information. But this requirement does not apply if neither you nor any third party retains the ability to install modified object code on the User Product (for example, the work has been installed in ROM).
+
+The requirement to provide Installation Information does not include a requirement to continue to provide support service, warranty, or updates for a work that has been modified or installed by the recipient, or for the User Product in which it has been modified or installed. Access to a network may be denied when the modification itself materially and adversely affects the operation of the network or violates the rules and protocols for communication across the network.
+
+Corresponding Source conveyed, and Installation Information provided, in accord with this section must be in a format that is publicly documented (and with an implementation available to the public in source code form), and must require no special password or key for unpacking, reading or copying.
+7. Additional Terms.
+
+"Additional permissions" are terms that supplement the terms of this License by making exceptions from one or more of its conditions. Additional permissions that are applicable to the entire Program shall be treated as though they were included in this License, to the extent that they are valid under applicable law. If additional permissions apply only to part of the Program, that part may be used separately under those permissions, but the entire Program remains governed by this License without regard to the additional permissions.
+
+When you convey a copy of a covered work, you may at your option remove any additional permissions from that copy, or from any part of it. (Additional permissions may be written to require their own removal in certain cases when you modify the work.) You may place additional permissions on material, added by you to a covered work, for which you have or can give appropriate copyright permission.
+
+Notwithstanding any other provision of this License, for material you add to a covered work, you may (if authorized by the copyright holders of that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the terms of sections 15 and 16 of this License; or
+ b) Requiring preservation of specified reasonable legal notices or author attributions in that material or in the Appropriate Legal Notices displayed by works containing it; or
+ c) Prohibiting misrepresentation of the origin of that material, or requiring that modified versions of such material be marked in reasonable ways as different from the original version; or
+ d) Limiting the use for publicity purposes of names of licensors or authors of the material; or
+ e) Declining to grant rights under trademark law for use of some trade names, trademarks, or service marks; or
+ f) Requiring indemnification of licensors and authors of that material by anyone who conveys the material (or modified versions of it) with contractual assumptions of liability to the recipient, for any liability that these contractual assumptions directly impose on those licensors and authors.
+
+All other non-permissive additional terms are considered "further restrictions" within the meaning of section 10. If the Program as you received it, or any part of it, contains a notice stating that it is governed by this License along with a term that is a further restriction, you may remove that term. If a license document contains a further restriction but permits relicensing or conveying under this License, you may add to a covered work material governed by the terms of that license document, provided that the further restriction does not survive such relicensing or conveying.
+
+If you add terms to a covered work in accord with this section, you must place, in the relevant source files, a statement of the additional terms that apply to those files, or a notice indicating where to find the applicable terms.
+
+Additional terms, permissive or non-permissive, may be stated in the form of a separately written license, or stated as exceptions; the above requirements apply either way.
+8. Termination.
+
+You may not propagate or modify a covered work except as expressly provided under this License. Any attempt otherwise to propagate or modify it is void, and will automatically terminate your rights under this License (including any patent licenses granted under the third paragraph of section 11).
+
+However, if you cease all violation of this License, then your license from a particular copyright holder is reinstated (a) provisionally, unless and until the copyright holder explicitly and finally terminates your license, and (b) permanently, if the copyright holder fails to notify you of the violation by some reasonable means prior to 60 days after the cessation.
+
+Moreover, your license from a particular copyright holder is reinstated permanently if the copyright holder notifies you of the violation by some reasonable means, this is the first time you have received notice of violation of this License (for any work) from that copyright holder, and you cure the violation prior to 30 days after your receipt of the notice.
+
+Termination of your rights under this section does not terminate the licenses of parties who have received copies or rights from you under this License. If your rights have been terminated and not permanently reinstated, you do not qualify to receive new licenses for the same material under section 10.
+9. Acceptance Not Required for Having Copies.
+
+You are not required to accept this License in order to receive or run a copy of the Program. Ancillary propagation of a covered work occurring solely as a consequence of using peer-to-peer transmission to receive a copy likewise does not require acceptance. However, nothing other than this License grants you permission to propagate or modify any covered work. These actions infringe copyright if you do not accept this License. Therefore, by modifying or propagating a covered work, you indicate your acceptance of this License to do so.
+10. Automatic Licensing of Downstream Recipients.
+
+Each time you convey a covered work, the recipient automatically receives a license from the original licensors, to run, modify and propagate that work, subject to this License. You are not responsible for enforcing compliance by third parties with this License.
+
+An "entity transaction" is a transaction transferring control of an organization, or substantially all assets of one, or subdividing an organization, or merging organizations. If propagation of a covered work results from an entity transaction, each party to that transaction who receives a copy of the work also receives whatever licenses to the work the party's predecessor in interest had or could give under the previous paragraph, plus a right to possession of the Corresponding Source of the work from the predecessor in interest, if the predecessor has it or can get it with reasonable efforts.
+
+You may not impose any further restrictions on the exercise of the rights granted or affirmed under this License. For example, you may not impose a license fee, royalty, or other charge for exercise of rights granted under this License, and you may not initiate litigation (including a cross-claim or counterclaim in a lawsuit) alleging that any patent claim is infringed by making, using, selling, offering for sale, or importing the Program or any portion of it.
+11. Patents.
+
+A "contributor" is a copyright holder who authorizes use under this License of the Program or a work on which the Program is based. The work thus licensed is called the contributor's "contributor version".
+
+A contributor's "essential patent claims" are all patent claims owned or controlled by the contributor, whether already acquired or hereafter acquired, that would be infringed by some manner, permitted by this License, of making, using, or selling its contributor version, but do not include claims that would be infringed only as a consequence of further modification of the contributor version. For purposes of this definition, "control" includes the right to grant patent sublicenses in a manner consistent with the requirements of this License.
+
+Each contributor grants you a non-exclusive, worldwide, royalty-free patent license under the contributor's essential patent claims, to make, use, sell, offer for sale, import and otherwise run, modify and propagate the contents of its contributor version.
+
+In the following three paragraphs, a "patent license" is any express agreement or commitment, however denominated, not to enforce a patent (such as an express permission to practice a patent or covenant not to sue for patent infringement). To "grant" such a patent license to a party means to make such an agreement or commitment not to enforce a patent against the party.
+
+If you convey a covered work, knowingly relying on a patent license, and the Corresponding Source of the work is not available for anyone to copy, free of charge and under the terms of this License, through a publicly available network server or other readily accessible means, then you must either (1) cause the Corresponding Source to be so available, or (2) arrange to deprive yourself of the benefit of the patent license for this particular work, or (3) arrange, in a manner consistent with the requirements of this License, to extend the patent license to downstream recipients. "Knowingly relying" means you have actual knowledge that, but for the patent license, your conveying the covered work in a country, or your recipient's use of the covered work in a country, would infringe one or more identifiable patents in that country that you have reason to believe are valid.
+
+If, pursuant to or in connection with a single transaction or arrangement, you convey, or propagate by procuring conveyance of, a covered work, and grant a patent license to some of the parties receiving the covered work authorizing them to use, propagate, modify or convey a specific copy of the covered work, then the patent license you grant is automatically extended to all recipients of the covered work and works based on it.
+
+A patent license is "discriminatory" if it does not include within the scope of its coverage, prohibits the exercise of, or is conditioned on the non-exercise of one or more of the rights that are specifically granted under this License. You may not convey a covered work if you are a party to an arrangement with a third party that is in the business of distributing software, under which you make payment to the third party based on the extent of your activity of conveying the work, and under which the third party grants, to any of the parties who would receive the covered work from you, a discriminatory patent license (a) in connection with copies of the covered work conveyed by you (or copies made from those copies), or (b) primarily for and in connection with specific products or compilations that contain the covered work, unless you entered into that arrangement, or that patent license was granted, prior to 28 March 2007.
+
+Nothing in this License shall be construed as excluding or limiting any implied license or other defenses to infringement that may otherwise be available to you under applicable patent law.
+12. No Surrender of Others' Freedom.
+
+If conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot convey a covered work so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not convey it at all. For example, if you agree to terms that obligate you to collect a royalty for further conveying from those to whom you convey the Program, the only way you could satisfy both those terms and this License would be to refrain entirely from conveying the Program.
+13. Remote Network Interaction; Use with the GNU General Public License.
+
+Notwithstanding any other provision of this License, if you modify the Program, your modified version must prominently offer all users interacting with it remotely through a computer network (if your version supports such interaction) an opportunity to receive the Corresponding Source of your version by providing access to the Corresponding Source from a network server at no charge, through some standard or customary means of facilitating copying of software. This Corresponding Source shall include the Corresponding Source for any work covered by version 3 of the GNU General Public License that is incorporated pursuant to the following paragraph.
+
+Notwithstanding any other provision of this License, you have permission to link or combine any covered work with a work licensed under version 3 of the GNU General Public License into a single combined work, and to convey the resulting work. The terms of this License will continue to apply to the part which is the covered work, but the work with which it is combined will remain governed by version 3 of the GNU General Public License.
+14. Revised Versions of this License.
+
+The Free Software Foundation may publish revised and/or new versions of the GNU Affero General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number. If the Program specifies that a certain numbered version of the GNU Affero General Public License "or any later version" applies to it, you have the option of following the terms and conditions either of that numbered version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of the GNU Affero General Public License, you may choose any version ever published by the Free Software Foundation.
+
+If the Program specifies that a proxy can decide which future versions of the GNU Affero General Public License can be used, that proxy's public statement of acceptance of a version permanently authorizes you to choose that version for the Program.
+
+Later license versions may give you additional or different permissions. However, no additional obligations are imposed on any author or copyright holder as a result of your choosing to follow a later version.
+15. Disclaimer of Warranty.
+
+THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+16. Limitation of Liability.
+
+IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
+17. Interpretation of Sections 15 and 16.
+
+If the disclaimer of warranty and limitation of liability provided above cannot be given local legal effect according to their terms, reviewing courts shall apply local law that most closely approximates an absolute waiver of all civil liability in connection with the Program, unless a warranty or assumption of liability accompanies a copy of the Program in return for a fee.
+
+END OF TERMS AND CONDITIONS \ No newline at end of file
diff --git a/Net.Messaging.FBM/README.md b/Net.Messaging.FBM/README.md
new file mode 100644
index 0000000..aabed75
--- /dev/null
+++ b/Net.Messaging.FBM/README.md
@@ -0,0 +1,3 @@
+# VNLib.Net.Messaging.FBM
+
+High performance structured web-socket based asynchronous request/response messaging library for .NET. \ No newline at end of file
diff --git a/Net.Messaging.FBM/src/Client/ClientExtensions.cs b/Net.Messaging.FBM/src/Client/ClientExtensions.cs
new file mode 100644
index 0000000..102b6c9
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/ClientExtensions.cs
@@ -0,0 +1,69 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: ClientExtensions.cs
+*
+* ClientExtensions.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Runtime.CompilerServices;
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+
+ public static class ClientExtensions
+ {
+ /// <summary>
+ /// Writes the location header of the requested resource
+ /// </summary>
+ /// <param name="request"></param>
+ /// <param name="location">The location address</param>
+ /// <exception cref="OutOfMemoryException"></exception>
+ public static void WriteLocation(this FBMRequest request, ReadOnlySpan<char> location)
+ {
+ request.WriteHeader(HeaderCommand.Location, location);
+ }
+
+ /// <summary>
+ /// Writes the location header of the requested resource
+ /// </summary>
+ /// <param name="request"></param>
+ /// <param name="location">The location address</param>
+ /// <exception cref="OutOfMemoryException"></exception>
+ public static void WriteLocation(this FBMRequest request, Uri location)
+ {
+ request.WriteHeader(HeaderCommand.Location, location.ToString());
+ }
+
+ /// <summary>
+ /// If the <see cref="FBMResponse.IsSet"/> property is false, raises an <see cref="InvalidResponseException"/>
+ /// </summary>
+ /// <param name="response"></param>
+ /// <exception cref="InvalidResponseException"></exception>
+ [MethodImpl(MethodImplOptions.AggressiveInlining)]
+ public static void ThrowIfNotSet(this in FBMResponse response)
+ {
+ if (!response.IsSet)
+ {
+ throw new InvalidResponseException("The response state is undefined (no response received)");
+ }
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/FBMClient.cs b/Net.Messaging.FBM/src/Client/FBMClient.cs
new file mode 100644
index 0000000..5353087
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/FBMClient.cs
@@ -0,0 +1,475 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMClient.cs
+*
+* FBMClient.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.IO;
+using System.Buffers;
+using System.Threading;
+using System.Net.WebSockets;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+using System.Collections.Concurrent;
+
+using VNLib.Net.Http;
+using VNLib.Utils;
+using VNLib.Utils.IO;
+using VNLib.Utils.Logging;
+using VNLib.Utils.Extensions;
+using VNLib.Utils.Memory.Caching;
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+ /// <summary>
+ /// A Fixed Buffer Message Protocol client. Allows for high performance client-server messaging
+ /// with minimal memory overhead.
+ /// </summary>
+ public class FBMClient : VnDisposeable, IStatefulConnection, ICacheHolder
+ {
+ /// <summary>
+ /// The WS connection query arguments to specify a receive buffer size
+ /// </summary>
+ public const string REQ_RECV_BUF_QUERY_ARG = "b";
+ /// <summary>
+ /// The WS connection query argument to suggest a maximum response header buffer size
+ /// </summary>
+ public const string REQ_HEAD_BUF_QUERY_ARG = "hb";
+ /// <summary>
+ /// The WS connection query argument to suggest a maximum message size
+ /// </summary>
+ public const string REQ_MAX_MESS_QUERY_ARG = "mx";
+
+ /// <summary>
+ /// Raised when the websocket has been closed because an error occured.
+ /// You may inspect the event args to determine the cause of the error.
+ /// </summary>
+ public event EventHandler<FMBClientErrorEventArgs>? ConnectionClosedOnError;
+ /// <summary>
+ /// Raised when the client listener operaiton has completed as a normal closure
+ /// </summary>
+ public event EventHandler? ConnectionClosed;
+
+ private readonly SemaphoreSlim SendLock;
+ private readonly ConcurrentDictionary<int, FBMRequest> ActiveRequests;
+ private readonly ReusableStore<FBMRequest> RequestRental;
+ private readonly FBMRequest _controlFrame;
+ /// <summary>
+ /// The configuration for the current client
+ /// </summary>
+ public FBMClientConfig Config { get; }
+ /// <summary>
+ /// A handle that is reset when a connection has been successfully set, and is set
+ /// when the connection exists
+ /// </summary>
+ public ManualResetEvent ConnectionStatusHandle { get; }
+ /// <summary>
+ /// The <see cref="ClientWebSocket"/> to send/recieve message on
+ /// </summary>
+ public ManagedClientWebSocket ClientSocket { get; }
+ /// <summary>
+ /// Gets the shared control frame for the current instance. The request is reset when
+ /// this property is called. (Not thread safe)
+ /// </summary>
+ protected FBMRequest ControlFrame
+ {
+ get
+ {
+ _controlFrame.Reset();
+ return _controlFrame;
+ }
+ }
+
+ /// <summary>
+ /// Creates a new <see cref="FBMClient"/> in a closed state
+ /// </summary>
+ /// <param name="config">The client configuration</param>
+ public FBMClient(FBMClientConfig config)
+ {
+ RequestRental = ObjectRental.CreateReusable(ReuseableRequestConstructor);
+ SendLock = new(1);
+ ConnectionStatusHandle = new(true);
+ ActiveRequests = new(Environment.ProcessorCount, 100);
+
+ Config = config;
+ //Init control frame
+ _controlFrame = new (Helpers.CONTROL_FRAME_MID, in config);
+ //Init the new client socket
+ ClientSocket = new(config.RecvBufferSize, config.RecvBufferSize, config.KeepAliveInterval, config.SubProtocol);
+ }
+
+ private void Debug(string format, params string[] strings)
+ {
+ if(Config.DebugLog != null)
+ {
+ Config.DebugLog.Debug($"[DEBUG] FBM Client: {format}", strings);
+ }
+ }
+ private void Debug(string format, long value, long other)
+ {
+ if (Config.DebugLog != null)
+ {
+ Config.DebugLog.Debug($"[DEBUG] FBM Client: {format}", value, other);
+ }
+ }
+
+ /// <summary>
+ /// Allocates and configures a new <see cref="FBMRequest"/> message object for use within the reusable store
+ /// </summary>
+ /// <returns>The configured <see cref="FBMRequest"/></returns>
+ protected virtual FBMRequest ReuseableRequestConstructor() => new(Config);
+
+ /// <summary>
+ /// Asynchronously opens a websocket connection with the specifed remote server
+ /// </summary>
+ /// <param name="address">The address of the server to connect to</param>
+ /// <param name="cancellation">A cancellation token</param>
+ /// <returns></returns>
+ public async Task ConnectAsync(Uri address, CancellationToken cancellation = default)
+ {
+ //Uribuilder to send config parameters to the server
+ UriBuilder urib = new(address);
+ urib.Query +=
+ $"{REQ_RECV_BUF_QUERY_ARG}={Config.RecvBufferSize}" +
+ $"&{REQ_HEAD_BUF_QUERY_ARG}={Config.MaxHeaderBufferSize}" +
+ $"&{REQ_MAX_MESS_QUERY_ARG}={Config.MaxMessageSize}";
+ Debug("Connection string {con}", urib.Uri.ToString());
+ //Connect to server
+ await ClientSocket.ConnectAsync(urib.Uri, cancellation);
+ //Reset wait handle before return
+ ConnectionStatusHandle.Reset();
+ //Begin listeing for requets in a background task
+ _ = Task.Run(ProcessContinuousRecvAsync, cancellation);
+ }
+
+ /// <summary>
+ /// Rents a new <see cref="FBMRequest"/> from the internal <see cref="ReusableStore{T}"/>.
+ /// Use <see cref="ReturnRequest(FBMRequest)"/> when request is no longer in use
+ /// </summary>
+ /// <returns>The configured (rented or new) <see cref="FBMRequest"/> ready for use</returns>
+ public FBMRequest RentRequest() => RequestRental.Rent();
+ /// <summary>
+ /// Stores (or returns) the reusable request in cache for use with <see cref="RentRequest"/>
+ /// </summary>
+ /// <param name="request">The request to return to the store</param>
+ /// <exception cref="InvalidOperationException"></exception>
+ public void ReturnRequest(FBMRequest request) => RequestRental.Return(request);
+
+ /// <summary>
+ /// Sends a <see cref="FBMRequest"/> to the connected server
+ /// </summary>
+ /// <param name="request">The request message to send to the server</param>
+ /// <param name="cancellationToken"></param>
+ /// <returns>When awaited, yields the server response</returns>
+ /// <exception cref="ArgumentException"></exception>
+ /// <exception cref="ObjectDisposedException"></exception>
+ /// <exception cref="InvalidOperationException"></exception>
+ /// <exception cref="FBMInvalidRequestException"></exception>
+ public async Task<FBMResponse> SendAsync(FBMRequest request, CancellationToken cancellationToken = default)
+ {
+ Check();
+ //Length of the request must contains at least 1 int and header byte
+ if (request.Length < 1 + sizeof(int))
+ {
+ throw new FBMInvalidRequestException("Message is not initialized");
+ }
+ //Store a null value in the request queue so the response can store a buffer
+ if (!ActiveRequests.TryAdd(request.MessageId, request))
+ {
+ throw new ArgumentException("Message with the same ID is already being processed");
+ }
+ try
+ {
+ Debug("Sending {bytes} with id {id}", request.RequestData.Length, request.MessageId);
+
+ //Reset the wait handle
+ request.ResponseWaitEvent.Reset();
+
+ //Wait for send-lock
+ using (SemSlimReleaser releaser = await SendLock.GetReleaserAsync(cancellationToken))
+ {
+ //Send the data to the server
+ await ClientSocket.SendAsync(request.RequestData, WebSocketMessageType.Binary, true, cancellationToken);
+ }
+
+ //wait for the response to be set
+ await request.WaitForResponseAsync(cancellationToken);
+
+ Debug("Received {size} bytes for message {id}", request.Response?.Length ?? 0, request.MessageId);
+
+ return request.GetResponse();
+ }
+ catch
+ {
+ //Remove the request since packet was never sent
+ ActiveRequests.Remove(request.MessageId, out _);
+ //Clear waiting flag
+ request.ResponseWaitEvent.Set();
+ throw;
+ }
+ }
+ /// <summary>
+ /// Streams arbitrary binary data to the server with the initial request message
+ /// </summary>
+ /// <param name="request">The request message to send to the server</param>
+ /// <param name="payload">Data to stream to the server</param>
+ /// <param name="ct">The content type of the stream of data</param>
+ /// <param name="cancellationToken"></param>
+ /// <returns>When awaited, yields the server response</returns>
+ /// <exception cref="ArgumentException"></exception>
+ /// <exception cref="ObjectDisposedException"></exception>
+ /// <exception cref="InvalidOperationException"></exception>
+ public async Task StreamDataAsync(FBMRequest request, Stream payload, ContentType ct, CancellationToken cancellationToken = default)
+ {
+ Check();
+ //Length of the request must contains at least 1 int and header byte
+ if(request.Length < 1 + sizeof(int))
+ {
+ throw new FBMInvalidRequestException("Message is not initialized");
+ }
+ //Store a null value in the request queue so the response can store a buffer
+ if (!ActiveRequests.TryAdd(request.MessageId, request))
+ {
+ throw new ArgumentException("Message with the same ID is already being processed");
+ }
+ try
+ {
+ Debug("Streaming {bytes} with id {id}", request.RequestData.Length, request.MessageId);
+ //Reset the wait handle
+ request.ResponseWaitEvent.Reset();
+ //Write an empty body in the request
+ request.WriteBody(ReadOnlySpan<byte>.Empty, ct);
+ //Wait for send-lock
+ using (SemSlimReleaser releaser = await SendLock.GetReleaserAsync(cancellationToken))
+ {
+ //Send the initial request packet
+ await ClientSocket.SendAsync(request.RequestData, WebSocketMessageType.Binary, false, cancellationToken);
+ //Calc buffer size
+ int bufSize = (int)Math.Clamp(payload.Length, Config.MessageBufferSize, Config.MaxMessageSize);
+ //Alloc a streaming buffer
+ using IMemoryOwner<byte> buffer = Config.BufferHeap.DirectAlloc<byte>(bufSize);
+ //Stream mesage body
+ do
+ {
+ //Read data
+ int read = await payload.ReadAsync(buffer.Memory, cancellationToken);
+ if (read == 0)
+ {
+ //No more data avialable
+ break;
+ }
+ //write message to socket, if the read data was smaller than the buffer, we can send the last packet
+ await ClientSocket.SendAsync(buffer.Memory[..read], WebSocketMessageType.Binary, read < bufSize, cancellationToken);
+
+ } while (true);
+ }
+ //wait for the server to respond
+ await request.WaitForResponseAsync(cancellationToken);
+
+ Debug("Response recieved {size} bytes for message {id}", request.Response?.Length ?? 0, request.MessageId);
+ }
+ catch
+ {
+ //Remove the request since packet was never sent or cancelled
+ ActiveRequests.Remove(request.MessageId, out _);
+ //Clear wait lock so the request state is reset
+ request.ResponseWaitEvent.Set();
+ throw;
+ }
+ }
+
+ /// <summary>
+ /// Begins listening for messages from the server on the internal socket (must be connected),
+ /// until the socket is closed, or canceled
+ /// </summary>
+ /// <returns></returns>
+ protected async Task ProcessContinuousRecvAsync()
+ {
+ Debug("Begining receive loop");
+ //Alloc recv buffer
+ IMemoryOwner<byte> recvBuffer = Config.BufferHeap.DirectAlloc<byte>(Config.RecvBufferSize);
+ try
+ {
+ //Recv event loop
+ while (true)
+ {
+ //Listen for incoming packets with the intial data buffer
+ ValueWebSocketReceiveResult result = await ClientSocket.ReceiveAsync(recvBuffer.Memory, CancellationToken.None);
+ //If the message is a close message, its time to exit
+ if (result.MessageType == WebSocketMessageType.Close)
+ {
+ //Notify the event handler that the connection was closed
+ ConnectionClosed?.Invoke(this, EventArgs.Empty);
+ break;
+ }
+ if (result.Count <= 4)
+ {
+ Debug("Empty message recieved from server");
+ continue;
+ }
+ //Alloc data buffer and write initial data
+ VnMemoryStream responseBuffer = new(Config.BufferHeap);
+ //Copy initial data
+ responseBuffer.Write(recvBuffer.Memory.Span[..result.Count]);
+ //Receive packets until the EOF is reached
+ while (!result.EndOfMessage)
+ {
+ //recive more data
+ result = await ClientSocket.ReceiveAsync(recvBuffer.Memory, CancellationToken.None);
+ //Make sure the buffer is not too large
+ if ((responseBuffer.Length + result.Count) > Config.MaxMessageSize)
+ {
+ //Dispose the buffer before exiting
+ responseBuffer.Dispose();
+ Debug("Recieved a message that was too large, skipped");
+ goto Skip;
+ }
+ //Copy continuous data
+ responseBuffer.Write(recvBuffer.Memory.Span[..result.Count]);
+ }
+ //Reset the buffer stream position
+ _ = responseBuffer.Seek(0, SeekOrigin.Begin);
+ ProcessResponse(responseBuffer);
+ //Goto skip statment to cleanup resources
+ Skip:;
+ }
+ }
+ catch (OperationCanceledException)
+ {
+ //Normal closeure, do nothing
+ }
+ catch (Exception ex)
+ {
+ //Error event args
+ FMBClientErrorEventArgs wsEventArgs = new()
+ {
+ Cause = ex,
+ ErrorClient = this
+ };
+ //Invoke error handler
+ ConnectionClosedOnError?.Invoke(this, wsEventArgs);
+ }
+ finally
+ {
+ //Dispose the recv buffer
+ recvBuffer.Dispose();
+ //Set all pending events
+ foreach (FBMRequest request in ActiveRequests.Values)
+ {
+ request.ResponseWaitEvent.Set();
+ }
+ //Clear dict
+ ActiveRequests.Clear();
+ //Cleanup the socket when exiting
+ ClientSocket.Cleanup();
+ //Set status handle as unset
+ ConnectionStatusHandle.Set();
+ //Invoke connection closed
+ ConnectionClosed?.Invoke(this, EventArgs.Empty);
+ }
+ Debug("Receive loop exited");
+ }
+
+ /// <summary>
+ /// Syncrhonously processes a buffered response packet
+ /// </summary>
+ /// <param name="responseMessage">The buffered response body recieved from the server</param>
+ /// <remarks>This method blocks the listening task. So operations should be quick</remarks>
+ protected virtual void ProcessResponse(VnMemoryStream responseMessage)
+ {
+ //read first response line
+ ReadOnlySpan<byte> line = Helpers.ReadLine(responseMessage);
+ //get the id of the message
+ int messageId = Helpers.GetMessageId(line);
+ //Finalze control frame
+ if(messageId == Helpers.CONTROL_FRAME_MID)
+ {
+ Debug("Control frame received");
+ ProcessControlFrame(responseMessage);
+ return;
+ }
+ else if (messageId < 0)
+ {
+ //Cannot process request
+ responseMessage.Dispose();
+ Debug("Invalid messageid");
+ return;
+ }
+ //Search for the request that has the same id
+ if (ActiveRequests.TryRemove(messageId, out FBMRequest? request))
+ {
+ //Set the new response message
+ request.SetResponse(responseMessage);
+ }
+ else
+ {
+ Debug("Message {id} was not found in the waiting message queue", messageId, 0);
+
+ //Cleanup no request was waiting
+ responseMessage.Dispose();
+ }
+ }
+ /// <summary>
+ /// Processes a control frame response from the server
+ /// </summary>
+ /// <param name="vms">The raw response packet from the server</param>
+ private void ProcessControlFrame(VnMemoryStream vms)
+ {
+ vms.Dispose();
+ }
+ /// <summary>
+ /// Processes a control frame response from the server
+ /// </summary>
+ /// <param name="response">The parsed response-packet</param>
+ protected virtual void ProcessControlFrame(in FBMResponse response)
+ {
+
+ }
+
+ /// <summary>
+ /// Closes the underlying <see cref="WebSocket"/> and cancels all pending operations
+ /// </summary>
+ /// <param name="cancellationToken"></param>
+ /// <returns></returns>
+ /// <exception cref="ObjectDisposedException"></exception>
+ public async Task DisconnectAsync(CancellationToken cancellationToken = default)
+ {
+ Check();
+ //Close the connection
+ await ClientSocket.CloseOutputAsync(WebSocketCloseStatus.NormalClosure, "Closing", cancellationToken);
+ }
+ ///<inheritdoc/>
+ protected override void Free()
+ {
+ //Dispose socket
+ ClientSocket.Dispose();
+ //Dispose client buffer
+ RequestRental.Dispose();
+ SendLock.Dispose();
+ ConnectionStatusHandle.Dispose();
+ }
+ ///<inheritdoc/>
+ public void CacheClear() => RequestRental.CacheClear();
+ ///<inheritdoc/>
+ public void CacheHardClear() => RequestRental.CacheHardClear();
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/FBMClientConfig.cs b/Net.Messaging.FBM/src/Client/FBMClientConfig.cs
new file mode 100644
index 0000000..229eb76
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/FBMClientConfig.cs
@@ -0,0 +1,81 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMClientConfig.cs
+*
+* FBMClientConfig.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Text;
+
+using VNLib.Utils.Memory;
+using VNLib.Utils.Logging;
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+ /// <summary>
+ /// A structure that defines readonly constants for the <see cref="FBMClient"/> to use
+ /// </summary>
+ public readonly struct FBMClientConfig
+ {
+ /// <summary>
+ /// The size (in bytes) of the internal buffer to use when receiving messages from the server
+ /// </summary>
+ public readonly int RecvBufferSize { get; init; }
+ /// <summary>
+ /// The size (in bytes) of the <see cref="FBMRequest"/> internal buffer size, when requests are rented from the client
+ /// </summary>
+ /// <remarks>
+ /// This is the entire size of the request buffer including headers and payload data, unless
+ /// data is streamed to the server
+ /// </remarks>
+ public readonly int MessageBufferSize { get; init; }
+ /// <summary>
+ /// The size (in chars) of the client/server message header buffer
+ /// </summary>
+ public readonly int MaxHeaderBufferSize { get; init; }
+ /// <summary>
+ /// The maximum size (in bytes) of messages sent or recieved from the server
+ /// </summary>
+ public readonly int MaxMessageSize { get; init; }
+ /// <summary>
+ /// The heap to allocate interal (and message) buffers from
+ /// </summary>
+ public readonly IUnmangedHeap BufferHeap { get; init; }
+ /// <summary>
+ /// The websocket keepalive interval to use (leaving this property default disables keepalives)
+ /// </summary>
+ public readonly TimeSpan KeepAliveInterval { get; init; }
+ /// <summary>
+ /// The websocket sub-protocol to use
+ /// </summary>
+ public readonly string? SubProtocol { get; init; }
+ /// <summary>
+ /// The encoding instance used to encode header values
+ /// </summary>
+ public readonly Encoding HeaderEncoding { get; init; }
+
+ /// <summary>
+ /// An optional log provider to write debug logs to. If this propery is not null,
+ /// debugging information will be logged with the debug log-level
+ /// </summary>
+ public readonly ILogProvider? DebugLog { get; init; }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/FBMClientWorkerBase.cs b/Net.Messaging.FBM/src/Client/FBMClientWorkerBase.cs
new file mode 100644
index 0000000..b4056dc
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/FBMClientWorkerBase.cs
@@ -0,0 +1,125 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMClientWorkerBase.cs
+*
+* FBMClientWorkerBase.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+using VNLib.Utils;
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+ /// <summary>
+ /// A base class for objects that implement <see cref="FBMClient"/>
+ /// operations
+ /// </summary>
+ public abstract class FBMClientWorkerBase : VnDisposeable, IStatefulConnection
+ {
+ /// <summary>
+ /// Allows configuration of websocket configuration options
+ /// </summary>
+ public ManagedClientWebSocket SocketOptions => Client.ClientSocket;
+
+#nullable disable
+ /// <summary>
+ /// The <see cref="FBMClient"/> to sent requests from
+ /// </summary>
+ public FBMClient Client { get; private set; }
+
+ /// <summary>
+ /// Raised when the client has connected successfully
+ /// </summary>
+ public event Action<FBMClient, FBMClientWorkerBase> Connected;
+#nullable enable
+
+ ///<inheritdoc/>
+ public event EventHandler ConnectionClosed
+ {
+ add => Client.ConnectionClosed += value;
+ remove => Client.ConnectionClosed -= value;
+ }
+
+ /// <summary>
+ /// Creates and initializes a the internal <see cref="FBMClient"/>
+ /// </summary>
+ /// <param name="config">The client config</param>
+ protected void InitClient(in FBMClientConfig config)
+ {
+ Client = new(config);
+ Client.ConnectionClosedOnError += Client_ConnectionClosedOnError;
+ Client.ConnectionClosed += Client_ConnectionClosed;
+ }
+
+ private void Client_ConnectionClosed(object? sender, EventArgs e) => OnDisconnected();
+ private void Client_ConnectionClosedOnError(object? sender, FMBClientErrorEventArgs e) => OnError(e);
+
+ /// <summary>
+ /// Asynchronously connects to a remote server by the specified uri
+ /// </summary>
+ /// <param name="serverUri">The remote uri of a server to connect to</param>
+ /// <param name="cancellationToken">A token to cancel the connect operation</param>
+ /// <returns>A task that compeltes when the client has connected to the remote server</returns>
+ public virtual async Task ConnectAsync(Uri serverUri, CancellationToken cancellationToken = default)
+ {
+ //Connect to server
+ await Client.ConnectAsync(serverUri, cancellationToken).ConfigureAwait(true);
+ //Invoke child on-connected event
+ OnConnected();
+ Connected?.Invoke(Client, this);
+ }
+
+ /// <summary>
+ /// Asynchronously disonnects a client only if the client is currently connected,
+ /// returns otherwise
+ /// </summary>
+ /// <param name="cancellationToken"></param>
+ /// <returns>A task that compeltes when the client has disconnected</returns>
+ public virtual Task DisconnectAsync(CancellationToken cancellationToken = default)
+ {
+ return Client.DisconnectAsync(cancellationToken);
+ }
+
+ /// <summary>
+ /// Invoked when a client has successfully connected to the remote server
+ /// </summary>
+ protected abstract void OnConnected();
+ /// <summary>
+ /// Invoked when the client has disconnected cleanly
+ /// </summary>
+ protected abstract void OnDisconnected();
+ /// <summary>
+ /// Invoked when the connected client is closed because of a connection error
+ /// </summary>
+ /// <param name="e">A <see cref="EventArgs"/> that contains the client error data</param>
+ protected abstract void OnError(FMBClientErrorEventArgs e);
+
+ ///<inheritdoc/>
+ protected override void Free()
+ {
+ Client.ConnectionClosedOnError -= Client_ConnectionClosedOnError;
+ Client.ConnectionClosed -= Client_ConnectionClosed;
+ Client.Dispose();
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/FBMRequest.cs b/Net.Messaging.FBM/src/Client/FBMRequest.cs
new file mode 100644
index 0000000..9d8af42
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/FBMRequest.cs
@@ -0,0 +1,302 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMRequest.cs
+*
+* FBMRequest.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Text;
+using System.Buffers;
+using System.Threading;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+
+using VNLib.Net.Http;
+using VNLib.Utils;
+using VNLib.Utils.IO;
+using VNLib.Utils.Memory;
+using VNLib.Utils.Extensions;
+using VNLib.Utils.Memory.Caching;
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+ /// <summary>
+ /// A reusable Fixed Buffer Message request container. This class is not thread-safe
+ /// </summary>
+ public sealed class FBMRequest : VnDisposeable, IReusable, IFBMMessage, IStringSerializeable
+ {
+ private class BufferWriter : IBufferWriter<byte>
+ {
+ private readonly FBMRequest _request;
+
+ public BufferWriter(FBMRequest request)
+ {
+ _request = request;
+ }
+
+ public void Advance(int count)
+ {
+ _request.Position += count;
+ }
+
+ public Memory<byte> GetMemory(int sizeHint = 0)
+ {
+ return sizeHint > 0 ? _request.RemainingBuffer[0..sizeHint] : _request.RemainingBuffer;
+ }
+
+ public Span<byte> GetSpan(int sizeHint = 0)
+ {
+ return sizeHint > 0 ? _request.RemainingBuffer.Span[0..sizeHint] : _request.RemainingBuffer.Span;
+ }
+ }
+
+ private readonly IMemoryOwner<byte> HeapBuffer;
+
+
+ private readonly BufferWriter _writer;
+ private int Position;
+
+ private readonly Encoding HeaderEncoding;
+ private readonly int ResponseHeaderBufferSize;
+ private readonly List<KeyValuePair<HeaderCommand, ReadOnlyMemory<char>>> ResponseHeaderList = new();
+ private char[]? ResponseHeaderBuffer;
+
+ /// <summary>
+ /// The size (in bytes) of the request message
+ /// </summary>
+ public int Length => Position;
+ private Memory<byte> RemainingBuffer => HeapBuffer.Memory[Position..];
+
+ /// <summary>
+ /// The id of the current request message
+ /// </summary>
+ public int MessageId { get; }
+ /// <summary>
+ /// The request message packet
+ /// </summary>
+ public ReadOnlyMemory<byte> RequestData => HeapBuffer.Memory[..Position];
+ /// <summary>
+ /// An <see cref="ManualResetEvent"/> to signal request/response
+ /// event completion
+ /// </summary>
+ internal ManualResetEvent ResponseWaitEvent { get; }
+
+ internal VnMemoryStream? Response { get; private set; }
+ /// <summary>
+ /// Initializes a new <see cref="FBMRequest"/> with the sepcified message buffer size,
+ /// and a random messageid
+ /// </summary>
+ /// <param name="config">The fbm client config storing required config variables</param>
+ public FBMRequest(in FBMClientConfig config) : this(Helpers.RandomMessageId, in config)
+ { }
+ /// <summary>
+ /// Initializes a new <see cref="FBMRequest"/> with the sepcified message buffer size and a custom MessageId
+ /// </summary>
+ /// <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)
+ {
+ //Setup response wait handle but make sure the contuation runs async
+ ResponseWaitEvent = new(true);
+
+ //Alloc the buffer as a memory owner so a memory buffer can be used
+ HeapBuffer = config.BufferHeap.DirectAlloc<byte>(config.MessageBufferSize);
+
+ MessageId = messageId;
+
+ HeaderEncoding = config.HeaderEncoding;
+ ResponseHeaderBufferSize = config.MaxHeaderBufferSize;
+
+ WriteMessageId();
+ _writer = new(this);
+ }
+
+ /// <summary>
+ /// Resets the internal buffer and writes the message-id header to the begining
+ /// of the buffer
+ /// </summary>
+ private void WriteMessageId()
+ {
+ //Get writer over buffer
+ ForwardOnlyWriter<byte> buffer = new(HeapBuffer.Memory.Span);
+ //write messageid header to the buffer
+ buffer.Append((byte)HeaderCommand.MessageId);
+ buffer.Append(MessageId);
+ buffer.WriteTermination();
+ //Store intial position
+ Position = buffer.Written;
+ }
+
+ ///<inheritdoc/>
+ public void WriteHeader(HeaderCommand header, ReadOnlySpan<char> value) => WriteHeader((byte)header, value);
+ ///<inheritdoc/>
+ public void WriteHeader(byte header, ReadOnlySpan<char> value)
+ {
+ ForwardOnlyWriter<byte> buffer = new(RemainingBuffer.Span);
+ buffer.WriteHeader(header, value, Helpers.DefaultEncoding);
+ //Update position
+ Position += buffer.Written;
+ }
+ ///<inheritdoc/>
+ public void WriteBody(ReadOnlySpan<byte> body, ContentType contentType = ContentType.Binary)
+ {
+ //Write content type header
+ WriteHeader(HeaderCommand.ContentType, HttpHelpers.GetContentTypeString(contentType));
+ //Get writer over buffer
+ ForwardOnlyWriter<byte> buffer = new(RemainingBuffer.Span);
+ //Now safe to write body
+ buffer.WriteBody(body);
+ //Update position
+ Position += buffer.Written;
+ }
+ /// <summary>
+ /// Returns buffer writer for writing the body data to the internal message buffer
+ /// </summary>
+ /// <returns>A <see cref="BufferWriter"/> to write message body to</returns>
+ public IBufferWriter<byte> GetBodyWriter()
+ {
+ //Write body termination
+ Helpers.Termination.CopyTo(RemainingBuffer);
+ Position += Helpers.Termination.Length;
+ //Return buffer writer
+ return _writer;
+ }
+
+ /// <summary>
+ /// Resets the internal buffer and allows for writing a new message with
+ /// the same message-id
+ /// </summary>
+ public void Reset()
+ {
+ //Re-writing the message-id will reset the buffer
+ WriteMessageId();
+ }
+
+ internal void SetResponse(VnMemoryStream? vms)
+ {
+ Response = vms;
+ ResponseWaitEvent.Set();
+ }
+
+ internal Task WaitForResponseAsync(CancellationToken token)
+ {
+ return ResponseWaitEvent.WaitAsync().WaitAsync(token);
+ }
+
+ ///<inheritdoc/>
+ protected override void Free()
+ {
+ HeapBuffer.Dispose();
+ ResponseWaitEvent.Dispose();
+ OnResponseDisposed();
+ }
+ void IReusable.Prepare() => Reset();
+ bool IReusable.Release()
+ {
+ //Clear old response data if error occured
+ Response?.Dispose();
+ Response = null;
+
+ return true;
+ }
+
+ /// <summary>
+ /// Gets the response of the sent message
+ /// </summary>
+ /// <returns>The response message for the current request</returns>
+ internal FBMResponse GetResponse()
+ {
+ if (Response != null)
+ {
+ /*
+ * NOTICE
+ *
+ * The FBM Client will position the response stream to the start
+ * of the header section (missing the message-id header)
+ *
+ * The message id belongs to this request so it cannot be mismatched
+ *
+ * The headers are read into a list of key-value pairs and the stream
+ * is positioned to the start of the message body
+ */
+
+
+ //Alloc rseponse buffer
+ ResponseHeaderBuffer ??= ArrayPool<char>.Shared.Rent(ResponseHeaderBufferSize);
+
+ //Parse message headers
+ HeaderParseError statusFlags = Helpers.ParseHeaders(Response, ResponseHeaderBuffer, ResponseHeaderList, HeaderEncoding);
+
+ //return response structure
+ return new(Response, statusFlags, ResponseHeaderList, OnResponseDisposed);
+ }
+ else
+ {
+ return new();
+ }
+ }
+
+ //Called when a response message is disposed to cleanup resources held by the response
+ private void OnResponseDisposed()
+ {
+ //Clear response header list
+ ResponseHeaderList.Clear();
+
+ //Clear old response
+ Response?.Dispose();
+ Response = null;
+
+ if (ResponseHeaderBuffer != null)
+ {
+ //Free response buffer
+ ArrayPool<char>.Shared.Return(ResponseHeaderBuffer!);
+ ResponseHeaderBuffer = null;
+ }
+ }
+
+ ///<inheritdoc/>
+ public string Compile()
+ {
+ int charSize = Helpers.DefaultEncoding.GetCharCount(RequestData.Span);
+ using UnsafeMemoryHandle<char> buffer = Memory.UnsafeAlloc<char>(charSize + 128);
+ ERRNO count = Compile(buffer.Span);
+ return buffer.AsSpan(0, count).ToString();
+ }
+ ///<inheritdoc/>
+ public void Compile(ref ForwardOnlyWriter<char> writer)
+ {
+ writer.Append("Message ID:");
+ writer.Append(MessageId);
+ writer.Append(Environment.NewLine);
+ Helpers.DefaultEncoding.GetChars(RequestData.Span, ref writer);
+ }
+ ///<inheritdoc/>
+ public ERRNO Compile(in Span<char> buffer)
+ {
+ ForwardOnlyWriter<char> writer = new(buffer);
+ Compile(ref writer);
+ return writer.Written;
+ }
+ ///<inheritdoc/>
+ public override string ToString() => Compile();
+
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/FBMResponse.cs b/Net.Messaging.FBM/src/Client/FBMResponse.cs
new file mode 100644
index 0000000..da36956
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/FBMResponse.cs
@@ -0,0 +1,106 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMResponse.cs
+*
+* FBMResponse.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Collections.Generic;
+
+using VNLib.Utils.IO;
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+ /// <summary>
+ /// A Fixed Buffer Message client response linked to the request that generated it.
+ /// Once the request is disposed or returned this message state is invalid
+ /// </summary>
+ public readonly struct FBMResponse : IDisposable, IEquatable<FBMResponse>
+ {
+ private readonly Action? _onDispose;
+
+ /// <summary>
+ /// True when a response body was recieved and properly parsed
+ /// </summary>
+ public readonly bool IsSet { get; }
+ /// <summary>
+ /// The raw response message packet
+ /// </summary>
+ public readonly VnMemoryStream? MessagePacket { get; }
+ /// <summary>
+ /// A collection of response message headers
+ /// </summary>
+ public readonly IReadOnlyList<KeyValuePair<HeaderCommand, ReadOnlyMemory<char>>> Headers { get; }
+ /// <summary>
+ /// Status flags of the message parse operation
+ /// </summary>
+ public readonly HeaderParseError StatusFlags { get; }
+ /// <summary>
+ /// The body segment of the response message
+ /// </summary>
+ public readonly ReadOnlySpan<byte> ResponseBody => IsSet ? Helpers.GetRemainingData(MessagePacket!) : ReadOnlySpan<byte>.Empty;
+
+ /// <summary>
+ /// Initailzies a response message structure and parses response
+ /// packet structure
+ /// </summary>
+ /// <param name="vms">The message buffer (message packet)</param>
+ /// <param name="status">The size of the buffer to alloc for header value storage</param>
+ /// <param name="headerList">The collection of headerse</param>
+ /// <param name="onDispose">A method that will be invoked when the message response body is disposed</param>
+ public FBMResponse(VnMemoryStream? vms, HeaderParseError status, IReadOnlyList<KeyValuePair<HeaderCommand, ReadOnlyMemory<char>>> headerList, Action onDispose)
+ {
+ MessagePacket = vms;
+ StatusFlags = status;
+ Headers = headerList;
+ IsSet = true;
+ _onDispose = onDispose;
+ }
+
+ /// <summary>
+ /// Creates an unset response structure
+ /// </summary>
+ public FBMResponse()
+ {
+ MessagePacket = null;
+ StatusFlags = HeaderParseError.InvalidHeaderRead;
+ Headers = Array.Empty<KeyValuePair<HeaderCommand, ReadOnlyMemory<char>>>();
+ IsSet = false;
+ _onDispose = null;
+ }
+
+ /// <summary>
+ /// Releases any resources associated with the response message
+ /// </summary>
+ public void Dispose() => _onDispose?.Invoke();
+ ///<inheritdoc/>
+ public override bool Equals(object? obj) => obj is FBMResponse response && Equals(response);
+ ///<inheritdoc/>
+ public override int GetHashCode() => IsSet ? MessagePacket!.GetHashCode() : 0;
+ ///<inheritdoc/>
+ public static bool operator ==(FBMResponse left, FBMResponse right) => left.Equals(right);
+ ///<inheritdoc/>
+ public static bool operator !=(FBMResponse left, FBMResponse right) => !(left == right);
+ ///<inheritdoc/>
+ public bool Equals(FBMResponse other) => IsSet && other.IsSet && MessagePacket == other.MessagePacket;
+
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/FMBClientErrorEventArgs.cs b/Net.Messaging.FBM/src/Client/FMBClientErrorEventArgs.cs
new file mode 100644
index 0000000..96e9414
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/FMBClientErrorEventArgs.cs
@@ -0,0 +1,46 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FMBClientErrorEventArgs.cs
+*
+* FMBClientErrorEventArgs.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+
+#nullable disable
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+ /// <summary>
+ /// <see cref="EventArgs"/> that is raised when an error occurs
+ /// in the background listener loop
+ /// </summary>
+ public class FMBClientErrorEventArgs : EventArgs
+ {
+ /// <summary>
+ /// The client that the exception was raised from
+ /// </summary>
+ public FBMClient ErrorClient { get; init; }
+ /// <summary>
+ /// The exception that was raised
+ /// </summary>
+ public Exception Cause { get; init; }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/HeaderCommand.cs b/Net.Messaging.FBM/src/Client/HeaderCommand.cs
new file mode 100644
index 0000000..5a57d85
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/HeaderCommand.cs
@@ -0,0 +1,57 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: HeaderCommand.cs
+*
+* HeaderCommand.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+namespace VNLib.Net.Messaging.FBM
+{
+ /// <summary>
+ /// A Fixed Buffer Message header command value
+ /// </summary>
+ public enum HeaderCommand : byte
+ {
+ /// <summary>
+ /// Default, do not use
+ /// </summary>
+ NOT_USED,
+ /// <summary>
+ /// Specifies the header for a message-id
+ /// </summary>
+ MessageId,
+ /// <summary>
+ /// Specifies a resource location
+ /// </summary>
+ Location,
+ /// <summary>
+ /// Specifies a standard MIME content type header
+ /// </summary>
+ ContentType,
+ /// <summary>
+ /// Specifies an action on a request
+ /// </summary>
+ Action,
+ /// <summary>
+ /// Specifies a status header
+ /// </summary>
+ Status
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/HeaderParseStatus.cs b/Net.Messaging.FBM/src/Client/HeaderParseStatus.cs
new file mode 100644
index 0000000..d38df26
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/HeaderParseStatus.cs
@@ -0,0 +1,40 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: HeaderParseStatus.cs
+*
+* HeaderParseStatus.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+
+namespace VNLib.Net.Messaging.FBM
+{
+ /// <summary>
+ /// Specifies the results of a response parsing operation
+ /// </summary>
+ [Flags]
+ public enum HeaderParseError
+ {
+ None = 0,
+ InvalidId = 1,
+ HeaderOutOfMem = 2,
+ InvalidHeaderRead = 4
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/Helpers.cs b/Net.Messaging.FBM/src/Client/Helpers.cs
new file mode 100644
index 0000000..8f895fa
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/Helpers.cs
@@ -0,0 +1,272 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: Helpers.cs
+*
+* Helpers.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.IO;
+using System.Text;
+using System.Collections.Generic;
+using System.Security.Cryptography;
+
+using VNLib.Utils;
+using VNLib.Utils.IO;
+using VNLib.Utils.Memory;
+using VNLib.Utils.Extensions;
+
+
+namespace VNLib.Net.Messaging.FBM
+{
+ /// <summary>
+ /// Contains FBM library helper methods
+ /// </summary>
+ public static class Helpers
+ {
+ /// <summary>
+ /// The message-id of a connection control frame / out of band message
+ /// </summary>
+ public const int CONTROL_FRAME_MID = -500;
+
+ public static readonly Encoding DefaultEncoding = Encoding.UTF8;
+ public static readonly ReadOnlyMemory<byte> Termination = new byte[] { 0xFF, 0xF1 };
+
+ /// <summary>
+ /// Parses the header line for a message-id
+ /// </summary>
+ /// <param name="line">A sequence of bytes that make up a header line</param>
+ /// <returns>The message-id if parsed, -1 if message-id is not valid</returns>
+ public static int GetMessageId(ReadOnlySpan<byte> line)
+ {
+ //Make sure the message line is large enough to contain a message-id
+ if (line.Length < 1 + sizeof(int))
+ {
+ return -1;
+ }
+ //The first byte should be the header id
+ HeaderCommand headerId = (HeaderCommand)line[0];
+ //Make sure the headerid is set
+ if (headerId != HeaderCommand.MessageId)
+ {
+ return -2;
+ }
+ //Get the messageid after the header byte
+ ReadOnlySpan<byte> messageIdSegment = line.Slice(1, sizeof(int));
+ //get the messageid from the messageid segment
+ return BitConverter.ToInt32(messageIdSegment);
+ }
+
+ /// <summary>
+ /// Alloctes a random integer to use as a message id
+ /// </summary>
+ public static int RandomMessageId => RandomNumberGenerator.GetInt32(1, int.MaxValue);
+ /// <summary>
+ /// Gets the remaining data after the current position of the stream.
+ /// </summary>
+ /// <param name="response">The stream to segment</param>
+ /// <returns>The remaining data segment</returns>
+ public static ReadOnlySpan<byte> GetRemainingData(VnMemoryStream response)
+ {
+ return response.AsSpan()[(int)response.Position..];
+ }
+
+ /// <summary>
+ /// Reads the next available line from the response message
+ /// </summary>
+ /// <param name="response"></param>
+ /// <returns>The read line</returns>
+ public static ReadOnlySpan<byte> ReadLine(VnMemoryStream response)
+ {
+ //Get the span from the current stream position to end of the stream
+ ReadOnlySpan<byte> line = GetRemainingData(response);
+ //Search for next line termination
+ int index = line.IndexOf(Termination.Span);
+ if (index == -1)
+ {
+ return ReadOnlySpan<byte>.Empty;
+ }
+ //Update stream position to end of termination
+ response.Seek(index + Termination.Length, SeekOrigin.Current);
+ //slice up line and exclude the termination
+ return line[..index];
+ }
+ /// <summary>
+ /// Parses headers from the request stream, stores headers from the buffer into the
+ /// header collection
+ /// </summary>
+ /// <param name="vms">The FBM packet buffer</param>
+ /// <param name="buffer">The header character buffer to write headers to</param>
+ /// <param name="headers">The collection to store headers in</param>
+ /// <param name="encoding">The encoding type used to deocde header values</param>
+ /// <returns>The results of the parse operation</returns>
+ public static HeaderParseError ParseHeaders(VnMemoryStream vms, char[] buffer, ICollection<KeyValuePair<HeaderCommand, ReadOnlyMemory<char>>> headers, Encoding encoding)
+ {
+ HeaderParseError status = HeaderParseError.None;
+ //sliding window
+ Memory<char> currentWindow = buffer;
+ //Accumulate headers
+ while (true)
+ {
+ //Read the next line from the current stream
+ ReadOnlySpan<byte> line = ReadLine(vms);
+ if (line.IsEmpty)
+ {
+ //Done reading headers
+ break;
+ }
+ HeaderCommand cmd = GetHeaderCommand(line);
+ //Get header value
+ ERRNO charsRead = GetHeaderValue(line, currentWindow.Span, encoding);
+ if (charsRead < 0)
+ {
+ //Out of buffer space
+ status |= HeaderParseError.HeaderOutOfMem;
+ break;
+ }
+ else if (!charsRead)
+ {
+ //Invalid header
+ status |= HeaderParseError.InvalidHeaderRead;
+ }
+ else
+ {
+ //Store header as a read-only sequence
+ headers.Add(new(cmd, currentWindow[..(int)charsRead]));
+ //Shift buffer window
+ currentWindow = currentWindow[(int)charsRead..];
+ }
+ }
+ return status;
+ }
+
+ /// <summary>
+ /// Gets a <see cref="HeaderCommand"/> enum from the first byte of the message
+ /// </summary>
+ /// <param name="line"></param>
+ /// <returns>The <see cref="HeaderCommand"/> enum value from hte first byte of the message</returns>
+ public static HeaderCommand GetHeaderCommand(ReadOnlySpan<byte> line)
+ {
+ return (HeaderCommand)line[0];
+ }
+ /// <summary>
+ /// Gets the value of the header following the colon bytes in the specifed
+ /// data message data line
+ /// </summary>
+ /// <param name="line">The message header line to get the value of</param>
+ /// <param name="output">The output character buffer to write characters to</param>
+ /// <param name="encoding">The encoding to decode the specified data with</param>
+ /// <returns>The number of characters encoded</returns>
+ public static ERRNO GetHeaderValue(ReadOnlySpan<byte> line, Span<char> output, Encoding encoding)
+ {
+ //Get the data following the header byte
+ ReadOnlySpan<byte> value = line[1..];
+ //Calculate the character account
+ int charCount = encoding.GetCharCount(value);
+ //Determine if the output buffer is large enough
+ if (charCount > output.Length)
+ {
+ return -1;
+ }
+ //Decode the characters and return the char count
+ _ = encoding.GetChars(value, output);
+ return charCount;
+ }
+
+ /// <summary>
+ /// Appends an arbitrary header to the current request buffer
+ /// </summary>
+ /// <param name="buffer"></param>
+ /// <param name="header">The <see cref="HeaderCommand"/> of the header</param>
+ /// <param name="value">The value of the header</param>
+ /// <param name="encoding">Encoding to use when writing character message</param>
+ /// <exception cref="OutOfMemoryException"></exception>
+ public static void WriteHeader(ref this ForwardOnlyWriter<byte> buffer, byte header, ReadOnlySpan<char> value, Encoding encoding)
+ {
+ //get char count
+ int byteCount = encoding.GetByteCount(value);
+ //make sure there is enough room in the buffer
+ if (buffer.RemainingSize < byteCount)
+ {
+ throw new OutOfMemoryException();
+ }
+ //Write header command enum value
+ buffer.Append(header);
+ //Convert the characters to binary and write to the buffer
+ encoding.GetBytes(value, ref buffer);
+ //Write termination (0)
+ buffer.WriteTermination();
+ }
+
+ /// <summary>
+ /// Ends the header section of the request and appends the message body to
+ /// the end of the request
+ /// </summary>
+ /// <param name="buffer"></param>
+ /// <param name="body">The message body to send with request</param>
+ /// <exception cref="OutOfMemoryException"></exception>
+ public static void WriteBody(ref this ForwardOnlyWriter<byte> buffer, ReadOnlySpan<byte> body)
+ {
+ //start with termination
+ buffer.WriteTermination();
+ //Write the body
+ buffer.Append(body);
+ }
+ /// <summary>
+ /// Writes a line termination to the message buffer
+ /// </summary>
+ /// <param name="buffer"></param>
+ public static void WriteTermination(ref this ForwardOnlyWriter<byte> buffer)
+ {
+ //write termination
+ buffer.Append(Termination.Span);
+ }
+
+ /// <summary>
+ /// Writes a line termination to the message buffer
+ /// </summary>
+ /// <param name="buffer"></param>
+ public static void WriteTermination(this IDataAccumulator<byte> buffer)
+ {
+ //write termination
+ buffer.Append(Termination.Span);
+ }
+
+ /// <summary>
+ /// Appends an arbitrary header to the current request buffer
+ /// </summary>
+ /// <param name="buffer"></param>
+ /// <param name="header">The <see cref="HeaderCommand"/> of the header</param>
+ /// <param name="value">The value of the header</param>
+ /// <param name="encoding">Encoding to use when writing character message</param>
+ /// <exception cref="ArgumentException"></exception>
+ public static void WriteHeader(this IDataAccumulator<byte> buffer, byte header, ReadOnlySpan<char> value, Encoding encoding)
+ {
+ //Write header command enum value
+ buffer.Append(header);
+ //Convert the characters to binary and write to the buffer
+ int written = encoding.GetBytes(value, buffer.Remaining);
+ //Advance the buffer
+ buffer.Advance(written);
+ //Write termination (0)
+ buffer.WriteTermination();
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/IFBMMessage.cs b/Net.Messaging.FBM/src/Client/IFBMMessage.cs
new file mode 100644
index 0000000..18f19ec
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/IFBMMessage.cs
@@ -0,0 +1,62 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: IFBMMessage.cs
+*
+* IFBMMessage.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+
+using VNLib.Net.Http;
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+ /// <summary>
+ /// Represents basic Fixed Buffer Message protocol operations
+ /// </summary>
+ public interface IFBMMessage
+ {
+ /// <summary>
+ /// The unique id of the message (nonzero)
+ /// </summary>
+ int MessageId { get; }
+ /// <summary>
+ /// Writes a data body to the message of the specified content type
+ /// </summary>
+ /// <param name="body">The body of the message to copy</param>
+ /// <param name="contentType">The content type of the message body</param>
+ /// <exception cref="OutOfMemoryException"></exception>
+ void WriteBody(ReadOnlySpan<byte> body, ContentType contentType = ContentType.Binary);
+ /// <summary>
+ /// Appends an arbitrary header to the current request buffer
+ /// </summary>
+ /// <param name="header">The header id</param>
+ /// <param name="value">The value of the header</param>
+ /// <exception cref="OutOfMemoryException"></exception>
+ void WriteHeader(byte header, ReadOnlySpan<char> value);
+ /// <summary>
+ /// Appends an arbitrary header to the current request buffer
+ /// </summary>
+ /// <param name="header">The <see cref="HeaderCommand"/> of the header</param>
+ /// <param name="value">The value of the header</param>
+ /// <exception cref="OutOfMemoryException"></exception>
+ void WriteHeader(HeaderCommand header, ReadOnlySpan<char> value);
+ }
+} \ No newline at end of file
diff --git a/Net.Messaging.FBM/src/Client/IStatefulConnection.cs b/Net.Messaging.FBM/src/Client/IStatefulConnection.cs
new file mode 100644
index 0000000..3b9dd3b
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/IStatefulConnection.cs
@@ -0,0 +1,54 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: IStatefulConnection.cs
+*
+* IStatefulConnection.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+ /// <summary>
+ /// An abstraction for a stateful connection client that reports its status
+ /// </summary>
+ public interface IStatefulConnection
+ {
+ /// <summary>
+ /// An event that is raised when the connection state has transition from connected to disconnected
+ /// </summary>
+ event EventHandler ConnectionClosed;
+ /// <summary>
+ /// Connects the client to the remote resource
+ /// </summary>
+ /// <param name="serverUri">The resource location to connect to</param>
+ /// <param name="cancellationToken">A token to cancel the connect opreation</param>
+ /// <returns>A task that compeltes when the connection has succedded</returns>
+ Task ConnectAsync(Uri serverUri, CancellationToken cancellationToken = default);
+ /// <summary>
+ /// Gracefully disconnects the client from the remote resource
+ /// </summary>
+ /// <param name="cancellationToken">A token to cancel the disconnect operation</param>
+ /// <returns>A task that completes when the client has been disconnected</returns>
+ Task DisconnectAsync(CancellationToken cancellationToken = default);
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/ManagedClientWebSocket.cs b/Net.Messaging.FBM/src/Client/ManagedClientWebSocket.cs
new file mode 100644
index 0000000..acac369
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/ManagedClientWebSocket.cs
@@ -0,0 +1,201 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: ManagedClientWebSocket.cs
+*
+* ManagedClientWebSocket.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Net;
+using System.Threading;
+using System.Net.Security;
+using System.Net.WebSockets;
+using System.Threading.Tasks;
+using System.Security.Cryptography.X509Certificates;
+
+using VNLib.Utils.Memory;
+
+#nullable enable
+
+namespace VNLib.Net.Messaging.FBM.Client
+{
+
+ /// <summary>
+ /// A wrapper container to manage client websockets
+ /// </summary>
+ public class ManagedClientWebSocket : WebSocket
+ {
+ private readonly int TxBufferSize;
+ private readonly int RxBufferSize;
+ private readonly TimeSpan KeepAliveInterval;
+ private readonly VnTempBuffer<byte> _dataBuffer;
+ private readonly string? _subProtocol;
+
+ /// <summary>
+ /// A collection of headers to add to the client
+ /// </summary>
+ public WebHeaderCollection Headers { get; }
+ public X509CertificateCollection Certificates { get; }
+ public IWebProxy? Proxy { get; set; }
+ public CookieContainer? Cookies { get; set; }
+ public RemoteCertificateValidationCallback? RemoteCertificateValidationCallback { get; set; }
+
+
+ private ClientWebSocket? _socket;
+
+ /// <summary>
+ /// Initiaizes a new <see cref="ManagedClientWebSocket"/> that accepts an optional sub-protocol for connections
+ /// </summary>
+ /// <param name="txBufferSize">The size (in bytes) of the send buffer size</param>
+ /// <param name="rxBufferSize">The size (in bytes) of the receive buffer size to use</param>
+ /// <param name="keepAlive">The WS keepalive interval</param>
+ /// <param name="subProtocol">The optional sub-protocol to use</param>
+ public ManagedClientWebSocket(int txBufferSize, int rxBufferSize, TimeSpan keepAlive, string? subProtocol)
+ {
+ //Init header collection
+ Headers = new();
+ Certificates = new();
+ //Alloc buffer
+ _dataBuffer = new(rxBufferSize);
+ TxBufferSize = txBufferSize;
+ RxBufferSize = rxBufferSize;
+ KeepAliveInterval = keepAlive;
+ _subProtocol = subProtocol;
+ }
+
+ /// <summary>
+ /// Asyncrhonously prepares a new client web-socket and connects to the remote endpoint
+ /// </summary>
+ /// <param name="serverUri">The endpoint to connect to</param>
+ /// <param name="token">A token to cancel the connect operation</param>
+ /// <returns>A task that compeltes when the client has connected</returns>
+ public async Task ConnectAsync(Uri serverUri, CancellationToken token)
+ {
+ //Init new socket
+ _socket = new();
+ try
+ {
+ //Set buffer
+ _socket.Options.SetBuffer(RxBufferSize, TxBufferSize, _dataBuffer);
+ //Set remaining stored options
+ _socket.Options.ClientCertificates = Certificates;
+ _socket.Options.KeepAliveInterval = KeepAliveInterval;
+ _socket.Options.Cookies = Cookies;
+ _socket.Options.Proxy = Proxy;
+ _socket.Options.RemoteCertificateValidationCallback = RemoteCertificateValidationCallback;
+ //Specify sub-protocol
+ if (!string.IsNullOrEmpty(_subProtocol))
+ {
+ _socket.Options.AddSubProtocol(_subProtocol);
+ }
+ //Set headers
+ for (int i = 0; i < Headers.Count; i++)
+ {
+ string name = Headers.GetKey(i);
+ string? value = Headers.Get(i);
+ //Set header
+ _socket.Options.SetRequestHeader(name, value);
+ }
+ //Connect to server
+ await _socket.ConnectAsync(serverUri, token);
+ }
+ catch
+ {
+ //Cleanup the socket
+ Cleanup();
+ throw;
+ }
+ }
+
+ /// <summary>
+ /// Cleans up internal resources to prepare for another connection
+ /// </summary>
+ public void Cleanup()
+ {
+ //Dispose old socket if set
+ _socket?.Dispose();
+ _socket = null;
+ }
+ ///<inheritdoc/>
+ public override WebSocketCloseStatus? CloseStatus => _socket?.CloseStatus;
+ ///<inheritdoc/>
+ public override string CloseStatusDescription => _socket?.CloseStatusDescription ?? string.Empty;
+ ///<inheritdoc/>
+ public override WebSocketState State => _socket?.State ?? WebSocketState.Closed;
+ ///<inheritdoc/>
+ public override string SubProtocol => _subProtocol ?? string.Empty;
+
+
+ ///<inheritdoc/>
+ public override void Abort()
+ {
+ _socket?.Abort();
+ }
+ ///<inheritdoc/>
+ public override Task CloseAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
+ {
+ return _socket?.CloseAsync(closeStatus, statusDescription, cancellationToken) ?? Task.CompletedTask;
+ }
+ ///<inheritdoc/>
+ public override Task CloseOutputAsync(WebSocketCloseStatus closeStatus, string? statusDescription, CancellationToken cancellationToken)
+ {
+ if (_socket?.State == WebSocketState.Open || _socket?.State == WebSocketState.CloseSent)
+ {
+ return _socket.CloseOutputAsync(closeStatus, statusDescription, cancellationToken);
+ }
+ return Task.CompletedTask;
+ }
+ ///<inheritdoc/>
+ public override ValueTask<ValueWebSocketReceiveResult> ReceiveAsync(Memory<byte> buffer, CancellationToken cancellationToken)
+ {
+ _ = _socket ?? throw new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "The connected socket has been disconnected");
+
+ return _socket.ReceiveAsync(buffer, cancellationToken);
+ }
+ ///<inheritdoc/>
+ public override Task<WebSocketReceiveResult> ReceiveAsync(ArraySegment<byte> buffer, CancellationToken cancellationToken)
+ {
+ _ = _socket ?? throw new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "The connected socket has been disconnected");
+
+ return _socket.ReceiveAsync(buffer, cancellationToken);
+ }
+ ///<inheritdoc/>
+ public override ValueTask SendAsync(ReadOnlyMemory<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
+ {
+ _ = _socket ?? throw new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "The connected socket has been disconnected");
+ return _socket.SendAsync(buffer, messageType, endOfMessage, cancellationToken);
+ }
+ ///<inheritdoc/>
+ public override Task SendAsync(ArraySegment<byte> buffer, WebSocketMessageType messageType, bool endOfMessage, CancellationToken cancellationToken)
+ {
+ _ = _socket ?? throw new WebSocketException(WebSocketError.ConnectionClosedPrematurely, "The connected socket has been disconnected");
+ return _socket.SendAsync(buffer, messageType, endOfMessage, cancellationToken);
+ }
+
+ ///<inheritdoc/>
+ public override void Dispose()
+ {
+ //Free buffer
+ _dataBuffer.Dispose();
+ _socket?.Dispose();
+ GC.SuppressFinalize(this);
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Client/README.md b/Net.Messaging.FBM/src/Client/README.md
new file mode 100644
index 0000000..5aa8e76
--- /dev/null
+++ b/Net.Messaging.FBM/src/Client/README.md
@@ -0,0 +1,169 @@
+# VNLib.Net.Messaging.FBM.Client
+
+Fixed Buffer Messaging Protocol client library. High performance statful messaging
+protocol built on top of HTTP web-sockets. Low/no allocation, completely asynchronous
+while providing a TPL API. This library provides a simple asynchronous request/response
+architecture to web-sockets. This was initially designed to provide an alternative to
+complete HTTP request/response overhead, but allow a simple control flow for work
+across a network.
+
+The base of the library relies on creating message objects that allocate fixed size
+buffers are configured when the IFBMMessageis constructed. All data is written to the
+internal buffer adhering to the format below.
+
+Messages consist of a 4 byte message id, a collection of headers, and a message body.
+The first 4 bytes of a message is the ID (for normal messages a signed integer greater than 0),
+0 is reserved for error conditions, and negative numbers are reserved for internal
+messages. Headers are identified by a single byte, followed by a variable length UTF8
+encoded character sequence, followed by a termination of 0xFF, 0xF1 (may change).
+
+### Message structure
+ 4 byte positive (signed 32-bit integer) message id
+ 2 byte termination
+ 1 byte header-id
+ variable length UTF8 value
+ 2 byte termination
+ -- other headers --
+ 2 byte termination (extra termination, ie: empty header)
+ variable length payload
+ (end of message is the end of the payload)
+
+Buffer sizes are generally negotiated on initial web-socket upgrade, so to buffer entire messages
+in a single read/write from the web-socket. Received messages are read into memory until
+the web-socket has no more data available. The message is then parsed and passed for processing
+on the server side, or complete a pending request on the client side. Servers may drop the
+web-socket connection and return an error if messages exceed the size of the pre-negotiated
+buffer. Servers should validate buffer sizes before accepting a connection.
+
+This client library allows for messages to be streamed to the server, however this library
+is optimized for fixed buffers, so streaming will not be the most efficient, and will likely
+cause slow-downs in message transmission. However, since FBM relies on a streaming protocol,
+so it was silly not to provide it. Streams add overhead of additional buffer allocation,
+additional copy, and message fragmentation (multiple writes to the web-socket). Since frames
+written to the web-socket must be synchronized, a mutex is held during transmission, which
+means the more message overhead, the longer the blocking period on new messages. Mutex
+acquisition will wait asynchronously when necessary.
+
+The goal of the FBM protocol for is to provide efficient use of resources (memory, network,
+and minimize GC load) to transfer small messages truly asynchronously, at wire speeds, with
+only web-socket and transport overhead. Using web-sockets simplifies implementation, and allows
+comparability across platforms, languages, and versions.
+
+## fundamentals
+
+The main implementation is the FBMClient class. This class provides the means for creating
+the stateful connection to the remote server. It also provides an internal FBMRequest message
+rental (object cache) that created initialized FBMRequest messages. This class may be derrived
+to provide additional functionality, such as handling control frames that may dynamically
+alter the state of the connection (negotiation etc). A mechanism to do so is provided.
+
+### FBMClient layout
+
+```
+ public class FBMClient : VnDisposeable, IStatefulConnection, ICacheHolder
+ {
+ //Raised when an error occurs during receiving or parsing
+ public event EventHandler<FMBClientErrorEventArgs>? ConnectionClosedOnError;
+
+ //Raised when connection is closed, regardless of the cause
+ public event EventHandler? ConnectionClosed;
+
+ //Connects to the remote server at the specified websocket address (ws:// or wss://)
+ public async Task ConnectAsync(Uri address, CancellationToken cancellation = default);
+
+ //When connected, sends the specified message to the remote server
+ public async Task<FBMResponse> SendAsync(FBMRequest request, CancellationToken cancellationToken = default);
+
+ //When connected, streams a message to the remote server, * the message payload must not be configured *
+ public async Task<FBMResponse> StreamDataAsync(FBMRequest request, Stream payload, ContentType ct, CancellationToken cancellationToken = default);
+
+ //Disconnects from the remote server
+ public async Task DisconnectAsync(CancellationToken cancellationToken = default);
+
+ //Releases all held resourses
+ public void Dispose(); //Inherrited from VnDisposeable
+
+ ICacheHolder.CacheClear(); //Inherited member, clears cached FBMRequest objects
+ ICacheHolder.CacheHardClear(); //Inherited member, clears cached FBMRequest objects
+ }
+```
+
+### Example usage
+```
+ FBMClientConfig config = new()
+ {
+ //The size (in bytes) of the internal buffer to use when receiving messages from the server
+ RecvBufferSize = 1024,
+
+ //FBMRequest buffer size (expected size of buffers, required for negotiation)
+ RequestBufferSize = 1024,
+
+ //The size (in chars) of headers the FBMResponse should expect to buffer from the server
+ ResponseHeaderBufSize = 1024,
+
+ //The absolute maximum message size to buffer from the server
+ MaxMessageSize = 10 * 1024 * 1024, //10KiB
+
+ //The unmanaged heap the allocate buffers from
+ BufferHeap = Memory.Shared,
+
+ //Web-socket keepalive frame interval
+ KeepAliveInterval = TimeSpan.FromSeconds(30),
+
+ //Web-socket sub-protocol header value
+ SubProtocol = null
+ };
+
+ //Craete client from the config
+ using (FBMClient client = new(config))
+ {
+ //Usually set some type of authentication headers before connecting
+
+ /*
+ client.ClientSocket.SetHeader("Authorization", "Authorization token");
+ */
+
+ //Connect to server
+ Uri address = new Uri("wss://localhost:8080/some/fbm/endpoint");
+ await client.ConnectAsync(address, CancellationToken.None);
+
+ do
+ {
+ //Rent request message
+ FBMRequest request = client.RentRequest();
+ //Some arbitrary header value (or preconfigured header)
+ request.WriteHeader(0x10, "Hello");
+ //Some arbitrary payload
+ request.WriteBody(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A }, ContentType.Binary);
+ //Send request message
+ using (FBMResponse response = await client.SendAsync(request, CancellationToken.None))
+ {
+ //Extension method to raise exception if an invalid response was received (also use the response.IsSet flag)
+ response.ThrowIfNotSet();
+
+ //Check headers (using Linq to get first header)
+ string header1 = response.Headers.First().Value.ToString();
+
+ //Get payload, data is valid until the response is disposed
+ ReadOnlySpan<byte> body = response.ResponseBody;
+ }
+ //Return request
+ client.ReturnRequest(request);
+ //request.Dispose(); //Alternativly dispose message
+
+ await Task.Delay(1000);
+ }
+ while(true);
+ }
+```
+
+## Final Notes
+
+XML Documentation is or will be provided for almost all public exports. APIs are intended to
+be sensibly public and immutable to allow for easy extensability (via extension methods). I
+often use extension libraries to provide additional functionality. (See cache library)
+
+This library is likely a niche use case, and is probably not for everyone. Unless you care
+about reasonably efficient high frequency request/response messaging, this probably isnt
+for you. This library provides a reasonable building block for distributed lock mechanisms
+and small data caching. \ No newline at end of file
diff --git a/Net.Messaging.FBM/src/Exceptions/FBMException.cs b/Net.Messaging.FBM/src/Exceptions/FBMException.cs
new file mode 100644
index 0000000..1d5c7db
--- /dev/null
+++ b/Net.Messaging.FBM/src/Exceptions/FBMException.cs
@@ -0,0 +1,52 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMException.cs
+*
+* FBMException.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Runtime.Serialization;
+
+namespace VNLib.Net.Messaging.FBM
+{
+ /// <summary>
+ /// A base exception class for all FBM Library exceptions
+ /// </summary>
+ public class FBMException : Exception
+ {
+ ///<inheritdoc/>
+ public FBMException()
+ {
+ }
+ ///<inheritdoc/>
+ public FBMException(string message) : base(message)
+ {
+ }
+ ///<inheritdoc/>
+ public FBMException(string message, Exception innerException) : base(message, innerException)
+ {
+ }
+ ///<inheritdoc/>
+ protected FBMException(SerializationInfo info, StreamingContext context) : base(info, context)
+ {
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Exceptions/FBMInvalidRequestException.cs b/Net.Messaging.FBM/src/Exceptions/FBMInvalidRequestException.cs
new file mode 100644
index 0000000..ae42797
--- /dev/null
+++ b/Net.Messaging.FBM/src/Exceptions/FBMInvalidRequestException.cs
@@ -0,0 +1,47 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMInvalidRequestException.cs
+*
+* FBMInvalidRequestException.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+
+
+namespace VNLib.Net.Messaging.FBM
+{
+ /// <summary>
+ /// Raised when a request message is not in a valid state and cannot be sent
+ /// </summary>
+ public class FBMInvalidRequestException : FBMException
+ {
+ public FBMInvalidRequestException()
+ {
+ }
+
+ public FBMInvalidRequestException(string message) : base(message)
+ {
+ }
+
+ public FBMInvalidRequestException(string message, Exception innerException) : base(message, innerException)
+ {
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Exceptions/InvalidResponseException.cs b/Net.Messaging.FBM/src/Exceptions/InvalidResponseException.cs
new file mode 100644
index 0000000..3f0b970
--- /dev/null
+++ b/Net.Messaging.FBM/src/Exceptions/InvalidResponseException.cs
@@ -0,0 +1,52 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: InvalidResponseException.cs
+*
+* InvalidResponseException.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Runtime.Serialization;
+
+namespace VNLib.Net.Messaging.FBM
+{
+ /// <summary>
+ /// Raised when a response to an FBM request is not in a valid state
+ /// </summary>
+ public class InvalidResponseException : FBMException
+ {
+ ///<inheritdoc/>
+ public InvalidResponseException()
+ {
+ }
+ ///<inheritdoc/>
+ public InvalidResponseException(string message) : base(message)
+ {
+ }
+ ///<inheritdoc/>
+ public InvalidResponseException(string message, Exception innerException) : base(message, innerException)
+ {
+ }
+ ///<inheritdoc/>
+ protected InvalidResponseException(SerializationInfo info, StreamingContext context) : base(info, context)
+ {
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Server/FBMContext.cs b/Net.Messaging.FBM/src/Server/FBMContext.cs
new file mode 100644
index 0000000..fb39d1b
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/FBMContext.cs
@@ -0,0 +1,85 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMContext.cs
+*
+* FBMContext.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System.Text;
+
+using VNLib.Utils.IO;
+using VNLib.Utils.Memory.Caching;
+
+namespace VNLib.Net.Messaging.FBM.Server
+{
+ /// <summary>
+ /// A request/response pair message context
+ /// </summary>
+ public sealed class FBMContext : IReusable
+ {
+ private readonly Encoding _headerEncoding;
+
+ /// <summary>
+ /// The request message to process
+ /// </summary>
+ public FBMRequestMessage Request { get; }
+ /// <summary>
+ /// The response message
+ /// </summary>
+ public FBMResponseMessage Response { get; }
+ /// <summary>
+ /// Creates a new reusable <see cref="FBMContext"/>
+ /// for use within a <see cref="ObjectRental{T}"/>
+ /// cache
+ /// </summary>
+ /// <param name="requestHeaderBufferSize">The size in characters of the request header buffer</param>
+ /// <param name="responseBufferSize">The size in characters of the response header buffer</param>
+ /// <param name="headerEncoding">The message header encoding instance</param>
+ public FBMContext(int requestHeaderBufferSize, int responseBufferSize, Encoding headerEncoding)
+ {
+ Request = new(requestHeaderBufferSize);
+ Response = new(responseBufferSize, headerEncoding);
+ _headerEncoding = headerEncoding;
+ }
+
+ /// <summary>
+ /// Initializes the context with the buffered request data
+ /// </summary>
+ /// <param name="requestData">The request data buffer positioned at the begining of the request data</param>
+ /// <param name="connectionId">The unique id of the connection</param>
+ internal void Prepare(VnMemoryStream requestData, string connectionId)
+ {
+ Request.Prepare(requestData, connectionId, _headerEncoding);
+ //Message id is set after the request parses the incoming message
+ Response.Prepare(Request.MessageId);
+ }
+
+ void IReusable.Prepare()
+ {
+ (Request as IReusable).Prepare();
+ (Response as IReusable).Prepare();
+ }
+
+ bool IReusable.Release()
+ {
+ return (Request as IReusable).Release() & (Response as IReusable).Release();
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Server/FBMListener.cs b/Net.Messaging.FBM/src/Server/FBMListener.cs
new file mode 100644
index 0000000..1d50953
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/FBMListener.cs
@@ -0,0 +1,389 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMListener.cs
+*
+* FBMListener.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.IO;
+using System.Buffers;
+using System.Threading;
+using System.Net.WebSockets;
+using System.Threading.Tasks;
+
+using VNLib.Utils;
+using VNLib.Utils.IO;
+using VNLib.Utils.Async;
+using VNLib.Utils.Memory;
+using VNLib.Utils.Extensions;
+using VNLib.Utils.Memory.Caching;
+using VNLib.Plugins.Essentials;
+
+namespace VNLib.Net.Messaging.FBM.Server
+{
+
+ /// <summary>
+ /// Method delegate for processing FBM messages from an <see cref="FBMListener"/>
+ /// when messages are received
+ /// </summary>
+ /// <param name="context">The message/connection context</param>
+ /// <param name="userState">The state parameter passed on client connected</param>
+ /// <param name="cancellationToken">A token that reflects the state of the listener</param>
+ /// <returns>A <see cref="Task"/> that resolves when processing is complete</returns>
+ public delegate Task RequestHandler(FBMContext context, object? userState, CancellationToken cancellationToken);
+
+ /// <summary>
+ /// A FBM protocol listener. Listens for messages on a <see cref="WebSocketSession"/>
+ /// and raises events on requests.
+ /// </summary>
+ public class FBMListener
+ {
+ class ListeningSession
+ {
+ private readonly ReusableStore<FBMContext> CtxStore;
+ private readonly CancellationTokenSource Cancellation;
+ private readonly CancellationTokenRegistration Registration;
+ private readonly FBMListenerSessionParams Params;
+
+
+ public readonly object? UserState;
+
+ public readonly SemaphoreSlim ResponseLock;
+
+ public readonly WebSocketSession Socket;
+
+ public readonly RequestHandler OnRecieved;
+
+ public CancellationToken CancellationToken => Cancellation.Token;
+
+
+ public ListeningSession(WebSocketSession session, RequestHandler onRecieved, in FBMListenerSessionParams args, object? userState)
+ {
+ Params = args;
+ Socket = session;
+ UserState = userState;
+ OnRecieved = onRecieved;
+
+ //Create cancellation and register for session close
+ Cancellation = new();
+ Registration = session.Token.Register(Cancellation.Cancel);
+
+
+ ResponseLock = new(1);
+ CtxStore = ObjectRental.CreateReusable(ContextCtor);
+ }
+
+ private FBMContext ContextCtor() => new(Params.MaxHeaderBufferSize, Params.ResponseBufferSize, Params.HeaderEncoding);
+
+ /// <summary>
+ /// Cancels any pending opreations relating to the current session
+ /// </summary>
+ public void CancelSession()
+ {
+ Cancellation.Cancel();
+
+ //If dispose happens without any outstanding requests, we can dispose the session
+ if (_counter == 0)
+ {
+ CleanupInternal();
+ }
+ }
+
+ private void CleanupInternal()
+ {
+ Registration.Dispose();
+ CtxStore.Dispose();
+ Cancellation.Dispose();
+ ResponseLock.Dispose();
+ }
+
+
+ private uint _counter;
+
+ /// <summary>
+ /// Rents a new <see cref="FBMContext"/> instance from the pool
+ /// and increments the counter
+ /// </summary>
+ /// <returns>The rented instance</returns>
+ /// <exception cref="ObjectDisposedException"></exception>
+ public FBMContext RentContext()
+ {
+
+ if (Cancellation.IsCancellationRequested)
+ {
+ throw new ObjectDisposedException("The instance has been disposed");
+ }
+
+ //Rent context
+ FBMContext ctx = CtxStore.Rent();
+ //Increment counter
+ Interlocked.Increment(ref _counter);
+
+ return ctx;
+ }
+
+ /// <summary>
+ /// Returns a previously rented context to the pool
+ /// and decrements the counter. If the session has been
+ /// cancelled, when the counter reaches 0, cleanup occurs
+ /// </summary>
+ /// <param name="ctx">The context to return</param>
+ public void ReturnContext(FBMContext ctx)
+ {
+ //Return the context
+ CtxStore.Return(ctx);
+
+ uint current = Interlocked.Decrement(ref _counter);
+
+ //No more contexts in use, dispose internals
+ if (Cancellation.IsCancellationRequested && current == 0)
+ {
+ ResponseLock.Dispose();
+ Cancellation.Dispose();
+ CtxStore.Dispose();
+ }
+ }
+ }
+
+ public const int SEND_SEMAPHORE_TIMEOUT_MS = 10 * 1000;
+
+ private readonly IUnmangedHeap Heap;
+
+ /// <summary>
+ /// Raised when a response processing error occured
+ /// </summary>
+ public event EventHandler<Exception>? OnProcessError;
+
+ /// <summary>
+ /// Creates a new <see cref="FBMListener"/> instance ready for
+ /// processing connections
+ /// </summary>
+ /// <param name="heap">The heap to alloc buffers from</param>
+ public FBMListener(IUnmangedHeap heap)
+ {
+ Heap = heap;
+ }
+
+ /// <summary>
+ /// Begins listening for requests on the current websocket until
+ /// a close message is received or an error occurs
+ /// </summary>
+ /// <param name="wss">The <see cref="WebSocketSession"/> to receive messages on</param>
+ /// <param name="handler">The callback method to handle incoming requests</param>
+ /// <param name="args">The arguments used to configured this listening session</param>
+ /// <param name="userState">A state parameter</param>
+ /// <returns>A <see cref="Task"/> that completes when the connection closes</returns>
+ public async Task ListenAsync(WebSocketSession wss, RequestHandler handler, FBMListenerSessionParams args, object? userState)
+ {
+ ListeningSession session = new(wss, handler, args, userState);
+ //Alloc a recieve buffer
+ using IMemoryOwner<byte> recvBuffer = Heap.DirectAlloc<byte>(args.RecvBufferSize);
+
+ //Init new queue for dispatching work
+ AsyncQueue<VnMemoryStream> workQueue = new(true, true);
+
+ //Start a task to process the queue
+ Task queueWorker = QueueWorkerDoWork(workQueue, session);
+
+ try
+ {
+ //Listen for incoming messages
+ while (true)
+ {
+ //Receive a message
+ ValueWebSocketReceiveResult result = await wss.ReceiveAsync(recvBuffer.Memory);
+ //If a close message has been received, we can gracefully exit
+ if (result.MessageType == WebSocketMessageType.Close)
+ {
+ //Return close message
+ await wss.CloseSocketAsync(WebSocketCloseStatus.NormalClosure, "Goodbye");
+ //break listen loop
+ break;
+ }
+ //create buffer for storing data
+ VnMemoryStream request = new(Heap);
+ //Copy initial data
+ request.Write(recvBuffer.Memory.Span[..result.Count]);
+ //Streaming read
+ while (!result.EndOfMessage)
+ {
+ //Read more data
+ result = await wss.ReceiveAsync(recvBuffer.Memory);
+ //Make sure the request is small enough to buffer
+ if (request.Length + result.Count > args.MaxMessageSize)
+ {
+ //dispose the buffer
+ request.Dispose();
+ //close the socket with a message too big
+ await wss.CloseSocketAsync(WebSocketCloseStatus.MessageTooBig, "Buffer space exceeded for message. Goodbye");
+ //break listen loop
+ goto Exit;
+ }
+ //write to buffer
+ request.Write(recvBuffer.Memory.Span[..result.Count]);
+ }
+ //Make sure data is available
+ if (request.Length == 0)
+ {
+ request.Dispose();
+ continue;
+ }
+ //reset buffer position
+ _ = request.Seek(0, SeekOrigin.Begin);
+ //Enqueue the request
+ await workQueue.EnqueueAsync(request);
+ }
+
+ Exit:
+ ;
+ }
+ finally
+ {
+ session.CancelSession();
+ await queueWorker.ConfigureAwait(false);
+ }
+ }
+
+ private async Task QueueWorkerDoWork(AsyncQueue<VnMemoryStream> queue, ListeningSession session)
+ {
+ try
+ {
+ while (true)
+ {
+ //Get work from queue
+ VnMemoryStream request = await queue.DequeueAsync(session.CancellationToken);
+ //Process request without waiting
+ _ = ProcessAsync(request, session).ConfigureAwait(false);
+ }
+ }
+ catch (OperationCanceledException)
+ { }
+ finally
+ {
+ //Cleanup any queued requests
+ while (queue.TryDequeue(out VnMemoryStream? stream))
+ {
+ stream.Dispose();
+ }
+ }
+ }
+
+ private async Task ProcessAsync(VnMemoryStream data, ListeningSession session)
+ {
+ //Rent a new request object
+ FBMContext context = session.RentContext();
+ try
+ {
+ //Prepare the request/response
+ context.Prepare(data, session.Socket.SocketID);
+
+ if ((context.Request.ParseStatus & HeaderParseError.InvalidId) > 0)
+ {
+ OnProcessError?.Invoke(this, new FBMException($"Invalid messageid {context.Request.MessageId}, message length {data.Length}"));
+ return;
+ }
+
+ //Check parse status flags
+ if ((context.Request.ParseStatus & HeaderParseError.HeaderOutOfMem) > 0)
+ {
+ OnProcessError?.Invoke(this, new OutOfMemoryException("Packet received with not enough space to store headers"));
+ }
+ //Determine if request is an out-of-band message
+ else if (context.Request.MessageId == Helpers.CONTROL_FRAME_MID)
+ {
+ //Process control frame
+ await ProcessOOBAsync(context);
+ }
+ else
+ {
+ //Invoke normal message handler
+ await session.OnRecieved.Invoke(context, session.UserState, session.CancellationToken);
+ }
+
+ //Get response data
+ await using IAsyncMessageReader messageEnumerator = await context.Response.GetResponseDataAsync(session.CancellationToken);
+
+ //Load inital segment
+ if (await messageEnumerator.MoveNextAsync() && !session.CancellationToken.IsCancellationRequested)
+ {
+ ValueTask sendTask;
+
+ //Syncrhonize access to send data because we may need to stream data to the client
+ await session.ResponseLock.WaitAsync(SEND_SEMAPHORE_TIMEOUT_MS);
+ try
+ {
+ do
+ {
+ bool eof = !messageEnumerator.DataRemaining;
+
+ //Send first segment
+ sendTask = session.Socket.SendAsync(messageEnumerator.Current, WebSocketMessageType.Binary, eof);
+
+ /*
+ * 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.
+ */
+
+ //Move to next segment
+ if (!await messageEnumerator.MoveNextAsync())
+ {
+ break;
+ }
+
+ //Await previous send
+ await sendTask;
+
+ } while (true);
+ }
+ finally
+ {
+ //release semaphore
+ session.ResponseLock.Release();
+ }
+
+ await sendTask;
+ }
+
+ //No data to send
+ }
+ catch (Exception ex)
+ {
+ OnProcessError?.Invoke(this, ex);
+ }
+ finally
+ {
+ session.ReturnContext(context);
+ }
+ }
+
+ /// <summary>
+ /// Processes an out-of-band request message (internal communications)
+ /// </summary>
+ /// <param name="outOfBandContext">The <see cref="FBMContext"/> containing the OOB message</param>
+ /// <returns>A <see cref="Task"/> that completes when the operation completes</returns>
+ protected virtual Task ProcessOOBAsync(FBMContext outOfBandContext)
+ {
+ return Task.CompletedTask;
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Server/FBMListenerBase.cs b/Net.Messaging.FBM/src/Server/FBMListenerBase.cs
new file mode 100644
index 0000000..3e9fde2
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/FBMListenerBase.cs
@@ -0,0 +1,113 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMListenerBase.cs
+*
+* FBMListenerBase.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+using VNLib.Utils.Logging;
+using VNLib.Utils.Memory;
+using VNLib.Plugins.Essentials;
+
+namespace VNLib.Net.Messaging.FBM.Server
+{
+ /// <summary>
+ /// Provides a simple base class for an <see cref="FBMListener"/>
+ /// processor
+ /// </summary>
+ public abstract class FBMListenerBase
+ {
+
+ /// <summary>
+ /// The initialzied listener
+ /// </summary>
+ protected FBMListener? Listener { get; private set; }
+ /// <summary>
+ /// A provider to write log information to
+ /// </summary>
+ protected abstract ILogProvider Log { get; }
+
+ /// <summary>
+ /// Initializes the <see cref="FBMListener"/>
+ /// </summary>
+ /// <param name="heap">The heap to alloc buffers from</param>
+ protected void InitListener(IUnmangedHeap heap)
+ {
+ Listener = new(heap);
+ //Attach service handler
+ Listener.OnProcessError += Listener_OnProcessError;
+ }
+
+ /// <summary>
+ /// A single event service routine for servicing errors that occur within
+ /// the listener loop
+ /// </summary>
+ /// <param name="sender"></param>
+ /// <param name="e">The exception that was raised</param>
+ protected virtual void Listener_OnProcessError(object? sender, Exception e)
+ {
+ //Write the error to the log
+ Log.Error(e);
+ }
+
+ private async Task OnReceivedAsync(FBMContext context, object? userState, CancellationToken token)
+ {
+ try
+ {
+ await ProcessAsync(context, userState, token);
+ }
+ catch (OperationCanceledException)
+ {
+ Log.Debug("Async operation cancelled");
+ }
+ catch(Exception ex)
+ {
+ Log.Error(ex);
+ }
+ }
+
+ /// <summary>
+ /// Begins listening for requests on the current websocket until
+ /// a close message is received or an error occurs
+ /// </summary>
+ /// <param name="wss">The <see cref="WebSocketSession"/> to receive messages on</param>
+ /// <param name="args">The arguments used to configured this listening session</param>
+ /// <param name="userState">A state token to use for processing events for this connection</param>
+ /// <returns>A <see cref="Task"/> that completes when the connection closes</returns>
+ public virtual async Task ListenAsync(WebSocketSession wss, FBMListenerSessionParams args, object? userState)
+ {
+ _ = Listener ?? throw new InvalidOperationException("The listener has not been intialized");
+ await Listener.ListenAsync(wss, OnReceivedAsync, args, userState);
+ }
+
+ /// <summary>
+ /// A method to service an incoming message
+ /// </summary>
+ /// <param name="context">The context containing the message to be serviced</param>
+ /// <param name="userState">A state token passed on client connected</param>
+ /// <param name="exitToken">A token that reflects the state of the listener</param>
+ /// <returns>A task that completes when the message has been serviced</returns>
+ protected abstract Task ProcessAsync(FBMContext context, object? userState, CancellationToken exitToken);
+ }
+}
diff --git a/Net.Messaging.FBM/src/Server/FBMListenerSessionParams.cs b/Net.Messaging.FBM/src/Server/FBMListenerSessionParams.cs
new file mode 100644
index 0000000..c327475
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/FBMListenerSessionParams.cs
@@ -0,0 +1,62 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMListenerSessionParams.cs
+*
+* FBMListenerSessionParams.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System.Text;
+
+namespace VNLib.Net.Messaging.FBM.Server
+{
+ /// <summary>
+ /// Represents a configuration structure for an <see cref="FBMListener"/>
+ /// listening session
+ /// </summary>
+ public readonly struct FBMListenerSessionParams
+ {
+ /// <summary>
+ /// The size of the buffer to use while reading data from the websocket
+ /// in the listener loop
+ /// </summary>
+ public readonly int RecvBufferSize { get; init; }
+ /// <summary>
+ /// The size of the character buffer to store FBMheader values in
+ /// the <see cref="FBMRequestMessage"/>
+ /// </summary>
+ public readonly int MaxHeaderBufferSize { get; init; }
+ /// <summary>
+ /// The size of the internal message response buffer when
+ /// not streaming
+ /// </summary>
+ public readonly int ResponseBufferSize { get; init; }
+ /// <summary>
+ /// The FMB message header character encoding
+ /// </summary>
+ public readonly Encoding HeaderEncoding { get; init; }
+
+ /// <summary>
+ /// The absolute maxium size (in bytes) message to process before
+ /// closing the websocket connection. This value should be negotiaed
+ /// by clients or hard-coded to avoid connection issues
+ /// </summary>
+ public readonly int MaxMessageSize { get; init; }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Server/FBMRequestMessage.cs b/Net.Messaging.FBM/src/Server/FBMRequestMessage.cs
new file mode 100644
index 0000000..ed36571
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/FBMRequestMessage.cs
@@ -0,0 +1,196 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMRequestMessage.cs
+*
+* FBMRequestMessage.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Text;
+using System.Buffers;
+using System.Text.Json;
+using System.Collections.Generic;
+
+using VNLib.Utils;
+using VNLib.Utils.IO;
+using VNLib.Utils.Memory;
+using VNLib.Utils.Extensions;
+using VNLib.Utils.Memory.Caching;
+
+namespace VNLib.Net.Messaging.FBM.Server
+{
+ /// <summary>
+ /// Represents a client request message to be serviced
+ /// </summary>
+ public sealed class FBMRequestMessage : IReusable
+ {
+ private readonly List<KeyValuePair<HeaderCommand, ReadOnlyMemory<char>>> _headers;
+ private readonly int HeaderCharBufferSize;
+ /// <summary>
+ /// Creates a new resusable <see cref="FBMRequestMessage"/>
+ /// </summary>
+ /// <param name="headerBufferSize">The size of the buffer to alloc during initialization</param>
+ internal FBMRequestMessage(int headerBufferSize)
+ {
+ HeaderCharBufferSize = headerBufferSize;
+ _headers = new();
+ }
+
+ private char[]? _headerBuffer;
+
+ /// <summary>
+ /// The ID of the current message
+ /// </summary>
+ public int MessageId { get; private set; }
+ /// <summary>
+ /// Gets the underlying socket-id fot the current connection
+ /// </summary>
+ public string? ConnectionId { get; private set; }
+ /// <summary>
+ /// The raw request message, positioned to the body section of the message data
+ /// </summary>
+ public VnMemoryStream? RequestBody { get; private set; }
+ /// <summary>
+ /// A collection of headers for the current request
+ /// </summary>
+ public IReadOnlyList<KeyValuePair<HeaderCommand, ReadOnlyMemory<char>>> Headers => _headers;
+ /// <summary>
+ /// Status flags set during the message parsing
+ /// </summary>
+ public HeaderParseError ParseStatus { get; private set; }
+ /// <summary>
+ /// The message body data as a <see cref="ReadOnlySpan{T}"/>
+ /// </summary>
+ public ReadOnlySpan<byte> BodyData => Helpers.GetRemainingData(RequestBody!);
+
+ /// <summary>
+ /// Determines if the current message is considered a control frame
+ /// </summary>
+ public bool IsControlFrame { get; private set; }
+
+ /// <summary>
+ /// Prepares the request to be serviced
+ /// </summary>
+ /// <param name="vms">The request data packet</param>
+ /// <param name="socketId">The unique id of the connection</param>
+ /// <param name="dataEncoding">The data encoding used to decode header values</param>
+ internal void Prepare(VnMemoryStream vms, string socketId, Encoding dataEncoding)
+ {
+ //Store request body
+ RequestBody = vms;
+ //Store message id
+ MessageId = Helpers.GetMessageId(Helpers.ReadLine(vms));
+ //Check mid for control frame
+ if(MessageId == Helpers.CONTROL_FRAME_MID)
+ {
+ IsControlFrame = true;
+ }
+ else if (MessageId < 1)
+ {
+ ParseStatus |= HeaderParseError.InvalidId;
+ return;
+ }
+
+ ConnectionId = socketId;
+
+ //sliding window over remaining data from internal buffer
+ ForwardOnlyMemoryWriter<char> writer = new(_headerBuffer);
+
+ //Accumulate headers
+ while (true)
+ {
+ //Read the next line from the current stream
+ ReadOnlySpan<byte> line = Helpers.ReadLine(vms);
+ if (line.IsEmpty)
+ {
+ //Done reading headers
+ break;
+ }
+ HeaderCommand cmd = Helpers.GetHeaderCommand(line);
+ //Get header value
+ ERRNO charsRead = Helpers.GetHeaderValue(line, writer.Remaining.Span, dataEncoding);
+ if (charsRead < 0)
+ {
+ //Out of buffer space
+ ParseStatus |= HeaderParseError.HeaderOutOfMem;
+ break;
+ }
+ else if (!charsRead)
+ {
+ //Invalid header
+ ParseStatus |= HeaderParseError.InvalidHeaderRead;
+ }
+ else
+ {
+ //Store header as a read-only sequence
+ _headers.Add(new(cmd, writer.Remaining[..(int)charsRead]));
+ //Shift buffer window
+ writer.Advance(charsRead);
+ }
+ }
+ }
+
+ /// <summary>
+ /// Deserializes the request body into a new specified object type
+ /// </summary>
+ /// <typeparam name="T">The type of the object to deserialize</typeparam>
+ /// <param name="jso">The <see cref="JsonSerializerOptions"/> to use while deserializing data</param>
+ /// <returns>The deserialized object from the request body</returns>
+ /// <exception cref="JsonException"></exception>
+ public T? DeserializeBody<T>(JsonSerializerOptions? jso = default)
+ {
+ return BodyData.IsEmpty ? default : BodyData.AsJsonObject<T>(jso);
+ }
+ /// <summary>
+ /// Gets a <see cref="JsonDocument"/> of the request body
+ /// </summary>
+ /// <returns>The parsed <see cref="JsonDocument"/> if parsed successfully, or null otherwise</returns>
+ /// <exception cref="JsonException"></exception>
+ public JsonDocument? GetBodyAsJson()
+ {
+ Utf8JsonReader reader = new(BodyData);
+ return JsonDocument.TryParseValue(ref reader, out JsonDocument? jdoc) ? jdoc : default;
+ }
+
+ void IReusable.Prepare()
+ {
+ ParseStatus = HeaderParseError.None;
+ //Alloc header buffer
+ _headerBuffer = ArrayPool<char>.Shared.Rent(HeaderCharBufferSize);
+ }
+
+
+ bool IReusable.Release()
+ {
+ //Dispose the request message
+ RequestBody?.Dispose();
+ RequestBody = null;
+ //Clear headers before freeing buffer
+ _headers.Clear();
+ //Free header-buffer
+ ArrayPool<char>.Shared.Return(_headerBuffer!);
+ _headerBuffer = null;
+ ConnectionId = null;
+ MessageId = 0;
+ IsControlFrame = false;
+ return true;
+ }
+ }
+}
diff --git a/Net.Messaging.FBM/src/Server/FBMResponseMessage.cs b/Net.Messaging.FBM/src/Server/FBMResponseMessage.cs
new file mode 100644
index 0000000..1536c99
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/FBMResponseMessage.cs
@@ -0,0 +1,226 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: FBMResponseMessage.cs
+*
+* FBMResponseMessage.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Text;
+using System.Threading;
+using System.Threading.Tasks;
+
+using VNLib.Net.Http;
+using VNLib.Utils.IO;
+using VNLib.Utils.Extensions;
+using VNLib.Utils.Memory.Caching;
+using VNLib.Net.Messaging.FBM.Client;
+
+namespace VNLib.Net.Messaging.FBM.Server
+{
+
+ /// <summary>
+ /// Represents an FBM request response container.
+ /// </summary>
+ public sealed class FBMResponseMessage : IReusable, IFBMMessage
+ {
+ internal FBMResponseMessage(int internalBufferSize, Encoding headerEncoding)
+ {
+ _headerAccumulator = new HeaderDataAccumulator(internalBufferSize);
+ _headerEncoding = headerEncoding;
+ _messageEnumerator = new(this);
+ }
+
+ private readonly MessageSegmentEnumerator _messageEnumerator;
+ private readonly ISlindingWindowBuffer<byte> _headerAccumulator;
+ private readonly Encoding _headerEncoding;
+
+ private IAsyncMessageBody? _messageBody;
+
+ ///<inheritdoc/>
+ public int MessageId { get; private set; }
+
+ void IReusable.Prepare()
+ {
+ (_headerAccumulator as HeaderDataAccumulator)!.Prepare();
+ }
+
+ bool IReusable.Release()
+ {
+ //Release header accumulator
+ _headerAccumulator.Close();
+
+ _messageBody = null;
+
+ MessageId = 0;
+
+ return true;
+ }
+
+ /// <summary>
+ /// Initializes the response message with the specified message-id
+ /// to respond with
+ /// </summary>
+ /// <param name="messageId">The message id of the context to respond to</param>
+ internal void Prepare(int messageId)
+ {
+ //Reset accumulator when message id is written
+ _headerAccumulator.Reset();
+ //Write the messageid to the begining of the headers buffer
+ MessageId = messageId;
+ _headerAccumulator.Append((byte)HeaderCommand.MessageId);
+ _headerAccumulator.Append(messageId);
+ _headerAccumulator.WriteTermination();
+ }
+
+ ///<inheritdoc/>
+ public void WriteHeader(HeaderCommand header, ReadOnlySpan<char> value)
+ {
+ WriteHeader((byte)header, value);
+ }
+ ///<inheritdoc/>
+ public void WriteHeader(byte header, ReadOnlySpan<char> value)
+ {
+ _headerAccumulator.WriteHeader(header, value, _headerEncoding);
+ }
+
+ ///<inheritdoc/>
+ public void WriteBody(ReadOnlySpan<byte> body, ContentType contentType = ContentType.Binary)
+ {
+ //Append content type header
+ WriteHeader(HeaderCommand.ContentType, HttpHelpers.GetContentTypeString(contentType));
+ //end header segment
+ _headerAccumulator.WriteTermination();
+ //Write message body
+ _headerAccumulator.Append(body);
+ }
+
+ /// <summary>
+ /// Sets the response message body
+ /// </summary>
+ /// <param name="messageBody">The <see cref="IAsyncMessageBody"/> to stream data from</param>
+ /// <exception cref="InvalidOperationException"></exception>
+ public void AddMessageBody(IAsyncMessageBody messageBody)
+ {
+ if(_messageBody != null)
+ {
+ throw new InvalidOperationException("The message body is already set");
+ }
+
+ //Append message content type header
+ WriteHeader(HeaderCommand.ContentType, HttpHelpers.GetContentTypeString(messageBody.ContentType));
+
+ //end header segment
+ _headerAccumulator.WriteTermination();
+
+ //Store message body
+ _messageBody = messageBody;
+
+ }
+
+ /// <summary>
+ /// Gets the internal message body enumerator and prepares the message for sending
+ /// </summary>
+ /// <param name="cancellationToken">A cancellation token</param>
+ /// <returns>A value task that returns the message body enumerator</returns>
+ internal async ValueTask<IAsyncMessageReader> GetResponseDataAsync(CancellationToken cancellationToken)
+ {
+ //try to buffer as much data in the header segment first
+ if(_messageBody?.RemainingSize > 0 && _headerAccumulator.RemainingSize > 0)
+ {
+ //Read data from the message
+ int read = await _messageBody.ReadAsync(_headerAccumulator.RemainingBuffer, cancellationToken);
+ //Advance accumulator to the read bytes
+ _headerAccumulator.Advance(read);
+ }
+ //return reusable enumerator
+ return _messageEnumerator;
+ }
+
+ private class MessageSegmentEnumerator : IAsyncMessageReader
+ {
+ private readonly FBMResponseMessage _message;
+
+ bool HeadersRead;
+
+ public MessageSegmentEnumerator(FBMResponseMessage message)
+ {
+ _message = message;
+ }
+
+ public ReadOnlyMemory<byte> Current { get; private set; }
+
+ public bool DataRemaining { get; private set; }
+
+ public async ValueTask<bool> MoveNextAsync()
+ {
+ //Attempt to read header segment first
+ if (!HeadersRead)
+ {
+ //Set the accumulated buffer
+ Current = _message._headerAccumulator.AccumulatedBuffer;
+
+ //Update data remaining flag
+ DataRemaining = _message._messageBody?.RemainingSize > 0;
+
+ //Set headers read flag
+ HeadersRead = true;
+
+ return true;
+ }
+ else if (_message._messageBody?.RemainingSize > 0)
+ {
+ //Use the header buffer as the buffer for the message body
+ Memory<byte> buffer = _message._headerAccumulator.Buffer;
+
+ //Read body segment
+ int read = await _message._messageBody.ReadAsync(buffer);
+
+ //Update data remaining flag
+ DataRemaining = _message._messageBody.RemainingSize > 0;
+
+ if (read > 0)
+ {
+ //Store the read segment
+ Current = buffer[..read];
+ return true;
+ }
+ }
+ return false;
+ }
+
+ public async ValueTask DisposeAsync()
+ {
+ //Clear current segment
+ Current = default;
+
+ //Reset headers read flag
+ HeadersRead = false;
+
+ //Dispose the message body if set
+ if (_message._messageBody != null)
+ {
+ await _message._messageBody.DisposeAsync();
+ }
+ }
+ }
+ }
+
+}
diff --git a/Net.Messaging.FBM/src/Server/HeaderDataAccumulator.cs b/Net.Messaging.FBM/src/Server/HeaderDataAccumulator.cs
new file mode 100644
index 0000000..78b378d
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/HeaderDataAccumulator.cs
@@ -0,0 +1,89 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: HeaderDataAccumulator.cs
+*
+* HeaderDataAccumulator.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Buffers;
+
+using VNLib.Utils.IO;
+
+
+namespace VNLib.Net.Messaging.FBM.Server
+{
+ /// <summary>
+ /// Reusable sliding window impl
+ /// </summary>
+ internal class HeaderDataAccumulator : ISlindingWindowBuffer<byte>
+ {
+ private readonly int BufferSize;
+
+ private byte[]? _memHandle;
+
+ public HeaderDataAccumulator(int bufferSize)
+ {
+ BufferSize = bufferSize;
+ }
+
+ ///<inheritdoc/>
+ public int WindowStartPos { get; private set; }
+ ///<inheritdoc/>
+ public int WindowEndPos { get; private set; }
+ ///<inheritdoc/>
+ public Memory<byte> Buffer => _memHandle.AsMemory();
+
+ ///<inheritdoc/>
+ public void Advance(int count) => WindowEndPos += count;
+
+ ///<inheritdoc/>
+ public void AdvanceStart(int count) => WindowEndPos += count;
+
+ ///<inheritdoc/>
+ public void Reset()
+ {
+ WindowStartPos = 0;
+ WindowEndPos = 0;
+ }
+
+ /// <summary>
+ /// Allocates the internal message buffer
+ /// </summary>
+ public void Prepare()
+ {
+ _memHandle ??= ArrayPool<byte>.Shared.Rent(BufferSize);
+ }
+
+ ///<inheritdoc/>
+ public void Close()
+ {
+ Reset();
+
+ if (_memHandle != null)
+ {
+ //Return the buffer to the pool
+ ArrayPool<byte>.Shared.Return(_memHandle);
+ _memHandle = null;
+ }
+ }
+ }
+
+}
diff --git a/Net.Messaging.FBM/src/Server/IAsyncMessageBody.cs b/Net.Messaging.FBM/src/Server/IAsyncMessageBody.cs
new file mode 100644
index 0000000..5566520
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/IAsyncMessageBody.cs
@@ -0,0 +1,57 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: IAsyncMessageBody.cs
+*
+* IAsyncMessageBody.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Threading;
+using System.Threading.Tasks;
+
+using VNLib.Net.Http;
+
+namespace VNLib.Net.Messaging.FBM
+{
+ /// <summary>
+ /// A disposable message body container for asynchronously reading a variable length message body
+ /// </summary>
+ public interface IAsyncMessageBody : IAsyncDisposable
+ {
+ /// <summary>
+ /// The message body content type
+ /// </summary>
+ ContentType ContentType { get; }
+
+ /// <summary>
+ /// The number of bytes remaining to be read from the message body
+ /// </summary>
+ int RemainingSize { get; }
+
+ /// <summary>
+ /// Reads the next chunk of data from the message body
+ /// </summary>
+ /// <param name="buffer">The buffer to copy output data to</param>
+ /// <param name="token">A token to cancel the operation</param>
+ /// <returns></returns>
+ ValueTask<int> ReadAsync(Memory<byte> buffer, CancellationToken token = default);
+ }
+
+}
diff --git a/Net.Messaging.FBM/src/Server/IAsyncMessageReader.cs b/Net.Messaging.FBM/src/Server/IAsyncMessageReader.cs
new file mode 100644
index 0000000..b2abe8d
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/IAsyncMessageReader.cs
@@ -0,0 +1,42 @@
+/*
+* Copyright (c) 2022 Vaughn Nugent
+*
+* Library: VNLib
+* Package: VNLib.Net.Messaging.FBM
+* File: IAsyncMessageReader.cs
+*
+* IAsyncMessageReader.cs is part of VNLib.Net.Messaging.FBM which is part of the larger
+* VNLib collection of libraries and utilities.
+*
+* VNLib.Net.Messaging.FBM is free software: you can redistribute it and/or modify
+* it under the terms of the GNU Affero General Public License as
+* published by the Free Software Foundation, either version 3 of the
+* License, or (at your option) any later version.
+*
+* VNLib.Net.Messaging.FBM is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU Affero General Public License for more details.
+*
+* You should have received a copy of the GNU Affero General Public License
+* along with this program. If not, see https://www.gnu.org/licenses/.
+*/
+
+using System;
+using System.Collections.Generic;
+
+
+namespace VNLib.Net.Messaging.FBM.Server
+{
+ /// <summary>
+ /// Internal message body reader/enumerator for FBM messages
+ /// </summary>
+ internal interface IAsyncMessageReader : IAsyncEnumerator<ReadOnlyMemory<byte>>
+ {
+ /// <summary>
+ /// A value that indicates if there is data remaining after a
+ /// </summary>
+ bool DataRemaining { get; }
+ }
+
+}
diff --git a/Net.Messaging.FBM/src/Server/readme.md b/Net.Messaging.FBM/src/Server/readme.md
new file mode 100644
index 0000000..489e58f
--- /dev/null
+++ b/Net.Messaging.FBM/src/Server/readme.md
@@ -0,0 +1,35 @@
+# VNLib.Net.Messaging.FBM.Server
+
+Fixed Buffer Messaging Protocol server library. High performance statful messaging
+protocol built on top of HTTP web-sockets. Low/no allocation, completely asynchronous
+while providing a TPL API. This library provides a simple asynchronous request/response
+architecture to web-sockets. This was initially designed to provide an alternative to
+complete HTTP request/response overhead, but allow a simple control flow for work
+across a network.
+
+Messages consist of a 4 byte message id, a collection of headers, and a message body.
+The first 4 bytes of a message is the ID (for normal messages a signed integer greater than 0),
+0 is reserved for error conditions, and negative numbers are reserved for internal
+messages. Headers are identified by a single byte, followed by a variable length UTF8
+encoded character sequence, followed by a termination of 0xFF, 0xF1 (may change).
+
+### Message structure
+ 4 byte positive (signed 32-bit integer) message id
+ 2 byte termination
+ 1 byte header-id
+ variable length UTF8 value
+ 2 byte termination
+ -- other headers --
+ 2 byte termination (extra termination, ie: empty header)
+ variable length payload
+ (end of message is the end of the payload)
+
+
+XML Documentation is or will be provided for almost all public exports. APIs are intended to
+be sensibly public and immutable to allow for easy extensability (via extension methods). I
+often use extension libraries to provide additional functionality. (See cache library)
+
+This library is likely a niche use case, and is probably not for everyone. Unless you care
+about reasonably efficient high frequency request/response messaging, this probably isnt
+for you. This library provides a reasonable building block for distributed lock mechanisms
+and small data caching. \ No newline at end of file
diff --git a/Net.Messaging.FBM/src/VNLib.Net.Messaging.FBM.csproj b/Net.Messaging.FBM/src/VNLib.Net.Messaging.FBM.csproj
new file mode 100644
index 0000000..9440e69
--- /dev/null
+++ b/Net.Messaging.FBM/src/VNLib.Net.Messaging.FBM.csproj
@@ -0,0 +1,33 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <TargetFramework>net6.0</TargetFramework>
+ <Authors>Vaughn Nugent</Authors>
+ <Version>1.0.1.1</Version>
+ <Copyright>Copyright © 2022 Vaughn Nugent</Copyright>
+ <Nullable>enable</Nullable>
+ <PackageProjectUrl>www.vaughnnugent.com/resources</PackageProjectUrl>
+ <AnalysisLevel>latest-all</AnalysisLevel>
+ <SignAssembly>True</SignAssembly>
+ <AssemblyOriginatorKeyFile>\\vaughnnugent.com\Internal\Folder Redirection\vman\Documents\Programming\Software\StrongNameingKey.snk</AssemblyOriginatorKeyFile>
+ </PropertyGroup>
+
+
+ <ItemGroup>
+ <PackageReference Include="ErrorProne.NET.CoreAnalyzers" Version="0.1.2">
+ <PrivateAssets>all</PrivateAssets>
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ </PackageReference>
+ <PackageReference Include="ErrorProne.NET.Structs" Version="0.1.2">
+ <PrivateAssets>all</PrivateAssets>
+ <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
+ </PackageReference>
+ </ItemGroup>
+
+ <ItemGroup>
+ <ProjectReference Include="..\..\Essentials\src\VNLib.Plugins.Essentials.csproj" />
+ <ProjectReference Include="..\..\Http\src\VNLib.Net.Http.csproj" />
+ <ProjectReference Include="..\..\Utils\src\VNLib.Utils.csproj" />
+ </ItemGroup>
+
+</Project>