WinConServ.NetMessage TestCommand(string[] args) { WinConServ.NetMessage response = new NetMessage() { data = "Plugin command OK" }; return response; }
public static NetMessage Speak(string[] args) { NetMessage response = new NetMessage(); if (args.Length != 2) { response.data = $"Invalid number of parameters. Usage {args[0]} <text>"; return response; } SpeechSynthesizer speech = new SpeechSynthesizer(); speech.Speak(args[1]); response.data = "OK"; return response; }
public static NetMessage MsgBox(string[] args) { NetMessage response = new NetMessage(); if (args.Length == 3) MessageBox.Show(args[2], args[1]); else if (args.Length == 2) MessageBox.Show(args[1]); else { response.data = $"Invalid number of parameters. Usage: {args[0]} <title> <text>"; return response; } response.data = "OK"; return response; }
public static void ServerLoop() { while (!stop) { WaitForConnection(); Console.WriteLine(Resource.connection_received, RemoteEndpoint); NetMessage msg = ReceiveMessage(); if (msg.type != MessageTypes.ClientHandshake) { Console.WriteLine(Resource.connection_handshake_invalid); NetMessage errorMsg = new NetMessage(); errorMsg.data = "Invalid handshake"; errorMsg.type = MessageTypes.ServerHandshakeResponse; SendMessage(errorMsg); serverSocket.Disconnect(true); continue; } NetMessage response = new NetMessage(); response.type = MessageTypes.ServerHandshakeResponse; response.data = "Connection successful"; SendMessage(response); Program.ShowNotification("Connection from " + RemoteEndpoint.ToString()); while (true) { NetMessage message = ReceiveMessage(); List<string> args = Regex.Matches(message.data, @"[\""].+?[\""]|[^ ]+") .Cast<Match>() .Select(m => m.Value) .ToList(); if (args.Count > 1) { for (int i = 0; i < args.Count; i++) { if (args[i].StartsWith("\"") && args[i].EndsWith("\"")) { args[i] = args[i].Substring(1, args[i].Length - 2); } } } Console.WriteLine(Resource.server_received_message, RemoteEndpoint, message.data); bool valid = false; foreach (var command in serverCommands) { if (command.Method.Name.ToLower() == args[0].ToLower()) { Console.WriteLine($"{args[0]} is a valid command. Waiting for command output..."); response.type = MessageTypes.ServerCommandResponse; response.data = "Valid. Waiting for command output..."; SendMessage(response); response = command(args.ToArray()); response.type = MessageTypes.ServerCommandResponse; Console.WriteLine("{0}: {1}", args[0], response.data); SendMessage(response); valid = true; } } if (valid == false) { response.type = MessageTypes.ServerCommandResponse; response.data = $"{args[0]}: invalid command."; SendMessage(response); } } } }
public static NetMessage ReceiveMessage() { byte[] typeBytes = new byte[4]; byte[] dataBytes = new byte[1024]; serverSocket.Receive(typeBytes, 0); int type = BitConverter.ToInt32(typeBytes, 0); int stringSize = serverSocket.Receive(dataBytes, 0); char[] stringBuffer = new char[stringSize]; _encoding.GetChars(dataBytes, 0, stringSize, stringBuffer, 0); string data = new string(stringBuffer); NetMessage msg = new NetMessage(); msg.type = (MessageTypes)type; msg.data = data; return msg; }
public static void SendMessage(NetMessage message) { if (message.data == "") message.data = "Empty"; try { byte[] typeBytes = BitConverter.GetBytes((int)message.type); byte[] dataBytes = _encoding.GetBytes(message.data); serverSocket.Send(typeBytes, 0); serverSocket.Send(dataBytes, 0); } catch (Exception e) { MessageBox.Show(e.ToString()); } }
private static NetMessage ProcessMessage(NetMessage message) { NetMessage response = new NetMessage {type = 0}; return response; }