Modified closing

This commit is contained in:
sta 2014-01-31 21:36:20 +09:00
parent 4e420c6bc3
commit 31463022ee
5 changed files with 79 additions and 106 deletions

View File

@ -237,13 +237,6 @@ namespace WebSocketSharp
: null; : null;
} }
internal static string CheckIfValidCloseData (this byte [] data)
{
return data.Length > 125
? "'reason' length must be less."
: null;
}
internal static string CheckIfValidCloseStatusCode (this ushort code) internal static string CheckIfValidCloseStatusCode (this ushort code)
{ {
return !code.IsCloseStatusCode () return !code.IsCloseStatusCode ()

View File

@ -6,7 +6,7 @@
* *
* The MIT License * 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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -535,6 +535,11 @@ namespace WebSocketSharp.Server
return true; return true;
} }
private string checkIfCanStop (Func<string> checkParams)
{
return _state.CheckIfStarted () ?? checkParams ();
}
private string checkIfCertExists () private string checkIfCertExists ()
{ {
return _secure && return _secure &&
@ -757,19 +762,19 @@ namespace WebSocketSharp.Server
/// and <see cref="string"/> used to stop the WebSocket services. /// and <see cref="string"/> used to stop the WebSocket services.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason /// A <see cref="ushort"/> that represents the status code indicating the
/// for stop. /// reason for stop.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop. /// A <see cref="string"/> that represents the reason for stop.
/// </param> /// </param>
public void Stop (ushort code, string reason) public void Stop (ushort code, string reason)
{ {
byte [] data = null; byte [] data = null;
lock (_sync) { lock (_sync) {
var msg = _state.CheckIfStarted () ?? var msg = checkIfCanStop (
code.CheckIfValidCloseStatusCode () ?? () => code.CheckIfValidCloseStatusCode () ??
(data = code.Append (reason)).CheckIfValidCloseData (); (data = code.Append (reason)).CheckIfValidControlData ("reason"));
if (msg != null) { if (msg != null) {
_logger.Error ( _logger.Error (
@ -789,23 +794,23 @@ namespace WebSocketSharp.Server
} }
/// <summary> /// <summary>
/// Stops receiving the HTTP requests with the specified /// Stops receiving the HTTP requests with the specified <see cref="CloseStatusCode"/>
/// <see cref="CloseStatusCode"/> and <see cref="string"/> used to stop the /// and <see cref="string"/> used to stop the WebSocket services.
/// WebSocket services.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that represent the status /// One of the <see cref="CloseStatusCode"/> enum values, represents the
/// codes indicating the reasons for stop. /// status code indicating the reasons for stop.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop. /// A <see cref="string"/> that represents the reason for stop.
/// </param> /// </param>
public void Stop (CloseStatusCode code, string reason) public void Stop (CloseStatusCode code, string reason)
{ {
byte [] data = null; byte [] data = null;
lock (_sync) { lock (_sync) {
var msg = _state.CheckIfStarted () ?? var msg = checkIfCanStop (
(data = ((ushort) code).Append (reason)).CheckIfValidCloseData (); () => (data = ((ushort) code).Append (reason))
.CheckIfValidControlData ("reason"));
if (msg != null) { if (msg != null) {
_logger.Error ( _logger.Error (

View File

@ -6,7 +6,7 @@
* *
* The MIT License * 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 * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -549,6 +549,11 @@ namespace WebSocketSharp.Server
return true; return true;
} }
private string checkIfCanStop (Func<string> checkParams)
{
return _state.CheckIfStarted () ?? checkParams ();
}
private string checkIfCertExists () private string checkIfCertExists ()
{ {
return _secure && _cert == null return _secure && _cert == null
@ -773,19 +778,19 @@ namespace WebSocketSharp.Server
/// <see cref="ushort"/> and <see cref="string"/>. /// <see cref="ushort"/> and <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that contains a status code indicating the reason /// A <see cref="ushort"/> that represents the status code indicating the
/// for stop. /// reason for stop.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop. /// A <see cref="string"/> that represents the reason for stop.
/// </param> /// </param>
public void Stop (ushort code, string reason) public void Stop (ushort code, string reason)
{ {
byte [] data = null; byte [] data = null;
lock (_sync) { lock (_sync) {
var msg = _state.CheckIfStarted () ?? var msg = checkIfCanStop (
code.CheckIfValidCloseStatusCode () ?? () => code.CheckIfValidCloseStatusCode () ??
(data = code.Append (reason)).CheckIfValidCloseData (); (data = code.Append (reason)).CheckIfValidControlData ("reason"));
if (msg != null) { if (msg != null) {
_logger.Error ( _logger.Error (
@ -809,18 +814,19 @@ namespace WebSocketSharp.Server
/// <see cref="CloseStatusCode"/> and <see cref="string"/>. /// <see cref="CloseStatusCode"/> and <see cref="string"/>.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that represent the status /// One of the <see cref="CloseStatusCode"/> enum values, represents the
/// codes indicating the reasons for stop. /// status code indicating the reason for stop.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that contains the reason for stop. /// A <see cref="string"/> that represents the reason for stop.
/// </param> /// </param>
public void Stop (CloseStatusCode code, string reason) public void Stop (CloseStatusCode code, string reason)
{ {
byte [] data = null; byte [] data = null;
lock (_sync) { lock (_sync) {
var msg = _state.CheckIfStarted () ?? var msg = checkIfCanStop (
(data = ((ushort) code).Append (reason)).CheckIfValidCloseData (); () => (data = ((ushort) code).Append (reason))
.CheckIfValidControlData ("reason"));
if (msg != null) { if (msg != null) {
_logger.Error ( _logger.Error (

View File

@ -400,48 +400,6 @@ namespace WebSocketSharp.Server
_websocket.SendAsync (stream, length, completed); _websocket.SendAsync (stream, length, completed);
} }
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance.
/// </summary>
protected void Stop ()
{
if (_websocket != null)
_websocket.Close ();
}
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance with the
/// specified <see cref="ushort"/> and <see cref="string"/>.
/// </summary>
/// <param name="code">
/// A <see cref="ushort"/> that represents the status code for stop.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that represents the reason for stop.
/// </param>
protected void Stop (ushort code, string reason)
{
if (_websocket != null)
_websocket.Close (code, reason);
}
/// <summary>
/// Stops the current <see cref="WebSocketService"/> instance with the
/// specified <see cref="CloseStatusCode"/> and <see cref="string"/>.
/// </summary>
/// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicate the status
/// codes for stop.
/// </param>
/// <param name="reason">
/// A <see cref="string"/> that represents the reason for stop.
/// </param>
protected void Stop (CloseStatusCode code, string reason)
{
if (_websocket != null)
_websocket.Close (code, reason);
}
/// <summary> /// <summary>
/// Validates the HTTP Cookies used in the WebSocket connection request. /// Validates the HTTP Cookies used in the WebSocket connection request.
/// </summary> /// </summary>

View File

@ -638,6 +638,11 @@ namespace WebSocketSharp
: null; : null;
} }
private string checkIfCanClose (Func<string> checkParams)
{
return _readyState.CheckIfClosable () ?? checkParams ();
}
private string checkIfCanConnect () private string checkIfCanConnect ()
{ {
return !_client && _readyState == WebSocketState.CLOSED return !_client && _readyState == WebSocketState.CLOSED
@ -1523,7 +1528,8 @@ namespace WebSocketSharp
/// isn't in the allowable range of the WebSocket close status code. /// isn't in the allowable range of the WebSocket close status code.
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that indicates the status code for closure. /// A <see cref="ushort"/> that represents the status code that indicates the
/// reason for closure.
/// </param> /// </param>
public void Close (ushort code) public void Close (ushort code)
{ {
@ -1535,8 +1541,8 @@ namespace WebSocketSharp
/// and releases all associated resources. /// and releases all associated resources.
/// </summary> /// </summary>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicate the status /// One of the <see cref="CloseStatusCode"/> enum values, represents the status
/// codes for closure. /// code that indicates the reason for closure.
/// </param> /// </param>
public void Close (CloseStatusCode code) public void Close (CloseStatusCode code)
{ {
@ -1550,10 +1556,11 @@ namespace WebSocketSharp
/// <remarks> /// <remarks>
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/> /// This method emits a <see cref="OnError"/> event if <paramref name="code"/>
/// isn't in the allowable range of the WebSocket close status code or the /// isn't in the allowable range of the WebSocket close status code or the
/// length of <paramref name="reason"/> is greater than 123 bytes. /// size of <paramref name="reason"/> is greater than 123 bytes.
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that indicates the status code for closure. /// A <see cref="ushort"/> that represents the status code that indicates the
/// reason for closure.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for closure. /// A <see cref="string"/> that represents the reason for closure.
@ -1561,15 +1568,15 @@ namespace WebSocketSharp
public void Close (ushort code, string reason) public void Close (ushort code, string reason)
{ {
byte [] data = null; byte [] data = null;
var msg = _readyState.CheckIfClosable () ?? var msg = checkIfCanClose (
code.CheckIfValidCloseStatusCode () ?? () => code.CheckIfValidCloseStatusCode () ??
(data = code.Append (reason)).CheckIfValidCloseData (); (data = code.Append (reason)).CheckIfValidControlData ("reason"));
if (msg != null) { if (msg != null) {
_logger.Error ( _logger.Error (
String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason));
error (msg);
error (msg);
return; return;
} }
@ -1582,12 +1589,12 @@ namespace WebSocketSharp
/// and <see cref="string"/>, and releases all associated resources. /// and <see cref="string"/>, and releases all associated resources.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method emits a <see cref="OnError"/> event if the length of /// This method emits a <see cref="OnError"/> event if the size of
/// <paramref name="reason"/> is greater than 123 bytes. /// <paramref name="reason"/> is greater than 123 bytes.
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicate the status /// One of the <see cref="CloseStatusCode"/> enum values, represents the
/// codes for closure. /// status code that indicates the reason for closure.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for closure. /// A <see cref="string"/> that represents the reason for closure.
@ -1595,14 +1602,15 @@ namespace WebSocketSharp
public void Close (CloseStatusCode code, string reason) public void Close (CloseStatusCode code, string reason)
{ {
byte [] data = null; byte [] data = null;
var msg = _readyState.CheckIfClosable () ?? var msg = checkIfCanClose (
(data = ((ushort) code).Append (reason)).CheckIfValidCloseData (); () => (data = ((ushort) code).Append (reason))
.CheckIfValidControlData ("reason"));
if (msg != null) { if (msg != null) {
_logger.Error ( _logger.Error (
String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason));
error (msg);
error (msg);
return; return;
} }
@ -1645,7 +1653,8 @@ namespace WebSocketSharp
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that indicates the status code for closure. /// A <see cref="ushort"/> that represents the status code that indicates the
/// reason for closure.
/// </param> /// </param>
public void CloseAsync (ushort code) public void CloseAsync (ushort code)
{ {
@ -1660,8 +1669,8 @@ namespace WebSocketSharp
/// This method doesn't wait for the close to be complete. /// This method doesn't wait for the close to be complete.
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicate the status /// One of the <see cref="CloseStatusCode"/> enum values, represents the
/// codes for closure. /// status code that indicates the reason for closure.
/// </param> /// </param>
public void CloseAsync (CloseStatusCode code) public void CloseAsync (CloseStatusCode code)
{ {
@ -1680,11 +1689,12 @@ namespace WebSocketSharp
/// <para> /// <para>
/// This method emits a <see cref="OnError"/> event if <paramref name="code"/> /// This method emits a <see cref="OnError"/> event if <paramref name="code"/>
/// isn't in the allowable range of the WebSocket close status code or the /// isn't in the allowable range of the WebSocket close status code or the
/// length of <paramref name="reason"/> is greater than 123 bytes. /// size of <paramref name="reason"/> is greater than 123 bytes.
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// A <see cref="ushort"/> that indicates the status code for closure. /// A <see cref="ushort"/> that represents the status code that indicates the
/// reason for closure.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for closure. /// A <see cref="string"/> that represents the reason for closure.
@ -1692,15 +1702,15 @@ namespace WebSocketSharp
public void CloseAsync (ushort code, string reason) public void CloseAsync (ushort code, string reason)
{ {
byte [] data = null; byte [] data = null;
var msg = _readyState.CheckIfClosable () ?? var msg = checkIfCanClose (
code.CheckIfValidCloseStatusCode () ?? () => code.CheckIfValidCloseStatusCode () ??
(data = code.Append (reason)).CheckIfValidCloseData (); (data = code.Append (reason)).CheckIfValidControlData ("reason"));
if (msg != null) { if (msg != null) {
_logger.Error ( _logger.Error (
String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason));
error (msg);
error (msg);
return; return;
} }
@ -1718,13 +1728,13 @@ namespace WebSocketSharp
/// This method doesn't wait for the close to be complete. /// This method doesn't wait for the close to be complete.
/// </para> /// </para>
/// <para> /// <para>
/// This method emits a <see cref="OnError"/> event if the length of /// This method emits a <see cref="OnError"/> event if the size of
/// <paramref name="reason"/> is greater than 123 bytes. /// <paramref name="reason"/> is greater than 123 bytes.
/// </para> /// </para>
/// </remarks> /// </remarks>
/// <param name="code"> /// <param name="code">
/// One of the <see cref="CloseStatusCode"/> values that indicate the status /// One of the <see cref="CloseStatusCode"/> enum values, represents the
/// codes for closure. /// status code that indicates the reason for closure.
/// </param> /// </param>
/// <param name="reason"> /// <param name="reason">
/// A <see cref="string"/> that represents the reason for closure. /// A <see cref="string"/> that represents the reason for closure.
@ -1732,14 +1742,15 @@ namespace WebSocketSharp
public void CloseAsync (CloseStatusCode code, string reason) public void CloseAsync (CloseStatusCode code, string reason)
{ {
byte [] data = null; byte [] data = null;
var msg = _readyState.CheckIfClosable () ?? var msg = checkIfCanClose (
(data = ((ushort) code).Append (reason)).CheckIfValidCloseData (); () => (data = ((ushort) code).Append (reason))
.CheckIfValidControlData ("reason"));
if (msg != null) { if (msg != null) {
_logger.Error ( _logger.Error (
String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason));
error (msg);
error (msg);
return; return;
} }