//using GeneralLib; using NetLib; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Windows.Forms; namespace SunVoteARSPPT { public partial class FrmServerSet : Form { public FrmServerSet() { InitializeComponent(); pnlInfo.Dock = DockStyle.Fill; } private void FrmServerSet_Load(object sender, EventArgs e) { //加载配置 //INIControl ini = INIControl.GetInstances(GlobalInfo.SYSTEM_CONFIG_PATH); //ControlOper.TrySetNumericUpDownValue(numPort, ini.ReadInt("System", "ServerPort", 8866)); //txtPwd.Text = ini.ReadString("System", "ServerPwd", ""); rbtViewConnect.Checked = true; rbtView_CheckedChanged(null, null); ShowStartState(VoteServer.IsStart); LoadSession(); IsStartVote = false;//ToDo:取ARS状态 ShowVoteState(); } private string StrOnLine = "Online"; private string StrClosed = "Closed"; public void LoadSession() { foreach (var v in VoteServer.DicSessionName) { NetSession session = v.Value; string name = v.Key; int i = dgvClient.Rows.Add(); DataGridViewRow row = dgvClient.Rows[i]; row.Tag = session.ID; row.Cells[colName.Name].Value = name; row.Cells[colIP.Name].Value = session.RemoteEndPoint.Address; row.Cells[colPort.Name].Value = session.RemoteEndPoint.Port; ShowRowMeetConnect(row, VoteServer.IsOnline(session)); } } void ShowRowMeetConnect(DataGridViewRow row, bool isOnline) { row.Cells[colState.Name].Value = isOnline ? StrOnLine : StrClosed; row.Cells[colState.Name].Style.ForeColor = isOnline ? Color.Blue : Color.Red; row.Cells[colState.Name].Style.SelectionForeColor = row.Cells[colState.Name].Style.ForeColor; } public void SessionChange(NetSession session, bool isOnline) { string name = VoteServer.GetSessionPar(session, VoteServer.KeyName); DataGridViewRow row = null; if (name.Length > 0) row = FindRowByName(name); else row = FindRowByID(session.ID); if (row == null) { if (!isOnline) return; int i = dgvClient.Rows.Add(); row = dgvClient.Rows[i]; row.Tag = session.ID; row.Cells[colNo.Name].Value = i + 1; row.Cells[colName.Name].Value = name; } if (isOnline) { row.Cells[colIP.Name].Value = session.RemoteEndPoint.Address; row.Cells[colPort.Name].Value = session.RemoteEndPoint.Port; } ShowRowMeetConnect(row, isOnline); } DataGridViewRow FindRowByName(string name) { DataGridViewRow res = null; foreach (DataGridViewRow v in dgvClient.Rows) { if ((v.Cells[colName.Name].Value + "") == name) { res = v; break; } } return res; } DataGridViewRow FindRowByID(string ID) { DataGridViewRow res = null; foreach (DataGridViewRow v in dgvClient.Rows) { if ((v.Tag + "") == ID) { res = v; break; } } return res; } void ShowStartState(bool isStart) { btnStart.Enabled = !isStart; btnStop.Enabled = isStart; pnlSet.Enabled = !isStart; btnResetClear.Enabled = !isStart; } void ShowVoteState() { btnStartVote.Enabled = !IsStartVote; btnStopVote.Enabled = IsStartVote; } private void btnStart_Click(object sender, EventArgs e) { //保存配置 //GlobalInfo.sysConfig.WriteSysConfig("System", "ServerPort", numPort.Value); //GlobalInfo.sysConfig.WriteSysConfig("System", "ServerPwd", txtPwd.Text); bool res = VoteServer.Start((int)numPort.Value, txtPwd.Text); ShowStartState(res); } private void btnStop_Click(object sender, EventArgs e) { VoteServer.Stop(); ShowStartState(false); } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void btnResetClear_Click(object sender, EventArgs e) { //MessageBox.Show(""); } private void btnSendImg_Click(object sender, EventArgs e) { Image img = VoteServer.GetScreenImg(); VoteServer.SendImg(img); } private void chkShowPwd_CheckedChanged(object sender, EventArgs e) { txtPwd.PasswordChar = chkShowPwd.Checked ? '\0' : '*'; } private bool IsStartVote = false; private void btnStartVote_Click(object sender, EventArgs e) { IsStartVote = true; VoteServer.StartVote(IsStartVote); ShowVoteState(); } private void btnStopVote_Click(object sender, EventArgs e) { IsStartVote = false; VoteServer.StartVote(IsStartVote); ShowVoteState(); } private void rbtView_CheckedChanged(object sender, EventArgs e) { dgvClient.Visible = rbtViewConnect.Checked; pnlInfo.Visible = rbtViewData.Checked; } public void ShowMsg(NetSession session, string msg) { msg = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + VoteServer.GetSessionPar(session, VoteServer.KeyName) + " " + msg; lstInfo.Items.Insert(0, msg); } private void lstInfo_SelectedIndexChanged(object sender, EventArgs e) { txtMsgSel.Text = lstInfo.Text; } } }