CMS 3D CMS Logo

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
 
IOSize read (void *into, IOSize n) override
 
IOSize readv (IOBuffer *into, IOSize buffers) override
 
virtual void setBlocking (bool value)
 
IOSize write (const void *from, IOSize n) override
 
IOSize writev (const IOBuffer *from, IOSize buffers) override
 
 ~IOChannel (void) override
 
- 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=nullptr)
 

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.

61 : m_fd(fd) {}
IOFD m_fd
Definition: IOChannel.h:38
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
IOChannel::~IOChannel ( void  )
override

Definition at line 63 of file IOChannel.cc.

63 {}

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

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

Referenced by esMonitoring.AsyncLineReaderMixin::handle_close(), esMonitoring.FDJsonServer::handle_close(), Vispa.Gui.BoxContentDialog.BoxContentDialog::keyPressEvent(), Vispa.Gui.FindDialog.FindDialog::keyPressEvent(), and sysclose().

84  {
85  if (fd() == EDM_IOFD_INVALID)
86  return;
87 
88  int error = 0;
89  if (!sysclose(fd(), &error))
90  throwStorageError("FileCloseError", "Calling IOChannel::close()", "sysclose()", error);
91 
93 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
bool sysclose(IOFD fd, int *error=nullptr)
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
IOFD IOChannel::fd ( void  ) const
virtual

Get the system file descriptor of the channel.

Definition at line 69 of file IOChannel.cc.

References m_fd.

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

69 { return m_fd; }
IOFD m_fd
Definition: IOChannel.h:38
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 73 of file IOChannel.cc.

References m_fd, and relativeConstraints::value.

73  {
74  // FIXME: close old one?
75  // FIXME: reset state?
76  m_fd = value;
77 }
IOFD m_fd
Definition: IOChannel.h:38
bool IOChannel::isBlocking ( void  ) const
virtual

Definition at line 112 of file UnixIOChannel.cc.

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

112  {
113 #ifdef O_NONBLOCK
114  int mode;
115  if ((mode = fcntl(fd(), F_GETFL, 0)) == -1)
116  throwStorageError("FileIsBlockingError", "Calling IOChannel::isBlocking()", "fcntl()", errno);
117 
118  return mode & (O_NDELAY | O_NONBLOCK);
119 #else // ! O_NONBLOCK
120  return true;
121 #endif // O_NONBLOCK
122 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
#define O_NONBLOCK
Definition: SysFile.h:21
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
IOSize IOChannel::read ( void *  into,
IOSize  n 
)
overridevirtual

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.

Definition at line 9 of file UnixIOChannel.cc.

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

9  {
10  ssize_t s;
11  do
12  s = ::read(fd(), into, n);
13  while (s == -1 && errno == EINTR);
14 
15  if (s == -1)
16  throwStorageError(edm::errors::FileReadError, "Calling IOChannel::read()", "read()", errno);
17 
18  return s;
19 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
int read(void)
Definition: IOInput.cc:52
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
IOSize IOChannel::readv ( IOBuffer into,
IOSize  buffers 
)
overridevirtual

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.

Definition at line 21 of file UnixIOChannel.cc.

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

Referenced by File::readv().

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

Definition at line 97 of file UnixIOChannel.cc.

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

97  {
98 #ifdef O_NONBLOCK
99  int mode;
100  int off = value ? ~0 : ~(O_NDELAY | O_NONBLOCK);
101  int on = value ? O_NONBLOCK : 0;
102 
103  if ((mode = fcntl(fd(), F_GETFL, 0)) == -1 || fcntl(fd(), F_SETFL, (mode & off) | on) == -1)
104  throwStorageError("FileSetBlockingError", "Calling IOChannel::setBlocking()", "fcntl()", errno);
105 #elif defined FIONBIO
106  int mode = value;
107  if (ioctl(fd(), FIONBIO, &value) == -1)
108  throwStorageError("FileSetBlockingError", "Calling IOChannel::setBlocking()", "ioctl()", errno);
109 #endif
110 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
Definition: value.py:1
#define O_NONBLOCK
Definition: SysFile.h:21
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
bool IOChannel::sysclose ( IOFD  fd,
int *  error = nullptr 
)
protected

Definition at line 127 of file UnixIOChannel.cc.

References close(), and runTheMatrix::ret.

Referenced by close().

127  {
128  int ret = ::close(fd);
129  if (error)
130  *error = errno;
131  return ret != -1;
132 }
ret
prodAgent to be discontinued
virtual void close(void)
Definition: IOChannel.cc:84
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
IOSize IOChannel::write ( const void *  from,
IOSize  n 
)
overridevirtual

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.

Definition at line 53 of file UnixIOChannel.cc.

References fd(), edm::errors::FileWriteError, alignCSCRings::s, and throwStorageError().

Referenced by File::write().

53  {
54  ssize_t s;
55  do
56  s = ::write(fd(), from, n);
57  while (s == -1 && errno == EINTR);
58 
59  if (s == -1 && errno != EWOULDBLOCK)
60  throwStorageError(edm::errors::FileWriteError, "Calling IOChannel::write()", "write()", errno);
61 
62  return s >= 0 ? s : 0;
63 }
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
IOSize write(const void *from, IOSize n) override
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
IOSize IOChannel::writev ( const IOBuffer from,
IOSize  buffers 
)
overridevirtual

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.

Definition at line 65 of file UnixIOChannel.cc.

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

Referenced by File::writev().

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

Member Data Documentation

IOFD IOChannel::m_fd
private

Definition at line 38 of file IOChannel.h.

Referenced by fd().