Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        private static HandleResult Client_OnReceive(TcpPullClient sender, int length)
        {
            #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(jpgFile, FileMode.Create))
                            {
                                fs.Write(bytes, 0, bytes.Length);
                                fs.Close();
                            }

                            required = pkgHeaderSize;
                        }

                        // 在后面赋值,因为前面需要用到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;
                    }
                }
            }
            #endregion

            if (File.Exists(jpgFile))
            {
                Console.WriteLine("succeed");
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }

            return(HandleResult.Ok);
        }