-
Notifications
You must be signed in to change notification settings - Fork 0
/
reactor.c
219 lines (172 loc) · 5.45 KB
/
reactor.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: reactor.c
* Author: osrec
*
* Created on 19 March 2018, 23:01
*/
#include "reactor.h"
#include "fserve.h"
#include "utils.h"
#include "nodeProtocol.h"
#define _GNU_SOURCE
#include <fcntl.h>
#include <errno.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/eventfd.h>
#include <sys/epoll.h>
int eventFD;
unsigned long incrementConnectionNumber()
{
//pthread_mutex_lock( &connectionNumberMutex );
int n = ++connectionNumber;
//pthread_mutex_unlock( &connectionNumberMutex );
return n;
}
void * reactorLoop(void * args)
{
// Initialise workers
int i, r;
// pthread_t workerThreads[NUMBER_OF_WORKERS];
//
// debug_print("Starting event loop thread");
//
// for (i = 0; i < NUMBER_OF_WORKERS; ++i)
// {
// int *workerNumber = malloc(sizeof(i));
// *workerNumber = i;
// pthread_create(&workerThreads[i], NULL, &workerLoop, workerNumber);
// }
eventFD = eventfd(0, EFD_NONBLOCK);
FSConnection * conn = malloc(sizeof(FSConnection));
conn->type = FS_CONN_TYPE_DUMMY;
struct epoll_event ee;
ee.data.ptr = conn;
ee.events = EPOLLIN | EPOLLET;
epoll_ctl(epollFD, EPOLL_CTL_ADD, eventFD, &ee);
debug_print("Getting core event loop started");
// Initialise Event Loop
int readyFileDescriptorCount;
int MAX_EVENTS = 64;
struct epoll_event epollEventsArray[MAX_EVENTS];
for(;;)
{
debug_print("### Awaiting events ...");
do
{
readyFileDescriptorCount = epoll_wait(epollFD, epollEventsArray, MAX_EVENTS, 5000);
}
while (readyFileDescriptorCount < 0 && errno == EINTR);
// debug_print("## readyFileDescriptorCount: %d", readyFileDescriptorCount);
if (readyFileDescriptorCount == -1)
{
exitWithError("reactor epoll_wait");
}
debug_print("### Got %d events ...", readyFileDescriptorCount);
for (int n = 0; n < readyFileDescriptorCount; ++n)
{
FSConnection * conn = (FSConnection *)epollEventsArray[n].data.ptr;
if(epollEventsArray[n].events & EPOLLIN) { debug_print("EPOLLIN %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLPRI) { debug_print("EPOLLPRI %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLOUT) { debug_print("EPOLLOUT %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLRDNORM) { debug_print("EPOLLRDNORM %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLRDBAND) { debug_print("EPOLLRDBAND %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLWRNORM) { debug_print("EPOLLWRNORM %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLWRBAND) { debug_print("EPOLLWRBAND %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLMSG) { debug_print("EPOLLMSG %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLERR) { debug_print("##################### EPOLLERR %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLHUP) { debug_print("EPOLLHUP %lu %d", conn->id, conn->fd); }
if(epollEventsArray[n].events & EPOLLRDHUP) { debug_print("EPOLLRDHUP %lu %d", conn->id, conn->fd); }
if(conn->type & FS_CONN_TYPE_LISTENER)
{
acceptConnections(listenerSocketFD);
}
if(conn->type & FS_CONN_TYPE_DUMMY)
{
uint64_t u;
read(eventFD, &u, sizeof(u));
acceptConnections(listenerSocketFD);
}
else if(conn->type & FS_CONN_TYPE_NODE)
{
if(epollEventsArray[n].events & (EPOLLHUP | EPOLLRDHUP | EPOLLERR) )
{
// debug_print("Ready for close");
handleNodeConnectionError(conn);
}
else
{
if(epollEventsArray[n].events & EPOLLIN)
{
// debug_print("Ready for read");
handleNodeConnectionRead(conn);
}
if(epollEventsArray[n].events & EPOLLOUT)
{
// debug_print("Ready for write");
handleNodeConnectionWrite(conn);
}
}
}
}
}
}
void acceptConnections(int listenerSocketFD)
{
int r = 0;
int maxAcceptances = 100;
uint64_t u = 1;
while(maxAcceptances--)
{
struct sockaddr incomingAddress;
socklen_t incomingAddressLength;
int incomingFD;
incomingAddressLength = sizeof incomingAddress;
incomingFD = accept(listenerSocketFD, &incomingAddress, &incomingAddressLength);
if (incomingFD == -1)
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
{
// We have processed all incoming connections
//debug_print("No more conns");
break;
}
else
{
perror ("Error accepting connections");
break;
}
}
r = makeSocketNonBlocking(incomingFD);
if (r == -1) { break; }
FSConnection * conn = malloc(sizeof(FSConnection));
conn->id = incrementConnectionNumber();
conn->fd = incomingFD;
conn->stateCode = FS_CONN_RECEIVING_COMMAND;
conn->type = FS_CONN_TYPE_NODE;
conn->offset = 0;
conn->length = 0;
conn->buffer = malloc(MAX_BUFFER_SIZE);
conn->workerNumber = conn->id % NUMBER_OF_WORKERS;
struct epoll_event eEvent;
eEvent.events = EPOLLIN | EPOLLOUT | EPOLLRDHUP | EPOLLHUP | EPOLLERR | EPOLLET;
eEvent.data.ptr = conn;
debug_print("Accepted connection %lu %d with buffer %p", conn->id, conn->fd, conn->buffer);
// printf("Accepted connection %lu\n", conn->id);
r = epoll_ctl(epollFD, EPOLL_CTL_ADD, incomingFD, &eEvent);
if(r == -1)
{
debug_print("Worker epoll error");
continue;
}
}
if(maxAcceptances == 0)
{
write(eventFD, &u, sizeof(uint64_t));
}
}