libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
FileDescriptorManager.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef FILEDESCRIPTORMANAGER_H_
32 #define FILEDESCRIPTORMANAGER_H_
33 
34 #include <memory>
35 #include <mutex>
36 #include <atomic>
37 
38 #include <sys/epoll.h>
39 #include <gnutls/gnutls.h>
40 
41 namespace BaseLib {
42 
43 class SharedObjects;
44 
46  int32_t id = 0;
47  std::atomic_int descriptor{-1};
48  std::atomic_int epoll_descriptor{-1};
49  gnutls_session_t tlsSession = nullptr;
50 } __attribute__((aligned(16)));
51 
52 typedef std::shared_ptr<FileDescriptor> PFileDescriptor;
53 
54 class FileDescriptorManager {
55  public:
56  FileDescriptorManager();
57  FileDescriptorManager(const FileDescriptorManager &) = delete; //Copy constructor
58  FileDescriptorManager(FileDescriptorManager &&) noexcept; //Move constructor
59  FileDescriptorManager &operator=(const FileDescriptorManager &) = delete; //Copy assignment operator
60  ~FileDescriptorManager();
61  void dispose();
62 
63  PFileDescriptor add(int fileDescriptor);
64  PFileDescriptor add(int file_descriptor, int epoll_file_descriptor, uint32_t epoll_events);
65  void remove(PFileDescriptor &descriptor);
66  void close(PFileDescriptor &descriptor);
67  void shutdown(PFileDescriptor &descriptor);
68  PFileDescriptor get(int fileDescriptor);
69  bool isValid(int fileDescriptor, int32_t id);
70  bool isValid(const PFileDescriptor &descriptor);
71  int32_t getMax();
72  std::unique_lock<std::mutex> getLock();
73  private:
74  struct OpaquePointer;
75  std::unique_ptr<OpaquePointer> opaque_pointer_;
76 
77 };
78 }
79 #endif
std::atomic_int descriptor
Definition: FileDescriptorManager.h:47
gnutls_session_t tlsSession
Definition: FileDescriptorManager.h:49
Definition: BaseLib.cpp:34
class BaseLib::FileDescriptorManager __attribute__
std::atomic_int epoll_descriptor
Definition: FileDescriptorManager.h:48
std::shared_ptr< FileDescriptor > PFileDescriptor
Definition: FileDescriptorManager.h:52
Definition: FileDescriptorManager.h:45