Exemplo n.º 1
0
        /// <summary>
        /// 从流中读取count字节的范围标记
        /// 并将流内的位置向前推进count个字节
        /// </summary>
        /// <param name="count">字节数</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public IByteRange ReadByteRange(int count)
        {
            var range = new ByteRange(base.GetBuffer(), this.Position, count);

            this.Position = this.Position + count;
            return(range);
        }
Exemplo n.º 2
0
            /// <summary>
            /// 分配一个缓冲区
            /// 当内存块满时返回null
            /// </summary>
            /// <returns></returns>
            public ByteRange AllocBuffer()
            {
                if (this.position == this.buffer.Length)
                {
                    return(null);
                }
                var byteRange = new ByteRange(this.buffer, this.position, this.itemSize);

                this.position = this.position + this.itemSize;
                return(byteRange);
            }
Exemplo n.º 3
0
        /// <summary>
        /// 分割数据并顺序添加到待发送数据集合
        /// </summary>
        /// <param name="byteRange">数据</param>
        /// <returns>返回拆分数量</returns>
        private int SplitByteRangeToQueue(ByteRange byteRange)
        {
            var byteRanges = byteRange.SplitBySize(EventArgBufferSetter.ARG_BUFFER_SIZE);

            lock (this.queueSync)
            {
                var count = 0;
                foreach (var range in byteRanges)
                {
                    this.byteRangeQueue.Enqueue(range);
                    count++;
                }
                return(count);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 发送一个小于缓冲区的数据范围
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        private bool SendByteRange(ByteRange byteRange)
        {
            // 如果发送过程已停止,则本次直接发送
            if (Interlocked.CompareExchange(ref this.pendingSendCount, 1, 0) == 0)
            {
                return(this.TrySendByteRangeAsync(byteRange));
            }

            // 添加数据到缓存区
            this.byteRangeQueue.Enqueue(byteRange);

            // 如果发送过程已停止,则启动发送缓存中的数据
            if (Interlocked.Increment(ref this.pendingSendCount) == 1)
            {
                return(this.TrySendByteRangeAsync(null));
            }
            return(true);
        }
Exemplo n.º 5
0
        /// <summary>
        /// 输出结果
        /// </summary>
        /// <param name="content">内容</param>
        public void ContentResult(string content)
        {
            var head = @"HTTP/1.1 200 OK
            Cache-Control: private
            Content-Type: text/html; charset=utf-8
            Vary: Accept-Encoding
            Server: Microsoft-IIS/7.5
            X-AspNetMvc-Version: 5.0
            X-AspNet-Version: 4.0.30319
            X-Powered-By: ASP.NET
            Date: Fri, 18 Sep 2015 05:39:50 GMT
            Content-Length: 12003

            ";
            var response = head + content;
            var bytes = new ByteRange(Encoding.UTF8.GetBytes(response));
            session.Send(bytes);
            session.Close();
        }
Exemplo n.º 6
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="SocketException"></exception>
        public void Send(ByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

            if (this.IsConnected == false)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }

            var byteRanges = byteRange.SplitBySize(EventArgBufferSetter.ARG_BUFFER_SIZE);

            foreach (var range in byteRanges)
            {
                this.SendByteRange(range);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 尝试异步发送一个ByteRange
        /// 发送完成将触发SendCompleted方法
        /// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
        /// </summary>
        private bool TrySendByteRangeAsync(ByteRange byteRange)
        {
            if (byteRange == null && this.byteRangeQueue.TryDequeue(out byteRange) == false)
            {
                Interlocked.Exchange(ref this.pendingSendCount, 0);
                return(false);
            }

            Buffer.BlockCopy(byteRange.Buffer, byteRange.Offset, this.sendArg.Buffer, this.sendArg.Offset, byteRange.Count);
            this.sendArg.SetBuffer(this.sendArg.Offset, byteRange.Count);

            return(this.TryInvoke(() =>
            {
                if (this.socket.SendAsync(this.sendArg) == false)
                {
                    this.SendCompleted(this.socket, this.sendArg);
                }
                this.ExtraState.SetSended(byteRange.Count); // 信息统计;
            }));
        }
Exemplo n.º 8
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="SocketException"></exception>
        public void Send(ByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

            if (this.IsConnected == false)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }

            // 拆分数据
            var count = this.SplitByteRangeToQueue(byteRange);

            // 启动发送
            if (Interlocked.Add(ref this.pendingSendCount, count) == count)
            {
                this.TrySend();
            }
        }
Exemplo n.º 9
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="SocketException"></exception>
        public void Send(ByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

            if (this.IsConnected == false)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }

            var sendArg = SendArgBag.Take();

            sendArg.SetBuffer(byteRange.Buffer, byteRange.Offset, byteRange.Count);

            if (this.socket.SendAsync(sendArg) == false)
            {
                SendArgBag.Add(sendArg);
            }

            this.ExtraState.SetSended(byteRange.Count);
        }
Exemplo n.º 10
0
 /// <summary>
 /// 调试视图
 /// </summary>
 /// <param name="view">查看的对象</param>
 public DebugView(ByteRange view)
 {
     this.view = view;
 }
Exemplo n.º 11
0
 /// <summary>
 /// 分配一个缓冲区
 /// 当内存块满时返回null
 /// </summary>
 /// <returns></returns>
 public ByteRange AllocBuffer()
 {
     if (this.position == this.buffer.Length)
     {
         return null;
     }
     var byteRange = new ByteRange(this.buffer, this.position, this.itemSize);
     this.position = this.position + this.itemSize;
     return byteRange;
 }
Exemplo n.º 12
0
 /// <summary>
 /// 输出内容
 /// </summary>
 /// <param name="range"></param>
 /// <returns></returns>
 public bool WriteContent(ByteRange range)
 {
     return this.TrySend(range);
 }
Exemplo n.º 13
0
 /// <summary>
 /// 调试视图
 /// </summary>
 /// <param name="view">查看的对象</param>
 public DebugView(ByteRange view)
 {
     this.view = view;
 }
Exemplo n.º 14
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>  
        /// <exception cref="ArgumentNullException"></exception>        
        /// <exception cref="SocketException"></exception>
        public void Send(ByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

            if (this.IsConnected == false)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }

            // 拆分数据
            var count = this.SplitByteRangeToQueue(byteRange);

            // 启动发送
            if (Interlocked.Add(ref this.pendingSendCount, count) == count)
            {
                this.TrySend();
            }
        }
