Inheritance: BasicAsyncResult
Beispiel #1
0
        }         // BufferCopy

        internal static IAsyncResult BeginAsyncCopyStream(
            Stream source, Stream target,
            bool asyncRead, bool asyncWrite,
            bool closeSource, bool closeTarget,
            AsyncCallback callback, Object state)
        {
            AsyncCopyStreamResult streamState = new AsyncCopyStreamResult(callback, state);

            byte[] buffer = CoreChannel.BufferPool.GetBuffer();

            streamState.Source      = source;
            streamState.Target      = target;
            streamState.Buffer      = buffer;
            streamState.AsyncRead   = asyncRead;
            streamState.AsyncWrite  = asyncWrite;
            streamState.CloseSource = closeSource;
            streamState.CloseTarget = closeTarget;

            try
            {
                AsyncCopyReadHelper(streamState);
            }
            catch (Exception e)
            {
                streamState.SetComplete(null, e);
            }
            catch {
                streamState.SetComplete(null, new Exception(CoreChannel.GetResourceString("Remoting_nonClsCompliantException")));
            }

            return(streamState);
        } // BeginAsyncCopyStream
Beispiel #2
0
        } // AsyncCopyWriteHelper

        private static void AsyncCopyStreamReadCallback(IAsyncResult iar)
        {
            AsyncCopyStreamResult state = (AsyncCopyStreamResult)iar.AsyncState;

            try
            {
                Stream source = state.Source;

                int bytesRead = source.EndRead(iar);
                if (bytesRead == 0)
                {
                    state.SetComplete(null, null);
                }
                else
                if (bytesRead < 0)
                {
                    throw new RemotingException(
                              CoreChannel.GetResourceString("Remoting_Stream_UnknownReadError"));
                }
                else
                {
                    AsyncCopyWriteHelper(state, bytesRead);
                }
            }
            catch (Exception e)
            {
                state.SetComplete(null, e);
            }
            catch {
                state.SetComplete(null, new Exception(CoreChannel.GetResourceString("Remoting_nonClsCompliantException")));
            }
        } // AsyncCopyStreamReadCallback
Beispiel #3
0
        } // EndAsyncCopyStream

        private static void AsyncCopyReadHelper(AsyncCopyStreamResult streamState)
        {
            // There is no try-catch here because the calling method always has a try-catch.

            if (streamState.AsyncRead)
            {
                byte[] buffer = streamState.Buffer;
                streamState.Source.BeginRead(buffer, 0, buffer.Length, _asyncCopyStreamReadCallback, streamState);
            }
            else
            {
                byte[] buffer    = streamState.Buffer;
                int    bytesRead = streamState.Source.Read(buffer, 0, buffer.Length);
                if (bytesRead == 0)
                {
                    streamState.SetComplete(null, null);
                }
                else
                if (bytesRead < 0)
                {
                    throw new RemotingException(
                              CoreChannel.GetResourceString("Remoting_Stream_UnknownReadError"));
                }
                else
                {
                    AsyncCopyWriteHelper(streamState, bytesRead);
                }
            }
        } // AsyncCopyReadHelper
 private static void AsyncCopyReadHelper(AsyncCopyStreamResult streamState)
 {
     if (streamState.AsyncRead)
     {
         byte[] buffer = streamState.Buffer;
         streamState.Source.BeginRead(buffer, 0, buffer.Length, _asyncCopyStreamReadCallback, streamState);
     }
     else
     {
         byte[] buffer2 = streamState.Buffer;
         int bytesRead = streamState.Source.Read(buffer2, 0, buffer2.Length);
         if (bytesRead == 0)
         {
             streamState.SetComplete(null, null);
         }
         else
         {
             if (bytesRead < 0)
             {
                 throw new RemotingException(CoreChannel.GetResourceString("Remoting_Stream_UnknownReadError"));
             }
             AsyncCopyWriteHelper(streamState, bytesRead);
         }
     }
 }
        private static void AsyncCopyStreamReadCallback(IAsyncResult iar)
        {
            AsyncCopyStreamResult asyncState = (AsyncCopyStreamResult)iar.AsyncState;

            try
            {
                int bytesRead = asyncState.Source.EndRead(iar);
                if (bytesRead == 0)
                {
                    asyncState.SetComplete(null, null);
                }
                else
                {
                    if (bytesRead < 0)
                    {
                        throw new RemotingException(CoreChannel.GetResourceString("Remoting_Stream_UnknownReadError"));
                    }
                    AsyncCopyWriteHelper(asyncState, bytesRead);
                }
            }
            catch (Exception exception)
            {
                asyncState.SetComplete(null, exception);
            }
        }
 private static void AsyncCopyReadHelper(AsyncCopyStreamResult streamState)
 {
     if (streamState.AsyncRead)
     {
         byte[] buffer = streamState.Buffer;
         streamState.Source.BeginRead(buffer, 0, buffer.Length, _asyncCopyStreamReadCallback, streamState);
     }
     else
     {
         byte[] buffer2   = streamState.Buffer;
         int    bytesRead = streamState.Source.Read(buffer2, 0, buffer2.Length);
         if (bytesRead == 0)
         {
             streamState.SetComplete(null, null);
         }
         else
         {
             if (bytesRead < 0)
             {
                 throw new RemotingException(CoreChannel.GetResourceString("Remoting_Stream_UnknownReadError"));
             }
             AsyncCopyWriteHelper(streamState, bytesRead);
         }
     }
 }
        internal static void EndAsyncCopyStream(IAsyncResult iar)
        {
            AsyncCopyStreamResult result = (AsyncCopyStreamResult)iar;

            if (!iar.IsCompleted)
            {
                iar.AsyncWaitHandle.WaitOne();
            }
            if (result.Exception != null)
            {
                throw result.Exception;
            }
        }
 private static void AsyncCopyWriteHelper(AsyncCopyStreamResult streamState, int bytesRead)
 {
     if (streamState.AsyncWrite)
     {
         byte[] buffer = streamState.Buffer;
         streamState.Target.BeginWrite(buffer, 0, bytesRead, _asyncCopyStreamWriteCallback, streamState);
     }
     else
     {
         byte[] buffer2 = streamState.Buffer;
         streamState.Target.Write(buffer2, 0, bytesRead);
         AsyncCopyReadHelper(streamState);
     }
 }
 private static void AsyncCopyWriteHelper(AsyncCopyStreamResult streamState, int bytesRead)
 {
     if (streamState.AsyncWrite)
     {
         byte[] buffer = streamState.Buffer;
         streamState.Target.BeginWrite(buffer, 0, bytesRead, _asyncCopyStreamWriteCallback, streamState);
     }
     else
     {
         byte[] buffer2 = streamState.Buffer;
         streamState.Target.Write(buffer2, 0, bytesRead);
         AsyncCopyReadHelper(streamState);
     }
 }
        private static void AsyncCopyStreamWriteCallback(IAsyncResult iar)
        {
            AsyncCopyStreamResult asyncState = (AsyncCopyStreamResult)iar.AsyncState;

            try
            {
                asyncState.Target.EndWrite(iar);
                AsyncCopyReadHelper(asyncState);
            }
            catch (Exception exception)
            {
                asyncState.SetComplete(null, exception);
            }
        }
