예제 #1
0
        public void BroadcastAsync(Stream stream, int length, Action completed)
        {
            if (_state != ServerState.Start)
            {
                var msg = "The current state of the manager is not Start.";
                throw new InvalidOperationException(msg);
            }

            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }

            if (!stream.CanRead)
            {
                var msg = "It cannot be read.";
                throw new ArgumentException(msg, "stream");
            }

            if (length < 1)
            {
                var msg = "Less than 1.";
                throw new ArgumentException(msg, "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");
            }

            if (len < length)
            {
                _log.Warn(
                    String.Format(
                        "Only {0} byte(s) of data could be read from the stream.",
                        len
                        )
                    );
            }

            if (len <= WebSocket.FragmentLength)
            {
                broadcastAsync(Opcode.Binary, bytes, completed);
            }
            else
            {
                broadcastAsync(Opcode.Binary, new MemoryStream(bytes), completed);
            }
        }