/// <summary> /// 菜单项“过滤本机流量”勾选状态改变时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 过滤本机流量ToolStripMenuItem_CheckedChanged(object sender, EventArgs e) { // 检查监视器是否在工作 if (开始监视ToolStripMenuItem.Text.Equals("开始监视")) { return; } // 暂停列表更新 ConnectionListUpdateTimer.Stop(); new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled); // 去除已有本机流量数据 var removeRows = ConnectionList.Rows.Cast <DataGridViewRow>().Where(row => IPAddress.Parse(row.Cells["SrcAddress"].Value.ToString().Substring(0, row.Cells["SrcAddress"].Value.ToString().LastIndexOf(':'))).Equals(Watcher.Ipv4Address) || IPAddress.Parse(row.Cells["DstAddress"].Value.ToString().Substring(0, row.Cells["DstAddress"].Value.ToString().LastIndexOf(':'))) .Equals(Watcher.Ipv4Address)).ToList(); foreach (var row in removeRows) { ConnectionList.Rows.Remove(row); } // 恢复列表更新 ConnectionListUpdateTimer.Start(); }
/// <summary> /// 禁用与 <see cref="Model.Watcher" /> 相关的控件。 /// </summary> private void DisableWatcherItems() { 启动监视模块ToolStripMenuItem.Text = "启动模块"; 开始监视ToolStripMenuItem.Enabled = false; 阻止此连接ToolStripMenuItem.Enabled = false; BlockList.Rows.Clear(); ConnectionListUpdateTimer.Stop(); new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled); ConnectionList.Rows.Clear(); }
/// <summary> /// 连接列表鼠标按键事件响应方法,暂停列表更新。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void ConnectionList_MouseEvent(object sender, MouseEventArgs e) { if (ConnectionListUpdateTimer.Enabled) { ConnectionListUpdateTimer.Stop(); new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled); } else if (!ConnectionListMenuStrip.Visible) { ConnectionListUpdateTimer.Start(); } }
/// <summary> /// 右键菜单打开后触发的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void ContextMenuStrip_Opened(object sender, EventArgs e) { var source = ((ContextMenuStrip)sender).SourceControl; DataGridView list; switch (source.Name) { case "HostList": list = HostList; break; case "Target1List": list = Target1List; break; case "Target2List": list = Target2List; break; case "ConnectionList": list = ConnectionList; // 菜单打开时暂时停止更新列表 ConnectionListUpdateTimer.Stop(); new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled); break; case "BlockList": list = BlockList; break; default: return; } var index = list.PointToClient(MousePosition).Y / list.ColumnHeadersHeight - 1 + list.FirstDisplayedScrollingRowIndex; if (index >= list.Rows.Count || list.Rows[index].Selected) { return; // 避免屏幕缩放比导致的索引越界 } list.ClearSelection(); list.Rows[index].Selected = true; }
/// <summary> /// 菜单项“启动模块”(监视)单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 启动监视模块ToolStripMenuItem_Click(object sender, EventArgs e) { // 判断工作模式 if (启动监视模块ToolStripMenuItem.Text == "启动模块") { if (!StartupModels(new[] { Watcher })) { MessageBox.Show("模块未能成功初始化,请检查。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (Watcher.CurDevName != "") { MessageBox.Show("模块已成功设置。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 启动所有模块ToolStripMenuItem.Text = "重启所有模块"; 启动监视模块ToolStripMenuItem.Text = "停止模块"; 开始监视ToolStripMenuItem.Enabled = true; } } else { // 创建载入界面 var task = new Thread(stop => { Watcher.Stop(); Watcher.Reset(); Watcher.CurDevName = ""; }) { Name = RegisteredThreadName.StopWatcher.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在停止,请稍候", task); loading.ShowDialog(); // 等待结果 new WaitTimeoutChecker(30000).ThreadSleep(500, () => { var msg = MessagePipe.GetNextOutMessage(task); switch (msg) { case Message.NoAvailableMessage: return(true); case Message.TaskOut: return(false); default: throw new Exception($"无效的消息类型:{msg}"); } }); // 模块已停止 MessagePipe.ClearAllMessage(task); MessageBox.Show("模块已停止。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 启动监视模块ToolStripMenuItem.Text = "启动模块"; 开始监视ToolStripMenuItem.Enabled = false; ConnectionListUpdateTimer.Stop(); new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled); ConnectionList.Rows.Clear(); // TODO:新增模块时请更新此处的代码 if (启动扫描模块ToolStripMenuItem.Text == "启动模块" && 启动毒化模块ToolStripMenuItem.Text == "启动模块") { 启动所有模块ToolStripMenuItem.Text = "启动所有模块"; } } }
/// <summary> /// 连接列表右键菜单关闭时触发的事件,重新开始更新列表。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void ConnectionListMenuStrip_Closed(object sender, ToolStripDropDownClosedEventArgs e) { ConnectionListUpdateTimer.Start(); }
/// <summary> /// 菜单项“开始毒化”单击时的事件。 /// </summary> /// <param name="sender">触发事件的控件对象。</param> /// <param name="e">事件的参数。</param> private void 开始监视ToolStripMenuItem_Click(object sender, EventArgs e) { if (开始监视ToolStripMenuItem.Text == "开始监视") { // 创建载入界面 var task = new Thread(watching => { try { Watcher.StartWatching(); } catch (ThreadAbortException) { // 用户中止了操作 Watcher.Stop(); } }) { Name = RegisteredThreadName.StartWatching.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在启动监视,请稍候", task); loading.ShowDialog(); // 等待结果 var result = Message.NoAvailableMessage; new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); // 用户取消 if (result == Message.UserCancel) { MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskCancel, task)); new WaitTimeoutChecker(30000).ThreadSleep(500, () => (result = MessagePipe.GetNextOutMessage(task)) == Message.NoAvailableMessage); switch (result) { case Message.TaskAborted: return; case Message.TaskOut: break; case Message.TaskNotFound: MessageBox.Show("未能找到指定名称的工作线程。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return; default: throw new Exception($"无效的消息类型:{result}"); } } MessagePipe.ClearAllMessage(task); MessageBox.Show("监视工作已启动。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); ConnectionListUpdateTimer.Start(); 开始监视ToolStripMenuItem.Text = "停止监视"; } else { // 创建载入界面 var task = new Thread(stop => { Watcher.Stop(); }) { Name = RegisteredThreadName.StopWatching.ToString() }; MessagePipe.SendInMessage(new KeyValuePair <Message, Thread>(Message.TaskIn, task)); var loading = new Loading("正在停止,请稍候", task); loading.ShowDialog(); // 等待结果 new WaitTimeoutChecker(30000).ThreadSleep(500, () => { var msg = MessagePipe.GetNextOutMessage(task); switch (msg) { case Message.NoAvailableMessage: return(true); case Message.TaskOut: return(false); default: throw new Exception($"无效的消息类型:{msg}"); } }); // 模块已停止 MessagePipe.ClearAllMessage(task); ConnectionListUpdateTimer.Stop(); new WaitTimeoutChecker(30000).ThreadSleep(100, () => ConnectionListUpdateTimer.Enabled); ConnectionList.Rows.Clear(); MessageBox.Show("监视工作已停止。", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information); 开始监视ToolStripMenuItem.Text = "开始监视"; } }