-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from HarryCordewener/Initial_MSDP
Initial msdp draft
- Loading branch information
Showing
20 changed files
with
420 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
namespace TelnetNegotiationCore.Functional | ||
|
||
open System.Text | ||
|
||
module MSDPLibrary = | ||
type Trigger = | ||
| NULL = 0uy | ||
| MSDP_VAR = 1uy | ||
| MSDP_VAL = 2uy | ||
| MSDP_TABLE_OPEN = 3uy | ||
| MSDP_TABLE_CLOSE = 4uy | ||
| MSDP_ARRAY_OPEN = 5uy | ||
| MSDP_ARRAY_CLOSE = 6uy | ||
|
||
[<TailCall>] | ||
let private MSDPScanTailRec (root: obj, array: seq<byte>, encoding: Encoding) = | ||
let rec scan accRoot accArray = | ||
if Seq.length accArray = 0 then (accRoot, accArray) | ||
else | ||
match accArray |> Seq.head with | ||
| 1uy -> | ||
let key = encoding.GetString(accArray |> Seq.skip(1) |> Seq.takeWhile(fun x -> x <> byte Trigger.MSDP_VAL) |> Array.ofSeq) | ||
let (calculatedValue, leftoverArray) = scan root (accArray |> Seq.skip(1) |> Seq.skipWhile(fun x -> x <> byte Trigger.MSDP_VAL)) | ||
scan ((accRoot :?> Map<string, obj>).Add(key, calculatedValue)) leftoverArray | ||
| 2uy -> | ||
if accRoot :? Map<string, obj> then | ||
scan (Map<string, obj> []) (accArray |> Seq.skip(1)) | ||
elif accRoot :? List<obj> then | ||
let (calculatedValue, leftoverArray) = scan (Map<string, obj> []) (accArray |> Seq.skip(1)) | ||
scan ((accRoot :?> List<obj>) @ [calculatedValue]) leftoverArray | ||
else | ||
scan accRoot (accArray |> Seq.skip(1)) | ||
| 3uy -> | ||
scan (Map<string, obj> []) (accArray |> Seq.skip(1)) | ||
| 5uy -> | ||
scan (List<obj>.Empty) (accArray |> Seq.skip(1)) | ||
| 4uy | 6uy -> | ||
(accRoot, accArray |> Seq.skip(1)) | ||
| _ -> | ||
(encoding.GetString(accArray |> Seq.takeWhile(fun x -> x > 6uy) |> Array.ofSeq), accArray |> Seq.skipWhile(fun x -> x > 6uy)) | ||
scan root array | ||
|
||
let public MSDPScan(array: seq<byte>, encoding) = | ||
let (result, _) = MSDPScanTailRec(Map<string,obj> [], array, encoding) | ||
result |
12 changes: 12 additions & 0 deletions
12
TelnetNegotiationCore.Functional/TelnetNegotiationCore.Functional.fsproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="MSDPLibrary.fs" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Serilog; | ||
|
||
namespace TelnetNegotiationCore.UnitTests | ||
{ | ||
public class BaseTest | ||
{ | ||
static BaseTest() | ||
{ | ||
var log = new LoggerConfiguration() | ||
.Enrich.FromLogContext() | ||
.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] ({TelnetMode}) {Message:lj}{NewLine}{Exception}") | ||
.MinimumLevel.Verbose() | ||
.CreateLogger(); | ||
|
||
Log.Logger = log; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using NUnit.Framework; | ||
using Serilog; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Text.Json; | ||
using TelnetNegotiationCore.Models; | ||
|
||
namespace TelnetNegotiationCore.UnitTests | ||
{ | ||
[TestFixture] | ||
public class MSDPTests : BaseTest | ||
{ | ||
static Encoding encoding = Encoding.ASCII; | ||
|
||
[TestCaseSource(nameof(FSharpTestSequences))] | ||
public void TestFSharp(byte[] testcase, dynamic expectedObject) | ||
{ | ||
var result = Functional.MSDPLibrary.MSDPScan(testcase, encoding); | ||
Log.Logger.Information("Serialized: {NewLine} {Serialized}", JsonSerializer.Serialize(result)); | ||
Assert.AreEqual(JsonSerializer.Serialize(expectedObject), JsonSerializer.Serialize(result)); | ||
} | ||
|
||
public static IEnumerable<TestCaseData> FSharpTestSequences | ||
{ | ||
get | ||
{ | ||
yield return new TestCaseData((byte[])[ | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("LIST"), | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("COMMANDS")], | ||
new { LIST = "COMMANDS" }); | ||
|
||
yield return new TestCaseData((byte[])[ | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("COMMANDS"), | ||
(byte)Trigger.MSDP_VAL, | ||
(byte)Trigger.MSDP_ARRAY_OPEN, | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("LIST"), | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("REPORT"), | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("SEND"), | ||
(byte)Trigger.MSDP_ARRAY_CLOSE], | ||
new { COMMANDS = (string[])["LIST", "REPORT", "SEND"] }); | ||
|
||
yield return new TestCaseData((byte[])[ | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("ROOM"), | ||
(byte)Trigger.MSDP_VAL, | ||
(byte)Trigger.MSDP_TABLE_OPEN, | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("VNUM"), | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("6008"), | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("NAME"), | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("The Forest clearing"), | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("AREA"), | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("Haon Dor"), | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("EXITS"), | ||
(byte)Trigger.MSDP_VAL, | ||
(byte)Trigger.MSDP_TABLE_OPEN, | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("n"), | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("6011"), | ||
(byte)Trigger.MSDP_VAR, | ||
.. encoding.GetBytes("e"), | ||
(byte)Trigger.MSDP_VAL, | ||
.. encoding.GetBytes("6012"), | ||
(byte)Trigger.MSDP_TABLE_CLOSE, | ||
(byte)Trigger.MSDP_TABLE_CLOSE | ||
], | ||
new { ROOM = new { AREA = "Haon Dor", EXITS = new { e = "6012", n = "6011" }, NAME = "The Forest clearing", VNUM = "6008" } }); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.