internal static string Bytes2StringWithLength(Stream stream) { string s = ""; byte[] buff = HelperTools.RentBuffer(256); if (stream.Read(buff, 0, 4) == 4) { int length = BitConverter.ToInt32(buff, 0); if (stream.Read(buff, 0, length) == length) { s = Encoding.ASCII.GetString(buff, 0, length); } } return(s); //int stringLength = BitConverter.ToInt32(_buff, _offset); //if (stringLength > 0 && stringLength < _buff.Length - _offset) //{ // s = Encoding.ASCII.GetString(_buff, _offset + 4, stringLength); //} //return s; }
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); }
public bool AddReplayAddress(string _ID, string _dstIP, int _dstPort, string _nic) { if (CurrentState != State.Stoped) { return(false); } if (_dstIP is null || _dstPort <= 0 || _dstPort > 65535) { 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); }
internal static ulong IPPort2Long(string _ip, int _port) { byte[] _buffer = new byte[8]; ushort[] ipChunks = new ushort[4]; string[] chunks = _ip.Split('.'); if (chunks.Length == 4) { for (int i = 0; i < 4; i++) { ipChunks[i] = ushort.Parse(chunks[i]); } } // IP 4 for (int i = 0; i < 4; i++) { _buffer[i] = (byte)ipChunks[i]; } // Port 4 HelperTools.Int32Bytes(_buffer, 4, _port); return(BitConverter.ToUInt64(_buffer, 0)); }
private void WriteIdx() { // Write index HelperTools.Int32Bytes(idxBuffer, 0, secTime); HelperTools.Long2Bytes(idxBuffer, 4, file.Position); idxFile.Write(idxBuffer, 0, HelperTools.idxIndexSize); }
public bool RemovePeer(string _id) { if (CurrentState != State.Stoped) { return(false); } return(udpSenders.Remove(HelperTools.GetDeterministicHashCode(_id))); }
public void SetPeerEnabled(string _id, bool enabled) { int hashedID = HelperTools.GetDeterministicHashCode(_id); if (udpSenders.TryGetValue(hashedID, out PlayPeerInfo p)) { p.IsEnabled = enabled; } }
private void FillHeader(long _time, string _ID, int _size, byte[] _buffer, ushort[] ipChunks, int _port) { // Time 8 HelperTools.Long2Bytes(_buffer, 0, _time); // Hashed ID 4 HelperTools.Int32Bytes(_buffer, 8, HelperTools.GetDeterministicHashCode(_ID)); // IP 4 for (int i = 0; i < 4; i++) { _buffer[12 + i] = (byte)ipChunks[i]; } // Port 4 HelperTools.Int32Bytes(_buffer, 16, _port); // Size 4 HelperTools.Int32Bytes(_buffer, 20, _size); }
public static (ICollection <PlayPeerInfo>, DateTime, int, long) GetRecordingInfo(string _idxFilePath) { int duration = 0; int nPeers = 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)) { byte[] auxBuff = new byte[1024]; // Read DateTime, Secs Interval and Peers info if (idxBinaryReader.Read(auxBuff, 0, 16) == 16) { date = HelperTools.fromMillis(BitConverter.ToInt64(auxBuff, 0)); int secsIdxInterval = BitConverter.ToInt32(auxBuff, 8); nPeers = BitConverter.ToInt32(auxBuff, 12); // 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); }
private void FillIDXHeader(FileStream _idxFile) { byte[] idxHeader = new byte[4096]; int i = 0; // Write idx Header // Time of recording i += HelperTools.Long2Bytes(idxHeader, i, HelperTools.millisFromEpochNow()); // Interval i += HelperTools.Int32Bytes(idxHeader, i, secsIdxInterval); // Number of peers i += HelperTools.Int32Bytes(idxHeader, i, udpListeners.Count); foreach (RecPeerInfo p in udpListeners.Values) { i += HelperTools.StringWithLength2Bytes(idxHeader, i, p.ID); i += HelperTools.StringWithLength2Bytes(idxHeader, i, p.IP); i += HelperTools.Int32Bytes(idxHeader, i, p.Port); } idxFile.Write(idxHeader, 0, i); }
private void RunReaderSenderProcessCallback(string _filePath, string _idxFilePath, CancellationToken token) { ulong ipport; int n_bytes = 0, size = 0; int hashedID = 0; long timeoffset = 0, waitTime = 0, nowTime = 0, time = 0; int lastTimeStatusS = 0, timeStatusS = 0; // Increase timers resolution WinAPI.WinAPITime.TimeBeginPeriod(1); // Open file using (FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { if (has2UpdatePosition) { file.Seek(newPosition, SeekOrigin.Begin); has2UpdatePosition = false; resetTimeOffsetRequired = true; } while (!token.IsCancellationRequested) { // Paused if (CurrentState == State.Paused) { Thread.Sleep(500); } else { if ((n_bytes = file.Read(buffer, 0, HelperTools.headerSize)) == HelperTools.headerSize) { // Get fields replayTime = time = BitConverter.ToInt64(buffer, 0); hashedID = BitConverter.ToInt32(buffer, 8); ipport = BitConverter.ToUInt64(buffer, 12); size = BitConverter.ToInt32(buffer, 20); // Update time in secs timeStatusS = (int)(replayTime / 1000_000); // Read Payload n_bytes = file.Read(buffer, 0, size); nowTime = HelperTools.GetLocalMicrosTime(); if (resetTimeOffsetRequired) { timeoffset = nowTime - time; waitTime = 0; resetTimeOffsetRequired = false; } else { nowTime -= timeoffset; waitTime = time - nowTime; if (waitTime > 1000) // 1ms { Thread.Sleep((int)(waitTime / 1000)); } } // Send Send(ipport, hashedID, buffer, 0, n_bytes); // Update Progress if (timeStatusS != lastTimeStatusS) { Task.Run(() => ProgressEvent?.Invoke(timeStatusS)); lastTimeStatusS = timeStatusS; } if (has2UpdatePosition) { file.Seek(newPosition, SeekOrigin.Begin); has2UpdatePosition = false; resetTimeOffsetRequired = true; } } else { // End of File cancelSource.Cancel(); // Update Stop Status CurrentState = State.Stoped; StatusEvent?.Invoke(CurrentState); foreach (var k in _dataRate.Keys.ToList()) { _dataRate[k] = 0; } DataRateEvent?.Invoke(_dataRate); // rewind Seek(0); eventTimer.Dispose(); eventTimer = null; ProgressEvent?.Invoke(0); } } } } WinAPI.WinAPITime.TimeEndPeriod(1); }
public ICollection <PlayPeerInfo> LoadFile(string _filePath, out DateTime _startTime, out int _lastTime) { Stop(); _lastTime = 0; filePath = _filePath; idxFilePath = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath)) + ".idx"; // Init times 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; byte[] auxBuff = bytePool.Rent(4096); // Read DateTime, Secs Interval and Peers info if (idxBinaryReader.Read(auxBuff, 0, 16) == 16) { RecDateTime = _startTime = HelperTools.fromMillis(BitConverter.ToInt64(auxBuff, 0)); secsIdxInterval = BitConverter.ToInt32(auxBuff, 8); nPeers = BitConverter.ToInt32(auxBuff, 12); // 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; } // Get All times to cache while (idxBinaryReader.Read(auxBuff, 0, HelperTools.idxIndexSize) == HelperTools.idxIndexSize) { time = BitConverter.ToInt32(auxBuff, 0); position = BitConverter.ToInt64(auxBuff, 4); try { timeIndexes.Add(time, position); } catch (Exception e) { logger?.LogError(e, "Error reading idx file. TimeIndexes"); } } _lastTime = lastTime = time; } bytePool.Return(auxBuff); } return(udpSenders.Values); }