Modified closing
This commit is contained in:
parent
0d938c6d5a
commit
11d3311f4f
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains the event data associated with a <see cref="WebSocket.OnClose"/> event.
|
||||
/// Contains the event data associated with a <see cref="WebSocket.OnClose"/>
|
||||
/// event.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// A <see cref="WebSocket.OnClose"/> event occurs when the WebSocket connection has been closed.
|
||||
/// If you want to get the reason for closure, you access the <see cref="CloseEventArgs.Code"/> or
|
||||
/// <see cref="CloseEventArgs.Reason"/> property.
|
||||
/// A <see cref="WebSocket.OnClose"/> event occurs when the WebSocket connection
|
||||
/// has been closed. If you want to get the reason for closure, you access the
|
||||
/// <see cref="Code"/> or <see cref="Reason"/> property.
|
||||
/// </remarks>
|
||||
public class CloseEventArgs : EventArgs
|
||||
{
|
||||
@ -79,7 +80,7 @@ namespace WebSocketSharp
|
||||
/// Gets the reason for closure.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// A <see cref="string"/> that contains the reason for closure if any.
|
||||
/// A <see cref="string"/> that represents the reason for closure if any.
|
||||
/// </value>
|
||||
public string Reason {
|
||||
get {
|
||||
@ -88,10 +89,12 @@ namespace WebSocketSharp
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the WebSocket connection has been closed cleanly.
|
||||
/// Gets a value indicating whether the WebSocket connection has been closed
|
||||
/// cleanly.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// <c>true</c> if the connection has been closed cleanly; otherwise, <c>false</c>.
|
||||
/// <c>true</c> if the WebSocket connection has been closed cleanly;
|
||||
/// otherwise, <c>false</c>.
|
||||
/// </value>
|
||||
public bool WasClean {
|
||||
get {
|
||||
|
@ -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
|
||||
{
|
||||
/// <summary>
|
||||
/// Contains the values of the status codes for the WebSocket connection closure.
|
||||
/// Contains the values of the status codes for the WebSocket connection
|
||||
/// closure.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// <para>
|
||||
/// The <b>CloseStatusCode</b> enumeration contains the values of the status codes for the WebSocket
|
||||
/// connection closure defined in <a href="http://tools.ietf.org/html/rfc6455#section-7.4.1">RFC 6455</a>
|
||||
/// for the WebSocket protocol.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// "<b>Reserved value</b>" 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.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// The CloseStatusCode enumeration contains the values of the status codes
|
||||
/// for the WebSocket connection closure defined in
|
||||
/// <a href="http://tools.ietf.org/html/rfc6455#section-7.4.1">RFC 6455</a>
|
||||
/// for the WebSocket protocol.
|
||||
/// </para>
|
||||
/// <para>
|
||||
/// "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.
|
||||
/// </para>
|
||||
/// </remarks>
|
||||
public enum CloseStatusCode : ushort
|
||||
{
|
||||
/// <summary>
|
||||
/// Equivalent to close status 1000. Indicates a normal closure.
|
||||
/// Equivalent to close status 1000.
|
||||
/// Indicates a normal closure.
|
||||
/// </summary>
|
||||
NORMAL = 1000,
|
||||
/// <summary>
|
||||
/// Equivalent to close status 1001. Indicates that an endpoint is "going away".
|
||||
/// Equivalent to close status 1001.
|
||||
/// Indicates that an endpoint is "going away".
|
||||
/// </summary>
|
||||
AWAY = 1001,
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
PROTOCOL_ERROR = 1002,
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
INCORRECT_DATA = 1003,
|
||||
/// <summary>
|
||||
/// Equivalent to close status 1004. Still undefined. Reserved value.
|
||||
/// Equivalent to close status 1004.
|
||||
/// Still undefined. Reserved value.
|
||||
/// </summary>
|
||||
UNDEFINED = 1004,
|
||||
/// <summary>
|
||||
@ -82,39 +88,42 @@ namespace WebSocketSharp
|
||||
ABNORMAL = 1006,
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
INCONSISTENT_DATA = 1007,
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
POLICY_VIOLATION = 1008,
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
TOO_BIG = 1009,
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
IGNORE_EXTENSION = 1010,
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
SERVER_ERROR = 1011,
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
TLS_HANDSHAKE_FAILURE = 1015
|
||||
}
|
||||
|
@ -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<PayloadData, bool, bool> 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<PayloadData, bool, bool> 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
|
||||
|
Loading…
Reference in New Issue
Block a user