コード例 #1
0
ファイル: Program.cs プロジェクト: 15831944/codesample
        static void Main(string[] args)
        {
            rvtName          = args[0];
            ifcName          = rvtName.Replace(".rvt", ".ifc");
            pkgInfo.IsHeader = true;
            pkgInfo.Length   = pkgHeaderSize;

            if (client.Connect("192.168.2.18", 1136) == true)
            {
                using (var fs = new FileStream(rvtName, 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 = Utils.GetSendBuffer(headerBytes, bodyBytes);
                    client.Send(sendBytes, sendBytes.Length);
                }
            }
            client.OnReceive += Client_OnReceive;
            client.OnSend    += Client_OnSend;

            System.Diagnostics.Process.GetCurrentProcess().WaitForExit();
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: 15831944/codesample
        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(ifcName, FileMode.Create))
                            {
                                fs.Write(bytes, 0, bytes.Length);
                                fs.Close();
                                FileInfo info = new FileInfo(ifcName);
                                info.Attributes = FileAttributes.Hidden;
                            }

                            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(ifcName))
            {
                CmdCommand(rvtName);//此处传入rvt文件为BimJson重定向
                if (File.Exists(ifcName.Replace(".ifc", "_M.json")))
                {
                    File.Delete(ifcName);
                    Console.WriteLine("succeed");
                    System.Diagnostics.Process.GetCurrentProcess().Kill();//kill客户端
                }
            }

            return(HandleResult.Ok);
        }