From 31463022ee9a739f8da3be44e2a61b2638055c8a Mon Sep 17 00:00:00 2001 From: sta Date: Fri, 31 Jan 2014 21:36:20 +0900 Subject: [PATCH] Modified closing --- websocket-sharp/Ext.cs | 7 --- websocket-sharp/Server/HttpServer.cs | 35 ++++++----- websocket-sharp/Server/WebSocketServer.cs | 30 +++++---- websocket-sharp/Server/WebSocketService.cs | 42 ------------- websocket-sharp/WebSocket.cs | 71 +++++++++++++--------- 5 files changed, 79 insertions(+), 106 deletions(-) diff --git a/websocket-sharp/Ext.cs b/websocket-sharp/Ext.cs index b08000fb..7e90ebf1 100644 --- a/websocket-sharp/Ext.cs +++ b/websocket-sharp/Ext.cs @@ -237,13 +237,6 @@ namespace WebSocketSharp : 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) { return !code.IsCloseStatusCode () diff --git a/websocket-sharp/Server/HttpServer.cs b/websocket-sharp/Server/HttpServer.cs index ca28cc8d..80876224 100644 --- a/websocket-sharp/Server/HttpServer.cs +++ b/websocket-sharp/Server/HttpServer.cs @@ -6,7 +6,7 @@ * * 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 @@ -535,6 +535,11 @@ namespace WebSocketSharp.Server return true; } + private string checkIfCanStop (Func checkParams) + { + return _state.CheckIfStarted () ?? checkParams (); + } + private string checkIfCertExists () { return _secure && @@ -757,19 +762,19 @@ namespace WebSocketSharp.Server /// and used to stop the WebSocket services. /// /// - /// A that contains a status code indicating the reason - /// for stop. + /// A that represents the status code indicating the + /// reason for stop. /// /// - /// A that contains the reason for stop. + /// A that represents the reason for stop. /// public void Stop (ushort code, string reason) { byte [] data = null; lock (_sync) { - var msg = _state.CheckIfStarted () ?? - code.CheckIfValidCloseStatusCode () ?? - (data = code.Append (reason)).CheckIfValidCloseData (); + var msg = checkIfCanStop ( + () => code.CheckIfValidCloseStatusCode () ?? + (data = code.Append (reason)).CheckIfValidControlData ("reason")); if (msg != null) { _logger.Error ( @@ -789,23 +794,23 @@ namespace WebSocketSharp.Server } /// - /// Stops receiving the HTTP requests with the specified - /// and used to stop the - /// WebSocket services. + /// Stops receiving the HTTP requests with the specified + /// and used to stop the WebSocket services. /// /// - /// One of the values that represent the status - /// codes indicating the reasons for stop. + /// One of the enum values, represents the + /// status code indicating the reasons for stop. /// /// - /// A that contains the reason for stop. + /// A that represents the reason for stop. /// public void Stop (CloseStatusCode code, string reason) { byte [] data = null; lock (_sync) { - var msg = _state.CheckIfStarted () ?? - (data = ((ushort) code).Append (reason)).CheckIfValidCloseData (); + var msg = checkIfCanStop ( + () => (data = ((ushort) code).Append (reason)) + .CheckIfValidControlData ("reason")); if (msg != null) { _logger.Error ( diff --git a/websocket-sharp/Server/WebSocketServer.cs b/websocket-sharp/Server/WebSocketServer.cs index 8e37d360..9d2f83cf 100644 --- a/websocket-sharp/Server/WebSocketServer.cs +++ b/websocket-sharp/Server/WebSocketServer.cs @@ -6,7 +6,7 @@ * * 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 @@ -549,6 +549,11 @@ namespace WebSocketSharp.Server return true; } + private string checkIfCanStop (Func checkParams) + { + return _state.CheckIfStarted () ?? checkParams (); + } + private string checkIfCertExists () { return _secure && _cert == null @@ -773,19 +778,19 @@ namespace WebSocketSharp.Server /// and . /// /// - /// A that contains a status code indicating the reason - /// for stop. + /// A that represents the status code indicating the + /// reason for stop. /// /// - /// A that contains the reason for stop. + /// A that represents the reason for stop. /// public void Stop (ushort code, string reason) { byte [] data = null; lock (_sync) { - var msg = _state.CheckIfStarted () ?? - code.CheckIfValidCloseStatusCode () ?? - (data = code.Append (reason)).CheckIfValidCloseData (); + var msg = checkIfCanStop ( + () => code.CheckIfValidCloseStatusCode () ?? + (data = code.Append (reason)).CheckIfValidControlData ("reason")); if (msg != null) { _logger.Error ( @@ -809,18 +814,19 @@ namespace WebSocketSharp.Server /// and . /// /// - /// One of the values that represent the status - /// codes indicating the reasons for stop. + /// One of the enum values, represents the + /// status code indicating the reason for stop. /// /// - /// A that contains the reason for stop. + /// A that represents the reason for stop. /// public void Stop (CloseStatusCode code, string reason) { byte [] data = null; lock (_sync) { - var msg = _state.CheckIfStarted () ?? - (data = ((ushort) code).Append (reason)).CheckIfValidCloseData (); + var msg = checkIfCanStop ( + () => (data = ((ushort) code).Append (reason)) + .CheckIfValidControlData ("reason")); if (msg != null) { _logger.Error ( diff --git a/websocket-sharp/Server/WebSocketService.cs b/websocket-sharp/Server/WebSocketService.cs index d8c3c063..2089c49a 100644 --- a/websocket-sharp/Server/WebSocketService.cs +++ b/websocket-sharp/Server/WebSocketService.cs @@ -400,48 +400,6 @@ namespace WebSocketSharp.Server _websocket.SendAsync (stream, length, completed); } - /// - /// Stops the current instance. - /// - protected void Stop () - { - if (_websocket != null) - _websocket.Close (); - } - - /// - /// Stops the current instance with the - /// specified and . - /// - /// - /// A that represents the status code for stop. - /// - /// - /// A that represents the reason for stop. - /// - protected void Stop (ushort code, string reason) - { - if (_websocket != null) - _websocket.Close (code, reason); - } - - /// - /// Stops the current instance with the - /// specified and . - /// - /// - /// One of the values that indicate the status - /// codes for stop. - /// - /// - /// A that represents the reason for stop. - /// - protected void Stop (CloseStatusCode code, string reason) - { - if (_websocket != null) - _websocket.Close (code, reason); - } - /// /// Validates the HTTP Cookies used in the WebSocket connection request. /// diff --git a/websocket-sharp/WebSocket.cs b/websocket-sharp/WebSocket.cs index 1551c76d..190d1594 100644 --- a/websocket-sharp/WebSocket.cs +++ b/websocket-sharp/WebSocket.cs @@ -638,6 +638,11 @@ namespace WebSocketSharp : null; } + private string checkIfCanClose (Func checkParams) + { + return _readyState.CheckIfClosable () ?? checkParams (); + } + private string checkIfCanConnect () { return !_client && _readyState == WebSocketState.CLOSED @@ -1523,7 +1528,8 @@ namespace WebSocketSharp /// isn't in the allowable range of the WebSocket close status code. /// /// - /// A that indicates the status code for closure. + /// A that represents the status code that indicates the + /// reason for closure. /// public void Close (ushort code) { @@ -1535,8 +1541,8 @@ namespace WebSocketSharp /// and releases all associated resources. /// /// - /// One of the values that indicate the status - /// codes for closure. + /// One of the enum values, represents the status + /// code that indicates the reason for closure. /// public void Close (CloseStatusCode code) { @@ -1550,10 +1556,11 @@ namespace WebSocketSharp /// /// This method emits a event if /// isn't in the allowable range of the WebSocket close status code or the - /// length of is greater than 123 bytes. + /// size of is greater than 123 bytes. /// /// - /// A that indicates the status code for closure. + /// A that represents the status code that indicates the + /// reason for closure. /// /// /// A that represents the reason for closure. @@ -1561,15 +1568,15 @@ namespace WebSocketSharp public void Close (ushort code, string reason) { byte [] data = null; - var msg = _readyState.CheckIfClosable () ?? - code.CheckIfValidCloseStatusCode () ?? - (data = code.Append (reason)).CheckIfValidCloseData (); + var msg = checkIfCanClose ( + () => code.CheckIfValidCloseStatusCode () ?? + (data = code.Append (reason)).CheckIfValidControlData ("reason")); if (msg != null) { _logger.Error ( String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); - error (msg); + error (msg); return; } @@ -1582,12 +1589,12 @@ namespace WebSocketSharp /// and , and releases all associated resources. /// /// - /// This method emits a event if the length of + /// This method emits a event if the size of /// is greater than 123 bytes. /// /// - /// One of the values that indicate the status - /// codes for closure. + /// One of the enum values, represents the + /// status code that indicates the reason for closure. /// /// /// A that represents the reason for closure. @@ -1595,14 +1602,15 @@ namespace WebSocketSharp public void Close (CloseStatusCode code, string reason) { byte [] data = null; - var msg = _readyState.CheckIfClosable () ?? - (data = ((ushort) code).Append (reason)).CheckIfValidCloseData (); + var msg = checkIfCanClose ( + () => (data = ((ushort) code).Append (reason)) + .CheckIfValidControlData ("reason")); if (msg != null) { _logger.Error ( String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); - error (msg); + error (msg); return; } @@ -1645,7 +1653,8 @@ namespace WebSocketSharp /// /// /// - /// A that indicates the status code for closure. + /// A that represents the status code that indicates the + /// reason for closure. /// public void CloseAsync (ushort code) { @@ -1660,8 +1669,8 @@ namespace WebSocketSharp /// This method doesn't wait for the close to be complete. /// /// - /// One of the values that indicate the status - /// codes for closure. + /// One of the enum values, represents the + /// status code that indicates the reason for closure. /// public void CloseAsync (CloseStatusCode code) { @@ -1680,11 +1689,12 @@ namespace WebSocketSharp /// /// This method emits a event if /// isn't in the allowable range of the WebSocket close status code or the - /// length of is greater than 123 bytes. + /// size of is greater than 123 bytes. /// /// /// - /// A that indicates the status code for closure. + /// A that represents the status code that indicates the + /// reason for closure. /// /// /// A that represents the reason for closure. @@ -1692,15 +1702,15 @@ namespace WebSocketSharp public void CloseAsync (ushort code, string reason) { byte [] data = null; - var msg = _readyState.CheckIfClosable () ?? - code.CheckIfValidCloseStatusCode () ?? - (data = code.Append (reason)).CheckIfValidCloseData (); + var msg = checkIfCanClose ( + () => code.CheckIfValidCloseStatusCode () ?? + (data = code.Append (reason)).CheckIfValidControlData ("reason")); if (msg != null) { _logger.Error ( String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); - error (msg); + error (msg); return; } @@ -1718,13 +1728,13 @@ namespace WebSocketSharp /// This method doesn't wait for the close to be complete. /// /// - /// This method emits a event if the length of + /// This method emits a event if the size of /// is greater than 123 bytes. /// /// /// - /// One of the values that indicate the status - /// codes for closure. + /// One of the enum values, represents the + /// status code that indicates the reason for closure. /// /// /// A that represents the reason for closure. @@ -1732,14 +1742,15 @@ namespace WebSocketSharp public void CloseAsync (CloseStatusCode code, string reason) { byte [] data = null; - var msg = _readyState.CheckIfClosable () ?? - (data = ((ushort) code).Append (reason)).CheckIfValidCloseData (); + var msg = checkIfCanClose ( + () => (data = ((ushort) code).Append (reason)) + .CheckIfValidControlData ("reason")); if (msg != null) { _logger.Error ( String.Format ("{0}\ncode: {1} reason: {2}", msg, code, reason)); - error (msg); + error (msg); return; }