CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
List of all members | Public Member Functions | Protected Member Functions | Private Attributes
IOChannel Class Reference

#include <IOChannel.h>

Inheritance diagram for IOChannel:
IOInput IOOutput File RemoteFile

Public Member Functions

virtual void close (void)
 
virtual IOFD fd (void) const
 
virtual void fd (IOFD value)
 
 IOChannel (IOFD fd=-1)
 
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)
 
- Public Member Functions inherited from IOInput
int read (void)
 
IOSize read (IOBuffer into)
 
IOSize xread (IOBuffer into)
 
IOSize xread (void *into, IOSize n)
 
IOSize xreadv (IOBuffer *into, IOSize buffers)
 
virtual ~IOInput (void)
 Destruct the stream. A no-op. More...
 
- Public Member Functions inherited from IOOutput
IOSize write (unsigned char byte)
 
IOSize write (IOBuffer from)
 
IOSize xwrite (const void *from, IOSize n)
 
IOSize xwrite (IOBuffer from)
 
IOSize xwritev (const IOBuffer *from, IOSize buffers)
 
virtual ~IOOutput (void)
 Destruct the stream. A no-op. More...
 

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 = -1)

Definition at line 61 of file IOChannel.cc.

62  : m_fd (fd)
63 {}
IOFD m_fd
Definition: IOChannel.h:39
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
IOChannel::~IOChannel ( void  )
virtual

Definition at line 65 of file IOChannel.cc.

66 {}

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 lumiQTWidget.ApplicationWindow::fileQuit(), Vispa.Gui.BoxContentDialog.BoxContentDialog::keyPressEvent(), Vispa.Gui.FindDialog.FindDialog::keyPressEvent(), and sysclose().

