public static void Deal(string message) { bool success = JsonUtil.ParseStruct(message, out MessageStruct ms); if (success) { switch (ms.type) { case MessageType.setting: SettingsManager.Deal(ms.content); break; case MessageType.action: ActionManager.Deal(ms.content); break; case MessageType.nursery: NurseryManager.Deal(ms.content); break; //case MessageType.log: break; default: LogClerk.Error("Invalid message type"); break; } } }
public bool AddNurseryItem(string pathName) { /* * NurseryMenu.DropDownItems ---------> ToolStripItemCollection<ToolStripItem> * ↑ * ToolStripItem ---> ToolStripSeparator -----------------------------| * ↓ | * ToolStripDropDownItem ----------------------------------------------| */ bool hasThisPS = false; foreach (ToolStripItem item in NurseryMenu.DropDownItems) { if (item.ToolTipText != null && item.ToolTipText.Equals(pathName)) { hasThisPS = true; } } if (!hasThisPS) { var newItem = ActionManager.GetItem(pathName); BeginInvoke(new CrossThreadDelegate(() => { NurseryMenu.DropDownItems.Add(newItem); })); LogClerk.Info($"Added {pathName}"); } return(!hasThisPS); }
public static void Send(object sdu) { MessageStruct?pdu = null; switch (sdu) { case ActionStruct ass: pdu = PDU(MessageType.action, JsonConvert.SerializeObject(ass)); break; case LoggingStruct ls: pdu = PDU(MessageType.logging, JsonConvert.SerializeObject(ls)); break; case NurseryStruct ns: pdu = PDU(MessageType.nursery, JsonConvert.SerializeObject(ns)); break; case SettingStruct ss: pdu = PDU(MessageType.setting, JsonConvert.SerializeObject(ss)); break; default: LogClerk.Error("Invalid message SDU type"); break; } if (pdu != null) { PipeMessenger.Post(JsonConvert.SerializeObject(pdu)); } }
private static void Send(object sdu, SettingCode sc) { SettingStruct?pdu = null; switch (sdu) { case FormSettingStruct fss: pdu = PDU(SettingType.Form, sc, JsonConvert.SerializeObject(fss)); break; case MessageSettingStruct mss: pdu = PDU(SettingType.Message, sc, JsonConvert.SerializeObject(mss)); break; case LogSettingStruct lss: pdu = PDU(SettingType.Log, sc, JsonConvert.SerializeObject(lss)); break; default: LogClerk.Error("Invalid setting message type.", 2); break; } if (pdu != null) { MessageManager.Send(pdu); } }
public static void Deal(string message) { bool success = JsonUtil.ParseStruct <SettingStruct>(message, out SettingStruct ss); if (success) { switch (ss.type) { case SettingType.Form: DealWithForm(ss.content); break; case SettingType.Message: DealWithMessage(ss.content); break; case SettingType.Log: DealWithLog(ss.content); break; default: LogClerk.Error("Invalid setting type"); break; } } }
public static void Deal(string content) { bool success = JsonUtil.ParseStruct <OperationStruct>(content, out OperationStruct os); if (success) { switch (os.type) { case OperationType.Add: OperationClerk.AddProcess(os.pathName); break; case OperationType.Remove: OperationClerk.RemoveProcess(os.pathName); break; case OperationType.Start: OperationClerk.StartProcess(os.pathName, os.args); break; case OperationType.Stop: OperationClerk.StopProcess(os.pathName); break; default: LogClerk.Warn("Invalid OperationType."); break; } } }
private void TheNotifyIcon_MouseClick(object sender, MouseEventArgs e) { MouseEventArgs _e = e as MouseEventArgs; if (_e.Button == MouseButtons.Left) { ActionManager.ShowWindow(); LogClerk.Debug("NotifyIcon MouseClick"); } }
private static void OnProcessExit(object sender, EventArgs e) { Process ps = sender as Process; string pathName = ps.StartInfo.FileName; ProcessStruct pst = Processes[pathName]; pst.isRunning = false; Processes[pathName] = pst; OperationClerk.OnProcessStopped(pathName, FPName[pathName]); LogClerk.Info($"Process {FPName[pathName]} exited"); }
private static void PipeServerThread() { server = new NamedPipeServerStream(pipeName, PipeDirection.InOut, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous, 1024, 1024, null, HandleInheritability.None, PipeAccessRights.ChangePermissions); PipeSecurity ps = server.GetAccessControl(); PipeAccessRule clientRule = new PipeAccessRule( new SecurityIdentifier("S-1-15-2-1"), PipeAccessRights.ReadWrite, AccessControlType.Allow); PipeAccessRule ownerRule = new PipeAccessRule( WindowsIdentity.GetCurrent().Owner, PipeAccessRights.FullControl, AccessControlType.Allow); ps.AddAccessRule(clientRule); ps.AddAccessRule(ownerRule); server.SetAccessControl(ps); LogClerk.Info("Waiting for connection."); server.WaitForConnection(); reader = new StreamReader(server); writer = new StreamWriter(server); LogClerk.Info($"Connection established: {pipeName}"); while (serverRun) { //if (!server.IsConnected) //{ // server.Disconnect(); // LogClerk.Info("Disconnected from FancyToys, waiting for its reconnection."); // server.WaitForConnection(); // LogClerk.Info("FancyToys reconnected"); //} if (server.IsConnected) { string message = reader.ReadLine(); if (!string.IsNullOrEmpty(message)) { MessageManager.Deal(message); } } else { break; } } }
private static void PipeConnectionDetector() { while (true) { if (!server.IsConnected) { server.Disconnect(); LogClerk.Info("Disconnected from FancyToys, waiting for its reconnection."); server.WaitForConnection(); LogClerk.Info("FancyToys reconnected"); } } }
public static bool ParseStruct <T>(string content, out T sdu) { try { sdu = JsonConvert.DeserializeObject <T>(content); return(true); } catch (JsonException e) { LogClerk.Warn($"Deserialize object failed: {e.Message}"); sdu = default; return(false); } }
private static void DealWithConfig(string content) { bool success = JsonUtil.ParseStruct <ConfigStruct>(content, out ConfigStruct cs); if (success) { switch (cs.type) { case ConfigType.FlushTime: InformationClerk.ThreadSleepSpan = cs.flushTime; Console.WriteLine($"Set flush time {InformationClerk.ThreadSleepSpan}"); LogClerk.Info($"Set flush time {InformationClerk.ThreadSleepSpan}"); break; } } }
public bool SetNurseryItemCheckState(string pathName, CheckState checkState) { foreach (ToolStripItem item in NurseryMenu.DropDownItems) { if (item.ToolTipText != null && item.ToolTipText.Equals(pathName)) { BeginInvoke(new CrossThreadDelegate(() => { (item as ToolStripMenuItem).CheckState = checkState; })); LogClerk.Info($"Set {item.Text} {checkState}"); return(true); } } return(false); }
public bool RemoveNurseryItem(string pathName) { foreach (ToolStripItem item in NurseryMenu.DropDownItems) { if (item.ToolTipText != null && item.ToolTipText.Equals(pathName)) { BeginInvoke(new CrossThreadDelegate(() => { NurseryMenu.DropDownItems.Remove(item); })); LogClerk.Info($"Removed {pathName}"); return(true); } } LogClerk.Warn($"Menu item not exit while removing it: {pathName}"); return(false); }
private static void DealWithLog(string sdu) { bool success = JsonUtil.ParseStruct <LogSettingStruct>(sdu, out LogSettingStruct lss); if (success) { switch (lss.type) { case LogSettingType.LogLevel: LogClerk.LogLevel = lss.logLevel; LogClerk.Debug($"Set log level {LogClerk.LogLevel}"); break; case LogSettingType.StdLevel: StdClerk.StdLevel = lss.stdLevel; LogClerk.Debug($"Set std level {StdClerk.StdLevel}"); break; } } }
public static void Deal(string content) { bool success = JsonUtil.ParseStruct <NurseryStruct>(content, out NurseryStruct ns); if (success) { switch (ns.type) { case NurseryType.Setting: DealWithConfig(ns.content); break; case NurseryType.Operation: OperationClerk.Deal(ns.content); break; default: LogClerk.Warn($"Invalid nursery type. {content}"); break; } } }
public static void Send(object sdu) { NurseryStruct?pdu = null; switch (sdu) { case OperationStruct os: pdu = PDU(NurseryType.Operation, JsonConvert.SerializeObject(os)); break; case Dictionary <int, InformationStruct> lis: pdu = PDU(NurseryType.Information, JsonConvert.SerializeObject(lis)); break; default: LogClerk.Warn("Invalid nursery SDU type.", 2); break; } if (pdu != null) { MessageManager.Send(pdu); } }
private static Process AddProcess(string pathName) { Process child = new Process(); child.StartInfo.RedirectStandardOutput = true; child.StartInfo.RedirectStandardError = true; child.StartInfo.FileName = pathName; child.StartInfo.CreateNoWindow = true; child.StartInfo.UseShellExecute = false; child.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(pathName); child.EnableRaisingEvents = true; // 这样才会引发 Process.Exited child.Exited += OnProcessExit; child.OutputDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) { StdClerk.StdOutput((s as Process).ProcessName, e.Data); } }; child.ErrorDataReceived += (s, e) => { if (!string.IsNullOrEmpty(e.Data)) { StdClerk.StdError((s as Process).ProcessName, e.Data); } }; Processes[pathName] = new ProcessStruct { process = child, isRunning = false }; FPName[pathName] = null; LogClerk.Info($"Added {pathName} to local process table."); return(child); }