Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » General » Downloads
 
Add your own
alternative version

Tagged as

Lazy Alternatives - LazyAndWeak and BackgroundLoader

, 1 Dec 2011
This article will present two alternatives to Lazy.
LazyAlternatives.zip
LazyAlternatives
LazyAlternatives.suo
LazyAlternativesSample
Properties
Settings.settings
Pfz
Caching
Collections
DataTypes
DynamicObjects
Internal
Extensions
Factoring
Pfz.csproj.user
Pfz.Phone.csproj.user
Pfz.ruleset
Pfz.Silverlight.csproj.user
Pfz.snk
Pfz.suo
PhoneSpecific
Properties
Remoting
Instructions
Internal
Serializers
Udp
Serialization
BinaryBuiltIn
Threading
Contexts
Disposers
Unsafe
using System;
using System.IO.MemoryMappedFiles;
using System.Threading;

namespace Pfz.Remoting
{
	/// <summary>
	/// This class represents a communication stream based on MemoryMappedFiles. It only works locally, but
	/// is faster than TCP/IP or NamedPipes.
	/// </summary>
	public sealed class MemoryMappedFileWriter:
		IDisposable
	{
		private MemoryMappedFile _file;
		private MemoryMappedViewAccessor _accessor;
		private EventWaitHandle _sentEvent;
		private EventWaitHandle _readEvent;
		private int _mappedFileLength;
		private unsafe byte *_pointer;
		private unsafe MemoryMappedFileReaderWriterData *_data;
		private bool _eventSet;

		/// <summary>
		/// Creates a new MemoryMappedFileWriter with the given name and length, and optionally with the given
		/// timeout, which will be the time it will wait for client connections.
		/// </summary>
		public MemoryMappedFileWriter(string name, int length, int connectionTimeout = -1)
		{
			if (name == null)
				throw new ArgumentNullException("name");

			if (length <= 0)
				throw new ArgumentOutOfRangeException("length");

			if (connectionTimeout < -1)
				throw new ArgumentOutOfRangeException("connectionTimeout");

			unsafe
			{
				_file = MemoryMappedFile.CreateNew(name, length + sizeof(MemoryMappedFileReaderWriterData));
				_accessor = _file.CreateViewAccessor();
				byte *pointer = null;
				_accessor.SafeMemoryMappedViewHandle.AcquirePointer(ref pointer);
				(*(int *)pointer) = length;
				_mappedFileLength = length;

				_pointer = pointer;
				_data = (MemoryMappedFileReaderWriterData *)(pointer + length);
			}
			_accessor.Flush();

			_sentEvent = new EventWaitHandle(false, EventResetMode.AutoReset, name + ":Sent");
			_sentEvent.Set();

			_readEvent = new EventWaitHandle(false, EventResetMode.AutoReset, name + ":Read");
			if (!_readEvent.WaitOne(connectionTimeout))
				throw new TimeoutException("Timed-out waiting for client connection.");
		}

		/// <summary>
		/// Releases all resources used by this Memory-Mapped-File.
		/// </summary>
		public void Dispose()
		{
			var sentEvent = _sentEvent;

			unsafe
			{
				if (_data != null && sentEvent != null)
				{
					_data->_ended = true;

					sentEvent.Set();
				}
			}

			var accessor = _accessor;
			if (accessor != null)
			{
				_accessor = null;
				accessor.Dispose();
			}

			var file = _file;
			if (file != null)
			{
				_file = null;
				file.Dispose();
			}

			if (sentEvent != null)
			{
				_sentEvent = null;
				sentEvent.Dispose();
			}

			var readEvent = _readEvent;
			if (readEvent != null)
			{
				_readEvent = null;
				readEvent.Dispose();
			}
		}

		private int _timeout = 60000;
		/// <summary>
		/// Gets or sets the time-out used for writes.
		/// This is not the same as the connection time out and must be set explicity.
		/// </summary>
		public int Timeout
		{
			get
			{
				return _timeout;
			}
			set
			{
				if (value < -1)
					throw new ArgumentOutOfRangeException("value");

				_timeout = value;
			}
		}

		/// <summary>
		/// Gets the Length of the memory-mapped-file.
		/// </summary>
		public int MemoryMappedFileLength
		{
			get
			{
				return _mappedFileLength;
			}
		}

		/// <summary>
		/// Writes the buffer data.
		/// </summary>
		public void Write(byte[] buffer)
		{
			if (buffer == null)
				throw new ArgumentNullException("buffer");

			_Write(buffer, 0, buffer.Length);
		}

		/// <summary>
		/// Writes the buffer data.
		/// </summary>
		public void Write(byte[] buffer, int offset)
		{
			if (buffer == null)
				throw new ArgumentNullException("buffer");

			if (offset < 0 || offset >= buffer.Length)
				throw new ArgumentOutOfRangeException("offset");

			_Write(buffer, offset, buffer.Length - offset);
		}

		/// <summary>
		/// Writes the buffer data.
		/// </summary>
		public void Write(byte[] buffer, int offset, int count)
		{
			if (buffer == null)
				throw new ArgumentNullException("buffer");

			if (offset < 0 || offset > buffer.Length)
				throw new ArgumentOutOfRangeException("offset");

			if (count < 0 || offset + count > buffer.Length)
				throw new ArgumentOutOfRangeException("count");

			_Write(buffer, offset, count);
		}
		private void _Write(byte[] buffer, int offset, int count)
		{
			unsafe
			{
				while(count > 0)
				{
					int writerPosition = _data->_writerPosition;
					bool writerState = _data->_writerState;
					int readerPosition;
					bool readerState;

					while (true)
					{
						readerPosition = _data->_readerPosition;
						readerState = _data->_readerState;

						if (writerPosition != readerPosition || writerState == readerState)
							break;

						_eventSet = false;
						_sentEvent.Set();
						if (!_readEvent.WaitOne(_timeout))
							throw new TimeoutException("Write timed-out.");
					}

					int remaining;
					if (readerState == writerState)
						remaining = _mappedFileLength - writerPosition;
					else
						remaining = readerPosition - writerPosition;

					int toSend = Math.Min(count, remaining);
					fixed(byte *sourcePointer = buffer)
						UnsafeBuffer.BlockCopy(sourcePointer + offset, _pointer + writerPosition, toSend);

					offset += toSend;
					count -= toSend;

					writerPosition += toSend;
					if (writerPosition >= _mappedFileLength)
					{
						writerPosition = 0;
						_data->_writerState = !writerState;
					}

					_data->_writerPosition = writerPosition;

					if (!_eventSet)
					{
						_eventSet = true;
						_sentEvent.Set();
					}
				}
			}
		}

		/// <summary>
		/// Tells the other side that it can start reading.
		/// </summary>
		public void Flush()
		{
			_sentEvent.Set();
		}

		/// <summary>
		/// Writes a single byte of data.
		/// </summary>
		public void WriteByte(byte value)
		{
			unsafe
			{
				int writerPosition = _data->_writerPosition;
				bool writerState = _data->_writerState;

				while (writerPosition == _data->_readerPosition && writerState != _data->_readerState)
				{
					_eventSet = false;
					_sentEvent.Set();
					if (!_readEvent.WaitOne(_timeout))
						throw new TimeoutException("Write timed-out.");
				}

				_pointer[writerPosition] = value;

				writerPosition ++;
				if (writerPosition >= _mappedFileLength)
				{
					writerPosition = 0;
					_data->_writerState = !writerState;
				}

				_data->_writerPosition = writerPosition;

				if (!_eventSet)
				{
					_eventSet = true;
					_sentEvent.Set();
				}
			}
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paulo Zemek
Architect
Canada Canada
I started to program computers when I was 11 years old, as a hobbist, programming in AMOS Basic and Blitz Basic for Amiga.
At 12 I had my first try with assembler, but it was too difficult at the time. Then, in the same year, I learned C and, after learning C, I was finally able to learn assembler (for Motorola 680x0).
Not sure, but probably between 12 and 13, I started to learn C++. I always programmed "in an object oriented way", but using function pointers instead of virtual methods.
 
At 15 I started to learn Pascal at school and to use Delphi. At 16 I started my first internship (using Delphi). At 18 I started to work professionally using C++ and since then I've developed my programming skills as a professional developer in C++ and C#, generally creating libraries that help other developers do they work easier, faster and with less errors.
 
Want more info or simply want to contact me?
Take a look at: http://paulozemek.azurewebsites.net/
Or e-mail me at: paulozemek@outlook.com
 
Codeproject MVP 2012
Microsoft MVP 2013

| Advertise | Privacy | Mobile
Web03 | 2.8.140821.2 | Last Updated 1 Dec 2011
Article Copyright 2011 by Paulo Zemek
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid