using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using LYFZ; using HPSocketCS.Extended; namespace LYFZ.CloudLoginServer { public partial class frmMain : LYFZ.ComponentLibrary.BaseContentsFormMain { public frmMain() { InitializeComponent(); this.btnSaveSet.Click += btnSaveSet_Click; this.Load += frmMain_Load; this.btnStart.Click += btnStart_Click; this.btnStop.Click += btnStop_Click; this.TraceListenerLog = new HPSocketCS.Extended.CustomTraceListener(this.txtTraceListenerLog); this.NetServer.TraceListenerLog = this.TraceListenerLog; this.NetServer.EventSerializationDataRequest += NetServer_EventSerializationDataRequest; ServerDataReturn = new LYFZ.CloudServerData.CloudLoginServerReturnProcess(this.loginServerCon); } LYFZ.CloudServerData.Model.DBConnection NetworkTrafficStatisticsDbConn = new CloudServerData.Model.DBConnection(); public void SetNetworkTrafficStatisticsDbConn() { NetworkTrafficStatisticsDbConn.DataBaseServer = this.loginServerCon.DataBaseServer; NetworkTrafficStatisticsDbConn.DataBaseName = "NetworkTrafficStatistics"; NetworkTrafficStatisticsDbConn.DataBasePort = this.loginServerCon.DataBasePort; NetworkTrafficStatisticsDbConn.DataBaseUser = this.loginServerCon.DataBaseUser; NetworkTrafficStatisticsDbConn.DataBasePass = this.loginServerCon.DataBasePass; } #region 网络通信事件处理 LYFZ.CloudServerData.CloudLoginServerReturnProcess ServerDataReturn = null; /// /// 接收到的客户端请求处理事件 /// /// /// /// /// void NetServer_EventSerializationDataRequest(HPSocketCS.TcpPackServer hp_Server, HPSocketCS.Extended.TcpHeadInfo header, IntPtr connId, byte[] bytes) { TransferRequestCommand Command = (TransferRequestCommand)Convert.ToInt32(header.RequestCommand); DataType MsgDataType = (DataType)header.MsgDataType; ReturnData returnData; try { RequestData requestData = (RequestData)DataSetSerializerDeserialize.ObjectDeserializeDecompress(bytes); HPSocketCS.Extended.RequestParameters reqParameters = requestData.AttachedDataToObject(); string _EnterpriseID = reqParameters.GetRequsetEnterpriseIDParameter(); SetNetworkTrafficStatisticsDbConn(); try { if (this.loginServerCon.NetworkTrafficStatistics) { LYFZ.CloudServerData.DAL.NetworkTrafficRecord.AddNetworkTrafficRecordThreadPool(NetworkTrafficStatisticsDbConn, _EnterpriseID, 1, CloudServerData.DAL.NetworkTrafficRecord.FlowDataType.CloudLogIn, bytes.LongLength,5); } if (LYFZ.CloudServerData.DAL.Enterprise.VerifyEnterpriseID(_EnterpriseID)) { if (MsgDataType == DataType.Serialization) { returnData = ServerDataReturn.SerializerRequestProcessed(hp_Server, header, connId, requestData); } else { returnData = new ReturnData("处理请求指令时失败,非法指令"); } } else { returnData = new ReturnData("你输入企业ID不是有效的企业ID"); } } catch (Exception ex) { returnData = new ReturnData("处理请求指令时出错:" + ex.Message); } if (returnData == null) { returnData = new ReturnData("请求指令失败,没有返回有效的结果"); } returnData.ServerStartTimeStamp = LYFZ.Network.TCPNetworkServer.TCP_NetworkServer.ServerStartTimeStamp; byte[] sendBytes = DataSetSerializerDeserialize.ObjectSerializerCompressionRetBytes(returnData); if (this.loginServerCon.NetworkTrafficStatistics) { LYFZ.CloudServerData.DAL.NetworkTrafficRecord.AddNetworkTrafficRecordThreadPool(NetworkTrafficStatisticsDbConn, _EnterpriseID, 0, CloudServerData.DAL.NetworkTrafficRecord.FlowDataType.CloudLogIn, sendBytes.LongLength,5); } hp_Server.AutoUnpackingAndSend(connId, sendBytes, header.TransportID, MsgDataType, Command); System.GC.Collect(); } catch (Exception ex) { try { returnData = new ReturnData("服务器处理客户端请求时出错:" + ex.Message); returnData.ServerStartTimeStamp = LYFZ.Network.TCPNetworkServer.TCP_NetworkServer.ServerStartTimeStamp; HPSocketCS.Extended.SystemFileLogs.WriteLogs(returnData.ReturnMessage, this.TraceListenerLog); byte[] sendBytes = DataSetSerializerDeserialize.ObjectSerializerCompressionRetBytes(returnData); hp_Server.AutoUnpackingAndSend(connId, sendBytes, header.TransportID, MsgDataType, Command); } catch { } } } #endregion #region 服务器网络 侦听处理 /// /// 服务器运行日志跟踪侦听器 /// HPSocketCS.Extended.CustomTraceListener TraceListenerLog = null; LYFZ.Network.TCPNetworkServer.TCP_NetworkServer NetServer = new Network.TCPNetworkServer.TCP_NetworkServer(); /// /// 开始网络侦听器 /// /// void StartNetworkServer(LYFZ.CloudServerData.Model.CloudLoginConfig _serverCon) { NetServer.StartRun(_serverCon.ServerIP, _serverCon.Port); } /// /// 停止侦听 /// /// /// void btnStop_Click(object sender, EventArgs e) { this.NetServer.StopRun(); this.btnStop.Enabled = this.NetServer.hp_Server.IsStarted; this.btnStart.Enabled = !this.btnStop.Enabled; } /// /// 开始侦听 /// /// /// void btnStart_Click(object sender, EventArgs e) { //测试 // ServerDataReturn.InstallationDatabase(new CloudServerData.Model.AuthorizationVerification(),null); this.loginServerCon.ServerIP = this.txtMonitorIP.Text.Trim(); if (String.IsNullOrEmpty(this.loginServerCon.ServerIP.Trim())) { MessageBoxCustom.Show(LYFZ.WinAPI.UserTips.GetCanNotBeEmptyTips(this.lbMonitorIP.Text)); return; } if (String.IsNullOrEmpty(this.txtMonitorPort.Text.Trim())) { MessageBoxCustom.Show(LYFZ.WinAPI.UserTips.GetCanNotBeEmptyTips(this.lbMonitorPort.Text)); return; } try { this.loginServerCon.Port = Convert.ToUInt16(this.txtMonitorPort.Text.Trim()); } catch { MessageBoxCustom.Show(this.lbMonitorPort.Text + "输入的格式不正确"); return; } LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, BackgroundWorker backgroundWorker) { backgroundWorker.ReportProgress(0, "正在保存..."); if (LYFZ.CloudServerData.SystemConfig.WriteCloudLoginConfig(this.loginServerCon)) { backgroundWorker.ReportProgress(0, "正在启动网络服务..."); try { StartNetworkServer(this.loginServerCon); } catch (Exception ex) { MessageBoxCustom.Show("启动网络服务失败:" + ex.Message, backgroundWorker: backgroundWorker); } } else { MessageBoxCustom.Show("配置保存失败,请重新启动程序后重试", backgroundWorker: backgroundWorker); } }); this.btnStart.Enabled = !this.NetServer.hp_Server.IsStarted; this.btnStop.Enabled = !this.btnStart.Enabled; } #endregion #region 服务器端配置 /// /// 当前服务器配置 /// LYFZ.CloudServerData.Model.CloudLoginConfig loginServerCon = LYFZ.CloudServerData.SystemConfig.ReadCloudLoginConfig(); void frmMain_Load(object sender, EventArgs e) { this.txtDataBaseServer.Text = this.loginServerCon.DataBaseServer; this.txtDataBasePort.Text = this.loginServerCon.DataBasePort.ToString_s(); this.txtDataBaseName.Text = this.loginServerCon.DataBaseName; this.txtDataBaseUser.Text = this.loginServerCon.DataBaseUser; this.txtDataBasePass.Text = this.loginServerCon.DataBasePass; this.txtMonitorIP.Text = this.loginServerCon.ServerIP.Trim(); this.txtMonitorPort.Text = this.loginServerCon.Port.ToString().Trim(); this.chkNetworkTrafficStatistics.Checked = this.loginServerCon.NetworkTrafficStatistics; } void btnSaveSet_Click(object sender, EventArgs e) { this.loginServerCon.DataBaseServer = this.txtDataBaseServer.Text.Trim(); this.loginServerCon.DataBaseName = this.txtDataBaseName.Text.Trim(); this.loginServerCon.DataBaseUser = this.txtDataBaseUser.Text.Trim(); this.loginServerCon.DataBasePass = this.txtDataBasePass.Text.Trim(); this.loginServerCon.NetworkTrafficStatistics = this.chkNetworkTrafficStatistics.Checked; if (String.IsNullOrEmpty(this.txtDataBasePort.Text.Trim())) { MessageBoxCustom.Show(LYFZ.WinAPI.UserTips.GetCanNotBeEmptyTips(this.lbDataBasePort.Text)); return; } try { loginServerCon.DataBasePort = Convert.ToInt32(this.txtDataBasePort.Text.Trim()); } catch { MessageBoxCustom.Show(this.lbDataBasePort.Text + "输入的格式不正确"); return; } if (String.IsNullOrEmpty(loginServerCon.DataBaseServer)) { MessageBoxCustom.Show(LYFZ.WinAPI.UserTips.GetCanNotBeEmptyTips(this.lbDataBaseServer.Text)); return; } if (String.IsNullOrEmpty(loginServerCon.DataBaseName)) { MessageBoxCustom.Show(LYFZ.WinAPI.UserTips.GetCanNotBeEmptyTips(this.lbDataBaseName.Text)); return; } if (String.IsNullOrEmpty(loginServerCon.DataBaseUser)) { MessageBoxCustom.Show(LYFZ.WinAPI.UserTips.GetCanNotBeEmptyTips(this.lbDataBaseUser.Text)); return; } if (String.IsNullOrEmpty(loginServerCon.DataBasePass)) { MessageBoxCustom.Show(LYFZ.WinAPI.UserTips.GetCanNotBeEmptyTips(this.lbDataBasePass.Text)); return; } LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, BackgroundWorker backgroundWorker) { backgroundWorker.ReportProgress(0, "正在保存..."); if (LYFZ.CloudServerData.SystemConfig.WriteCloudLoginConfig(loginServerCon)) { ServerDataReturn = new LYFZ.CloudServerData.CloudLoginServerReturnProcess(this.loginServerCon); MessageBoxCustom.Show("配置保存成功,需要重启服务器后才能生效", backgroundWorker: backgroundWorker); } else { MessageBoxCustom.Show("配置保存失败,请重新启动程序后重试", backgroundWorker: backgroundWorker); } }); //this.Invoke(new LYFZ.WinAPI.DUpdateControl(delegate() //{ // })); } #endregion } }