[Modify] Polish it

This commit is contained in:
sta 2016-06-22 15:11:33 +09:00
parent e2ab760ca6
commit 40c993a2de

View File

@ -650,39 +650,44 @@ namespace WebSocketSharp
} }
internal static void ReadBytesAsync ( internal static void ReadBytesAsync (
this Stream stream, int length, Action<byte[]> completed, Action<Exception> error) this Stream stream, int length, Action<byte[]> completed, Action<Exception> error
)
{ {
var buff = new byte[length]; var buff = new byte[length];
var offset = 0; var offset = 0;
var retry = 0; var retry = 0;
AsyncCallback callback = null; AsyncCallback callback = null;
callback = ar => { callback =
try { ar => {
var nread = stream.EndRead (ar); try {
if (nread == 0 && retry < _retry) { var nread = stream.EndRead (ar);
retry++; if (nread == 0 && retry < _retry) {
} retry++;
else if (nread == 0 || nread == length) { stream.BeginRead (buff, offset, length, callback, null);
if (completed != null)
completed (buff.SubArray (0, offset + nread)); return;
}
if (nread == 0 || nread == length) {
if (completed != null)
completed (buff.SubArray (0, offset + nread));
return;
}
return;
}
else {
retry = 0; retry = 0;
offset += nread;
length -= nread;
stream.BeginRead (buff, offset, length, callback, null);
} }
catch (Exception ex) {
offset += nread; if (error != null)
length -= nread; error (ex);
}
stream.BeginRead (buff, offset, length, callback, null); };
}
catch (Exception ex) {
if (error != null)
error (ex);
}
};
try { try {
stream.BeginRead (buff, offset, length, callback, null); stream.BeginRead (buff, offset, length, callback, null);