Changes in benchmark/io/http/protocol.cfa [2cd784a:b57db73]
- File:
-
- 1 edited
-
benchmark/io/http/protocol.cfa (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
benchmark/io/http/protocol.cfa
r2cd784a rb57db73 20 20 #include "options.hfa" 21 21 22 #define PLAINTEXT_1WRITE 23 #define PLAINTEXT_NOCOPY 24 25 struct https_msg_str { 26 char msg[512]; 27 size_t len; 28 }; 29 30 const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 }; 22 const char * volatile date = 0p; 23 24 const char * http_msgs[] = { 25 "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: %zu \n\n", 26 "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", 27 "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", 28 "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", 29 "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", 30 "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", 31 "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n", 32 }; 31 33 32 34 _Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0]))); 33 35 34 const int http_codes[KNOWN_CODES] = { 35 200, 36 const int http_codes[] = { 36 37 200, 37 38 400, … … 52 53 while(len > 0) { 53 54 // Call write 54 int ret = cfa_send(fd, it, len, 0, CFA_IO_LAZY); 55 int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p); 56 // int ret = write(fd, it, len); 55 57 if( ret < 0 ) { 56 58 if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET; … … 70 72 /* paranoid */ assert( code < KNOWN_CODES && code != OK200 ); 71 73 int idx = (int)code; 72 return answer( fd, http_msgs[idx] ->msg, http_msgs[idx]->len);74 return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) ); 73 75 } 74 76 75 77 int answer_header( int fd, size_t size ) { 76 char buffer[512]; 77 char * it = buffer; 78 memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len); 79 it += http_msgs[OK200]->len; 80 int len = http_msgs[OK200]->len; 81 len += snprintf(it, 512 - len, "%d \n\n", size); 78 const char * fmt = http_msgs[OK200]; 79 int len = 200; 80 char buffer[len]; 81 len = snprintf(buffer, len, fmt, date, size); 82 82 return answer( fd, buffer, len ); 83 83 } 84 84 85 #if defined(PLAINTEXT_NOCOPY) 86 int answer_plaintext( int fd ) { 87 return answer(fd, http_msgs[OK200_PlainText]->msg, http_msgs[OK200_PlainText]->len + 1); // +1 cause snprintf doesn't count nullterminator 88 } 89 #elif defined(PLAINTEXT_1WRITE) 90 int answer_plaintext( int fd ) { 91 char text[] = "Hello, World!\n"; 92 char buffer[512 + sizeof(text)]; 93 char * it = buffer; 94 memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len); 95 it += http_msgs[OK200]->len; 96 int len = http_msgs[OK200]->len; 97 int r = snprintf(it, 512 - len, "%d \n\n", sizeof(text)); 98 it += r; 99 len += r; 100 memcpy(it, text, sizeof(text)); 101 return answer(fd, buffer, len + sizeof(text)); 102 } 103 #else 104 int answer_plaintext( int fd ) { 105 char text[] = "Hello, World!\n"; 106 int ret = answer_header(fd, sizeof(text)); 85 int answer_plain( int fd, char buffer[], size_t size ) { 86 int ret = answer_header(fd, size); 107 87 if( ret < 0 ) return ret; 108 return answer(fd, text, sizeof(text)); 109 } 110 #endif 88 return answer(fd, buffer, size); 89 } 111 90 112 91 int answer_empty( int fd ) { … … 115 94 116 95 117 [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len ) {96 [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) { 118 97 char * it = buffer; 119 98 size_t count = len - 1; … … 121 100 READ: 122 101 for() { 123 int ret = cfa_re cv(fd, (void*)it, count, 0, CFA_IO_LAZY);102 int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p); 124 103 // int ret = read(fd, (void*)it, count); 125 104 if(ret == 0 ) return [OK200, true, 0, 0]; … … 160 139 ssize_t ret; 161 140 SPLICE1: while(count > 0) { 162 ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, CFA_IO_LAZY); 141 ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, 0, -1`s, 0p, 0p); 142 // ret = splice(ans_fd, &offset, pipe[1], 0p, count, sflags); 163 143 if( ret < 0 ) { 164 144 if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1; … … 172 152 size_t in_pipe = ret; 173 153 SPLICE2: while(in_pipe > 0) { 174 ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, CFA_IO_LAZY); 154 ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, 0, -1`s, 0p, 0p); 155 // ret = splice(pipe[0], 0p, fd, 0p, in_pipe, sflags); 175 156 if( ret < 0 ) { 176 157 if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2; … … 192 173 #include <thread.hfa> 193 174 194 const char * original_http_msgs[] = {195 "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",196 "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 15\n\nHello, World!\n",197 "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",198 "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",199 "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",200 "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",201 "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",202 "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",203 };204 205 175 struct date_buffer { 206 https_msg_str strs[KNOWN_CODES];176 char buff[100]; 207 177 }; 208 178 … … 213 183 214 184 void ?{}( DateFormater & this ) { 215 ((thread&)this){ "Server Date Thread", *options.clopts.instance [0]};185 ((thread&)this){ "Server Date Thread", *options.clopts.instance }; 216 186 this.idx = 0; 217 memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );218 memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );187 memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) ); 188 memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) ); 219 189 } 220 190 … … 226 196 or else {} 227 197 228 229 char buff[100];230 198 Time now = getTimeNsec(); 231 strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now ); 232 sout | "Updated date to '" | buff | "'"; 233 234 for(i; KNOWN_CODES) { 235 size_t len = snprintf( this.buffers[this.idx].strs[i].msg, 512, original_http_msgs[i], buff ); 236 this.buffers[this.idx].strs[i].len = len; 237 } 238 239 for(i; KNOWN_CODES) { 240 https_msg_str * next = &this.buffers[this.idx].strs[i]; 241 __atomic_exchange_n((https_msg_str * volatile *)&http_msgs[i], next, __ATOMIC_SEQ_CST); 242 } 199 200 strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now ); 201 202 char * next = this.buffers[this.idx].buff; 203 __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST); 243 204 this.idx = (this.idx + 1) % 2; 244 245 sout | "Date thread sleeping";246 205 247 206 sleep(1`s);
Note:
See TracChangeset
for help on using the changeset viewer.