static void Main(string[] args) { dwgFile = args[0]; dwgPath = dwgFile.Replace(".dwg", "") + "\\"; pkgInfo.IsHeader = true; pkgInfo.Length = pkgHeaderSize; if (client.Connect("192.168.2.18", 1137) == true) { using (var fs = new FileStream(dwgFile, FileMode.Open)) { // 封包体 byte[] bodyBytes = new byte[fs.Length]; fs.Read(bodyBytes, 0, bodyBytes.Length); fs.Close(); // 封包头 PkgHeader header = new PkgHeader(); header.Id = ++id; header.BodySize = bodyBytes.Length; byte[] headerBytes = client.StructureToByte <PkgHeader>(header); // 组合最终发送的封包 (封包头+封包体) byte[] sendBytes = DwgUtils.GetSendBuffer(headerBytes, bodyBytes); client.Send(sendBytes, sendBytes.Length); } } client.OnClose += Client_OnClose; client.OnConnect += Client_OnConnect; client.OnReceive += Client_OnReceive; client.OnSend += Client_OnSend; System.Diagnostics.Process.GetCurrentProcess().WaitForExit(); }
private static HandleResult Client_OnReceive(TcpPullClient sender, int length) { if (!Directory.Exists(dwgPath)) { Directory.CreateDirectory(dwgPath); } zipFile = dwgPath + Path.GetFileNameWithoutExtension(dwgFile) + ".zip"; try { #region 收数据 // 需要长度 int required = pkgInfo.Length; // 剩余大小 int remain = length; while (remain >= required) { IntPtr bufferPtr = IntPtr.Zero; try { remain -= required; bufferPtr = Marshal.AllocHGlobal(required); if (client.Fetch(bufferPtr, required) == FetchResult.Ok) { if (pkgInfo.IsHeader == true) { PkgHeader header = (PkgHeader)Marshal.PtrToStructure(bufferPtr, typeof(PkgHeader)); required = header.BodySize; } else { //intptr转byte[] byte[] bytes = new byte[required]; Marshal.Copy(bufferPtr, bytes, 0, required); using (var fs = new FileStream(zipFile, FileMode.Create)) { fs.Write(bytes, 0, bytes.Length); fs.Close(); } required = pkgHeaderSize; DwgUtils.UnZip(zipFile, dwgPath, "123456"); DwgUtils.renameFile(dwgPath); } // 在后面赋值,因为前面需要用到pkgInfo.Length pkgInfo.IsHeader = !pkgInfo.IsHeader; pkgInfo.Length = required; if (client.SetExtra(pkgInfo) == false) { return(HandleResult.Error); } } } catch { return(HandleResult.Error); } finally { if (bufferPtr != IntPtr.Zero) { Marshal.FreeHGlobal(bufferPtr); bufferPtr = IntPtr.Zero; } if (File.Exists(dwgPath + Path.GetFileNameWithoutExtension(dwgFile) + "_G.json")) { insertData(dwgPath + Path.GetFileNameWithoutExtension(dwgFile) + "_G.json"); Console.WriteLine("succeed"); System.Diagnostics.Process.GetCurrentProcess().Kill(); } } } #endregion } catch (Exception ex) { Console.WriteLine(ex); } return(HandleResult.Ok); }
private HandleResult Server_OnReceive(IntPtr connId, int length) { if (!Directory.Exists(listPath))//文件夹不存在则创建 { Directory.CreateDirectory(listPath); } #region 写文件 // 数据到达了 // clientInfo 就是accept里传入的附加数据了 var clientInfo = server.GetExtra(connId); if (clientInfo == null) { return(HandleResult.Error); } PkgInfo pkgInfo = clientInfo.PkgInfo; // 需要长度 int required = pkgInfo.Length; // 剩余大小 int remain = length; while (remain >= required) { IntPtr bufferPtr = IntPtr.Zero; try { remain -= required; bufferPtr = Marshal.AllocHGlobal(required);; if (server.Fetch(connId, bufferPtr, required) == FetchResult.Ok) { if (pkgInfo.IsHeader == true) { PkgHeader header = (PkgHeader)Marshal.PtrToStructure(bufferPtr, typeof(PkgHeader)); required = header.BodySize; } else { //intptr转byte[] byte[] bytes = new byte[required]; Marshal.Copy(bufferPtr, bytes, 0, required); var fs = new FileStream(dwgFile, FileMode.Create); fs.Write(bytes, 0, bytes.Length); fs.Close(); required = pkgHeaderSize; } // 在后面赋值,因为前面需要用到pkgInfo.Length pkgInfo.IsHeader = !pkgInfo.IsHeader; pkgInfo.Length = required; if (server.SetExtra(connId, clientInfo) == false) { return(HandleResult.Error); } } } catch { return(HandleResult.Error); } finally { if (bufferPtr != IntPtr.Zero) { Marshal.FreeHGlobal(bufferPtr); bufferPtr = IntPtr.Zero; } } #endregion } if (File.Exists(dwgFile) && count == 0) { Application.Idle += Application_Idle; } return(HandleResult.Ok); }
private void Application_Idle(object sender, EventArgs e) { if (count != 0) { return; } try { Document doc = Application.DocumentManager.Open(dwgFile, true); Database db = doc.Database; Autodesk.AutoCAD.DatabaseServices.TransactionManager tm = db.TransactionManager; using (Transaction tan = tm.StartTransaction()) { db.DxfOut(dxfFile, 16, DwgVersion.Newest); Utils.CmdCommand(dwgFile.Replace(".dwg", "")); Utils.GetDwgProperty(dwgFile.Replace(".dwg", "")); Utils.ReadGeometry(dwgFile.Replace(".dwg", "")); Utils.TreeToJson(dwgFile.Replace(".dwg", "") + "_T.json"); Utils.PropertyToJson(dwgFile.Replace(".dwg", "") + "_P.json"); Utils.EntityColor.Clear(); Utils.EntityHandle.Clear(); Utils.LayerColor.Clear(); Utils.LayerName.Clear(); Utils.Text.Clear(); Utils.TextRotation.Clear(); tan.Commit(); } Application.Idle -= Application_Idle; } catch (Autodesk.AutoCAD.Runtime.Exception ex) { Application.ShowAlertDialog(ex.ToString()); } finally { try { Application.DocumentManager.CloseAll(); //Thread th1 = new Thread(Utils.delFile); //th1.Start(); //Thread.Sleep(3000); File.Delete(dxfFile); Utils.zipFile(listPath, "E:\\doc-fangfenglin\\" + now + ".zip"); using (var fs = new FileStream("E:\\doc-fangfenglin\\" + now + ".zip", FileMode.Open)) { // 封包体 byte[] bodyBytes = new byte[fs.Length]; fs.Read(bodyBytes, 0, bodyBytes.Length); fs.Close(); // 封包头 PkgHeader header = new PkgHeader(); header.Id = ++id; header.BodySize = bodyBytes.Length; byte[] headerBytes = server.StructureToByte <PkgHeader>(header); // 组合最终发送的封包 (封包头+封包体) byte[] sendBytes = Utils.GetSendBuffer(headerBytes, bodyBytes); server.Send(ID, sendBytes, sendBytes.Length); } } catch (System.Exception ex) { Application.ShowAlertDialog(ex.ToString()); } count++; } }