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 void fd (IOFD value)
 
virtual IOFD fd (void) const
 
 IOChannel (IOFD fd=-1)
 
virtual bool isBlocking (void) const
 
IOSize read (IOBuffer into)
 
IOSize read (void *into, IOSize n) override
 
virtual IOSize read (void *into, IOSize n)=0
 
int read (void)
 
IOSize readv (IOBuffer *into, IOSize buffers) override
 
virtual void setBlocking (bool value)
 
IOSize write (const void *from, IOSize n) override
 
virtual IOSize write (const void *from, IOSize n)=0
 
IOSize write (IOBuffer from)
 
IOSize write (unsigned char byte)
 
IOSize writev (const IOBuffer *from, IOSize buffers) override
 
 ~IOChannel (void) override
 
- Public Member Functions inherited from IOInput
IOSize read (IOBuffer into)
 
int read (void)
 
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 (IOBuffer from)
 
IOSize write (unsigned char byte)
 
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::IOChannel ( IOFD  fd = -1)

Definition at line 61 of file IOChannel.cc.

61 : m_fd(fd) {}

◆ ~IOChannel()

IOChannel::~IOChannel ( void  )
override

Definition at line 63 of file IOChannel.cc.

63 {}

Member Function Documentation

◆ close()

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.

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 }

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

Referenced by esMonitoring.AsyncLineReaderMixin::handle_close(), esMonitoring.FDJsonServer::handle_close(), and sysclose().

◆ fd() [1/2]

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.

73  {
74  // FIXME: close old one?
75  // FIXME: reset state?
76  m_fd = value;
77 }

References m_fd, and relativeConstraints::value.

◆ fd() [2/2]

IOFD IOChannel::fd ( void  ) const
virtual

Get the system file descriptor of the channel.

Definition at line 69 of file IOChannel.cc.

69 { return m_fd; }

References m_fd.

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

◆ isBlocking()

bool IOChannel::isBlocking ( void  ) const
virtual

Definition at line 112 of file UnixIOChannel.cc.

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 }

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

◆ read() [1/4]

IOSize IOInput::read

Read from the input stream into the buffer starting at into and of size n.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

The base class implementation simply forwards the call to read(void *, IOSize) method.

Returns
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons.
Exceptions
Incase of error, a #IOError exception is thrown. This includes the situation where the input stream is in non-blocking mode and no input is currently available (FIXME: make this simpler; clarify which exception).

Definition at line 80 of file IOInput.cc.

80 { return read(into.data(), into.size()); }

◆ read() [2/4]

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.

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 }

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

◆ read() [3/4]

IOSize IOInput::read

Read into into at most n number of bytes.

If this is a blocking stream, the call will block until some data can be read, end of input is reached, or an exception is thrown. For a non-blocking stream the available input is returned. If none is available, an exception is thrown.

Returns
The number of bytes actually read. This is less or equal to the size of the buffer. Zero indicates that the end of the input has been reached: end of file, or remote end closing for a connected channel like a pipe or a socket. Otherwise the value can be less than requested if limited amount of input is currently available for platform or implementation reasons.
Exceptions
Incase of error, a #IOError exception is thrown. This includes the situation where the input stream is in non-blocking mode and no input is currently available (FIXME: make this simpler; clarify which exception).

◆ read() [4/4]

int IOInput::read

Read the next single byte from the input stream and return it as an unsigned char cast to an int, -1 to indicate end of intput data.

If this is a blocking stream, the call will block until the byte can be read, end of data is reached, or an exception is thrown. For a non-blocking input a character is returned if one is available, otherwise an exception is thrown.

The base class implementation simply forwards the call to read(void *, IOSize) method.

Returns
The byte cast from a unsigned char to an int (in range 0...255, inclusive) if one could be read, or -1 to indicate end of input data.
Exceptions
Incase of error, a #IOError exception is thrown. This includes the situation where the input stream is in non-blocking mode and no input is currently available (FIXME: make this simpler; clarify which exception).

Definition at line 52 of file IOInput.cc.

52  {
53  unsigned char byte;
54  IOSize n = read(&byte, 1);
55  return n == 0 ? -1 : byte;
56 }

◆ readv()

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.

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 }

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

Referenced by File::readv().

◆ setBlocking()

void IOChannel::setBlocking ( bool  value)
virtual

Definition at line 97 of file UnixIOChannel.cc.

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 }

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

◆ sysclose()

