Skip to content

Commit

Permalink
Make MSSP Serializable. Use a function for the whole object rather th…
Browse files Browse the repository at this point in the history
…an each item. Speeds up everything, and allows the same functionality.
  • Loading branch information
HarryCordewener committed Jan 2, 2024
1 parent 2ed86f5 commit b39a797
Show file tree
Hide file tree
Showing 16 changed files with 237 additions and 234 deletions.
6 changes: 3 additions & 3 deletions TelnetNegotiationCore.TestClient/MockClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Net;
using System.Net.Sockets;
using System.Text;
using TelnetNegotiationCore.Interpretors;
using TelnetNegotiationCore.Interpreters;

namespace TelnetNegotiationCore.TestClient
{
Expand Down Expand Up @@ -38,7 +38,7 @@ public Task SignalNAWS(int height, int width)
public void Handle(TcpClient client)
{
int port = -1;
TelnetInterpretor? telnet = null;
TelnetInterpreter? telnet = null;

try
{
Expand All @@ -48,7 +48,7 @@ public void Handle(TcpClient client)
using var input = new StreamReader(stream);
using var output = new StreamWriter(stream) { AutoFlush = true };

telnet = new TelnetInterpretor(TelnetInterpretor.TelnetMode.Client, _Logger.ForContext<TelnetInterpretor>())
telnet = new TelnetInterpreter(TelnetInterpreter.TelnetMode.Client, _Logger.ForContext<TelnetInterpreter>())
{
CallbackOnSubmit = WriteBack,
CallbackNegotiation = (x) => WriteToOutputStream(x, output),
Expand Down
22 changes: 11 additions & 11 deletions TelnetNegotiationCore.TestServer/MockServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using TelnetNegotiationCore.Interpretors;
using TelnetNegotiationCore.Interpreters;
using TelnetNegotiationCore.Models;

namespace TelnetNegotiationCore.TestServer
Expand All @@ -18,7 +18,7 @@ public class MockServer
readonly TcpListener server = null;
readonly ILogger _Logger;

private readonly ConcurrentDictionary<int, TelnetInterpretor> Clients = new();
private readonly ConcurrentDictionary<int, TelnetInterpreter> Clients = new();

public MockServer(string ip, int port, ILogger logger = null)
{
Expand Down Expand Up @@ -66,7 +66,7 @@ public Task SignalNAWS(int height, int width)
public void HandleDevice(object obj)
{
int port = -1;
TelnetInterpretor telnet = null;
TelnetInterpreter telnet = null;
TcpClient client = null;

try
Expand All @@ -78,22 +78,22 @@ public void HandleDevice(object obj)
using var input = new StreamReader(stream);
using var output = new StreamWriter(stream) { AutoFlush = true };

telnet = new TelnetInterpretor(TelnetInterpretor.TelnetMode.Server, _Logger.ForContext<TelnetInterpretor>())
telnet = new TelnetInterpreter(TelnetInterpreter.TelnetMode.Server, _Logger.ForContext<TelnetInterpreter>())
{
CallbackOnSubmit = WriteBack,
CallbackNegotiation = (x) => WriteToOutputStream(x, output),
NAWSCallback = SignalNAWS,
CharsetOrder = new[] { Encoding.GetEncoding("utf-8"), Encoding.GetEncoding("iso-8859-1") }
}
.RegisterMSSPConfig(new MSSPConfig
.RegisterMSSPConfig(() => new MSSPConfig
{
Name = () => "My Telnet Negotiated Server",
UTF_8 = () => true,
Gameplay = () => new[] { "ABC", "DEF" },
Extended = new Dictionary<string, Func<dynamic>>
Name = "My Telnet Negotiated Server",
UTF_8 = true,
Gameplay = new[] { "ABC", "DEF" },
Extended = new Dictionary<string, dynamic>
{
{ "Foo", () => "Bar"},
{ "Baz", () => new [] {"Moo", "Meow" }}
{ "Foo", "Bar"},
{ "Baz", new [] {"Moo", "Meow" }}
}
}).Validate().Build().GetAwaiter().GetResult();

Expand Down
38 changes: 19 additions & 19 deletions TelnetNegotiationCore.UnitTests/CHARSETTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelnetNegotiationCore.Interpretors;
using TelnetNegotiationCore.Interpreters;
using TelnetNegotiationCore.Models;

namespace TelnetNegotiationCore.UnitTests
Expand Down Expand Up @@ -43,23 +43,23 @@ public void Setup()
Log.Logger = log;
}

[TestCaseSource(nameof(ServerCHARSETSequences), Category = nameof(TelnetInterpretor.TelnetMode.Server))]
[TestCaseSource(nameof(ServerCHARSETSequences), Category = nameof(TelnetInterpreter.TelnetMode.Server))]
public async Task ServerEvaluationCheck(IEnumerable<byte[]> clientSends, IEnumerable<byte[]> serverShouldRespondWith, IEnumerable<Encoding> currentEncoding)
{
var server_ti = await new TelnetInterpretor(TelnetInterpretor.TelnetMode.Server)
var server_ti = await new TelnetInterpreter(TelnetInterpreter.TelnetMode.Server)
{
CallbackNegotiation = ServerWriteBackToNegotiate,
CallbackOnSubmit = WriteBackToOutput,
CallbackOnByte = (x, y) => Task.CompletedTask,
}.RegisterMSSPConfig(new MSSPConfig
}.RegisterMSSPConfig(() => new MSSPConfig
{
Name = () => "My Telnet Negotiated Server",
UTF_8 = () => true,
Gameplay = () => new[] { "ABC", "DEF" },
Extended = new Dictionary<string, Func<dynamic>>
Name = "My Telnet Negotiated Server",
UTF_8 = true,
Gameplay = new[] { "ABC", "DEF" },
Extended = new Dictionary<string,dynamic>
{
{ "Foo", () => "Bar"},
{ "Baz", () => new [] {"Moo", "Meow" }}
{ "Foo", "Bar"},
{ "Baz", new [] {"Moo", "Meow" }}
}
}).Validate().Build();

Expand All @@ -79,24 +79,24 @@ public async Task ServerEvaluationCheck(IEnumerable<byte[]> clientSends, IEnumer
}
}

[TestCaseSource(nameof(ClientCHARSETSequences), Category = nameof(TelnetInterpretor.TelnetMode.Client))]
[TestCaseSource(nameof(ClientCHARSETSequences), Category = nameof(TelnetInterpreter.TelnetMode.Client))]
public async Task ClientEvaluationCheck(IEnumerable<byte[]> serverSends, IEnumerable<byte[]> serverShouldRespondWith, IEnumerable<Encoding> currentEncoding)
{
var client_ti = await new TelnetInterpretor(TelnetInterpretor.TelnetMode.Client)
var client_ti = await new TelnetInterpreter(TelnetInterpreter.TelnetMode.Client)
{
CallbackNegotiation = ClientWriteBackToNegotiate,
CallbackOnSubmit = WriteBackToOutput,
CallbackOnByte = (x, y) => Task.CompletedTask,
CharsetOrder = new[] { Encoding.GetEncoding("utf-8"), Encoding.GetEncoding("iso-8859-1") }
}.RegisterMSSPConfig(new MSSPConfig
}.RegisterMSSPConfig(() => new MSSPConfig
{
Name = () => "My Telnet Negotiated Client",
UTF_8 = () => true,
Gameplay = () => new[] { "ABC", "DEF" },
Extended = new Dictionary<string, Func<dynamic>>
Name = "My Telnet Negotiated Client",
UTF_8 = true,
Gameplay = new[] { "ABC", "DEF" },
Extended = new Dictionary<string, dynamic>
{
{ "Foo", () => "Bar"},
{ "Baz", () => new [] {"Moo", "Meow" }}
{ "Foo", "Bar"},
{ "Baz", new [] {"Moo", "Meow" }}
}
}).Validate().Build();

Expand Down
20 changes: 10 additions & 10 deletions TelnetNegotiationCore.UnitTests/CreateDotGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
using TelnetNegotiationCore.Interpretors;
using TelnetNegotiationCore.Interpreters;
using TelnetNegotiationCore.Models;

namespace TelnetNegotiationCore.UnitTests
Expand All @@ -26,7 +26,7 @@ public CreateDotGraph()
[Test]
public async Task WriteClientDotGraph()
{
var telnet = await new TelnetInterpretor(TelnetInterpretor.TelnetMode.Client, _Logger.ForContext<TelnetInterpretor>())
var telnet = await new TelnetInterpreter(TelnetInterpreter.TelnetMode.Client, _Logger.ForContext<TelnetInterpreter>())
{
CallbackOnSubmit = WriteBack,
CallbackNegotiation = WriteToOutputStream,
Expand All @@ -41,22 +41,22 @@ public async Task WriteClientDotGraph()
[Test]
public async Task WriteServerDotGraph()
{
var telnet = await new TelnetInterpretor(TelnetInterpretor.TelnetMode.Server, _Logger.ForContext<TelnetInterpretor>())
var telnet = await new TelnetInterpreter(TelnetInterpreter.TelnetMode.Server, _Logger.ForContext<TelnetInterpreter>())
{
CallbackOnSubmit = WriteBack,
CallbackNegotiation = WriteToOutputStream,
NAWSCallback = SignalNAWS,
CharsetOrder = new[] { Encoding.GetEncoding("utf-8"), Encoding.GetEncoding("iso-8859-1") }
}
.RegisterMSSPConfig(new MSSPConfig
.RegisterMSSPConfig(() => new MSSPConfig
{
Name = () => "My Telnet Negotiated Server",
UTF_8 = () => true,
Gameplay = () => new[] { "ABC", "DEF" },
Extended = new Dictionary<string, Func<dynamic>>
Name = "My Telnet Negotiated Server",
UTF_8 = true,
Gameplay = new[] { "ABC", "DEF" },
Extended = new Dictionary<string, dynamic>
{
{ "Foo", () => "Bar"},
{ "Baz", () => new [] {"Moo", "Meow" }}
{ "Foo", "Bar"},
{ "Baz", new [] {"Moo", "Meow" }}
}
}).Validate().Build();

Expand Down
38 changes: 19 additions & 19 deletions TelnetNegotiationCore.UnitTests/TTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelnetNegotiationCore.Interpretors;
using TelnetNegotiationCore.Interpreters;
using TelnetNegotiationCore.Models;

namespace TelnetNegotiationCore.UnitTests
{
[TestFixture]
public class TTypeTests
{
private TelnetInterpretor _server_ti;
private TelnetInterpretor _client_ti;
private TelnetInterpreter _server_ti;
private TelnetInterpreter _client_ti;
private byte[] _negotiationOutput;

private Task WriteBackToOutput(byte[] arg1, Encoding arg2) => throw new NotImplementedException();
Expand All @@ -36,37 +36,37 @@ public async Task Setup()

Log.Logger = log;

_server_ti = await new TelnetInterpretor(TelnetInterpretor.TelnetMode.Server)
_server_ti = await new TelnetInterpreter(TelnetInterpreter.TelnetMode.Server)
{
CallbackNegotiation = WriteBackToNegotiate,
CallbackOnSubmit = WriteBackToOutput,
CallbackOnByte = (x, y) => Task.CompletedTask,
}.RegisterMSSPConfig(new MSSPConfig
}.RegisterMSSPConfig(() => new MSSPConfig
{
Name = () => "My Telnet Negotiated Server",
UTF_8 = () => true,
Gameplay = () => new[] { "ABC", "DEF" },
Extended = new Dictionary<string, Func<dynamic>>
Name = "My Telnet Negotiated Server",
UTF_8 = true,
Gameplay = new[] { "ABC", "DEF" },
Extended = new Dictionary<string, dynamic>
{
{ "Foo", () => "Bar"},
{ "Baz", () => new [] {"Moo", "Meow" }}
{ "Foo", "Bar"},
{ "Baz", new [] {"Moo", "Meow" }}
}
}).Validate().Build();

_client_ti = await new TelnetInterpretor(TelnetInterpretor.TelnetMode.Client)
_client_ti = await new TelnetInterpreter(TelnetInterpreter.TelnetMode.Client)
{
CallbackNegotiation = WriteBackToNegotiate,
CallbackOnSubmit = WriteBackToOutput,
CallbackOnByte = (x, y) => Task.CompletedTask,
}.RegisterMSSPConfig(new MSSPConfig
}.RegisterMSSPConfig(() => new MSSPConfig
{
Name = () => "My Telnet Negotiated Client",
UTF_8 = () => true,
Gameplay = () => new[] { "ABC", "DEF" },
Extended = new Dictionary<string, Func<dynamic>>
Name = "My Telnet Negotiated Client",
UTF_8 = true,
Gameplay = new[] { "ABC", "DEF" },
Extended = new Dictionary<string, dynamic>
{
{ "Foo", () => "Bar"},
{ "Baz", () => new [] {"Moo", "Meow" }}
{ "Foo", "Bar"},
{ "Baz", new [] {"Moo", "Meow" }}
}
}).Validate().Build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
using Stateless;
using TelnetNegotiationCore.Models;

namespace TelnetNegotiationCore.Interpretors
namespace TelnetNegotiationCore.Interpreters
{
/// <summary>
/// Implements RFC 2066:
///
/// RFC 2066: http://www.faqs.org/rfcs/rfc2066.html
/// </summary>
public partial class TelnetInterpretor
public partial class TelnetInterpreter
{
/// <summary>
/// Internal Charset Byte State
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using Stateless;
using TelnetNegotiationCore.Models;

namespace TelnetNegotiationCore.Interpretors
namespace TelnetNegotiationCore.Interpreters
{
public partial class TelnetInterpretor
public partial class TelnetInterpreter
{
private bool? _doEOR = null;

Expand Down
Loading

0 comments on commit b39a797

Please sign in to comment.