public void Dispose()
        {
            object tmp = Inner;

            if (tmp == null)
            {
                throw new ObjectDisposedException("Cannot dispose already disposed object.");
            }
            lock (tmp)
            {
                container.NotifyDisposed(mode);
                container = null;

                // No need for finalizer.
                GC.SuppressFinalize(this);
            }
        }
Example #2
0
        /// <summary>
        /// Helper; creates cached TS.
        /// </summary>
        private ManagedTypedStream GetCachedTypedStream(string t, OpenMode mode)
        {
            // First update all unused typed streams containers.
            typedStreams.RemoveAll(delegate(TypedStreamContainer c) {
                if (!c.IsAlive)
                {
                    return(true);
                }
                return(false);
            });

            // Foreach stream.
            foreach (TypedStreamContainer c in typedStreams)
            {
                if (c.Type == t)
                {
                    ManagedTypedStream stream = c.Create(mode);
                    if (stream != null)
                    {
                        return(stream);
                    }
                    break;
                }
            }

            // We have to create it.


            // We ask node to provide TS.
            IDriverTypedStream s = node.GetTypedStream(OpenMode.ReadWrite, t);

            if (s == null)
            {
                return(null);
            }

            TypedStreamContainer cont = new TypedStreamContainer(t, s);

            // We add it in front, to make sure we first check this new type.
            typedStreams.Insert(0, cont);
            return(cont.Create(mode));
        }
Example #3
0
        public void RemoveTypedStream(Type t)
        {
            lock (SyncRoot)
            {
                AssertNotDisposed();
                AssertCurrentVersion();

                // We first make sure it exists.
                ManagedTypedStream stream = GetCachedTypedStream(t.FullName, OpenMode.Read);
                if (stream == null)
                {
                    throw new TypedStreamNotFoundException("Cannot remove unexisting typed stream.");
                }

                // We check it's usage, there must only one read stream opened.
                TypedStreamContainer container = typedStreams.Find(
                    delegate(TypedStreamContainer c)
                {
                    if (c.Type == t.FullName)
                    {
                        return(true);
                    }
                    return(false);
                });

                // We obtained the container, make sure only one stream is using it.
                if (container.ReadCount != 1)
                {
                    stream.Dispose();
                    throw new InvalidOperationException("Cannot delete typed stream in usage.");
                }

                // We must dispose it, invalidating the container.
                stream.Dispose();

                // We can delete it.
                node.RemoveTypedStream(t.FullName);
            }
        }
 /// <summary>
 /// Constructor.
 /// </summary>
 /// <param name="c">Container.</param>
 /// <param name="openMode">The open mode.</param>
 internal ManagedTypedStream(TypedStreamContainer container, OpenMode openMode)
 {
     this.container = container;
     mode           = openMode;
     usesRaw        = Inner.UsesRaw;
 }