Exemplo n.º 1
0
        //public bool RemovePeer(string _id)
        //{
        //    if (CurrentState != State.Stoped)
        //        return false;

        //    return udpSenders.Remove(HelperTools.GetDeterministicHashCode(_id));
        //}

        /// <summary>
        /// Resets the ID Peer to its default
        /// </summary>
        /// <param name="_id">ID of the peer</param>
        /// <param name="pOriginal">Returns the default peer tha was present in the file</param>
        /// <returns></returns>
        public bool RemoveReplayAddress(string _id, out PlayPeerInfo pOriginal)
        {
            pOriginal = null;

            if (CurrentState != State.Stoped)
            {
                return(false);
            }

            int hashedID = HelperTools.GetDeterministicHashCode(_id);

            if (udpSenders.TryGetValue(hashedID, out PlayPeerInfo p))
            {
                var original = udpSendersOriginal[hashedID];

                p.IP   = original.IP;
                p.Port = original.Port;
                p.NIC  = original.NIC;

                pOriginal = original;

                return(true);
            }

            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Enables or disables a given peer by ID
        /// </summary>
        /// <param name="_id">ID of the peer</param>
        /// <param name="enabled">Enable if true, disable if false</param>
        public void SetPeerEnabled(string _id, bool enabled)
        {
            int hashedID = HelperTools.GetDeterministicHashCode(_id);

            if (udpSenders.TryGetValue(hashedID, out PlayPeerInfo p))
            {
                p.IsEnabled = enabled;
            }
        }
Exemplo n.º 3
0
        private void WriteHeader(long _time, string _ID, int _size, uint _ip, int _port)
        {
            Span <byte> headerSpan = stackalloc byte[HelperTools.headerSize];

            // Time 8
            BinaryPrimitives.WriteInt64LittleEndian(headerSpan, _time);
            // Hashed ID 4
            BinaryPrimitives.WriteInt32LittleEndian(headerSpan.Slice(8), HelperTools.GetDeterministicHashCode(_ID));
            // IP 4
            BinaryPrimitives.WriteUInt32LittleEndian(headerSpan.Slice(12), _ip);
            // Port 4
            BinaryPrimitives.WriteInt32LittleEndian(headerSpan.Slice(16), _port);
            // Size 4
            BinaryPrimitives.WriteInt32LittleEndian(headerSpan.Slice(20), _size);

            // Write
            file.Write(headerSpan);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Override the default destination ip of a recorded peer with a given one.
        /// </summary>
        /// <param name="_ID">ID of the peer</param>
        /// <param name="_dstIP">New destination IP</param>
        /// <param name="_dstPort">New destination Port</param>
        /// <param name="_nic">NIC to send data from</param>
        /// <returns></returns>
        public bool AddReplayAddress(string _ID, string _dstIP, int _dstPort, string _nic)
        {
            if (CurrentState != State.Stoped)
            {
                return(false);
            }

            int hashedID = HelperTools.GetDeterministicHashCode(_ID);

            if (udpSenders.TryGetValue(hashedID, out PlayPeerInfo p))
            {
                p.IP   = _dstIP;
                p.Port = _dstPort;
                p.NIC  = _nic;

                return(true);
            }

            return(false);
        }
Exemplo n.º 5
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);
        }