#include <IOChannel.h>
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 |
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.
IOChannel::IOChannel | ( | IOFD | fd = EDM_IOFD_INVALID | ) |
Definition at line 61 of file IOChannel.cc.
IOChannel::~IOChannel | ( | void | ) | [virtual] |
Definition at line 65 of file IOChannel.cc.
{}
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.
bool IOChannel::isBlocking | ( | void | ) | const [virtual] |
Definition at line 126 of file UnixIOChannel.cc.
References fd(), 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 }
Read at most n bytes from the channel into the buffer into.
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; }
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(), 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] |
Write n bytes from the buffer at from.
Implements IOOutput.
Reimplemented in File.
Definition at line 59 of file UnixIOChannel.cc.
References fd(), Capri::details::from(), n, alignCSCRings::s, and throwStorageError().
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; }
IOFD IOChannel::m_fd [private] |
Definition at line 39 of file IOChannel.h.
Referenced by fd().