From 6e6bab85af6bfa861175f39773dedcf564b6414e Mon Sep 17 00:00:00 2001 From: sta Date: Wed, 16 Sep 2015 14:39:58 +0900 Subject: [PATCH] [Modify] Polish it --- websocket-sharp/Ext.cs | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 8fcd72ce..cf63bc36 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -619,7 +619,38 @@ namespace WebSocketSharp internal static void ReadBytesAsync ( this Stream stream, int length, Action completed, Action error) { - stream.readBytesAsync (new byte[length], 0, length, completed, error); + var buff = new byte[length]; + var offset = 0; + + AsyncCallback callback = null; + callback = ar => { + try { + var nread = stream.EndRead (ar); + if (nread == 0 || nread == length) { + if (completed != null) + completed (buff.SubArray (0, offset + nread)); + + return; + } + + offset += nread; + length -= nread; + + stream.BeginRead (buff, offset, length, callback, null); + } + catch (Exception ex) { + if (error != null) + error (ex); + } + }; + + try { + stream.BeginRead (buff, offset, length, callback, null); + } + catch (Exception ex) { + if (error != null) + error (ex); + } } internal static void ReadBytesAsync (