void ProcessMessage(ExecuteCommandMessage m) { foreach (ServerCommand c in commands) { if (c.Id == m.CommandId) { var result = c.Function(); SendCommandResult(c, m.ExecutionId, result); DebugPrint("Executed Command " + c.Name); } } }
void Run() { // Preallocate messages (not thread-safe) var header = new Header(); var getVariablesMessage = new GetVariablesMessage(); var setVariableValueMessage = new SetVariableValueMessage(); var getCommandsMessage = new GetCommandsMessage(); var executeCommandMessage = new ExecuteCommandMessage(); for (; ;) { try { ReadHeader(header); DebugPrint("Received header: " + header.ToString()); switch (header.Operation) { case ControlOp.GetVariables: ReadMessage(getVariablesMessage); ProcessMessage(getVariablesMessage); break; case ControlOp.SetVariableValue: ReadMessage(setVariableValueMessage); ProcessMessage(setVariableValueMessage); break; case ControlOp.GetCommands: ReadMessage(getCommandsMessage); ProcessMessage(getCommandsMessage); break; case ControlOp.ExecuteCommand: ReadMessage(executeCommandMessage); ProcessMessage(executeCommandMessage); break; default: // Unrecognized operation UnknownMessage unknownMessage = new UnknownMessage(); ReadMessage(unknownMessage); break; } } catch (Exception ex) { DebugPrint("!! " + ex + "\n"); throw; } } }