Exemplo n.º 1
0
        // save
        private void store(bool force = false)
        {
            // thresholds
            const long save_interval_ms = 60 * 1000;  // every minute
            const int  save_threshold   = 200 * 1000; // 200k messages

            // lock
            m_file_mutex.WaitOne();

            // do nothing if not allowed
            if (!m_data_save_allowed)
            {
                m_file_mutex.ReleaseMutex();
                return;
            }

            // do
            if (storage_msg_list.Count > 0)
            {
                // now
                DateTimeOffset now    = DateTimeOffset.Now;
                long           now_ts = now.ToUnixTimeMilliseconds();

                if (storage_ts == 0)
                {
                    storage_ts = now_ts + save_interval_ms;
                }

                // check the conditions
                if (storage_msg_list.Count >= save_threshold || now_ts >= storage_ts || force)
                {
                    bool file_just_created = false;

                    if (string.IsNullOrEmpty(storage_file_path))
                    {
                        storage_file_path = string.Format("{0}\\trace\\",
                                                          System.IO.Path.GetDirectoryName(Application.ExecutablePath));
                    }
                    if (string.IsNullOrEmpty(storage_file_name))
                    {
                        storage_file_name = string.Format("trace_{0:yyyy_MM_dd_HH_mm_ss}.ctd", now);
                        file_just_created = true;
                    }

                    // make sure the dir exists
                    if (!Directory.Exists(storage_file_path))
                    {
                        Directory.CreateDirectory(storage_file_path);
                    }

                    // one more time
                    if (Directory.Exists(storage_file_path))
                    {
                        // compress
                        byte[] comp = Compression.CanMessagesCompress(storage_msg_list);
                        if (comp != null)
                        {
                            // store
                            using (var stream = new FileStream(storage_file_path + storage_file_name, FileMode.Append))
                            {
                                // append the title string
                                if (file_just_created)
                                {
                                    string title = string.Format(
                                        "User: {0}\nCAN Speed: {1}\n",
                                        Environment.UserName, can_speed);
                                    var buff = Encoding.ASCII.GetBytes(title + Environment.NewLine);
                                    stream.Write(buff, 0, buff.Length);
                                }
                                // data
                                stream.Write(comp, 0, comp.Length);
                            }
                        }
                    }

                    // clean
                    storage_msg_list.Clear();
                    storage_ts = now_ts + save_interval_ms;
                }
            }
            // unlock
            m_file_mutex.ReleaseMutex();
        }