Пример #1
0
        public void OnDataBlockReading(DataBlockReadingEventArgs e)
        {
            if (_isCompleted) {
                e.IsCompleted = true;
                if (_reader != null) {
                    _reader.Dispose();
                }
                return;
            }

            if (_reader == null) {
                _reader = new BinaryReader(_fileStream);
            }

            var bytes = _reader.ReadBytes(e.MaxBlockSize);
            e.ResponseWriter.Write(bytes);

            _isCompleted = _reader.BaseStream.Position == _reader.BaseStream.Length;
        }
Пример #2
0
 protected virtual void OnDataBlockReading(DataBlockReadingEventArgs e)
 {
     var handler = ResponseDataReading;
     if (handler != null) {
         handler(this, e);
     }
 }
Пример #3
0
        private int ReadResponse(IntPtr self, IntPtr dataout, int bytestoread, ref int bytesread, IntPtr callback)
        {
            using (var writer = new BinaryWriter(new MemoryStream(), Encoding.UTF8)) {
                var e = new DataBlockReadingEventArgs(writer) {
                    MaxBlockSize = bytestoread,
                    DelayController =
                        CallbackObject.FromHandle(callback)
                };

                OnDataBlockReading(e);

                if (e.DelayController.IsPaused) {
                    // Data retrieval can be resumed by calling Resume() on the controller.
                    bytesread = 0;
                    return 1;
                }

                e.ResponseWriter.Flush();
                e.ResponseWriter.Seek(0, SeekOrigin.Begin);

                if (e.ResponseWriter.BaseStream.Length == 0) {
                    bytesread = 0;
                    return 0;
                }

                using (var reader = new BinaryReader(e.ResponseWriter.BaseStream)) {
                    var bytes = reader.ReadBytes(e.MaxBlockSize);
                    bytesread = bytes.Length;
                    Marshal.Copy(bytes, 0, dataout, bytesread);
                }

                return e.IsCompleted ? 0 : 1;
            }
        }