1 | #include "worker.hfa" |
---|
2 | |
---|
3 | #include <errno.h> |
---|
4 | #include <stdio.h> |
---|
5 | #include <string.h> |
---|
6 | #include <unistd.h> |
---|
7 | |
---|
8 | #include <fstream.hfa> |
---|
9 | #include <iofwd.hfa> |
---|
10 | #include <mutex_stmt.hfa> |
---|
11 | |
---|
12 | #include "options.hfa" |
---|
13 | #include "protocol.hfa" |
---|
14 | #include "filecache.hfa" |
---|
15 | |
---|
16 | //============================================================================================= |
---|
17 | // Generic connection handling |
---|
18 | //============================================================================================= |
---|
19 | static void handle_connection( connection & this, volatile int & fd, char * buffer, size_t len, io_future_t * f, unsigned long long & last ) { |
---|
20 | REQUEST: |
---|
21 | for() { |
---|
22 | bool closed; |
---|
23 | HttpCode code; |
---|
24 | const char * file; |
---|
25 | size_t name_size; |
---|
26 | |
---|
27 | // Read the http request |
---|
28 | if( options.log ) sout | "=== Reading request ==="; |
---|
29 | [code, closed, file, name_size] = http_read(fd, buffer, len, f); |
---|
30 | f = 0p; |
---|
31 | |
---|
32 | // if we are done, break out of the loop |
---|
33 | if( closed ) break REQUEST; |
---|
34 | |
---|
35 | // If this wasn't a request retrun 400 |
---|
36 | if( code != OK200 ) { |
---|
37 | sout | "=== Invalid Request :" | code_val(code) | "==="; |
---|
38 | answer_error(fd, code); |
---|
39 | continue REQUEST; |
---|
40 | } |
---|
41 | |
---|
42 | if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) { |
---|
43 | if( options.log ) sout | "=== Request for /plaintext ==="; |
---|
44 | |
---|
45 | int ret = answer_plaintext(fd); |
---|
46 | if( ret == -ECONNRESET ) break REQUEST; |
---|
47 | |
---|
48 | if( options.log ) sout | "=== Answer sent ==="; |
---|
49 | continue REQUEST; |
---|
50 | } |
---|
51 | |
---|
52 | if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) { |
---|
53 | if( options.log ) sout | "=== Request for /ping ==="; |
---|
54 | |
---|
55 | // Send the header |
---|
56 | int ret = answer_empty(fd); |
---|
57 | if( ret == -ECONNRESET ) break REQUEST; |
---|
58 | |
---|
59 | if( options.log ) sout | "=== Answer sent ==="; |
---|
60 | continue REQUEST; |
---|
61 | } |
---|
62 | |
---|
63 | if( options.log ) { |
---|
64 | sout | "=== Request for file " | nonl; |
---|
65 | write(sout, file, name_size); |
---|
66 | sout | " ==="; |
---|
67 | } |
---|
68 | |
---|
69 | if( !options.file_cache.path ) { |
---|
70 | if( options.log ) { |
---|
71 | sout | "=== File Not Found (" | nonl; |
---|
72 | write(sout, file, name_size); |
---|
73 | sout | ") ==="; |
---|
74 | } |
---|
75 | answer_error(fd, E405); |
---|
76 | continue REQUEST; |
---|
77 | } |
---|
78 | |
---|
79 | // Get the fd from the file cache |
---|
80 | int ans_fd; |
---|
81 | size_t count; |
---|
82 | [ans_fd, count] = get_file( file, name_size ); |
---|
83 | |
---|
84 | // If we can't find the file, return 404 |
---|
85 | if( ans_fd < 0 ) { |
---|
86 | if( options.log ) { |
---|
87 | sout | "=== File Not Found (" | nonl; |
---|
88 | write(sout, file, name_size); |
---|
89 | sout | ") ==="; |
---|
90 | } |
---|
91 | answer_error(fd, E404); |
---|
92 | continue REQUEST; |
---|
93 | } |
---|
94 | |
---|
95 | // Send the desired file |
---|
96 | int ret = answer_sendfile( this.pipe, fd, ans_fd, count, this.stats.sendfile ); |
---|
97 | if( ret == -ECONNRESET ) break REQUEST; |
---|
98 | |
---|
99 | if( options.log ) sout | "=== Answer sent ==="; |
---|
100 | } |
---|
101 | |
---|
102 | if (stats_thrd) { |
---|
103 | unsigned long long next = rdtscl(); |
---|
104 | if(next > (last + 500000000)) { |
---|
105 | if(try_lock(stats_thrd->stats.lock)) { |
---|
106 | push(this.stats.sendfile, stats_thrd->stats.send); |
---|
107 | unlock(stats_thrd->stats.lock); |
---|
108 | last = next; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | //============================================================================================= |
---|
115 | // Self Accepting Worker Thread |
---|
116 | //============================================================================================= |
---|
117 | void ?{}( AcceptWorker & this ) { |
---|
118 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 }; |
---|
119 | options.clopts.thrd_cnt++; |
---|
120 | this.done = false; |
---|
121 | } |
---|
122 | |
---|
123 | void main( AcceptWorker & this ) { |
---|
124 | park(); |
---|
125 | unsigned long long last = rdtscl(); |
---|
126 | /* paranoid */ assert( this.conn.pipe[0] != -1 ); |
---|
127 | /* paranoid */ assert( this.conn.pipe[1] != -1 ); |
---|
128 | for() { |
---|
129 | if( options.log ) sout | "=== Accepting connection ==="; |
---|
130 | int fd = cfa_accept4( this.sockfd, this.[addr, addrlen, flags], CFA_IO_LAZY ); |
---|
131 | if(fd < 0) { |
---|
132 | if( errno == ECONNABORTED ) break; |
---|
133 | if( this.done && (errno == EINVAL || errno == EBADF) ) break; |
---|
134 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
135 | } |
---|
136 | if(this.done) break; |
---|
137 | |
---|
138 | if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ==="; |
---|
139 | size_t len = options.socket.buflen; |
---|
140 | char buffer[len]; |
---|
141 | handle_connection( this.conn, fd, buffer, len, 0p, last ); |
---|
142 | |
---|
143 | if( options.log ) sout | "=== Connection closed ==="; |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | |
---|
148 | //============================================================================================= |
---|
149 | // Channel Worker Thread |
---|
150 | //============================================================================================= |
---|
151 | void ?{}( ChannelWorker & this ) { |
---|
152 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 }; |
---|
153 | options.clopts.thrd_cnt++; |
---|
154 | this.done = false; |
---|
155 | } |
---|
156 | |
---|
157 | void main( ChannelWorker & this ) { |
---|
158 | park(); |
---|
159 | unsigned long long last = rdtscl(); |
---|
160 | /* paranoid */ assert( this.conn.pipe[0] != -1 ); |
---|
161 | /* paranoid */ assert( this.conn.pipe[1] != -1 ); |
---|
162 | for() { |
---|
163 | size_t len = options.socket.buflen; |
---|
164 | char buffer[len]; |
---|
165 | PendingRead p; |
---|
166 | p.in.buf = (void*)buffer; |
---|
167 | p.in.len = len; |
---|
168 | push(*this.queue, &p); |
---|
169 | |
---|
170 | if( options.log ) sout | "=== Waiting new connection ==="; |
---|
171 | handle_connection( this.conn, p.out.fd, buffer, len, &p.f, last ); |
---|
172 | |
---|
173 | if( options.log ) sout | "=== Connection closed ==="; |
---|
174 | if(this.done) break; |
---|
175 | } |
---|
176 | } |
---|
177 | |
---|
178 | extern "C" { |
---|
179 | extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags); |
---|
180 | } |
---|
181 | |
---|
182 | void ?{}( Acceptor & this ) { |
---|
183 | ((thread&)this){ "Server Worker Thread", *options.clopts.instance, 64000 }; |
---|
184 | options.clopts.thrd_cnt++; |
---|
185 | this.done = false; |
---|
186 | } |
---|
187 | |
---|
188 | void main( Acceptor & this ) { |
---|
189 | park(); |
---|
190 | unsigned long long last = rdtscl(); |
---|
191 | if( options.log ) sout | "=== Accepting connection ==="; |
---|
192 | for() { |
---|
193 | int fd = accept4(this.sockfd, this.[addr, addrlen, flags]); |
---|
194 | if(fd < 0) { |
---|
195 | if( errno == EWOULDBLOCK) { |
---|
196 | this.stats.eagains++; |
---|
197 | yield(); |
---|
198 | continue; |
---|
199 | } |
---|
200 | if( errno == ECONNABORTED ) break; |
---|
201 | if( this.done && (errno == EINVAL || errno == EBADF) ) break; |
---|
202 | abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
203 | } |
---|
204 | this.stats.accepts++; |
---|
205 | |
---|
206 | if(this.done) return; |
---|
207 | |
---|
208 | if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ==="; |
---|
209 | |
---|
210 | if(fd) { |
---|
211 | PendingRead * p = 0p; |
---|
212 | for() { |
---|
213 | if(this.done) return; |
---|
214 | p = pop(*this.queue); |
---|
215 | if(p) break; |
---|
216 | yield(); |
---|
217 | this.stats.creates++; |
---|
218 | }; |
---|
219 | |
---|
220 | p->out.fd = fd; |
---|
221 | async_recv(p->f, p->out.fd, p->in.buf, p->in.len, 0, CFA_IO_LAZY); |
---|
222 | } |
---|
223 | |
---|
224 | if (stats_thrd) { |
---|
225 | unsigned long long next = rdtscl(); |
---|
226 | if(next > (last + 500000000)) { |
---|
227 | if(try_lock(stats_thrd->stats.lock)) { |
---|
228 | push(this.stats, stats_thrd->stats.accpt); |
---|
229 | unlock(stats_thrd->stats.lock); |
---|
230 | last = next; |
---|
231 | } |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | if( options.log ) sout | "=== Accepting connection ==="; |
---|
236 | } |
---|
237 | } |
---|