bool IOChannel::sysclose ( IOFD  fd,
int *  error = nullptr 
)
protected

Definition at line 127 of file UnixIOChannel.cc.

127  {
128  int ret = ::close(fd);
129  if (error)
130  *error = errno;
131  return ret != -1;
132 }

References close(), relativeConstraints::error, fd(), and runTheMatrix::ret.

Referenced by close().

◆ write() [1/4]

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.

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 }

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

Referenced by File::write().

◆ write() [2/4]

IOSize IOOutput::write

Write n bytes of data starting at address from.

Returns
The number of bytes written. Normally this will be n, but can be less, even zero, for example if the stream is non-blocking mode and cannot accept input at this time.
Exceptions
Incase of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception – the return value will be less than requested.

◆ write() [3/4]

IOSize IOOutput::write

Write to the output stream the buffer from. This method is simply redirected to write(const void *, IOSize).

Note that derived classes should not normally call this method, as it simply routes the call back to derived class through the other virtual functions. Use this method only at the "outside edge" when transferring calls from one object to another, not in up/down calls in the inheritance tree.

Returns
The number of bytes actually written. This is less or equal to the size of the buffer. The value can be less than requested if the stream is unable to accept all the output for platform or implementation reasons.
Exceptions
Incase of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception – the return value will be less than requested.

Definition at line 59 of file IOOutput.cc.

59 { return write(from.data(), from.size()); }

◆ write() [4/4]

IOSize IOOutput::write

Write a single byte to the output stream. This method is simply redirected to write(const void *, IOSize).

Note that derived classes should not normally call this method, as it simply routes the call back to derived class through the other virtual functions. Use this method only at the "outside edge" when transferring calls from one object to another, not in up/down calls in the inheritance tree.

Returns
The number of bytes written. Normally this will be one, but can be zero if the stream is in non-blocking mode and cannot accept output at this time.
Exceptions
Incase of error, an exception is thrown. However if the stream is in non-blocking mode and cannot accept output, it will not throw an exception – zero will be returned.

Definition at line 39 of file IOOutput.cc.

39 { return write(&byte, 1); }

◆ writev()

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.

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 }

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

Referenced by File::writev().

Member Data Documentation

◆ m_fd

IOFD IOChannel::m_fd
private

Definition at line 38 of file IOChannel.h.

Referenced by fd().

runTheMatrix.ret
ret
prodAgent to be discontinued
Definition: runTheMatrix.py:373
IOChannel::readv
IOSize readv(IOBuffer *into, IOSize buffers) override
Definition: UnixIOChannel.cc:21
IOBuffer::size
IOSize size(void) const
Definition: IOBuffer.h:34
mps_fire.i
i
Definition: mps_fire.py:428
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
IOChannel::close
virtual void close(void)
Definition: IOChannel.cc:84
ALCARECOPromptCalibProdSiPixelAli0T_cff.mode
mode
Definition: ALCARECOPromptCalibProdSiPixelAli0T_cff.py:96
edm::errors::FileWriteError
Definition: EDMException.h:66
cms::cuda::assert
assert(be >=bs)
relativeConstraints.error
error
Definition: relativeConstraints.py:53
alignCSCRings.s
s
Definition: alignCSCRings.py:92
IOChannel::writev
IOSize writev(const IOBuffer *from, IOSize buffers) override
Definition: UnixIOChannel.cc:65
value
Definition: value.py:1
throwStorageError
void throwStorageError(const char *category, const char *context, const char *call, int error)
Definition: Throw.cc:6
IOChannel::sysclose
bool sysclose(IOFD fd, int *error=nullptr)
Definition: UnixIOChannel.cc:127
IOChannel::m_fd
IOFD m_fd
Definition: IOChannel.h:38
EDM_IOFD_INVALID
#define EDM_IOFD_INVALID
Definition: IOTypes.h:8
relativeConstraints.value
value
Definition: relativeConstraints.py:53
IOChannel::write
IOSize write(const void *from, IOSize n) override
Definition: UnixIOChannel.cc:53
IOInput::read
int read(void)
Definition: IOInput.cc:52
O_NONBLOCK
#define O_NONBLOCK
Definition: SysFile.h:21
data
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:79
IOChannel::fd
virtual IOFD fd(void) const
Definition: IOChannel.cc:69
IOSize
size_t IOSize
Definition: IOTypes.h:14
edm::errors::FileReadError
Definition: EDMException.h:50