Skip to content

Commit

Permalink
Basic GMCP support functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
HarryCordewener committed Jan 3, 2024
1 parent 434fa01 commit b79f2c1
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions TelnetNegotiationCore/Interpreters/TelnetGMCPInterpreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class TelnetInterpreter

private StateMachine<State, Trigger> SetupGMCPNegotiation(StateMachine<State, Trigger> tsm)
{
if(Mode == TelnetMode.Server)
if (Mode == TelnetMode.Server)
{
tsm.Configure(State.Do)
.Permit(Trigger.GMCP, State.DoGMCP);
Expand Down Expand Up @@ -93,6 +93,22 @@ private void RegisterGMCPValue(OneOf<byte, Trigger> b)
_gmcpBytes.Add(b.AsT0);
}

public Task SendGMCPCommand(string package, string command) =>
SendGMCPCommand(CurrentEncoding.GetBytes(package), CurrentEncoding.GetBytes(command));

public Task SendGMCPCommand(string package, byte[] command) =>
SendGMCPCommand(CurrentEncoding.GetBytes(package), command);

public async Task SendGMCPCommand(byte[] package, byte[] command)
{
await CallbackNegotiation(
new byte[] { (byte)Trigger.IAC, (byte)Trigger.SB, (byte)Trigger.GMCP }
.Concat(package)
.Concat(CurrentEncoding.GetBytes(" "))
.Concat(command)
.Concat(new byte[] { (byte)Trigger.IAC, (byte)Trigger.SE }).ToArray());
}

/// <summary>
/// Completes the GMCP Negotiation. This is currently assuming a golden path.
/// </summary>
Expand All @@ -101,8 +117,8 @@ private void RegisterGMCPValue(OneOf<byte, Trigger> b)
private async Task CompleteGMCPNegotiation(StateMachine<State, Trigger>.Transition _)
{
var space = CurrentEncoding.GetBytes(" ").First();
var firstSpace = _gmcpBytes.FindIndex(x => x == space);
var packageBytes = _gmcpBytes.Skip(1).Take(firstSpace-1).ToArray();
var firstSpace = _gmcpBytes.FindIndex(x => x == space);
var packageBytes = _gmcpBytes.Skip(1).Take(firstSpace - 1).ToArray();
var rest = _gmcpBytes.Skip(firstSpace + 1).ToArray();
await CallbackOnGMCP((Package: CurrentEncoding.GetString(packageBytes), Info: rest), CurrentEncoding);
}
Expand Down

0 comments on commit b79f2c1

Please sign in to comment.