Beispiel #11
0
        } // AsyncCopyReadHelper

        private static void AsyncCopyWriteHelper(AsyncCopyStreamResult streamState, int bytesRead)
        {
            // There is no try-catch here because the calling method always has a try-catch.

            if (streamState.AsyncWrite)
            {
                byte[] buffer = streamState.Buffer;
                streamState.Target.BeginWrite(buffer, 0, bytesRead, _asyncCopyStreamWriteCallback, streamState);
            }
            else
            {
                byte[] buffer = streamState.Buffer;
                streamState.Target.Write(buffer, 0, bytesRead);

                AsyncCopyReadHelper(streamState);
            }
        } // AsyncCopyWriteHelper
Beispiel #12
0
        } // AsyncCopyStreamReadCallback

        private static void AsyncCopyStreamWriteCallback(IAsyncResult iar)
        {
            AsyncCopyStreamResult state = (AsyncCopyStreamResult)iar.AsyncState;

            try
            {
                state.Target.EndWrite(iar);

                AsyncCopyReadHelper(state);
            }
            catch (Exception e)
            {
                state.SetComplete(null, e);
            }
            catch {
                state.SetComplete(null, new Exception(CoreChannel.GetResourceString("Remoting_nonClsCompliantException")));
            }
        } // AsyncCopyStreamWriteCallback
                } // BufferCopy 

 


        internal static IAsyncResult BeginAsyncCopyStream(
            Stream source, Stream target, 
            bool asyncRead, bool asyncWrite,
            bool closeSource, bool closeTarget, 
            AsyncCallback callback, Object state) 
        {
            AsyncCopyStreamResult streamState = new AsyncCopyStreamResult(callback, state); 

            byte[] buffer = CoreChannel.BufferPool.GetBuffer();

            streamState.Source = source; 
            streamState.Target = target;
            streamState.Buffer = buffer; 
            streamState.AsyncRead = asyncRead; 
            streamState.AsyncWrite = asyncWrite;
            streamState.CloseSource = closeSource; 
            streamState.CloseTarget = closeTarget;

            try
            { 
                AsyncCopyReadHelper(streamState);
            } 
            catch (Exception e) 
            {
                streamState.SetComplete(null, e); 
            }

            return streamState;
        } // BeginAsyncCopyStream 
        } // AsyncCopyReadHelper
 
 
        private static void AsyncCopyWriteHelper(AsyncCopyStreamResult streamState, int bytesRead)
        { 
            // There is no try-catch here because the calling method always has a try-catch.

            if (streamState.AsyncWrite)
            { 
                byte[] buffer = streamState.Buffer;
                streamState.Target.BeginWrite(buffer, 0, bytesRead, _asyncCopyStreamWriteCallback, streamState); 
            } 
            else
            { 
                byte[] buffer = streamState.Buffer;
                streamState.Target.Write(buffer, 0, bytesRead);

                AsyncCopyReadHelper(streamState); 
            }
        } // AsyncCopyWriteHelper 
        } // EndAsyncCopyStream


        private static void AsyncCopyReadHelper(AsyncCopyStreamResult streamState) 
        {
            // There is no try-catch here because the calling method always has a try-catch. 
 
            if (streamState.AsyncRead)
            { 
                byte[] buffer = streamState.Buffer;
                streamState.Source.BeginRead(buffer, 0, buffer.Length, _asyncCopyStreamReadCallback, streamState);
            }
            else 
            {
                byte[] buffer = streamState.Buffer; 
                int bytesRead = streamState.Source.Read(buffer, 0, buffer.Length); 
                if (bytesRead == 0)
                { 
                    streamState.SetComplete(null, null);
                }
                else
                if (bytesRead < 0) 
                {
                    throw new RemotingException( 
                        CoreChannel.GetResourceString("Remoting_Stream_UnknownReadError")); 
                }
                else 
                {
                    AsyncCopyWriteHelper(streamState, bytesRead);
                }
            } 
        } // AsyncCopyReadHelper