Exemplo n.º 15
0
 /// <summary>
 /// 分割数据并顺序添加到待发送数据集合
 /// </summary>
 /// <param name="byteRange">数据</param>
 /// <returns>返回拆分数量</returns>
 private int SplitByteRangeToQueue(ByteRange byteRange)
 {
     var byteRanges = byteRange.SplitBySize(EventArgBufferSetter.ARG_BUFFER_SIZE);
     lock (this.queueSync)
     {
         var count = 0;
         foreach (var range in byteRanges)
         {
             this.byteRangeQueue.Enqueue(range);
             count++;
         }
         return count;
     }
 }
Exemplo n.º 16
0
        /// <summary>
        /// 尝试异步发送一个ByteRange
        /// 发送完成将触发SendCompleted方法
        /// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
        /// </summary>
        private bool TrySendByteRangeAsync(ByteRange byteRange)
        {
            if (byteRange == null && this.byteRangeQueue.TryDequeue(out byteRange) == false)
            {
                Interlocked.Exchange(ref this.pendingSendCount, 0);
                return false;
            }

            Buffer.BlockCopy(byteRange.Buffer, byteRange.Offset, this.sendArg.Buffer, this.sendArg.Offset, byteRange.Count);
            this.sendArg.SetBuffer(this.sendArg.Offset, byteRange.Count);

            return this.TryInvoke(() =>
            {
                if (this.socket.SendAsync(this.sendArg) == false)
                {
                    this.SendCompleted(this.socket, this.sendArg);
                }
                this.ExtraState.SetSended(byteRange.Count); // 信息统计;
            });
        }
Exemplo n.º 17
0
        /// <summary>
        /// 发送一个小于缓冲区的数据范围
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        private bool SendByteRange(ByteRange byteRange)
        {
            // 如果发送过程已停止,则本次直接发送
            if (Interlocked.CompareExchange(ref this.pendingSendCount, 1, 0) == 0)
            {
                return this.TrySendByteRangeAsync(byteRange);
            }

            // 添加数据到缓存区
            this.byteRangeQueue.Enqueue(byteRange);

            // 如果发送过程已停止,则启动发送缓存中的数据
            if (Interlocked.Increment(ref this.pendingSendCount) == 1)
            {
                return this.TrySendByteRangeAsync(null);
            }
            return true;
        }
Exemplo n.º 18
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>  
        /// <exception cref="ArgumentNullException"></exception>        
        /// <exception cref="SocketException"></exception>
        public void Send(ByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

            if (this.IsConnected == false)
            {
                throw new SocketException((int)SocketError.NotConnected);
            }

            var byteRanges = byteRange.SplitBySize(EventArgBufferSetter.ARG_BUFFER_SIZE);
            foreach (var range in byteRanges)
            {
                this.SendByteRange(range);
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// 尝试发送数据到客户端
        /// </summary>
        /// <param name="range"></param>
        /// <returns></returns>
        private bool TrySend(ByteRange range)
        {
            if (range == null)
            {
                return false;
            }

            try
            {
                this.session.Send(range);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Exemplo n.º 20
0
 /// <summary>
 /// 从流中读取count字节的范围标记
 /// 并将流内的位置向前推进count个字节
 /// </summary>
 /// <param name="count">字节数</param>
 /// <returns></returns>
 /// <exception cref="ArgumentNullException"></exception>
 public IByteRange ReadByteRange(int count)
 {
     var range = new ByteRange(base.GetBuffer(), this.Position, count);
     this.Position = this.Position + count;
     return range;
 }