namespace LSFileClient { using LSServerService; using LSUtils; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; public class FrmBase : Form { protected LSClientMain clientMain; protected S2C_DownloadFile m_curDownloadingFileInfo; protected ListNodeData m_currentNodeData; protected string m_databaseName; protected List m_listNode; protected string m_serverIP; protected const int WM_COPYDATA = 74; protected string isConsumer; public FrmBase() : this(null, "", "","1") { } public FrmBase(LSClientMain _clientMain, string _serverIP, string _databaseName, string _isConsumer) { this.m_serverIP = ""; this.m_databaseName = ""; this.m_currentNodeData = null; this.m_listNode = new List(); this.clientMain = _clientMain; this.m_serverIP = _serverIP; this.m_databaseName = _databaseName; this.isConsumer = _isConsumer; } protected ListNodeData getByItem(ListViewItem item) { foreach (ListNodeData data in this.m_listNode) { if (data.item == item) { return data; } } return null; } protected ListNodeData getDataById(int fileId) { foreach (ListNodeData data in this.m_listNode) { if (data.fileId == fileId) { return data; } } return null; } protected ListNodeData getDataByName(string fileName) { foreach (ListNodeData data in this.m_listNode) { if (Path.GetFileName(data.absoluteFileName) == fileName) { return data; } } return null; } protected virtual bool hasParsedMsg(byte[] byteData, LSSocket client, MsgHeader header) { return false; } [DllImport("user32.dll")] protected static extern bool IsWindow(IntPtr hWnd); public void ParseDataFromServerMsg(LSSocketRev socketRev) { byte[] byteData = socketRev.ByteData; LSSocket clientSocket = socketRev.ClientSocket; MsgHeader header = (MsgHeader) PubUtils.BytesToStruct(byteData, typeof(MsgHeader)); if (header.dwType == S2C2S_Heart.ID) { clientSocket.Send(byteData); } else if (!this.hasParsedMsg(byteData, clientSocket, header)) { switch (header.dwType) { case 18: { S2C_AddFile file = (S2C_AddFile) PubUtils.BytesToStruct(byteData, typeof(S2C_AddFile)); if (file.dwErrId <= 0) { this.m_currentNodeData.fileId = file.fi.dwFileId; this.updateInfoForUp(file.fi.dwFileId); this.uploadFile(file.fi.dwFileId); return; } SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, new IntPtr(file.dwErrId), IntPtr.Zero); return; } case 20: { S2C_DownloadFile file3 = (S2C_DownloadFile) PubUtils.BytesToStruct(byteData, typeof(S2C_DownloadFile)); this.m_currentNodeData = null; if (file3.dwErrId <= 0) { this.m_currentNodeData = this.getDataById(file3.fi.dwFileId); SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.NOTIFY_READY, IntPtr.Zero, IntPtr.Zero); string strName = file3.fi.strName; LSFileData fd = LSFileManage.createClientFile(strName); if (fd == null) { SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, IntPtr.Zero, IntPtr.Zero); } else { fd.dwUserId = LSGlobleVariable.userId; fd.dwFileId = file3.fi.dwFileId; LSFileManage.insertFileData(fd); this.m_curDownloadingFileInfo = file3; LSWriteLog.WriteLog("downloadfile.log", false, "准备下载文件《{0}》", new object[] { strName }); } return; } SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, IntPtr.Zero, IntPtr.Zero); return; } case 23: { S2C_DeleteFile file2 = (S2C_DeleteFile) PubUtils.BytesToStruct(byteData, typeof(S2C_DeleteFile)); SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.DELETEFILE_MESSAGE, new IntPtr(file2.dwErrId), new IntPtr(file2.dwFileId)); return; } case 4097: { C2S2C_FileData data = (C2S2C_FileData) PubUtils.BytesToStruct(byteData, typeof(C2S2C_FileData)); if (data.dwErrId <= 0) { C2S2C_FileTranslateComplete complete2; int num = (data.dwTotalBytes == 0) ? 100 : ((int) (100.0 * ((data.dwPacketOrder * 1.0) / ((double) data.dwTotalCount)))); SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.SETPROGRESS, (IntPtr) num, IntPtr.Zero); if (LSFileManage.createFileData(data, out complete2)) { SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.SUCCESS_MESSAGE, IntPtr.Zero, IntPtr.Zero); if (this.m_curDownloadingFileInfo.dwTotalCount <= this.m_curDownloadingFileInfo.dwCurrentIndex) { SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.CLOSE_WINDOW, IntPtr.Zero, IntPtr.Zero); } } return; } LSFileManage.eraseFile(this.m_curDownloadingFileInfo.fi.dwFileId); SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, IntPtr.Zero, IntPtr.Zero); return; } case 4098: { C2S2C_FileTranslateComplete complete = (C2S2C_FileTranslateComplete) PubUtils.BytesToStruct(byteData, typeof(C2S2C_FileTranslateComplete)); if (complete.dwErrId <= 0) { SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.SUCCESS_MESSAGE, IntPtr.Zero, IntPtr.Zero); return; } SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, IntPtr.Zero, IntPtr.Zero); return; } } LSWriteLog.WriteLog("cantParseData.log", "从服务端接收到的数据不能解析,类型是={0},数据长度={1} 数据是:\n{2} ", new object[] { header.dwType, byteData.Length, byteData }); } } protected void removeItem(ListViewItem item) { ListNodeData data = this.getByItem(item); if (data != null) { this.m_listNode.Remove(data); } } [DllImport("user32.dll", EntryPoint="SendMessage")] protected static extern IntPtr SendCopyData(IntPtr hWnd, int msg, IntPtr wParam, ref COPYDATASTRUCT lParam); [DllImport("user32.dll")] protected static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam); protected void setItemState(int success) { this.setItemState(success, ""); } protected void setItemState(int success, string desc) { if (this.m_currentNodeData != null) { this.m_currentNodeData.success = success; this.m_currentNodeData.selected = false; string str = this.m_currentNodeData.isDownload ? "下载" : "上传"; this.m_currentNodeData.item.SubItems[5].Text = str + ((success == 2) ? "成功" : "失败"); if (success == 3) { this.m_currentNodeData.item.SubItems[6].Text = desc + "请联系管理员!"; } } LSGlobleVariable.notifyEvent.Set(); } private void updateInfoForUp(int dwFileId) { LSWriteLog.WriteLog("updateInfoForUp.log", false, "开始dwFileId=《{0}》", new object[] { dwFileId }); ListNodeData data = this.getDataById(dwFileId); if (data != null) { //string connectionString = LSIniFile.ReadValue("/Root/ClientSettings", "connectionStr"); //if (connectionString == "") //{ string connectionString = string.Format("Server={0};Database={1};Persist Security Info=True;User ID=wlms;Password=mymsserver", this.m_serverIP, this.m_databaseName); if (isConsumer.Equals("0")) connectionString = string.Format("Server=114.116.145.208,14331;Database=lscrm;Persist Security Info=True;User ID=Lserp_crm;Password=lserp_ERP_@#$_123", this.m_serverIP, this.m_databaseName); //} SqlConnection connection = new SqlConnection(connectionString); try { connection.Open(); SqlCommand command = new SqlCommand(string.Format("update P_fm_FileTab set fileNo='{0}', speciesno='{1}',SPNAME='{2}' where fileId={3}", new object[] { data.fileNo, data.specieNo, data.specieName, data.fileId }), connection); command.ExecuteNonQuery(); command.Dispose(); command = null; } catch (Exception exception) { PubUtils.WriteLog("updateInfoForUp error: {0}", new object[] { exception }); MessageBox.Show("连接服务器失败,请联系管理员."); } connection.Close(); connection = null; LSWriteLog.WriteLog("updateInfoForUp.log", false, "结束dwFileId=《{0}》", new object[] { dwFileId }); } } private void uploadFile(int dwFileId) { FileStream stream = null; try { stream = File.Open(this.m_currentNodeData.absoluteFileName, FileMode.Open, FileAccess.Read); int length = (int) stream.Length; int num2 = (int) Math.Ceiling((double) ((1.0 * length) / 1024.0)); int num3 = 1; do { C2S2C_FileData structObj = new C2S2C_FileData(true); int count = length - ((int) stream.Position); count = (count > 1024) ? 1024 : count; stream.Read(structObj.FileData, 0, count); structObj.dwErrId = 0; structObj.dwFileId = dwFileId; structObj.dwTotalBytes = length; structObj.dwTotalCount = num2; structObj.dwPacketOrder = num3; structObj.dwPacketLen = count; this.clientMain.clientSocket.Send(structObj); num3++; int num5 = (length == 0) ? 100 : ((int) (100.0 * ((stream.Position * 1.0) / ((double) length)))); SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.SETPROGRESS, (IntPtr) num5, IntPtr.Zero); } while (stream.Position < length); if (stream != null) { stream.Close(); stream = null; } } catch (Exception exception) { PubUtils.WriteLog("uploadFile error: {0}", new object[] { exception }); C2S2C_FileData data2 = new C2S2C_FileData(true) { dwErrId = ErrorType.UNKNOWN, dwFileId = dwFileId }; this.clientMain.clientSocket.Send(data2); SendMessage(LSGlobleVariable.hWndMain, LSCustomWinMsg.FAIL_MESSAGE, IntPtr.Zero, IntPtr.Zero); } finally { if (stream != null) { stream.Close(); stream = null; } } } [StructLayout(LayoutKind.Sequential)] public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } protected class ListNodeData { public string absoluteFileName = ""; public int fileId = 0; public string fileNo = ""; public bool isDownload = false; public ListViewItem item = null; public bool selected = false; public string specieName = ""; public string specieNo = ""; public int success = 0; } } }