CMS 3D CMS Logo

IOChannel Class Reference

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

#include <Utilities/StorageFactory/interface/IOChannel.h>

Inheritance diagram for IOChannel:

IOInput IOOutput File RemoteFile

List of all members.

Public Member Functions

virtual void close (void)
 Close the channel.
virtual void fd (IOFD value)
 Set the system file descriptor of the channel.
virtual IOFD fd (void) const
 Get the system file descriptor of the channel.
 IOChannel (IOFD fd=EDM_IOFD_INVALID)
virtual bool isBlocking (void) const
virtual IOSize read (void *into, IOSize n)
 Read at most n bytes from the channel into the buffer into.
virtual IOSize readv (IOBuffer *into, IOSize buffers)
 Read into scattered buffers.
virtual void setBlocking (bool value)
virtual IOSize write (const void *from, IOSize n)
 Write n bytes from the buffer at from.
virtual IOSize writev (const IOBuffer *from, IOSize buffers)
 Write from a scattered buffer.
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 62 of file IOChannel.cc.

00063   : m_fd (fd)
00064 {}

IOChannel::~IOChannel ( void   )  [virtual]

Definition at line 66 of file IOChannel.cc.

00067 {}


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 93 of file IOChannel.cc.

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

Referenced by sysclose().

00094 {
00095   if (fd () == EDM_IOFD_INVALID)
00096     return;
00097 
00098   int error = 0;
00099   if (! sysclose (fd (), &error))
00100     throwStorageError ("IOChannel::sysclose()", "close()", error);
00101 
00102   fd (EDM_IOFD_INVALID);
00103 }

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 80 of file IOChannel.cc.

References m_fd.

00081 {
00082   // FIXME: close old one?
00083   // FIXME: reset state?
00084   m_fd = value;
00085 }

IOFD IOChannel::fd ( void   )  const [virtual]

Get the system file descriptor of the channel.

Definition at line 74 of file IOChannel.cc.

References m_fd.

Referenced by File::abort(), File::attach(), LocalCacheFile::cache(), File::close(), close(), File::duplicate(), File::File(), File::flush(), isBlocking(), RemoteFile::local(), File::open(), File::position(), File::prefetch(), read(), File::read(), readv(), File::resize(), setBlocking(), File::size(), write(), File::write(), and writev().

00075 { return m_fd; }

bool IOChannel::isBlocking ( void   )  const [virtual]

Definition at line 127 of file UnixIOChannel.cc.

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

00128 {
00129 #ifdef O_NONBLOCK
00130   int mode;
00131   if ((mode = fcntl (fd (), F_GETFL, 0)) == -1)
00132     throwStorageError ("IOChannel::isBlocking()", "fcntl()", errno);
00133 
00134   return mode & (O_NDELAY | O_NONBLOCK);
00135 #else // ! O_NONBLOCK
00136   return true;
00137 #endif // O_NONBLOCK
00138 }

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 11 of file UnixIOChannel.cc.

References fd(), IOInput::read(), s, and throwStorageError().

00012 {
00013   ssize_t s;
00014   do
00015     s = ::read (fd (), into, n);
00016   while (s == -1 && errno == EINTR);
00017 
00018   if (s == -1)
00019     throwStorageError ("IOChannel::read()", "read()", errno);
00020 
00021   return s;
00022 }

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 25 of file UnixIOChannel.cc.

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

Referenced by File::readv().

00026 {
00027   assert (! buffers || into);
00028 
00029   // readv may not support zero buffers.
00030   if (! buffers)
00031     return 0;
00032 
00033   ssize_t n = 0;
00034 
00035   // Convert the buffers to system format.
00036   std::vector<iovec> bufs (buffers);
00037   for (IOSize i = 0; i < buffers; ++i)
00038   {
00039     bufs [i].iov_len  = into [i].size ();
00040     bufs [i].iov_base = (caddr_t) into [i].data ();
00041   }
00042 
00043   // Read as long as signals cancel the read before doing anything.
00044   do
00045     n = ::readv (fd (), &bufs [0], buffers);
00046   while (n == -1 && errno == EINTR);
00047 
00048   // If it was serious error, throw it.
00049   if (n == -1)
00050     throwStorageError ("IOChannel::readv", "readv()", errno);
00051 
00052   // Return the number of bytes actually read.
00053   return n;
00054 }

void IOChannel::setBlocking ( bool  value  )  [virtual]

Definition at line 109 of file UnixIOChannel.cc.

References fd(), mode, O_NONBLOCK, off, and throwStorageError().

00110 {
00111 #ifdef O_NONBLOCK
00112   int mode;
00113   int off = value ? ~0 : ~(O_NDELAY | O_NONBLOCK);
00114   int on  = value ? O_NONBLOCK : 0;
00115 
00116   if ((mode = fcntl (fd (), F_GETFL, 0)) == -1
00117       || fcntl (fd (), F_SETFL, (mode & off) | on) == -1)
00118     throwStorageError ("IOChannel::setBlocking()", "fcntl()", errno);
00119 #elif defined FIONBIO
00120   int mode = value;
00121   if (ioctl (fd (), FIONBIO, &value) == -1)
00122     throwStorageError ("IOChannel::setBlocking()", "ioctl()", errno);
00123 #endif
00124 }

bool IOChannel::sysclose ( IOFD  fd,
int error = 0 
) [protected]

Reimplemented in File.

Definition at line 144 of file UnixIOChannel.cc.

References close().

Referenced by close().

00145 {
00146   int ret = ::close (fd);
00147   if (error) *error = errno;
00148   return ret != -1;
00149 }

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 60 of file UnixIOChannel.cc.

References fd(), s, and throwStorageError().

Referenced by File::write().

00061 {
00062   ssize_t s;
00063   do
00064     s = ::write (fd (), from, n);
00065   while (s == -1 && errno == EINTR);
00066 
00067   if (s == -1 && errno != EWOULDBLOCK)
00068     throwStorageError ("IOChannel::write()", "write()", errno);
00069 
00070   return s >= 0 ? s : 0;
00071 }

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 74 of file UnixIOChannel.cc.

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

Referenced by File::writev().

00075 {
00076   assert (! buffers || from);
00077 
00078   // writev may not support zero buffers.
00079   if (! buffers)
00080     return 0;
00081 
00082   ssize_t n = 0;
00083 
00084   // Convert the buffers to system format.
00085   std::vector<iovec> bufs (buffers);
00086   for (IOSize i = 0; i < buffers; ++i)
00087   {
00088     bufs [i].iov_len  = from [i].size ();
00089     bufs [i].iov_base = (caddr_t) from [i].data ();
00090   }
00091 
00092   // Read as long as signals cancel the read before doing anything.
00093   do
00094     n = ::writev (fd (), &bufs [0], buffers);
00095   while (n == -1 && errno == EINTR);
00096 
00097   // If it was serious error, throw it.
00098   if (n == -1)
00099     throwStorageError ("IOChannel::writev()", "writev()", errno);
00100 
00101   // Return the number of bytes actually written.
00102   return n;
00103 }


Member Data Documentation

IOFD IOChannel::m_fd [private]

Definition at line 39 of file IOChannel.h.

Referenced by fd().


The documentation for this class was generated from the following files:
Generated on Tue Jun 9 18:25:54 2009 for CMSSW by  doxygen 1.5.4