Skip to content

Instantly share code, notes, and snippets.

@Tamschi
Last active August 29, 2015 13:59
Show Gist options
  • Save Tamschi/10505717 to your computer and use it in GitHub Desktop.
Save Tamschi/10505717 to your computer and use it in GitHub Desktop.
Blocking Stream ratchet thing
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
using System;
using System.IO;
using System.Threading;
public class RatchetStream : Stream
{
byte[] _buffer;
int _offset = 0;
AutoResetEvent _writeLock = new AutoResetEvent(true);
AutoResetEvent _readLock = new AutoResetEvent(false);
public RatchetStream(int bufferSize) { }
#region Stream implementation
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return false; } }
public override bool CanWrite { get { return true; } }
public override void Flush() { /* do nothing */ }
public override long Length { get { throw new NotSupportedException(); } }
public override long Position
{
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public override long Seek(long offset, SeekOrigin origin) { throw new NotSupportedException(); }
public override void SetLength(long value) { throw new NotSupportedException(); }
#endregion
public override int Read(byte[] buffer, int offset, int count)
{
_readLock.WaitOne();
count = Math.Min(count, _buffer.Length - _offset);
Array.Copy(_buffer, 0, buffer, offset, count);
_offset += count;
// Advance read but don't discard end signal.
if (_offset > 0 && _offset == _buffer.Length)
{
_offset = 0;
_buffer = null;
_writeLock.Set();
}
else
{ _readLock.Set(); }
return count;
}
public override void Write(byte[] buffer, int offset, int count)
{
_writeLock.WaitOne();
if (_buffer.Length == 0)
{
_writeLock.Set();
throw new InvalidOperationException("Can't write past end of stream signal.");
}
_buffer = new byte[count];
Array.Copy(buffer, offset, _buffer, 0, count);
if (count == 0)
{ _writeLock.Set(); }
_readLock.Set();
}
public void SignalEnd()
{ Write(new byte[0], 0, 0); }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment