diff --git a/websocket-sharp/CloseEventArgs.cs b/websocket-sharp/CloseEventArgs.cs index 35dba173..aea038bf 100644 --- a/websocket-sharp/CloseEventArgs.cs +++ b/websocket-sharp/CloseEventArgs.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 @@ -32,12 +32,13 @@ using System.Text; namespace WebSocketSharp { /// - /// Contains the event data associated with a event. + /// Contains the event data associated with a + /// event. /// /// - /// A event occurs when the WebSocket connection has been closed. - /// If you want to get the reason for closure, you access the or - /// property. + /// A event occurs when the WebSocket connection + /// has been closed. If you want to get the reason for closure, you access the + /// or property. /// public class CloseEventArgs : EventArgs { @@ -79,7 +80,7 @@ namespace WebSocketSharp /// Gets the reason for closure. /// /// - /// A that contains the reason for closure if any. + /// A that represents the reason for closure if any. /// public string Reason { get { @@ -88,10 +89,12 @@ namespace WebSocketSharp } /// - /// Gets a value indicating whether the WebSocket connection has been closed cleanly. + /// Gets a value indicating whether the WebSocket connection has been closed + /// cleanly. /// /// - /// true if the connection has been closed cleanly; otherwise, false. + /// true if the WebSocket connection has been closed cleanly; + /// otherwise, false. /// public bool WasClean { get { diff --git a/websocket-sharp/CloseStatusCode.cs b/websocket-sharp/CloseStatusCode.cs index 1cf2f058..bc2f6508 100644 --- a/websocket-sharp/CloseStatusCode.cs +++ b/websocket-sharp/CloseStatusCode.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 @@ -31,43 +31,49 @@ using System; namespace WebSocketSharp { /// - /// Contains the values of the status codes for the WebSocket connection closure. + /// Contains the values of the status codes for the WebSocket connection + /// closure. /// /// - /// - /// The CloseStatusCode enumeration contains the values of the status codes for the WebSocket - /// connection closure defined in RFC 6455 - /// for the WebSocket protocol. - /// - /// - /// "Reserved value" must not be set as a status code in a close control frame by an endpoint. - /// It is designated for use in applications expecting a status code to indicate that connection - /// was closed due to a system grounds. - /// + /// + /// The CloseStatusCode enumeration contains the values of the status codes + /// for the WebSocket connection closure defined in + /// RFC 6455 + /// for the WebSocket protocol. + /// + /// + /// "Reserved value" must not be set as a status code in a close control frame + /// by an endpoint. It's designated for use in applications expecting a status + /// code to indicate that the connection was closed due to a system grounds. + /// /// public enum CloseStatusCode : ushort { /// - /// Equivalent to close status 1000. Indicates a normal closure. + /// Equivalent to close status 1000. + /// Indicates a normal closure. /// NORMAL = 1000, /// - /// Equivalent to close status 1001. Indicates that an endpoint is "going away". + /// Equivalent to close status 1001. + /// Indicates that an endpoint is "going away". /// AWAY = 1001, /// /// Equivalent to close status 1002. - /// Indicates that an endpoint is terminating the connection due to a protocol error. + /// Indicates that an endpoint is terminating the connection due to a protocol + /// error. /// PROTOCOL_ERROR = 1002, /// /// Equivalent to close status 1003. - /// Indicates that an endpoint is terminating the connection because it has received - /// a type of data it cannot accept. + /// Indicates that an endpoint is terminating the connection because it has + /// received a type of data it cannot accept. /// INCORRECT_DATA = 1003, /// - /// Equivalent to close status 1004. Still undefined. Reserved value. + /// Equivalent to close status 1004. + /// Still undefined. Reserved value. /// UNDEFINED = 1004, /// @@ -82,39 +88,42 @@ namespace WebSocketSharp ABNORMAL = 1006, /// /// Equivalent to close status 1007. - /// Indicates that an endpoint is terminating the connection because it has received - /// data within a message that was not consistent with the type of the message. + /// Indicates that an endpoint is terminating the connection because it has + /// received the data within a message that wasn't consistent with the type of + /// the message. /// INCONSISTENT_DATA = 1007, /// /// Equivalent to close status 1008. - /// Indicates that an endpoint is terminating the connection because it has received - /// a message that violates its policy. + /// Indicates that an endpoint is terminating the connection because it has + /// received a message that violates its policy. /// POLICY_VIOLATION = 1008, /// /// Equivalent to close status 1009. - /// Indicates that an endpoint is terminating the connection because it has received - /// a message that is too big to process. + /// Indicates that an endpoint is terminating the connection because it has + /// received a message that is too big to process. /// TOO_BIG = 1009, /// /// Equivalent to close status 1010. - /// Indicates that an endpoint (client) is terminating the connection because it has expected - /// the server to negotiate one or more extension, but the server didn't return them - /// in the response message of the WebSocket handshake. + /// Indicates that an endpoint (client) is terminating the connection because + /// it has expected the server to negotiate one or more extension, but the + /// server didn't return them in the response message of the WebSocket + /// handshake. /// IGNORE_EXTENSION = 1010, /// /// Equivalent to close status 1011. - /// Indicates that a server is terminating the connection because it encountered - /// an unexpected condition that prevented it from fulfilling the request. + /// Indicates that the server is terminating the connection because it has + /// encountered an unexpected condition that prevented it from fulfilling the + /// request. /// SERVER_ERROR = 1011, /// /// Equivalent to close status 1015. - /// Indicates that the connection was closed due to a failure to perform a TLS handshake. - /// Reserved value. + /// Indicates that the connection was closed due to a failure to perform a TLS + /// handshake. Reserved value. /// TLS_HANDSHAKE_FAILURE = 1015 } diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index a3cd6e68..1d50cc42 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -555,17 +555,6 @@ namespace WebSocketSharp : null; } - private void close (CloseEventArgs args) - { - try { - OnClose.Emit (this, args); - } - catch (Exception ex) { - _logger.Fatal (ex.ToString ()); - error ("An exception has occurred while OnClose."); - } - } - private void close (CloseStatusCode code, string reason, bool wait) { close ( @@ -587,35 +576,56 @@ namespace WebSocketSharp _readyState = WebSocketState.CLOSING; } + _logger.Trace ("Start closing handshake."); + var args = new CloseEventArgs (payload); + args.WasClean = + _client + ? closeHandshake ( + send ? WsFrame.CreateCloseFrame (Mask.MASK, payload).ToByteArray () + : null, + wait ? 5000 : 0, + closeClientResources) + : closeHandshake ( + send ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray () + : null, + wait ? 1000 : 0, + closeServerResources); + + _logger.Trace ("End closing handshake."); + + _readyState = WebSocketState.CLOSED; try { - _logger.Trace ("Start closing handshake."); - - args.WasClean = - _client - ? close ( - send ? WsFrame.CreateCloseFrame (Mask.MASK, payload).ToByteArray () - : null, - wait ? 5000 : 0, - closeClientResources) - : close ( - send ? WsFrame.CreateCloseFrame (Mask.UNMASK, payload).ToByteArray () - : null, - wait ? 1000 : 0, - closeServerResources); - - _logger.Trace ("End closing handshake."); + OnClose.Emit (this, args); } catch (Exception ex) { _logger.Fatal (ex.ToString ()); - error ("An exception has occurred while closing."); + error ("An exception has occurred while OnClose."); } - - _readyState = WebSocketState.CLOSED; - close (args); } - private bool close (byte [] frameAsBytes, int timeOut, Action release) + private void closeAsync (PayloadData payload, bool send, bool wait) + { + Action closer = close; + closer.BeginInvoke ( + payload, send, wait, ar => closer.EndInvoke (ar), null); + } + + // As client + private void closeClientResources () + { + if (_stream != null) { + _stream.Dispose (); + _stream = null; + } + + if (_tcpClient != null) { + _tcpClient.Close (); + _tcpClient = null; + } + } + + private bool closeHandshake (byte [] frameAsBytes, int timeOut, Action release) { var sent = frameAsBytes != null && _stream.Write (frameAsBytes); var received = timeOut == 0 || @@ -640,27 +650,6 @@ namespace WebSocketSharp return result; } - private void closeAsync (PayloadData payload, bool send, bool wait) - { - Action closer = close; - closer.BeginInvoke ( - payload, send, wait, ar => closer.EndInvoke (ar), null); - } - - // As client - private void closeClientResources () - { - if (_stream != null) { - _stream.Dispose (); - _stream = null; - } - - if (_tcpClient != null) { - _tcpClient.Close (); - _tcpClient = null; - } - } - // As server private void closeServerResources () { @@ -1401,15 +1390,16 @@ namespace WebSocketSharp _readyState = WebSocketState.CLOSING; } + args.WasClean = closeHandshake ( + frameAsBytes, waitTimeOut, closeServerResources); + + _readyState = WebSocketState.CLOSED; try { - args.WasClean = close (frameAsBytes, waitTimeOut, closeServerResources); + OnClose.Emit (this, args); } catch (Exception ex) { _logger.Fatal (ex.ToString ()); } - - _readyState = WebSocketState.CLOSED; - close (args); } // As server