Belle II Software  release-05-02-19
FileDescriptor.cc
1 #include "daq/slc/system/FileDescriptor.h"
2 
3 #include <daq/slc/base/IOException.h>
4 
5 #include <unistd.h>
6 #include <sys/select.h>
7 #include <stdio.h>
8 
9 using namespace Belle2;
10 
11 FileDescriptor::FileDescriptor()
12 {
13  m_fd = -1;
14 }
15 
16 FileDescriptor::FileDescriptor(const FileDescriptor& fd)
17 {
18  m_fd = fd.m_fd;
19 }
20 
21 FileDescriptor::FileDescriptor(int fd)
22 {
23  m_fd = fd;
24 }
25 
26 FileDescriptor::~FileDescriptor()
27 {
28 
29 }
30 
31 int FileDescriptor::get_fd() const
32 {
33  return m_fd;
34 }
35 
36 bool FileDescriptor::select(int sec, int usec)
37 {
38  if (m_fd <= 0) {
39  return false;
40  }
41  fd_set fds;
42  FD_ZERO(&fds);
43  FD_SET(m_fd, &fds);
44  int ret;
45  if (sec >= 0 && usec >= 0) {
46  timeval t = {sec, usec};
47  ret = ::select(FD_SETSIZE, &fds, NULL, NULL, &t);
48  } else {
49  ret = ::select(FD_SETSIZE, &fds, NULL, NULL, NULL);
50  }
51  if (ret < 0) {
52  perror("select");
53  throw (IOException("Failed to select"));
54  }
55  if (FD_ISSET(m_fd, &fds)) {
56  return true;
57  } else {
58  return false;
59  }
60 }
61 
62 bool FileDescriptor::select2(int sec, int usec)
63 {
64  if (m_fd <= 0) {
65  return false;
66  }
67  fd_set fds;
68  FD_ZERO(&fds);
69  FD_SET(m_fd, &fds);
70  int ret;
71  if (sec >= 0 && usec >= 0) {
72  timeval t = {sec, usec};
73  ret = ::select(FD_SETSIZE, NULL, &fds, NULL, &t);
74  } else {
75  ret = ::select(FD_SETSIZE, NULL, &fds, NULL, NULL);
76  }
77  if (ret < 0) {
78  perror("select");
79  throw (IOException("Failed to select"));
80  }
81  if (FD_ISSET(m_fd, &fds)) {
82  return true;
83  } else {
84  return false;
85  }
86 }
87 
88 bool FileDescriptor::close()
89 {
90  if (m_fd > 0) {
91  if (::close(m_fd) != 0) {
92  return false;
93  }
94  }
95  m_fd = -1;
96  return true;
97 }
Belle2::IOException
Definition: IOException.h:12
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::FileDescriptor
Definition: FileDescriptor.h:10