93 {
94  if (fd () == EDM_IOFD_INVALID)
95  return;
96 
97  int error = 0;
98  if (! sysclose (fd (), &error))
99  throwStorageError ("FileCloseError", "Calling IOChannel::close()",
100  "sysclose()", error);
101 
103 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:7
bool sysclose(IOFD fd, int *error=0)
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
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(), write(), and writev().

74 { return m_fd; }
IOFD m_fd
Definition: IOChannel.h:39
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.

80 {
81  // FIXME: close old one?
82  // FIXME: reset state?
83  m_fd = value;
84 }
IOFD m_fd
Definition: IOChannel.h:39
bool IOChannel::isBlocking ( void  ) const
virtual

Definition at line 126 of file UnixIOChannel.cc.

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

127 {
128 #ifdef O_NONBLOCK
129  int mode;
130  if ((mode = fcntl (fd (), F_GETFL, 0)) == -1)
131  throwStorageError ("FileIsBlockingError", "Calling IOChannel::isBlocking()", "fcntl()", errno);
132 
133  return mode & (O_NDELAY | O_NONBLOCK);
134 #else // ! O_NONBLOCK
135  return true;
136 #endif // O_NONBLOCK
137 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:7
#define O_NONBLOCK
Definition: SysFile.h:21
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
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, IOInput::read(), alignCSCRings::s, and throwStorageError().

11 {
12  ssize_t s;
13  do
14  s = ::read (fd (), into, n);
15  while (s == -1 && errno == EINTR);
16 
17  if (s == -1)
18  throwStorageError (edm::errors::FileReadError, "Calling IOChannel::read()", "read()", errno);
19 
20  return s;
21 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:7
int read(void)
Definition: IOInput.cc:54
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
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().

25 {
26  assert (! buffers || into);
27 
28  // readv may not support zero buffers.
29  if (! buffers)
30  return 0;
31 
32  ssize_t n = 0;
33 
34  // Convert the buffers to system format.
35  std::vector<iovec> bufs (buffers);
36  for (IOSize i = 0; i < buffers; ++i)
37  {
38  bufs [i].iov_len = into [i].size ();
39  bufs [i].iov_base = (caddr_t) into [i].data ();
40  }
41 
42  // Read as long as signals cancel the read before doing anything.
43  do
44  n = ::readv (fd (), &bufs [0], buffers);
45  while (n == -1 && errno == EINTR);
46 
47  // If it was serious error, throw it.
48  if (n == -1)
49  throwStorageError (edm::errors::FileReadError, "Calling IOChannel::readv", "readv()", errno);
50 
51  // Return the number of bytes actually read.
52  return n;
53 }
int i
Definition: DBlmapReader.cc:9
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:7
IOSize size(void) const
Definition: IOBuffer.h:50
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
size_t IOSize
Definition: IOTypes.h:14
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
virtual IOSize readv(IOBuffer *into, IOSize buffers)
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.

109 {
110 #ifdef O_NONBLOCK
111  int mode;
112  int off = value ? ~0 : ~(O_NDELAY | O_NONBLOCK);
113  int on = value ? O_NONBLOCK : 0;
114 
115  if ((mode = fcntl (fd (), F_GETFL, 0)) == -1
116  || fcntl (fd (), F_SETFL, (mode & off) | on) == -1)
117  throwStorageError ("FileSetBlockingError", "Calling IOChannel::setBlocking()", "fcntl()", errno);
118 #elif defined FIONBIO
119  int mode = value;
120  if (ioctl (fd (), FIONBIO, &value) == -1)
121  throwStorageError ("FileSetBlockingError", "Calling IOChannel::setBlocking()", "ioctl()", errno);
122 #endif
123 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:7
#define O_NONBLOCK
Definition: SysFile.h:21
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
bool IOChannel::sysclose ( IOFD  fd,
int *  error = 0 
)
protected

Definition at line 143 of file UnixIOChannel.cc.

References close(), and run_regression::ret.

Referenced by close().

144 {
145  int ret = ::close (fd);
146  if (error) *error = errno;
147  return ret != -1;
148 }
virtual void close(void)
Definition: IOChannel.cc:92
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
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(), alignCSCRings::s, and throwStorageError().

Referenced by File::write().

60 {
61  ssize_t s;
62  do
63  s = ::write (fd (), from, n);
64  while (s == -1 && errno == EINTR);
65 
66  if (s == -1 && errno != EWOULDBLOCK)
67  throwStorageError ("FileWriteError", "Calling IOChannel::write()", "write()", errno);
68 
69  return s >= 0 ? s : 0;
70 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:7
virtual IOSize write(const void *from, IOSize n)
static std::string from(" from ")
virtual IOFD fd(void) const
Definition: IOChannel.cc:73
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().

74 {
75  assert (! buffers || from);
76 
77  // writev may not support zero buffers.
78  if (! buffers)
79  return 0;
80 
81  ssize_t n = 0;
82 
83  // Convert the buffers to system format.
84  std::vector<iovec> bufs (buffers);
85  for (IOSize i = 0; i < buffers; ++i)
86  {
87  bufs [i].iov_len = from [i].size ();
88  bufs [i].iov_base = (caddr_t) from [i].data ();
89  }
90 
91  // Read as long as signals cancel the read before doing anything.
92  do
93  n = ::writev (fd (), &bufs [0], buffers);
94  while (n == -1 && errno == EINTR);
95 
96  // If it was serious error, throw it.
97  if (n == -1)
98  throwStorageError ("FileWriteError", "Calling IOChannel::writev()", "writev()", errno);
99 
100  // Return the number of bytes actually written.
101  return n;
102 }
int i
Definition: DBlmapReader.cc:9
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:7
virtual IOSize writev(const IOBuffer *from, IOSize buffers)
IOSize size(void) const
Definition: IOBuffer.h:50
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
size_t IOSize
Definition: IOTypes.h:14
virtual IOFD fd(void) const
Definition: IOChannel.cc:73

Member Data Documentation

IOFD IOChannel::m_fd
private

Definition at line 39 of file IOChannel.h.

Referenced by fd().