void RateTimer_Elapsed(object sender, ElapsedEventArgs e) { try { RateSync.Wait(1); BytesPerSecond = RateCount; RateCount = 0; } finally { RateSync.Release(1); } if (BytesPerSecondUpdated != null) { BytesPerSecondUpdated(this, EventArgs.Empty); } }
/// <summary> /// Send a whole file, given a prebuffer and a postbuffer, and if the 8 bytes length is sent before the real message. /// </summary> /// <param name="filepath"></param> /// <param name="preBuffer"></param> /// <param name="postBuffer"></param> /// <param name="withLengthPrefixed"></param> /// <param name="preBufferIsBeforeLength">Weither the prebuffer is placed before the length prefix (if applicable)</param> /// <remarks>If withLengthPrefixed is true, it's important for the receiving end to know that he is receiving a longer a 8 bytes length prefix. For the receiving end and within this class, you can set ReadNextAsLong to true to do so.</remarks> public void SendFile(string filepath, byte[] preBuffer = null, byte[] postBuffer = null, bool withLengthPrefixed = true, bool preBufferIsBeforeLength = false) { try { WriteSync.Wait(1); if (preBuffer != null && preBufferIsBeforeLength) { FinalStream.Write(preBuffer, 0, preBuffer.Length); } if (withLengthPrefixed) { FinalStream.Write(BitConverter.GetBytes(IPAddress.HostToNetworkOrder(new FileInfo(filepath).Length)), 0, 8); } if (preBuffer != null && preBufferIsBeforeLength == false) { FinalStream.Write(preBuffer, 0, preBuffer.Length); } var buffer = new byte[FILEBUFFERSIZE]; using (var filefs = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read)) { int read = 0; while (true) { read = filefs.Read(buffer, 0, FILEBUFFERSIZE); if (read == 0) { break; } FinalStream.Write(buffer, 0, read); } } if (postBuffer != null) { FinalStream.Write(postBuffer, 0, postBuffer.Length); } } finally { WriteSync.Release(1); } }