[Modify] Throw exceptions

This commit is contained in:
sta 2017-08-05 17:25:54 +09:00
parent d58cfbbb14
commit 954f3dbbd0

View File

@ -579,36 +579,41 @@ namespace WebSocketSharp.Server
/// </param> /// </param>
public void BroadcastAsync (Stream stream, int length, Action completed) public void BroadcastAsync (Stream stream, int length, Action completed)
{ {
var msg = _state.CheckIfAvailable (false, true, false) ?? if (_state != ServerState.Start) {
WebSocket.CheckSendParameters (stream, length); var msg = "The current state of the manager is not Start.";
throw new InvalidOperationException (msg);
if (msg != null) {
_logger.Error (msg);
return;
} }
stream.ReadBytesAsync ( if (stream == null)
length, throw new ArgumentNullException ("stream");
data => {
var len = data.Length; if (!stream.CanRead)
throw new ArgumentException ("It cannot be read.", "stream");
if (length < 1)
throw new ArgumentException ("Less than 1.", "length");
var bytes = stream.ReadBytes (length);
var len = bytes.Length;
if (len == 0) { if (len == 0) {
_logger.Error ("The data cannot be read from 'stream'."); var msg = "No data could be read from it.";
return; throw new ArgumentException (msg, "stream");
} }
if (len < length) if (len < length) {
_logger.Warn ( _logger.Warn (
String.Format ( String.Format (
"The data with 'length' cannot be read from 'stream':\n expected: {0}\n actual: {1}", "Only {0} byte(s) of data could be read from the specified stream.",
length, len
len)); )
);
}
if (len <= WebSocket.FragmentLength) if (len <= WebSocket.FragmentLength)
broadcast (Opcode.Binary, data, completed); broadcastAsync (Opcode.Binary, bytes, completed);
else else
broadcast (Opcode.Binary, new MemoryStream (data), completed); broadcastAsync (Opcode.Binary, new MemoryStream (bytes), completed);
},
ex => _logger.Fatal (ex.ToString ()));
} }
/// <summary> /// <summary>