#include <Utilities/StorageFactory/interface/IOChannel.h>
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 |
Definition at line 9 of file IOChannel.h.
IOChannel::IOChannel | ( | IOFD | fd = EDM_IOFD_INVALID |
) |
IOChannel::~IOChannel | ( | 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 }
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.
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; }
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 }
Read at most n bytes from the channel into the buffer into.
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 }
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 }
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 }
Write n bytes from the buffer at from.
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 }
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 }
IOFD IOChannel::m_fd [private] |