Beispiel #1
0
        private void OnDataAvailable(IAsyncResult result)
        {
            DataAvailableCallback callback      = (DataAvailableCallback)result.AsyncState;
            Exception             readException = null;

            try
            {
                // complete read operation
                _readPos   = 0;
                _dataCount = _stream.EndRead(result);

                if (_dataCount <= 0)
                {
                    readException = new IOException("No data available");
                }
            }
            catch (Exception ex)
            {
                // unable to read data, connection is broken?
                readException = ex;
            }

            // invoke client callback
            callback(readException, _dataAvailableState);
        }
Beispiel #2
0
        private void InvokeCallback(object state)
        {
            // invoke client callback
            DataAvailableCallback callback = (DataAvailableCallback)state;

            callback(null, _dataAvailableState);
        }
Beispiel #3
0
        public void BeginRead(byte[] data, DataAvailableCallback callback)
        {
            if (_isClosed)
            {
                return;
            }

            _isReading = true;
            beginRead(data, (d, l) => onDataRead(d, l, callback));
        }
Beispiel #4
0
        private void onDataRead(byte[] data, int length, DataAvailableCallback callback)
        {
            _isReading = false;

            if (_isClosed)
            {
                return;
            }

            try
            {
                callback(data, length);
            }
            catch (IOException ex)
            {
                Log.Error("DataStream.onDataRead: {0}", ex.ToString());
            }
        }
Beispiel #5
0
        public RequestQueue(int maxQueueSize, int queueTimeout, int maxActiveWorkItems, int minFreeThreads)
        {
            // parameters validation
            if (maxQueueSize < 1)
                throw new ArgumentOutOfRangeException("maxQueueSize");
            if (queueTimeout < 1)
                throw new ArgumentOutOfRangeException("minFreeThreads");
            if (maxActiveWorkItems < 1)
                throw new ArgumentOutOfRangeException("maxActiveWorkItems");

            _maxRequestQueueSize = maxQueueSize;
            _queueTimeout = TimeSpan.FromSeconds(queueTimeout);
            _maxWorkItemsCount = maxActiveWorkItems;
            _minFreeThreads = minFreeThreads;

            _requestAvailableCallback = OnDataAvailable;
            _workItemCallback = OnNewWorkItem;
        }
Beispiel #6
0
        /// <summary>
        /// Asynchronously reads data from stream to fill buffer
        /// </summary>
        public void FillBuffer(DataAvailableCallback callback, object state)
        {
            if (_dataCount > 0)
            {
                // schedule client callback execution
                _dataAvailableState = state;
                ThreadPool.QueueUserWorkItem(new WaitCallback(InvokeCallback), callback);
            }
            else
            {
                if (_writePos > 0)
                {
                    // flush buffered data to the stream
                    FlushWrite();
                }

                // begin read operation
                _dataAvailableState = state;
                _stream.BeginRead(_buffer, 0, _bufferSize, _dataAvailableCallback, callback);
            }
        }
Beispiel #7
0
        public RequestQueue(int maxQueueSize, int queueTimeout, int maxActiveWorkItems, int minFreeThreads)
        {
            // parameters validation
            if (maxQueueSize < 1)
            {
                throw new ArgumentOutOfRangeException("maxQueueSize");
            }
            if (queueTimeout < 1)
            {
                throw new ArgumentOutOfRangeException("minFreeThreads");
            }
            if (maxActiveWorkItems < 1)
            {
                throw new ArgumentOutOfRangeException("maxActiveWorkItems");
            }

            _maxRequestQueueSize = maxQueueSize;
            _queueTimeout        = TimeSpan.FromSeconds(queueTimeout);
            _maxWorkItemsCount   = maxActiveWorkItems;
            _minFreeThreads      = minFreeThreads;

            _requestAvailableCallback = new DataAvailableCallback(OnDataAvailable);
            _workItemCallback         = new WaitCallback(OnNewWorkItem);
        }
        /// <summary>
        /// Asynchronously reads data from stream to fill buffer
        /// </summary>
        public void FillBuffer(DataAvailableCallback callback, object state)
        {
            if (_dataCount > 0)
            {
                // schedule client callback execution
                _dataAvailableState = state;
                ThreadPool.QueueUserWorkItem(InvokeCallback, callback);
            }
            else
            {
                if (_writePos > 0)
                {
                    // flush buffered data to the stream
                    FlushWrite();
                }

                // begin read operation
                _dataAvailableState = state;
                _stream.BeginRead(_buffer, 0, BufferSize, _dataAvailableCallback, callback);
            }
        }
 internal static extern bool RegisterOnDataFromDataChannelReady(
     IntPtr connection, DataAvailableCallback callback);
Beispiel #10
0
 protected abstract void beginRead(byte[] data, DataAvailableCallback callback);
Beispiel #11
0
 public void BeginReceive(DataAvailableCallback callback, object state)
 {
     // begin asynchronous receive operation
     _bufferedStream.FillBuffer(callback, state);
 }
Beispiel #12
0
 /// <inheritdoc/>
 protected override void beginRead(byte[] data, DataAvailableCallback callback)
 {
     _buffer = data;
     _stream.BeginRead(_buffer, 0, _buffer.Length, dataAvailable, callback);
 }
 public void BeginReceive(DataAvailableCallback callback, object state)
 {
     // begin asynchronous receive operation
     _bufferedStream.FillBuffer(callback, state);
 }