Exemplo n.º 1
0
        public void ReadAsync(byte[] buffer, int offset, int count, Action <int> endAction)
        {
            if (_stream.DataAvailable)
            {
                Logger.Trace("Reading data synchronously");
                var read = _stream.Read(buffer, offset, count);
                if (Logger.IsTraceEnabled)
                {
                    Logger.TraceFormat("Read data synchronously {0}", read);
                }

                _daemon.Schedule(
                    () => endAction(read));
            }
            Logger.Trace("Reading data asynchronously");
            try
            {
                _stream.BeginRead(buffer, offset, count,
                                  asyncResult => _daemon.Schedule(
                                      () => HandleEndRead(endAction, asyncResult)), null);
            }
            catch (IOException exception)
            {
                Logger.Debug("Connection Reset by Peer", exception);
                _daemon.Shutdown();
            }
        }