/// <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); }
/// <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); }
/// <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); } }
/// <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); }
/// <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(); }
/// <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); } }
/// <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); // 信息统计; })); }
/// <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(); } }
/// <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); }
/// <summary> /// 调试视图 /// </summary> /// <param name="view">查看的对象</param> public DebugView(ByteRange view) { this.view = view; }
/// <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; }
/// <summary> /// 输出内容 /// </summary> /// <param name="range"></param> /// <returns></returns> public bool WriteContent(ByteRange range) { return this.TrySend(range); }
/// <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; } }
/// <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); // 信息统计; }); }
/// <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; }
/// <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; } }
/// <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; }