Ejemplo n.º 1
0
        private void Watcher_Created(object sender, FileSystemEventArgs e)
        {
            if (!File.Exists(jpgFile))
            {
                return;
            }
            else
            {
                Thread.Sleep(5000);
                using (var fs = new FileStream(jpgFile, 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);
                }
            }
            count            = 0;
            watcher.Created -= Watcher_Created;
            watcher.Dispose();
            Utils.delDir(path + now);
        }
Ejemplo n.º 2
0
        private HandleResult Server_OnReceive(IntPtr connId, int length)
        {
            if (count == 0)
            {
                now = DateTime.Now.ToString("yyyyMMddhhmmss");
            }
            if (!Directory.Exists(path + now))
            {
                Directory.CreateDirectory(path + now);
                dwfFile = path + now + "\\" + now + ".dwf";
                jpgFile = dwfFile.Replace(".dwf", ".jpg");
                dwgFile = dwfFile.Replace(".dwf", ".dwg");
            }

            #region 收数据
            // 需要长度
            int required = pkgInfo.Length;

            // 剩余大小
            int remain = length;

            while (remain >= required)
            {
                IntPtr bufferPtr = IntPtr.Zero;
                try
                {
                    remain   -= required;
                    bufferPtr = Marshal.AllocHGlobal(required);
                    if (server.Fetch(ID, 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(dwfFile, FileMode.Create))
                            {
                                fs.Write(bytes, 0, bytes.Length);
                                fs.Close();
                            }

                            required = pkgHeaderSize;
                        }

                        // 在后面赋值,因为前面需要用到pkgInfo.Length
                        pkgInfo.IsHeader = !pkgInfo.IsHeader;
                        pkgInfo.Length   = required;
                        if (server.SetExtra(ID, pkgInfo) == false)
                        {
                            return(HandleResult.Error);
                        }
                    }
                }
                catch
                {
                    return(HandleResult.Error);
                }
                finally
                {
                    if (bufferPtr != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(bufferPtr);
                        bufferPtr = IntPtr.Zero;
                    }
                }
            }
            #endregion
            if (count == 0)
            {
                Application.Idle += Application_Idle;
                count             = 1;
            }

            return(HandleResult.Ok);
        }