Ejemplo n.º 1
0
        /// <summary>
        /// Initialize the buffer header file with empty values
        /// </summary>
        private void InitializeHeader()
        {
            try
            {
                //create a new buffer header
                _header = new NiawaMmfBufferHeader();
                SortedList<int, KeyValuePair<string, DateTime>> entries = new SortedList<int, KeyValuePair<string, DateTime>>();
                //populate the buffer header with blank values
                int ix = 1;
                while (ix <= 100)
                {
                    entries.Add(ix, new KeyValuePair<string, DateTime>(string.Empty, new DateTime(1900, 1, 1)));
                    ix++;
                }
                _header.Entries = entries;
                _header.LatestUpdateDate = new DateTime(1900, 1, 1);
                _header.LatestEntryID = 0;

                //convert buffer header to bytes
                byte[] buffer = _header.ToByteArray();

                //locked section
                using (new Niawa.Utilities.SingleGlobalInstance(1000, _ipcType + "_header")) //1000ms timeout on global lock
                {
                    int msgLength = buffer.Length;
                    int msgID = 0;

                    //open mmf
                    MemoryMappedFile mmf = MemoryMappedFile.CreateOrOpen(_ipcType + "_header", _bufferLength);
                    MemoryMappedViewAccessor accessor1Len = mmf.CreateViewAccessor(0, 4);
                    MemoryMappedViewAccessor accessor2ID = mmf.CreateViewAccessor(8, 4);
                    MemoryMappedViewAccessor accessor3Data = mmf.CreateViewAccessor(16, msgLength);

                    //write data
                    accessor1Len.Write<Int32>(0, ref msgLength);
                    accessor2ID.Write<Int32>(0, ref msgID);
                    accessor3Data.WriteArray<byte>(0, buffer, 0, msgLength);

                } //end locked section

            }
            catch (Exception ex)
            {
                logger.Error("[" + _description + "-M] Exception while initializing buffer header: " + ex.Message, ex);
                if (!_ignoreExceptions)
                    throw ex;
            }
        }