[Modify] Throw exceptions

master
sta 8 years ago
parent d58cfbbb14
commit 954f3dbbd0

@ -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) { if (stream == null)
_logger.Error (msg); throw new ArgumentNullException ("stream");
return;
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) {
var msg = "No data could be read from it.";
throw new ArgumentException (msg, "stream");
} }
stream.ReadBytesAsync ( if (len < length) {
length, _logger.Warn (
data => { String.Format (
var len = data.Length; "Only {0} byte(s) of data could be read from the specified stream.",
if (len == 0) { len
_logger.Error ("The data cannot be read from 'stream'."); )
return; );
} }
if (len < length) if (len <= WebSocket.FragmentLength)
_logger.Warn ( broadcastAsync (Opcode.Binary, bytes, completed);
String.Format ( else
"The data with 'length' cannot be read from 'stream':\n expected: {0}\n actual: {1}", broadcastAsync (Opcode.Binary, new MemoryStream (bytes), completed);
length,
len));
if (len <= WebSocket.FragmentLength)
broadcast (Opcode.Binary, data, completed);
else
broadcast (Opcode.Binary, new MemoryStream (data), completed);
},
ex => _logger.Fatal (ex.ToString ()));
} }
/// <summary> /// <summary>

Loading…
Cancel
Save