From 2a8a9ca061e8ffa48666288c976baf50aa9fb02d Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 24 Feb 2014 19:59:46 +0900 Subject: [PATCH] Refactored WebSocket.cs --- websocket-sharp/Ext.cs | 2 +- websocket-sharp/WebSocket.cs | 66 ++++++++++++++++++++++-------------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index 5db2cda2..df6d04cb 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -481,7 +481,7 @@ namespace WebSocketSharp : code == CloseStatusCode.INCONSISTENT_DATA ? "An inconsistent data has been received." : code == CloseStatusCode.POLICY_VIOLATION - ? "A policy violation data has been received." + ? "A policy violation has occurred." : code == CloseStatusCode.TOO_BIG ? "A too big data has been received." : code == CloseStatusCode.IGNORE_EXTENSION diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 7569decd..c8d724e3 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -810,38 +810,52 @@ namespace WebSocketSharp private bool concatenateFragmentsInto (Stream dest) { - var frame = _stream.ReadFrame (); + while (true) { + var frame = _stream.ReadFrame (); - // MORE & CONT - if (!frame.IsFinal && frame.IsContinuation) { - dest.WriteBytes (frame.PayloadData.ApplicationData); - return concatenateFragmentsInto (dest); - } + if (frame.IsFinal) { + // FINAL - // FINAL & CONT - if (frame.IsFinal && frame.IsContinuation) { - dest.WriteBytes (frame.PayloadData.ApplicationData); - return true; - } + // CONT + if (frame.IsContinuation) { + dest.WriteBytes (frame.PayloadData.ApplicationData); + break; + } - // FINAL & PING - if (frame.IsFinal && frame.IsPing) { - acceptPingFrame (frame); - return concatenateFragmentsInto (dest); - } + // PING + if (frame.IsPing) { + acceptPingFrame (frame); + continue; + } - // FINAL & PONG - if (frame.IsFinal && frame.IsPong) { - acceptPongFrame (frame); - return concatenateFragmentsInto (dest); - } + // PONG + if (frame.IsPong) { + acceptPongFrame (frame); + continue; + } - // FINAL & CLOSE - if (frame.IsFinal && frame.IsClose) - return acceptCloseFrame (frame); + // CLOSE + if (frame.IsClose) + return acceptCloseFrame (frame); + } + else { + // MORE + + // CONT + if (frame.IsContinuation) { + dest.WriteBytes (frame.PayloadData.ApplicationData); + continue; + } + } + + // ? + return acceptUnsupportedFrame ( + frame, + CloseStatusCode.INCORRECT_DATA, + "An incorrect data has been received while receiving fragmented data."); + } - // ? - return acceptUnsupportedFrame (frame, CloseStatusCode.INCORRECT_DATA, null); + return true; } private bool connect ()