From 7eb4e2ec613a5600b0a24fe0ec5b8e61ab1e57b7 Mon Sep 17 00:00:00 2001 From: sta Date: Sun, 24 Aug 2014 16:01:29 +0900 Subject: [PATCH] Refactored MessageEventArgs.cs --- websocket-sharp/MessageEventArgs.cs | 68 +++++++++++++++++------------ websocket-sharp/WebSocket.cs | 2 +- 2 files changed, 41 insertions(+), 29 deletions(-) diff --git a/websocket-sharp/MessageEventArgs.cs b/websocket-sharp/MessageEventArgs.cs index f61f8a2e..ec257c39 100644 --- a/websocket-sharp/MessageEventArgs.cs +++ b/websocket-sharp/MessageEventArgs.cs @@ -4,8 +4,8 @@ * * The MIT License * - * Copyright (c) 2012-2013 sta.blockhead - * + * Copyright (c) 2012-2014 sta.blockhead + * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights @@ -15,7 +15,7 @@ * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. - * + * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -35,10 +35,14 @@ namespace WebSocketSharp /// Contains the event data associated with a event. /// /// - /// A event occurs when the receives - /// a text or binary data frame. - /// If you want to get the received data, you access the or - /// property. + /// + /// A event occurs when the receives + /// a text or binary message. + /// + /// + /// If you would like to get the message data, you should access + /// the or property. + /// /// public class MessageEventArgs : EventArgs { @@ -52,21 +56,21 @@ namespace WebSocketSharp #region Internal Constructors - internal MessageEventArgs (Opcode opcode, byte[] data) + internal MessageEventArgs (WebSocketFrame frame) { - if ((ulong) data.LongLength > PayloadData.MaxLength) + _opcode = frame.Opcode; + _rawData = frame.PayloadData.ApplicationData; + _data = convertToString (_opcode, _rawData); + } + + internal MessageEventArgs (Opcode opcode, byte[] rawData) + { + if ((ulong) rawData.LongLength > PayloadData.MaxLength) throw new WebSocketException (CloseStatusCode.TooBig); _opcode = opcode; - _rawData = data; - _data = convertToString (opcode, data); - } - - internal MessageEventArgs (Opcode opcode, PayloadData payload) - { - _opcode = opcode; - _rawData = payload.ApplicationData; - _data = convertToString (opcode, _rawData); + _rawData = rawData; + _data = convertToString (opcode, rawData); } #endregion @@ -74,10 +78,18 @@ namespace WebSocketSharp #region Public Properties /// - /// Gets the received data as a . + /// Gets the message data as a . /// + /// + /// + /// If the message data is empty, this property returns . + /// + /// + /// Or if the message is a binary message, this property returns "Binary". + /// + /// /// - /// A that contains the received data. + /// A that represents the message data. /// public string Data { get { @@ -86,22 +98,22 @@ namespace WebSocketSharp } /// - /// Gets the received data as an array of . + /// Gets the message data as an array of . /// /// - /// An array of that contains the received data. + /// An array of that represents the message data. /// - public byte [] RawData { + public byte[] RawData { get { return _rawData; } } /// - /// Gets the type of the received data. + /// Gets the type of the message. /// /// - /// One of the values, indicates the type of the received data. + /// or . /// public Opcode Type { get { @@ -113,12 +125,12 @@ namespace WebSocketSharp #region Private Methods - private static string convertToString (Opcode opcode, byte [] data) + private static string convertToString (Opcode opcode, byte[] rawData) { - return data.LongLength == 0 + return rawData.LongLength == 0 ? String.Empty : opcode == Opcode.Text - ? Encoding.UTF8.GetString (data) + ? Encoding.UTF8.GetString (rawData) : opcode.ToString (); } diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 16d7399a..33e5c2e4 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -922,7 +922,7 @@ namespace WebSocketSharp var e = frame.IsCompressed ? new MessageEventArgs ( frame.Opcode, frame.PayloadData.ApplicationData.Decompress (_compression)) - : new MessageEventArgs (frame.Opcode, frame.PayloadData); + : new MessageEventArgs (frame); enqueueToMessageEventQueue (e); return true;