namespace LSFileClient { using LSServerService; using System; using System.ComponentModel; using System.Drawing; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; using CommonLib; using System.Data; public partial class frmSingleDownload : FrmBase { public frmSingleDownload() : this(null, "", "", "1") { } public frmSingleDownload(LSClientMain _clientMain, string _serverIP, string _databaseName, string _isConsumer) : base(_clientMain, _serverIP, _databaseName, _isConsumer) { this.fs = null; this.canClosed = false; this.components = null; try { string file = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory)), "SystemResources.ini"); string connectionStr = ""; if (File.Exists(file)) { string lastIPPort = string.Format("{0}:{1}", IniUtil.Read(file, "LocalLastIP"), IniUtil.Read(file, "LocalLastPort")); string downloadAESStrKey = string.Format("DownloadAESStr_{0}", lastIPPort); string[] downloadAESStrValue = IniUtil.Read(file, downloadAESStrKey).Split('^'); string downloadAESStr = downloadAESStrValue.Length == 2 ? downloadAESStrValue[1] : ""; string configFlag = IniUtil.Read(file, "FrmConfigFlag"); if (configFlag.Equals("1")) { //string args = AESUtil.Decrypt(File.ReadAllText(file)); if (!string.IsNullOrEmpty(downloadAESStr)) { string args = AESUtil.Decrypt(downloadAESStr); string[] controlAfgs = args.Split('^'); if (controlAfgs != null && controlAfgs.Length == 5) { connectionStr = "Server=" + controlAfgs[0] + ";Database=" + controlAfgs[1] + ";Persist Security Info=True;User ID=" + controlAfgs[2] + ";Password=" + controlAfgs[3] + ";Connection Timeout=5;MultipleActiveResultSets=true"; } } } else { connectionStr = DBConfig.Instance.GetConStr(); } } if (string.IsNullOrEmpty(connectionStr)) { connectionStr = DBConfig.Instance.GetConStr(); } DBConfig.Instance.CreateNewConnection(connectionStr); } catch (Exception ex) { MessageBox.Show(ex.Message); } this.isConsumer = _isConsumer; this.InitializeComponent(); } private bool closeFile() { if (this.fs == null) { return false; } try { this.fs.Flush(); this.fs.Close(); this.fs.Dispose(); this.fs = null; } catch (Exception exception) { PubUtils.WriteLog("frmSingleDownload.hasParsedMsg error: {0}", new object[] { exception }); return false; } return true; } private void closeWin() { this.canClosed = true; this.closeFile(); base.Close(); } private void frmSingleDownload_FormClosed(object sender, FormClosedEventArgs e) { } private void frmSingleDownload_FormClosing(object sender, FormClosingEventArgs e) { if (!this.canClosed) { e.Cancel = true; } } private void frmSingleDownload_Load(object sender, EventArgs e) { //MessageBox.Show(LSParamsFormatter.downloadPath); LSParamsFormatter.serverPath = getServerPath(); LSGlobleVariable.masterForm = this; LSGlobleVariable.hWndMain = base.Handle; FrmBase.SendMessage(LSParamsFormatter.mainHandle, 1298, base.Handle, IntPtr.Zero); this.Text = Path.GetFileName(LSParamsFormatter.downloadPath) + " 下载中............"; C2S_ReqBackgroundDownload structObj = new C2S_ReqBackgroundDownload(true) { strPath = LSParamsFormatter.downloadPath }; if (Path.GetFileName(LSParamsFormatter.downloadPath) == "") { structObj.reqType = 2; } try { base.clientMain.Send(structObj); } catch (Exception exception) { //MessageBox.Show(exception.Message); PubUtils.WriteLog("frmSingleDownload.frmSingleDownload_Load error: {0}", new object[] { exception }); FrmBase.SendMessage(LSParamsFormatter.mainHandle, 1305, base.Handle, IntPtr.Zero); } } protected override bool hasParsedMsg(byte[] byteData, LSSocket client, MsgHeader header) { Exception exception; if (header.dwType == 65544) { S2C_ReqBackgroundDownload structure = (S2C_ReqBackgroundDownload)PubUtils.BytesToStruct(byteData, typeof(S2C_ReqBackgroundDownload)); if (structure.dwErrId > 0) { FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, new IntPtr(structure.dwErrId), IntPtr.Zero); return true; } try { if (structure.isDownloadModel) { this.fs = File.Open(LSParamsFormatter.savePath, FileMode.Create, FileAccess.Write, FileShare.Read); } else if (structure.isGetFileListModel) { int startIndex = Marshal.SizeOf(structure); StringBuilder builder = new StringBuilder(); for (int i = 0; i < structure.dwCount; i++) { StructFileInfo info = (StructFileInfo)PubUtils.BytesToStruct(byteData, typeof(StructFileInfo), startIndex); startIndex += Marshal.SizeOf(info); builder.AppendLine(info.strName); builder.AppendLine(info.strCreateTime); } DWriteClipData method = new DWriteClipData(this.writeClipData); base.Invoke(method, new object[] { builder.ToString() }); method = null; FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.SUCCESS_MESSAGE, IntPtr.Zero, IntPtr.Zero); } } catch (Exception exception1) { exception = exception1; PubUtils.WriteLog("frmSingleDownload.hasParsedMsg error: {0}", new object[] { exception }); FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, new IntPtr(structure.dwErrId), IntPtr.Zero); return false; } return true; } if (header.dwType == 65545) { C2S2C_BackgroundFileData data2 = (C2S2C_BackgroundFileData)PubUtils.BytesToStruct(byteData, typeof(C2S2C_BackgroundFileData)); if (data2.dwErrId > 0) { FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, new IntPtr(data2.dwErrId), IntPtr.Zero); return true; } try { this.fs.Write(data2.FileData, 0, data2.dwPacketLen); } catch (Exception exception2) { exception = exception2; PubUtils.WriteLog("frmSingleDownload.hasParsedMsg error: {0}", new object[] { exception }); FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, new IntPtr(data2.dwErrId), IntPtr.Zero); return false; } int num3 = (int)(100.0 * ((data2.dwPacketOrder * 1.0) / ((double)data2.dwTotalCount))); FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.SETPROGRESS, (IntPtr)num3, IntPtr.Zero); if (data2.dwTotalCount == data2.dwPacketOrder) { if (!this.closeFile()) { FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, new IntPtr(data2.dwErrId), IntPtr.Zero); return false; } FrmBase.SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.SUCCESS_MESSAGE, IntPtr.Zero, IntPtr.Zero); } return true; } return false; } protected override void WndProc(ref Message msg) { if (msg.Msg == LSCustomWinMsg.SUCCESS_MESSAGE) { FrmBase.SendMessage(LSParamsFormatter.mainHandle, LSCustomWinMsg.DOWNLOADCOMPLETED, base.Handle, IntPtr.Zero); this.closeWin(); } else if (msg.Msg == LSCustomWinMsg.FAIL_MESSAGE) { MessageBox.Show("下载失败,请联系管理员!"); FrmBase.SendMessage(LSParamsFormatter.mainHandle, LSCustomWinMsg.DOWNLOADERROR, base.Handle, IntPtr.Zero); this.closeWin(); } else if (msg.Msg == LSCustomWinMsg.CONNECT_ERROR) { MessageBox.Show("连接服务器失败,请联系管理员!"); FrmBase.SendMessage(LSParamsFormatter.mainHandle, LSCustomWinMsg.DOWNLOADERROR, base.Handle, IntPtr.Zero); this.closeWin(); } else if (msg.Msg == LSCustomWinMsg.SETPROGRESS) { this.proTranslate.Value = (int)msg.WParam; } else { base.WndProc(ref msg); } } private void writeClipData(string str) { Clipboard.Clear(); Clipboard.SetDataObject(str, true); } private string getServerPath() { DataTable table = SqlHelper.ExecuteDataTable("select ServerAttachPath from P_systemtab"); if (table != null && table.Rows.Count > 0) return table.Rows[0]["ServerAttachPath"] + ""; return ""; } private delegate void DWriteClipData(string str); } }