[Modify] Polish it

This commit is contained in:
sta 2015-09-17 14:08:40 +09:00
parent 6e6bab85af
commit 6833c2b014

View File

@ -140,45 +140,6 @@ namespace WebSocketSharp
} }
} }
private static void readBytesAsync (
this Stream stream,
byte[] buffer,
int offset,
int count,
Action<byte[]> completed,
Action<Exception> error)
{
AsyncCallback callback = null;
callback = ar => {
try {
var nread = stream.EndRead (ar);
if (nread == 0 || nread == count) {
if (completed != null)
completed (buffer.SubArray (0, offset + nread));
return;
}
offset += nread;
count -= nread;
stream.BeginRead (buffer, offset, count, callback, null);
}
catch (Exception ex) {
if (error != null)
error (ex);
}
};
try {
stream.BeginRead (buffer, offset, count, callback, null);
}
catch (Exception ex) {
if (error != null)
error (ex);
}
}
private static void times (this ulong n, Action action) private static void times (this ulong n, Action action)
{ {
for (ulong i = 0; i < n; i++) for (ulong i = 0; i < n; i++)
@ -668,35 +629,45 @@ namespace WebSocketSharp
if (len < bufferLength) if (len < bufferLength)
bufferLength = (int) len; bufferLength = (int) len;
stream.readBytesAsync ( stream.BeginRead (
buff, buff,
0, 0,
bufferLength, bufferLength,
bytes => { ar => {
var nread = bytes.Length; try {
if (nread > 0) var nread = stream.EndRead (ar);
dest.Write (bytes, 0, nread); if (nread > 0)
dest.Write (buff, 0, nread);
if (nread == 0 || nread == len) { if (nread == 0 || nread == len) {
if (completed != null) { if (completed != null) {
dest.Close (); dest.Close ();
completed (dest.ToArray ()); completed (dest.ToArray ());
}
dest.Dispose ();
return;
} }
dest.Dispose (); read (len - nread);
return; }
catch (Exception ex) {
dest.Dispose ();
if (error != null)
error (ex);
} }
read (len - nread);
}, },
ex => { null);
dest.Dispose ();
if (error != null)
error (ex);
});
}; };
read (length); try {
read (length);
}
catch (Exception ex) {
dest.Dispose ();
if (error != null)
error (ex);
}
} }
internal static string RemovePrefix (this string value, params string[] prefixes) internal static string RemovePrefix (this string value, params string[] prefixes)