From 4d510902b885244db53f9cce79b37dfa85b39b36 Mon Sep 17 00:00:00 2001 From: sta Date: Thu, 6 Apr 2017 17:21:14 +0900 Subject: [PATCH] [Modify] Throw exceptions --- .../Server/WebSocketServiceManager.cs | 30 ++++++++++++------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/websocket-sharp/Server/WebSocketServiceManager.cs b/websocket-sharp/Server/WebSocketServiceManager.cs index b42aed68..152cd084 100644 --- a/websocket-sharp/Server/WebSocketServiceManager.cs +++ b/websocket-sharp/Server/WebSocketServiceManager.cs @@ -815,19 +815,27 @@ namespace WebSocketSharp.Server /// public Dictionary> Broadping (string message) { - if (message == null || message.Length == 0) - return Broadping (); - - byte[] data = null; - var msg = _state.CheckIfAvailable (false, true, false) ?? - WebSocket.CheckPingParameter (message, out data); - - if (msg != null) { - _logger.Error (msg); - return null; + if (_state != ServerState.Start) { + var msg = "The current state of the manager is not Start."; + throw new InvalidOperationException (msg); } - return broadping (WebSocketFrame.CreatePingFrame (data, false).ToArray (), _waitTime); + if (message == null) + throw new ArgumentNullException ("message"); + + byte[] bytes; + if (!message.TryGetUTF8EncodedBytes (out bytes)) { + var msg = "It could not be UTF-8-encoded."; + throw new ArgumentException (msg, "message"); + } + + if (bytes.Length > 125) { + var msg = "Its size is greater than 125 bytes."; + throw new ArgumentOutOfRangeException ("message", msg); + } + + var frame = WebSocketFrame.CreatePingFrame (bytes, false); + return broadping (frame.ToArray (), _waitTime); } ///