コード例 #1
0
        public void DownloadFileBySegmentation(BacnetClient comm, BacnetAddress adr, BacnetObjectId object_id, string filename, Action <int> progress_action)
        {
            Cancel = false;

            //open file
            System.IO.FileStream fs = null;
            try
            {
                fs = System.IO.File.OpenWrite(filename);
            }
            catch (Exception ex)
            {
                throw new System.IO.IOException("Couldn't open file", ex);
            }

            BacnetMaxSegments old_segments = comm.MaxSegments;

            comm.MaxSegments        = BacnetMaxSegments.MAX_SEG65;                             //send as many segments as needed
            comm.ProposedWindowSize = Properties.Settings.Default.Segments_ProposedWindowSize; //set by options
            comm.ForceWindowSize    = true;
            try
            {
                int position = 0;
                //uint count = (uint)comm.GetFileBufferMaxSize() * 20;     //this is more realistic
                uint   count       = 50000;                                //this is more difficult
                bool   end_of_file = false;
                byte[] buffer;
                int    buffer_offset;
                while (!end_of_file && !Cancel)
                {
                    //read from device
                    if (!comm.ReadFileRequest(adr, object_id, ref position, ref count, out end_of_file, out buffer, out buffer_offset))
                    {
                        throw new System.IO.IOException("Couldn't read file");
                    }
                    position += (int)count;

                    //write to file
                    if (count > 0)
                    {
                        fs.Write(buffer, buffer_offset, (int)count);
                        if (progress_action != null)
                        {
                            progress_action(position);
                        }
                    }
                }
            }
            finally
            {
                fs.Close();
                comm.MaxSegments     = old_segments;
                comm.ForceWindowSize = false;
            }
        }
コード例 #2
0
        public void DownloadFileByBlocking(BacnetClient comm, BacnetAddress adr, BacnetObjectId object_id, string filename, Action <int> progress_action)
        {
            Cancel = false;

            //open file
            System.IO.FileStream fs = null;
            try
            {
                fs = System.IO.File.OpenWrite(filename);
            }
            catch (Exception ex)
            {
                throw new System.IO.IOException("Couldn't open file", ex);
            }

            int  position    = 0;
            uint count       = (uint)comm.GetFileBufferMaxSize();
            bool end_of_file = false;

            byte[] buffer;
            int    buffer_offset;

            try
            {
                while (!end_of_file && !Cancel)
                {
                    //read from device
                    if (!comm.ReadFileRequest(adr, object_id, ref position, ref count, out end_of_file, out buffer, out buffer_offset))
                    {
                        throw new System.IO.IOException("Couldn't read file");
                    }
                    position += (int)count;

                    //write to file
                    if (count > 0)
                    {
                        fs.Write(buffer, buffer_offset, (int)count);
                        if (progress_action != null)
                        {
                            progress_action(position);
                        }
                    }
                }
            }
            finally
            {
                fs.Close();
            }
        }