Exemplo n.º 1
0
        public void Write(MtStreamWritingContext ctx, byte[] stuff)
        {
#if DEBUG && !SILVERLIGHT
            Debug.Print("Write");
#endif

            try
            {
                AsyncCallback wroteCallback = null;
                wroteCallback = r =>
                {
                    _stream.EndWrite(r);

                    ctx._position += stuff.Length;
                };

                // Put cursor in the right place
                //_stream.Position = ctx._position;
                // TODO Fix this, for now ignore (assume cursor on the right place)

#if DEBUG && !SILVERLIGHT
                MultiTasksRuntime.DebugDisplayInfo();
#endif
                _stream.BeginWrite(stuff, 0, stuff.Length, wroteCallback, null);
            }
            catch (Exception e)
            {
                throw new Exception("Exception on Stream.write", e);
            }
        }
Exemplo n.º 2
0
        public void Read(MtStreamReadingContext ctx, Action <byte[], bool> readStuffCallback, Action doneCallback)
        {
#if DEBUG && !SILVERLIGHT
            Debug.Print("Read");
#endif

            try
            {
                var buffer = new byte[ReadBufferSize];

                AsyncCallback readCallback = null;

                readCallback = r =>
                {
                    var count = _stream.EndRead(r);

                    // Create a new array, copy read bytes to there
                    var read = new byte[count];

                    // Update position
                    ctx._position += count;

                    if (count > 0)
                    {
                        // Copy buffer to final count array
                        Array.Copy(buffer, read, count);

                        readStuffCallback(read, ctx._position >= _stream.Length);
                    }

                    if (ctx._position >= _stream.Length)
                    {
                        doneCallback();
                    }
                };

                if (ctx._position >= _stream.Length)
                {
                    throw new Exception("Can't read further, end of stream!");
                }

                // Put cursor in the right place
                _stream.Position = ctx._position;

#if DEBUG && !SILVERLIGHT
                MultiTasksRuntime.DebugDisplayInfo();
#endif
                _stream.BeginRead(buffer, 0, ReadBufferSize, readCallback, null);
            }
            catch (Exception e)
            {
                throw new Exception("Exception on Stream.read.", e);
            }
        }
Exemplo n.º 3
0
        internal MtResult SetValue(Func<object, MtObject> generateO)
        {
            // TODO Review this
            lock(_sync) 
            {
                if (generateO == null)
                    throw new Exception("Do not call MtResult.SetValue(async) with null!");

                if (_hasValue)
                    throw new Exception("Already has value!");

                try
                {
#if DEBUG && !SILVERLIGHT
                    MultiTasksRuntime.DebugDisplayInfo();
#endif

#if ALL_SYNC

                    {
                        var o = generateO(null);
#else // ALL_SYNC
                    ThreadPool.QueueUserWorkItem(state =>
                    {
                        var o = generateO(state);    
#endif // ALL_SYNC

                        _o = o;
                        _hasValue = true;

                        // Raise event!
                        _receivedValue.Set();
#if ALL_SYNC
                    }
#else // ALL_SYNC
                    });
#endif // ALL_SYNC

                    return this;
                }