TDME2 1.9.121
KernelEventMechanism.cpp
Go to the documentation of this file.
1#include <tdme/tdme.h>
2
3#if defined(_WIN32)
4 #include <winsock2.h>
5#else
6 #include <sys/select.h>
7 #include <sys/time.h>
8#endif
9
15
19
20KernelEventMechanism::KernelEventMechanism() : initialized(false), _psd(NULL) {
21 // allocate platform specific data
22 _psd = static_cast<void*>(new KernelEventMechanismPSD());
23
24 //
25 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
26
27 // clear fd sets
28 FD_ZERO(&psd->rfds);
29 FD_ZERO(&psd->wfds);
30}
31
32KernelEventMechanism::~KernelEventMechanism() {
33 // delete platform specific data
34 delete static_cast<KernelEventMechanismPSD*>(_psd);
35}
36
37void KernelEventMechanism::setSocketInterest(const NetworkSocket& socket, const NIOInterest lastInterest, const NIOInterest interest, const void* cookie) {
38 // skip if not initialized
39 if (initialized == false) return;
40
41 //
42 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
43
44 // synchronize fd set access
45 psd->fdsMutex.lock();
46
47 // remove fd from fds
48 psd->fds.erase(socket.descriptor);
49
50 // remove last read interest
51 if ((lastInterest & NIO_INTEREST_READ) == NIO_INTEREST_READ) {
52 FD_CLR(socket.descriptor, &psd->rfds);
53 }
54 // remove last write interest
55 if ((lastInterest & NIO_INTEREST_WRITE) == NIO_INTEREST_WRITE) {
56 FD_CLR(socket.descriptor, &psd->wfds);
57 }
58
59 // have interest?
60 bool haveInterest = false;
61
62 // add read interest
63 if ((interest & NIO_INTEREST_READ) == NIO_INTEREST_READ) {
64 FD_SET(socket.descriptor, &psd->rfds);
65 haveInterest = true;
66 }
67 // add write interest
68 if ((interest & NIO_INTEREST_WRITE) == NIO_INTEREST_WRITE) {
69 FD_SET(socket.descriptor, &psd->wfds);
70 haveInterest = true;
71 }
72
73 // add fd to fds
74 if (haveInterest == true) {
75 if (socket.descriptor > psd->maxFd) psd->maxFd = socket.descriptor;
76 psd->fds[socket.descriptor] = (void*)cookie;
77 }
78
79 // done synchronize fd set access
80 psd->fdsMutex.unlock();
81}
82
83void KernelEventMechanism::initKernelEventMechanism(const unsigned int maxCCU) {
85
86 //
87 initialized = true;
88}
89
91 // skip if not initialized
92 if (initialized == false) return;
93
94 // platform specific data
95 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
96}
97
99 // skip if not initialized
100 if (initialized == false) return -1;
101
102 // platform specific data
103 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
104
105 // have a timeout of 1ms
106 // as we only can delegate interest changes to the kernel by
107 // select
108 struct timeval timeout = {0, 1L * 1000L};
109
110 // clone fd sets
111 psd->fdsMutex.lock();
112 fd_set rfds = psd->rfds;
113 fd_set wfds = psd->wfds;
114 psd->fdsMutex.unlock();
115
116 // run select
117 auto result = select(psd->maxFd + 1, &rfds, &wfds, NULL, &timeout);
118 if (result == -1) {
119 throw NetworkKEMException("select failed");
120 }
121
122 // compile list of events
123 psd->fdsMutex.lock();
124 psd->events.clear();
125 for (auto fdIt = psd->fds.begin(); fdIt != psd->fds.end(); ++fdIt) {
126 if (FD_ISSET(fdIt->first, &rfds) != 0) {
127 KernelEventMechanismPSD::Event event;
128 event.descriptor = fdIt->first;
129 event.interest = NIO_INTEREST_READ;
130 event.cookie = fdIt->second;
131 psd->events.push_back(event);
132 }
133 if (FD_ISSET(fdIt->first, &wfds) != 0) {
134 KernelEventMechanismPSD::Event event;
135 event.descriptor = fdIt->first;
136 event.interest = NIO_INTEREST_WRITE;
137 event.cookie = fdIt->second;
138 psd->events.push_back(event);
139 }
140 }
141 psd->fdsMutex.unlock();
142
143 //
144 return psd->events.size();
145}
146
147void KernelEventMechanism::decodeKernelEvent(const unsigned int index, NIOInterest &interest, void*& cookie) {
148 // skip if not initialized
149 if (initialized == false) return;
150
151 // platform specific data
152 auto psd = static_cast<KernelEventMechanismPSD*>(_psd);
153
154 // read interest and cookie from event
155 KernelEventMechanismPSD::Event& event = psd->events[index];
156 interest = event.interest;
157 cookie = event.cookie;
158}
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
Fallback/select kernel event mechanism platform specific data.
void lock()
Locks the mutex, additionally mutex locks will block until other locks have been unlocked.
Definition: Mutex.cpp:39
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