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