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