Пример #1
0
        public override void Send(Stream stream, TimeSpan timeout, Information options)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (!_connect)
            {
                throw new ConnectionException();
            }
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            if (stream.Length == 0)
            {
                throw new ArgumentOutOfRangeException("stream");
            }

            lock (_sendLock)
            {
                using (RangeStream targetStream = new RangeStream(stream, stream.Position, stream.Length - stream.Position, true))
                {
                    try
                    {
                        _sendStopwatch.Restart();

                        Stream headerStream = new BufferStream(_bufferManager);
                        headerStream.Write(NetworkConverter.GetBytes((int)targetStream.Length), 0, 4);

                        byte[] sendBuffer = null;

                        try
                        {
                            sendBuffer = _bufferManager.TakeBuffer(1024 * 4);

                            using (Stream dataStream = new UniteStream(headerStream, new WrapperStream(targetStream, true)))
                            {
                                for (; ;)
                                {
                                    int sendLength = (int)Math.Min(dataStream.Length - dataStream.Position, sendBuffer.Length);
                                    if (sendLength == 0)
                                    {
                                        break;
                                    }

                                    if (_bandwidthLimit != null)
                                    {
                                        sendLength = _bandwidthLimit.GetOutBandwidth(this, sendLength);
                                        if (sendLength < 0)
                                        {
                                            throw new ConnectionException();
                                        }
                                    }

                                    dataStream.Read(sendBuffer, 0, sendLength);

                                    var time = BaseConnection.CheckTimeout(_sendStopwatch.Elapsed, timeout);
                                    time = (time < _sendTimeSpan) ? time : _sendTimeSpan;

                                    _cap.Send(sendBuffer, 0, sendLength, time);

                                    _aliveStopwatch.Restart();
                                    _sentByteCount.Add(sendLength);
                                }
                            }
                        }
                        finally
                        {
                            _bufferManager.ReturnBuffer(sendBuffer);
                        }

                        _aliveTimer.Change(1000 * 30);
                    }
                    catch (ConnectionException e)
                    {
                        throw e;
                    }
                    catch (Exception e)
                    {
                        throw new ConnectionException(e.Message, e);
                    }
                }
            }
        }
Пример #2
0
        public override Stream Receive(TimeSpan timeout, Information options)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }
            if (!_connect)
            {
                throw new ConnectionException();
            }

            lock (_receiveLock)
            {
                try
                {
                    _receiveStopwatch.Restart();

                    Restart :;

                    int length = 0;

                    {
                        byte[] lengthbuffer = new byte[4];

                        var time = BaseConnection.CheckTimeout(_receiveStopwatch.Elapsed, timeout);
                        time = (time < _receiveTimeSpan) ? time : _receiveTimeSpan;

                        _cap.Receive(lengthbuffer, time);

                        _receivedByteCount.Add(4);

                        length = NetworkConverter.ToInt32(lengthbuffer);
                    }

                    if (length == 0)
                    {
                        Thread.Sleep(100);
                        goto Restart;
                    }
                    else if (length > _maxReceiveCount)
                    {
                        throw new ConnectionException();
                    }

                    BufferStream bufferStream = null;

                    try
                    {
                        bufferStream = new BufferStream(_bufferManager);
                        byte[] receiveBuffer = null;

                        try
                        {
                            receiveBuffer = _bufferManager.TakeBuffer(1024 * 4);

                            do
                            {
                                int receiveLength = Math.Min(receiveBuffer.Length, length);

                                if (_bandwidthLimit != null)
                                {
                                    receiveLength = _bandwidthLimit.GetInBandwidth(this, receiveLength);
                                    if (receiveLength < 0)
                                    {
                                        throw new ConnectionException();
                                    }
                                }

                                var time = BaseConnection.CheckTimeout(_receiveStopwatch.Elapsed, timeout);
                                time = (time < _receiveTimeSpan) ? time : _receiveTimeSpan;

                                _cap.Receive(receiveBuffer, 0, receiveLength, time);

                                _receivedByteCount.Add(receiveLength);
                                bufferStream.Write(receiveBuffer, 0, receiveLength);

                                length -= receiveLength;
                            } while (length > 0);
                        }
                        finally
                        {
                            _bufferManager.ReturnBuffer(receiveBuffer);
                        }
                    }
                    catch (Exception e)
                    {
                        if (bufferStream != null)
                        {
                            bufferStream.Dispose();
                        }

                        throw e;
                    }

                    bufferStream.Seek(0, SeekOrigin.Begin);
                    return(bufferStream);
                }
                catch (ConnectionException e)
                {
                    throw e;
                }
                catch (Exception e)
                {
                    throw new ConnectionException(e.Message, e);
                }
            }
        }