Exemplo n.º 1
0
        /// <summary>
        /// Retrieves information from the recording idx file
        /// </summary>
        /// <param name="_idxFilePath"></param>
        /// <returns>Peers, recording date and time, duration in secs, recording size in bytes</returns>
        public static (ICollection <PlayPeerInfo>, DateTime, int, long) GetRecordingInfo(string _idxFilePath)
        {
            int                 duration        = 0;
            int                 nPeers          = 0;
            int                 secsIdxInterval = 0;
            DateTime            date            = DateTime.MinValue;
            List <PlayPeerInfo> peers           = new List <PlayPeerInfo>();

            using (FileStream idxFile = new FileStream(_idxFilePath, FileMode.Open, FileAccess.Read))
                using (BinaryReader idxBinaryReader = new BinaryReader(idxFile))
                {
                    Span <byte> auxBuff = stackalloc byte[1024];

                    // Read DateTime, Secs Interval and Peers info
                    date            = HelperTools.fromMillis(idxBinaryReader.ReadInt64());
                    secsIdxInterval = idxBinaryReader.ReadInt32();
                    nPeers          = idxBinaryReader.ReadInt32();

                    // Read peers
                    for (int i = 0; i < nPeers; i++)
                    {
                        string ID   = HelperTools.Bytes2StringWithLength(idxFile);
                        string IP   = HelperTools.Bytes2StringWithLength(idxFile);
                        int    Port = idxBinaryReader.ReadInt32();

                        peers.Add(new PlayPeerInfo
                        {
                            ID   = ID,
                            IP   = IP,
                            Port = Port
                        });
                    }

                    // Guess duration
                    FileInfo fInfo = new FileInfo(_idxFilePath);
                    duration = ((int)((fInfo.Length - idxBinaryReader.BaseStream.Position) / HelperTools.idxIndexSize)) * secsIdxInterval;
                }

            string   rawFile = Path.Combine(Path.GetDirectoryName(_idxFilePath), Path.GetFileNameWithoutExtension(_idxFilePath)) + ".raw";
            FileInfo fRaw    = new FileInfo(rawFile);

            return(peers, date, duration, fRaw.Length);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load a recording file
        /// </summary>
        /// <param name="_filePath">Raw file path</param>
        /// <param name="_startTime">out Start date and time</param>
        /// <param name="_lastTime">out Last time in recording</param>
        /// <returns>Collection of peers recorded in the file</returns>
        public ICollection <PlayPeerInfo> LoadFile(string _filePath, out DateTime _startTime, out int _lastTime)
        {
            if (CurrentState != State.Stoped)
            {
                throw new InvalidOperationException("Playing in progress. Must stop to load file");
            }

            filePath    = _filePath;
            idxFilePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath)) + ".idx";

            // Init stuff
            _lastTime  = 0;
            _startTime = DateTime.MinValue;
            timeIndexes.Clear();
            int nPeers = 0;

            udpSendersOriginal.Clear();
            udpSenders.Clear();
            _dataRate.Clear();

            using (FileStream idxFile = new FileStream(idxFilePath, FileMode.Open, FileAccess.Read))
                using (BinaryReader idxBinaryReader = new BinaryReader(idxFile))
                {
                    int         time = 0; long position = 0;
                    Span <byte> auxBuff = stackalloc byte[4096];

                    // Read DateTime, Secs Interval and Peers info
                    RecDateTime     = _startTime = HelperTools.fromMillis(idxBinaryReader.ReadInt64());
                    secsIdxInterval = idxBinaryReader.ReadInt32();
                    nPeers          = idxBinaryReader.ReadInt32();

                    // Read peers
                    for (int i = 0; i < nPeers; i++)
                    {
                        string ID   = HelperTools.Bytes2StringWithLength(idxFile);
                        string IP   = HelperTools.Bytes2StringWithLength(idxFile);
                        int    Port = idxBinaryReader.ReadInt32();

                        udpSenders.Add(HelperTools.GetDeterministicHashCode(ID),
                                       new PlayPeerInfo
                        {
                            ID   = ID,
                            IP   = IP,
                            Port = Port
                        });

                        udpSendersOriginal.Add(HelperTools.GetDeterministicHashCode(ID),
                                               new PlayPeerInfo
                        {
                            ID   = ID,
                            IP   = IP,
                            Port = Port
                        });

                        _dataRate[ID] = 0f;
                    }

                    Span <byte> timeBuff = stackalloc byte[HelperTools.idxIndexSize];
                    // Get All times to cache
                    while (idxFile.Read(timeBuff) == HelperTools.idxIndexSize)
                    {
                        time     = BinaryPrimitives.ReadInt32LittleEndian(timeBuff);
                        position = BinaryPrimitives.ReadInt64LittleEndian(timeBuff.Slice(4));

                        try
                        {
                            timeIndexes.Add(time, position);
                        }
                        catch (Exception e)
                        {
                            logger.Error(e, "Error reading idx file. TimeIndexes");
                        }
                    }
                    _lastTime = lastTime = time;
                }
            return(udpSenders.Values);
        }