namespace LSFileClient { using LSServerService; using LSUtils; using System; using System.Data.SqlClient; using System.Net; using System.Runtime.InteropServices; using System.Threading; using System.Timers; using System.Windows.Forms; public class LSClientMain { private LSSocket m_clientSocket; public bool checkSocketState() { return ((this.m_clientSocket != null) && (this.m_clientSocket.SocketState == 1)); } private void ConnectServer(string ServerIP, string DBName, string isInternet, string IsConsumer) { Exception exception; //uint nPort = uint.Parse(LSIniFile.ReadValue("/Root/ClientSettings", "Port")); //string connectionString = LSIniFile.ReadValue("/Root/ClientSettings", "connectionStr"); //if (connectionString == "") //{ uint nPort = 7859; string connectionString = string.Format("Server={0};Database={1};Persist Security Info=True;User ID=wlms;Password=mymsserver", ServerIP, DBName); if (IsConsumer=="0") connectionString = string.Format("Server=114.116.145.208,14331;Database=lscrm;Persist Security Info=True;User ID=Lserp_crm;Password=lserp_ERP_@#$_123", ServerIP, DBName); //} try { using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand command = new SqlCommand(string.Format("select LanIP,LanPort,InternetIP,InternetPort from p_systemtab ", new object[0]), connection); using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { if (isInternet == "1") { ServerIP = reader["InternetIP"].ToString(); nPort = uint.Parse(reader["InternetPort"].ToString()); } else { ServerIP = reader["LanIP"].ToString(); nPort = uint.Parse(reader["LanPort"].ToString()); } } reader.Close(); } command.Dispose(); connection.Close(); } } catch (Exception exception1) { exception = exception1; //MessageBox.Show("ServerIP:" + ServerIP + ";port:" + nPort + exception1.Message); PubUtils.WriteLog("ConnectServer 连接数据库错误 error: {0}", new object[] { exception }); return; } LSWriteLog.WriteTrace(" 准备连接服务的端口={0},ServerIP={1}", new object[] { nPort, ServerIP }); // 已调整到lsMain程序中进行解析 //try //{ // //ServerIP = Dns.GetHostEntry(ServerIP).AddressList[0].ToString(); // foreach (IPAddress ip in Dns.GetHostEntry(ServerIP).AddressList) // { // if (ip.AddressFamily.ToString() == "InterNetwork") // { // ServerIP = ip.ToString(); // break; // } // } //} //catch(Exception ex) //{ // LSWriteLog.WriteTrace("---------------"+ ex.Message); //} LSWriteLog.WriteTrace(" 解析服务器={0},ServerIP={1}", new object[] { nPort, ServerIP }); try { LSWriteLog.WriteTrace(" 准备连接服务的端口={0},ServerIP={1}", new object[] { nPort, ServerIP }); this.m_clientSocket = new LSSocket(); this.m_clientSocket.Connect(ServerIP, nPort); this.m_clientSocket.ParseReceiveDataEvent += new LSSocket.ParseReceiveDataEventHandle(this.onRevServerSocketData); this.m_clientSocket.SocketCloseEvent += new LSASyncSocket.SocketCloseEventHandle(this.SocketClientCloseDone); } catch (Exception exception2) { exception = exception2; PubUtils.WriteLog("LSClientMain.ConnectServer error: {0}", new object[] { exception }); SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.CONNECT_ERROR, IntPtr.Zero, IntPtr.Zero); } } public void FreeAllResouce() { Thread.Sleep(100); LSGlobleVariable.bNeedExit = true; Thread.Sleep(1000); LSGlobleVariable.hThread.Interrupt(); LSFileManage.dispose(); LSWriteLog.CloseAllFile(); } public bool getDirectoryList(int dwReqType, int parentId) { return false; } private void onHeartTimer(object sender, ElapsedEventArgs e) { S2C2S_Heart structObj = new S2C2S_Heart(true); this.m_clientSocket.Send(structObj); } private void onRevServerSocketData(LSSocket ClientSocket, byte[] byteData) { LSSocketRev socketRev = new LSSocketRev { ClientSocket = ClientSocket, ByteData = byteData }; LSQueueManage.InsertSocketRevData(socketRev); LSWriteLog.WriteTrace("onRevServerSocketData 插入列表数据执行完毕", new object[0]); } private void ProcessRevDataFromServer() { Thread.CurrentThread.IsBackground = true; LSSocketRev socketRev = null; while (!LSGlobleVariable.bNeedExit) { Exception exception; try { try { socketRev = LSQueueManage.GetSocketRevData(); LSWriteLog.WriteTrace("------------------ProcessRevDataFromServer----已经获取到列表数据", new object[0]); if (LSGlobleVariable.bNeedExit) { break; } } catch (Exception exception1) { exception = exception1; PubUtils.WriteLog("ProcessRevDataFromServer 前面的 error: {0}", new object[] { exception.Message.ToString() }); throw new Exception(exception.Message.ToString()); } LSGlobleVariable.masterForm.ParseDataFromServerMsg(socketRev); } catch (ThreadAbortException exception2) { PubUtils.WriteLog("ProcessRevDataFromServer ThreadAbortException: {0}", new object[] { exception2 }); break; } catch (ThreadInterruptedException exception3) { PubUtils.WriteLog("ProcessRevDataFromServer ThreadInterruptedException: {0}", new object[] { exception3 }); break; } catch (Exception exception6) { exception = exception6; if (socketRev != null) { PubUtils.WriteLog("ProcessRevDataFromServer error: {0},ProxySocket.SocketState: {1} ,byte data is:\n{2}", new object[] { exception, socketRev.ClientSocket.SocketState, socketRev.ByteData }); } else { PubUtils.WriteLog("ProcessRevDataFromServer error: {0}", new object[] { exception }); } } finally { if (socketRev != null) { socketRev = null; } } } } public void ProgramEntry(string ServerIP, string DBName, string isInternet, string IsConsumer) { try { LSWriteLog.Init("clientLogs", "TraceClient.log"); //LSIniFile.IniPath = "LSClientConfig.xml"; PubUtils.WriteLog("程序被调用", new object[0]); LSGlobleVariable.hThread = new Thread(new ThreadStart(this.ProcessRevDataFromServer)); LSGlobleVariable.hThread.Start(); this.ConnectServer(ServerIP, DBName, isInternet, IsConsumer); LSGlobleVariable.timer = new System.Timers.Timer(30000.0); LSGlobleVariable.timer.Elapsed += new ElapsedEventHandler(this.onHeartTimer); } catch (Exception exception) { PubUtils.WriteLog("error: {0}", new object[] { exception }); } } public bool readyCreateDir(int parentId, string sDirName) { return false; } public bool readyCreateFile(int parentId, string sName, int fileSize, int productId) { try { C2S_AddFile structObj = new C2S_AddFile(true); structObj.header.dwUserId = LSGlobleVariable.userId; structObj.fi.dwParentId = parentId; structObj.fi.strName = sName; structObj.fi.dwCreator = (int) LSGlobleVariable.userId; structObj.fi.dwFileSize = fileSize; this.m_clientSocket.Send(structObj); return true; } catch (Exception exception) { PubUtils.WriteLog("LSClientMain.readyCreateFile error: {0}", new object[] { exception }); } return false; } public bool requestDownloadFile(C2S_DownloadFile reqDownload) { try { reqDownload.header.dwUserId = LSGlobleVariable.userId; this.m_clientSocket.Send(reqDownload); return true; } catch (Exception exception) { PubUtils.WriteLog("LSClientMain.requestDownloadFile error: {0}", new object[] { exception }); } return false; } public void Send(object structObj) { this.m_clientSocket.Send(structObj); } [DllImport("user32.dll")] private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); private void SocketClientCloseDone(LSASyncSocket AsyncSocket) { try { ((LSSocket) AsyncSocket).HandledCloseEvent(); LSWriteLog.WriteTrace("连接断开了,通知外部", new object[0]); } catch (Exception exception) { PubUtils.WriteLog("LSClientMain.FlashClientCloseDone error: {0},", new object[] { exception }); } } public LSSocket clientSocket { get { return this.m_clientSocket; } } } }