/// <summary> /// Initialize all buttons /// </summary> private void InitButtons() { this.A = new GenericConsoleButton(ConsoleNameEnum.NES, GenericKeyNameEnum.A, ConsoleKeyStateEnum.NONE); this.B = new GenericConsoleButton(ConsoleNameEnum.NES, GenericKeyNameEnum.B, ConsoleKeyStateEnum.NONE); this.Select = new GenericConsoleButton(ConsoleNameEnum.NES, GenericKeyNameEnum.SELECT, ConsoleKeyStateEnum.NONE); this.Start = new GenericConsoleButton(ConsoleNameEnum.NES, GenericKeyNameEnum.START, ConsoleKeyStateEnum.NONE); this.Left = new GenericConsoleButton(ConsoleNameEnum.NES, GenericKeyNameEnum.LEFTARROW, ConsoleKeyStateEnum.NONE); this.Right = new GenericConsoleButton(ConsoleNameEnum.NES, GenericKeyNameEnum.RIGHTARROW, ConsoleKeyStateEnum.NONE); this.Up = new GenericConsoleButton(ConsoleNameEnum.NES, GenericKeyNameEnum.UPARROW, ConsoleKeyStateEnum.NONE); this.Down = new GenericConsoleButton(ConsoleNameEnum.NES, GenericKeyNameEnum.DOWNARROW, ConsoleKeyStateEnum.NONE); }
/// <summary> /// The event from the TCP client that fires when data is received /// </summary> /// <param name="sender">The sender object</param> /// <param name="e">The message</param> private void TcpClient_DataReceived(object sender, Message e) { // Split all inputs (useful if there are a lot of inputs at the same time) string[] inputs = e.MessageString.ReturnCleanASCII().Split(new char[] { ' ' }, 100, StringSplitOptions.RemoveEmptyEntries); foreach (string input in inputs) { if (!string.IsNullOrWhiteSpace(input)) { IConsoleButton data = ServerManagerHelpers.ParseConsoleButtonFromCSV(null, input.ReturnCleanASCII()); this.ClientDataReceived?.Invoke(this, new ConsoleButtonEventArgs(data)); } } }
/// <summary> /// Parse the button to CSV and send it to the server /// </summary> /// <param name="button">The button to be sent</param> /// <param name="seperator">The character that will separate different requests</param> /// <param name="isWriteLine">If <see cref="Write(string)"/> or <see cref="WriteLine(string)"/> will be used</param> public void SendConsoleButtonAsCSV(IConsoleButton button, char seperator = ' ', bool isWriteLine = true) { if (button != null) { string message = $"{ button.ParseToCSV() }{ seperator }"; if (isWriteLine) { this.WriteLine(message); } else { this.Write(message); } } }
/// <summary> /// The event from the singleton server that fires when a message is received /// </summary> /// <param name="sender">The sender object</param> /// <param name="e">The received message</param> private void SingleTCPServer_DataReceived(object sender, SimpleTCPStandar.Message e) { Task.Run(() => { // Split all inputs (useful if there are a lot of inputs at the same time) string[] inputs = e.MessageString.ReturnCleanASCII().Split(new char[] { ' ' }, 100, StringSplitOptions.RemoveEmptyEntries); foreach (string input in inputs) { if (!string.IsNullOrWhiteSpace(input)) { IConsoleButton data = this.ParseConsoleButtonFromCSV(input.ReturnCleanASCII()); this.ServerDataReceived?.Invoke(this, new ConsoleButtonEventArgs(data)); } } }); }
/// <summary> /// Default constructor /// </summary> /// <param name="button">The <see cref="IConsoleButton"/> that this event will pass</param> public ConsoleButtonEventArgs(IConsoleButton button) { this.ConsoleButton = button; }
/// <summary> /// Parse a <see cref="IConsoleButton"/> to a CSV string /// (ConsoleName,KeyName,KeyState) /// </summary> /// <param name="button">The button that will be parsed</param> public static string ParseToCSV(this IConsoleButton button) { return($"{ button.ConsoleName },{ button.KeyName },{ button.KeyState }"); }