Commit 9684afb8 authored by Jeremy Meng's avatar Jeremy Meng

Keep the exception throwing behavior close to the desktop implementation.

Trace IOException for stream.Read(). Otherwise the IO exception would be wrapped in an AggregateException, which would not be caught by the try-catch around BeginRead() method call.

Also unwrap AggregateException in EndRead() and re-throw its inner exception.
parent 17e82256
...@@ -659,6 +659,14 @@ void BeginReading() ...@@ -659,6 +659,14 @@ void BeginReading()
int space = EnsureSpaceAndComputeBytesToRead(); int space = EnsureSpaceAndComputeBytesToRead();
multiplexer.Trace("Beginning async read...", physicalName); multiplexer.Trace("Beginning async read...", physicalName);
var result = netStream.BeginRead(ioBuffer, ioBufferBytes, space, endRead, this); var result = netStream.BeginRead(ioBuffer, ioBufferBytes, space, endRead, this);
#if DNXCORE50
Task<int> t = (Task<int>)result;
if (t.Result == -1)
{
multiplexer.Trace("Could not connect: ", physicalName);
return;
}
#endif
if (result.CompletedSynchronously) if (result.CompletedSynchronously)
{ {
multiplexer.Trace("Completed synchronously: processing immediately", physicalName); multiplexer.Trace("Completed synchronously: processing immediately", physicalName);
...@@ -666,7 +674,13 @@ void BeginReading() ...@@ -666,7 +674,13 @@ void BeginReading()
} }
} while (keepReading); } while (keepReading);
} }
catch(System.IO.IOException ex) #if DNXCORE50
catch (AggregateException ex)
{
throw ex.InnerException;
}
#endif
catch (System.IO.IOException ex)
{ {
multiplexer.Trace("Could not connect: " + ex.Message, physicalName); multiplexer.Trace("Could not connect: " + ex.Message, physicalName);
} }
...@@ -1085,15 +1099,32 @@ internal static class StreamExtensions ...@@ -1085,15 +1099,32 @@ internal static class StreamExtensions
{ {
internal static IAsyncResult BeginRead(this Stream stream, byte[] buffer, int offset, int count, AsyncCallback ac, object state) internal static IAsyncResult BeginRead(this Stream stream, byte[] buffer, int offset, int count, AsyncCallback ac, object state)
{ {
Task<int> f = Task<int>.Factory.StartNew(_ => stream.Read(buffer, offset, count), state); Task<int> f = Task<int>.Factory.StartNew(_ => {
try
{
return stream.Read(buffer, offset, count);
}
catch (IOException ex)
{
System.Diagnostics.Trace.WriteLine("Could not connect: " + ex.InnerException.Message);
return -1;
}
}, state);
if (ac != null) f.ContinueWith(res => ac(f)); if (ac != null) f.ContinueWith(res => ac(f));
return f; return f;
} }
internal static int EndRead(this Stream stream, IAsyncResult ar) internal static int EndRead(this Stream stream, IAsyncResult ar)
{
try
{ {
return ((Task<int>)ar).Result; return ((Task<int>)ar).Result;
} }
catch (AggregateException ex)
{
throw ex.InnerException;
}
}
} }
#endif #endif
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment