CMS 3D CMS Logo

Public Member Functions | Protected Member Functions | Private Attributes

IOChannel Class Reference

#include <IOChannel.h>

Inheritance diagram for IOChannel:
IOInput IOOutput File RemoteFile

List of all members.

Public Member Functions

virtual void close (void)
virtual IOFD fd (void) const
virtual void fd (IOFD value)
 IOChannel (IOFD fd=EDM_IOFD_INVALID)
virtual bool isBlocking (void) const
virtual IOSize read (void *into, IOSize n)
virtual IOSize readv (IOBuffer *into, IOSize buffers)
virtual void setBlocking (bool value)
virtual IOSize write (const void *from, IOSize n)
virtual IOSize writev (const IOBuffer *from, IOSize buffers)
virtual ~IOChannel (void)

Protected Member Functions

bool sysclose (IOFD fd, int *error=0)

Private Attributes

IOFD m_fd

Detailed Description

Base class for stream-oriented I/O sources and targets, based on the operating system file descriptor.

Definition at line 9 of file IOChannel.h.


Constructor & Destructor Documentation

IOChannel::IOChannel ( IOFD  fd = EDM_IOFD_INVALID)

Definition at line 61 of file IOChannel.cc.

  : m_fd (fd)
{}
IOChannel::~IOChannel ( void  ) [virtual]

Definition at line 65 of file IOChannel.cc.

{}

Member Function Documentation

void IOChannel::close ( void  ) [virtual]

Close the channel. By default closes the underlying operating system file descriptor.

Reimplemented in File, and RemoteFile.

Definition at line 92 of file IOChannel.cc.

References EDM_IOFD_INVALID, error, fd(), sysclose(), and throwStorageError().

Referenced by sysclose().

{
  if (fd () == EDM_IOFD_INVALID)
    return;

  int error = 0;
  if (! sysclose (fd (), &error))
    throwStorageError ("FileCloseError", "Calling IOChannel::close()",
                       "sysclose()", error);

  fd (EDM_IOFD_INVALID);
}
IOFD IOChannel::fd ( void  ) const [virtual]

Get the system file descriptor of the channel.

Definition at line 73 of file IOChannel.cc.

References m_fd.

Referenced by LocalCacheFile::cache(), close(), File::duplicate(), isBlocking(), RemoteFile::local(), read(), readv(), setBlocking(), sysclose(), write(), and writev().

{ return m_fd; }
void IOChannel::fd ( IOFD  value) [virtual]

Set the system file descriptor of the channel. (FIXME: This is dangerous. How to deal with WIN32 flags and state object?)

Definition at line 79 of file IOChannel.cc.

References m_fd, and relativeConstraints::value.

{
  // FIXME: close old one?
  // FIXME: reset state?
  m_fd = value;
}
bool IOChannel::isBlocking ( void  ) const [virtual]

Definition at line 126 of file UnixIOChannel.cc.

References fd(), alignBH_cfg::mode, O_NONBLOCK, and throwStorageError().

{
#ifdef O_NONBLOCK
  int mode;
  if ((mode = fcntl (fd (), F_GETFL, 0)) == -1)
    throwStorageError ("FileIsBlockingError", "Calling IOChannel::isBlocking()", "fcntl()", errno);

  return mode & (O_NDELAY | O_NONBLOCK);
#else // ! O_NONBLOCK
  return true;
#endif // O_NONBLOCK
}
IOSize IOChannel::read ( void *  into,
IOSize  n 
) [virtual]

Read at most n bytes from the channel into the buffer into.

Returns:
The number of bytes actually read into the buffer. This is always less or equal to n. It can be less if there is limited amount of input currently available; zero means that the end of file has been reached. For a connected channel like a socket or pipe this indicates the remote end has closed the connection. If the channel is in non-blocking mode and no input is currently available, an exception is thrown (FIXME: make this simpler; clarify which exception?).

Implements IOInput.

Reimplemented in File.

Definition at line 10 of file UnixIOChannel.cc.

References fd(), edm::errors::FileReadError, n, IOInput::read(), alignCSCRings::s, and throwStorageError().

{
  ssize_t s;
  do
    s = ::read (fd (), into, n);
  while (s == -1 && errno == EINTR);

  if (s == -1)
    throwStorageError (edm::errors::FileReadError, "Calling IOChannel::read()", "read()", errno);

  return s;
}
IOSize IOChannel::readv ( IOBuffer into,
IOSize  buffers 
) [virtual]

Read into scattered buffers.

This operation may ignore errors. If some data are already read and an error occurs, the call returns the number of bytes read up to that point, hiding the error. It is assumed that a subsequent read will discover persistent errors and that sporadic errors such as indication that the read would block can be ignored.

The operation always fills a buffer completely before proceeding to the next one. The call is handled by the operating system if possible; the fall back is to use the single read() repeatedly.

Reimplemented from IOInput.

Reimplemented in File.

Definition at line 24 of file UnixIOChannel.cc.

References data, fd(), edm::errors::FileReadError, i, n, IOBuffer::size(), and throwStorageError().

Referenced by File::readv().

{
  assert (! buffers || into);

  // readv may not support zero buffers.
  if (! buffers)
    return 0;

  ssize_t n = 0;

  // Convert the buffers to system format.
  std::vector<iovec> bufs (buffers);
  for (IOSize i = 0; i < buffers; ++i)
  {
    bufs [i].iov_len  = into [i].size ();
    bufs [i].iov_base = (caddr_t) into [i].data ();
  }

  // Read as long as signals cancel the read before doing anything.
  do
    n = ::readv (fd (), &bufs [0], buffers);
  while (n == -1 && errno == EINTR);

  // If it was serious error, throw it.
  if (n == -1)
    throwStorageError (edm::errors::FileReadError, "Calling IOChannel::readv", "readv()", errno);

  // Return the number of bytes actually read.
  return n;
}
void IOChannel::setBlocking ( bool  value) [virtual]

Definition at line 108 of file UnixIOChannel.cc.

References fd(), alignBH_cfg::mode, O_NONBLOCK, throwStorageError(), and relativeConstraints::value.

{
#ifdef O_NONBLOCK
  int mode;
  int off = value ? ~0 : ~(O_NDELAY | O_NONBLOCK);
  int on  = value ? O_NONBLOCK : 0;

  if ((mode = fcntl (fd (), F_GETFL, 0)) == -1
      || fcntl (fd (), F_SETFL, (mode & off) | on) == -1)
    throwStorageError ("FileSetBlockingError", "Calling IOChannel::setBlocking()", "fcntl()", errno);
#elif defined FIONBIO
  int mode = value;
  if (ioctl (fd (), FIONBIO, &value) == -1)
    throwStorageError ("FileSetBlockingError", "Calling IOChannel::setBlocking()", "ioctl()", errno);
#endif
}
bool IOChannel::sysclose ( IOFD  fd,
int *  error = 0 
) [protected]

Reimplemented in File.

Definition at line 143 of file UnixIOChannel.cc.

References close(), fd(), and run_regression::ret.

Referenced by close().

{
  int ret = ::close (fd);
  if (error) *error = errno;
  return ret != -1;
}
IOSize IOChannel::write ( const void *  from,
IOSize  n 
) [virtual]

Write n bytes from the buffer at from.

Returns:
The number of bytes actually written. This is always less or equal to the size of the buffer (n). It can be less if the channel is unable to accept some of the output. This can happen among others if the channel is in non-blocking mode, but also for other implementation and platform-dependent reasons.

Implements IOOutput.

Reimplemented in File.

Definition at line 59 of file UnixIOChannel.cc.

References fd(), n, alignCSCRings::s, and throwStorageError().

{
  ssize_t s;
  do
    s = ::write (fd (), from, n);
  while (s == -1 && errno == EINTR);

  if (s == -1 && errno != EWOULDBLOCK)
    throwStorageError ("FileWriteError", "Calling IOChannel::write()", "write()", errno);

  return s >= 0 ? s : 0;
}
IOSize IOChannel::writev ( const IOBuffer from,
IOSize  buffers 
) [virtual]

Write from a scattered buffer.

This operation may ignore errors. If some data is already written and an error occurs, the call returns the number of bytes written up to that point, hiding the error. It is assumed that a subsequent write will discover persistent errors.

Always writes a complete buffer before proceeding to the next one. The call is delegated to the operating system if possible. If the system does not support this, falls back on multiple calls to single-buffer write().

Reimplemented from IOOutput.

Reimplemented in File.

Definition at line 73 of file UnixIOChannel.cc.

References data, fd(), i, n, IOBuffer::size(), and throwStorageError().

Referenced by File::writev().

{
  assert (! buffers || from);

  // writev may not support zero buffers.
  if (! buffers)
    return 0;

  ssize_t n = 0;

  // Convert the buffers to system format.
  std::vector<iovec> bufs (buffers);
  for (IOSize i = 0; i < buffers; ++i)
  {
    bufs [i].iov_len  = from [i].size ();
    bufs [i].iov_base = (caddr_t) from [i].data ();
  }

  // Read as long as signals cancel the read before doing anything.
  do
    n = ::writev (fd (), &bufs [0], buffers);
  while (n == -1 && errno == EINTR);

  // If it was serious error, throw it.
  if (n == -1)
    throwStorageError ("FileWriteError", "Calling IOChannel::writev()", "writev()", errno);

  // Return the number of bytes actually written.
  return n;
}

Member Data Documentation

IOFD IOChannel::m_fd [private]

Definition at line 39 of file IOChannel.h.

Referenced by fd().