FrmServerSet.cs 5.48 KB
//using GeneralLib;
using NetLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
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();
        }

        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", "");
            
            ShowStartState(VoteServer.IsStart);
            LoadSession();
        }

        private string StrOnLine = "Online";
        private string StrClosed = "Closed";

        public void LoadSession()
        {
            dgvClient.Rows.Clear();
            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[colNo.Name].Value = VoteServer.GetMeetNo(name);
                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));
                ShowSessionRosterCount(name);
            }
        }

        public void ShowSessionRosterCount(string name)
        {
            DataGridViewRow row = FindRowByName(name);
            if (row == null)
                return;
            int count = 0;
            if (VoteServer.DicRosterMeet.ContainsKey(name))
                count = VoteServer.DicRosterMeet[name].Count - 2;
            row.Cells[colRosterCount.Name].Value = count;
        }

        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 = VoteServer.GetMeetNo(name);
                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;
        }
        
        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 chkShowPwd_CheckedChanged(object sender, EventArgs e)
        {
            txtPwd.PasswordChar = chkShowPwd.Checked ? '\0' : '*';
        }
        
        private void btnSaveRoster_Click(object sender, EventArgs e)
        {
            btnSaveRoster.Enabled = false;
            this.Cursor = Cursors.WaitCursor;
            RosterList.RosterLoad.SaveRosterMeet();
            btnSaveRoster.Enabled = true;
            this.Cursor = Cursors.Default;
        }
    }
}