TDME2 1.9.121
KernelEventMechanism.cpp
Go to the documentation of this file.
1/**
2 * @version $Id: 48c210d2bc49b9f43a27196102d30eda9d1ef12d $
3 */
4
5#include <errno.h>
6#include <stdlib.h>
7#include <string.h>
8#include <sys/epoll.h>
9#include <unistd.h>
10
11#include <tdme/tdme.h>
17
21
22KernelEventMechanism::KernelEventMechanism() : initialized(false), _psd(NULL) {
23 // allocate platform specific data
24 _psd = static_cast<void*>(new KernelEventMechanismPSD());
25}
26
27KernelEventMechanism::~KernelEventMechanism() {
28 // delete platform specific data
29 delete static_cast<KernelEventMechanismPSD*>(_psd);
30}
31
32void KernelEventMechanism::setSocketInterest(const NetworkSocket& socket, const NIOInterest lastInterest, const NIOInterest interest, const void* cookie) {
33 // platform specific data
34 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
35
36 // setup new event
37 struct epoll_event event;
38 event.events = EPOLLET;
39 event.data.ptr = (void*)cookie;
40
41 // handle read interest
42 if ((interest & NIO_INTEREST_READ) == NIO_INTEREST_READ) {
43 event.events|= EPOLLIN;
44 }
45 // handle write interest
46 if ((interest & NIO_INTEREST_WRITE) == NIO_INTEREST_WRITE) {
47 event.events|= EPOLLOUT;
48 }
49
50 //
51 if (epoll_ctl(
52 psd->ep,
53 lastInterest == NIO_INTEREST_NONE?EPOLL_CTL_ADD:EPOLL_CTL_MOD,
54 socket.descriptor,
55 &event) == -1) {
56 //
57 std::string msg = "Could not add epoll event: ";
58 msg+= strerror(errno);
59 throw NetworkKEMException(msg);
60 }
61}
62
63void KernelEventMechanism::initKernelEventMechanism(const unsigned int maxCCU) {
64 // platform specific data
65 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
66
67 // epoll event list, maxCCU
68 psd->epEventListMax = maxCCU;
69 psd->epEventList = new epoll_event[psd->epEventListMax];
70
71 // start epoll and get the descriptor
72 psd->ep = epoll_create1(0);
73 if (psd->ep == -1) {
74 delete [] psd->epEventList;
75 std::string msg = "Could not create epoll: ";
76 msg+= strerror(errno);
77 throw NetworkKEMException(msg);
78 }
79
80 //
81 initialized = true;
82}
83
85 // skip if not initialized
86 if (initialized == false) return;
87
88 // platform specific data
89 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
90
91 //
92 close(psd->ep);
93 delete [] psd->epEventList;
94}
95
97 // platform specific data
98 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
99
100 while (true == true) {
101 //
102 auto events = epoll_wait(psd->ep, psd->epEventList, psd->epEventListMax, 5);
103
104 // check for error
105 if (events == -1) {
106 if (errno == EINTR) {
107 // epoll_wait was interrupted by system call, so ignore this and restart
108 } else {
109 std::string msg = "epoll_wait failed: ";
110 msg+= strerror(errno);
111 throw NetworkKEMException(msg);
112 }
113 } else {
114 //
115 return events;
116 }
117 }
118}
119
120void KernelEventMechanism::decodeKernelEvent(const unsigned int index, NIOInterest &interest, void*& cookie) {
121 // platform specific data
122 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
123
124 //
125 auto event = &psd->epEventList[index];
126
127 // we only support user data
128 cookie = (void*)event->data.ptr;
129
130 // set up interest
131 interest = NIO_INTEREST_NONE;
132 if ((event->events & EPOLLIN) == EPOLLIN) {
133 interest|= NIO_INTEREST_READ;
134 }
135 if ((event->events & EPOLLOUT) == EPOLLOUT) {
136 interest|= NIO_INTEREST_WRITE;
137 }
138}
Interface to kernel event mechanismns.
void shutdownKernelEventMechanism()
shutdowns the kernel event mechanism
void setSocketInterest(const NetworkSocket &socket, const NIOInterest lastInterest, const NIOInterest interest, const void *cookie)
sets a non blocked socket io interest
int doKernelEventMechanism()
do the kernel event mechanism
void initKernelEventMechanism(const unsigned int maxCCU)
initializes the kernel event mechanism
void decodeKernelEvent(const unsigned int index, NIOInterest &interest, void *&cookie)
decodes a kernel event
Kernel event mechanism exception class.
Base class of network sockets.
Definition: NetworkSocket.h:17
Linux kernel event mechanism platform specific data.
const NIOInterest NIO_INTEREST_NONE
Definition: NIOInterest.h:11
const NIOInterest NIO_INTEREST_READ
Definition: NIOInterest.h:12
uint8_t NIOInterest
type definiton for network UI interest
Definition: NIOInterest.h:10
const NIOInterest NIO_INTEREST_WRITE
Definition: NIOInterest.h:13