Beispiel #1
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="SocketException"></exception>
        public override void SendAsync(IByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

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

            // 如果发送过程已停止,则本次直接发送
            if (Interlocked.CompareExchange(ref this.PendingSendCount, 1, 0) == 0)
            {
                this.TrySendByteRangeAsync(byteRange);
            }
            else
            {
                this.PendingSendByteRanges.Enqueue(byteRange);
                // 如果发送过程已停止,则启动发送缓存中的数据
                if (Interlocked.Increment(ref this.PendingSendCount) == 1)
                {
                    this.TrySendByteRangeAsync(null);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing">是否也释放托管资源</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing == true)
            {
                this.targetHost  = null;
                this.sslStream   = null;
                this.certificate = null;
                this.certificateValidationCallback = null;
                this.bufferRange = null;
            }
        }
Beispiel #3
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="SocketException"></exception>
        public override void Send(IByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

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

            this.sslStream.Write(byteRange.Buffer, byteRange.Offset, byteRange.Count);
        }
Beispiel #4
0
        /// <summary>
        /// 尝试异步发送一个ByteRange
        /// 发送完成将触发SendCompleted方法
        /// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
        /// </summary>
        private void TrySendByteRangeAsync(IByteRange byteRange)
        {
            if (byteRange == null && this.PendingSendByteRanges.TryDequeue(out byteRange) == false)
            {
                Interlocked.Exchange(ref this.PendingSendCount, 0);
                return;
            }

            base.TryInvokeAction(() =>
                                 this.sslStream.BeginWrite(
                                     byteRange.Buffer,
                                     byteRange.Offset,
                                     byteRange.Count,
                                     this.EndWirte,
                                     null));
        }
Beispiel #5
0
        /// <summary>
        /// 尝试异步发送一个ByteRange
        /// 发送完成将触发SendCompleted方法
        /// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
        /// </summary>
        private void TrySendByteRangeAsync(IByteRange byteRange)
        {
            if (byteRange == null && this.PendingSendByteRanges.TryDequeue(out byteRange) == false)
            {
                Interlocked.Exchange(ref this.PendingSendCount, 0);
                return;
            }

            base.TryInvokeAction(() =>
            {
                this.sendArg.SetBuffer(byteRange.Buffer, byteRange.Offset, byteRange.Count);
                if (this.Socket.SendAsync(this.sendArg) == false)
                {
                    this.SendCompleted(this.Socket, this.sendArg);
                }
            });
        }
Beispiel #6
0
        /// <summary>
        /// 发送一个小于缓冲区的数据范围
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        private bool SendByteRange(IByteRange 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);
        }
Beispiel #7
0
        /// <summary>
        /// 尝试异步发送一个ByteRange
        /// 发送完成将触发SendCompleted方法
        /// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
        /// </summary>
        private bool TrySendByteRangeAsync(IByteRange 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);
                }
            }));
        }
Beispiel #8
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>
        /// <exception cref="ArgumentNullException"></exception>
        /// <exception cref="SocketException"></exception>
        public void Send(IByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

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

            var byteRanges = byteRange.SplitBySize(BufferSetter.ItemSize);

            foreach (var range in byteRanges)
            {
                this.SendByteRange(range);
            }
        }
Beispiel #9
0
 /// <summary>
 /// 同步发送数据
 /// </summary>
 /// <param name="byteRange">数据范围</param>
 public abstract void Send(IByteRange byteRange);
Beispiel #10
0
 /// <summary>
 /// 异步发送数据
 /// </summary>
 /// <param name="byteRange">数据范围</param>
 public abstract void SendAsync(IByteRange byteRange);
Beispiel #11
0
 /// <summary>
 /// 异步发送数据
 /// </summary>
 /// <param name="byteRange">数据范围</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="SocketException"></exception>
 public virtual void SendAsync(IByteRange byteRange)
 {
     this.session.SendAsync(byteRange);
 }
Beispiel #12
0
 /// <summary>
 /// 异步发送数据
 /// </summary>
 /// <param name="byteRange">数据范围</param>
 /// <exception cref="ArgumentNullException"></exception>
 /// <exception cref="SocketException"></exception>
 public void Send(IByteRange byteRange)
 {
     this.session.Send(byteRange);
 }
Beispiel #13
0
        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing">是否也释放托管资源</param>
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);

            if (disposing == true)
            {
                this.targetHost = null;
                this.sslStream = null;
                this.certificate = null;
                this.certificateValidationCallback = null;
                this.bufferRange = null;
            }
        }
Beispiel #14
0
        /// <summary>
        /// 尝试异步发送一个ByteRange
        /// 发送完成将触发SendCompleted方法
        /// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
        /// </summary>
        private void TrySendByteRangeAsync(IByteRange byteRange)
        {
            if (byteRange == null && this.PendingSendByteRanges.TryDequeue(out byteRange) == false)
            {
                Interlocked.Exchange(ref this.PendingSendCount, 0);
                return;
            }

            base.TryInvokeAction(() =>
                this.sslStream.BeginWrite(
                byteRange.Buffer,
                byteRange.Offset,
                byteRange.Count,
                this.EndWirte,
                null));
        }
Beispiel #15
0
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="byteRange">数据范围</param>  
        /// <exception cref="ArgumentNullException"></exception>        
        /// <exception cref="SocketException"></exception>
        public override void Send(IByteRange byteRange)
        {
            if (byteRange == null)
            {
                throw new ArgumentNullException();
            }

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

            // 如果发送过程已停止,则本次直接发送
            if (Interlocked.CompareExchange(ref this.PendingSendCount, 1, 0) == 0)
            {
                this.TrySendByteRangeAsync(byteRange);
            }
            else
            {
                this.PendingSendByteRanges.Enqueue(byteRange);
                // 如果发送过程已停止,则启动发送缓存中的数据
                if (Interlocked.Increment(ref this.PendingSendCount) == 1)
                {
                    this.TrySendByteRangeAsync(null);
                }
            }
        }
Beispiel #16
0
        /// <summary>
        /// 尝试异步发送一个ByteRange
        /// 发送完成将触发SendCompleted方法
        /// <param name="byteRange">数据范围,为null则从缓冲中区获取</param>
        /// </summary>
        private void TrySendByteRangeAsync(IByteRange byteRange)
        {
            if (byteRange == null && this.PendingSendByteRanges.TryDequeue(out byteRange) == false)
            {
                Interlocked.Exchange(ref this.PendingSendCount, 0);
                return;
            }

            base.TryInvokeAction(() =>
            {
                this.sendArg.SetBuffer(byteRange.Buffer, byteRange.Offset, byteRange.Count);
                if (this.Socket.SendAsync(this.sendArg) == false)
                {
                    this.SendCompleted(this.Socket, this.sendArg);
                }
            });
        }