Ignore:
Timestamp:
Mar 2, 2021, 5:28:32 PM (8 months ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
arm-eh, jacob/cs343-translation, master, new-ast-unique-expr
Children:
6083392
Parents:
182256b (diff), 9eb7a53 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • benchmark/io/http/protocol.cfa

    r182256b r266ecf1  
    2020#include "options.hfa"
    2121
    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 };
     22#define PLAINTEXT_1WRITE
     23#define PLAINTEXT_NOCOPY
     24
     25struct https_msg_str {
     26        char msg[512];
     27        size_t len;
     28};
     29
     30const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
    3331
    3432_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
    3533
    36 const int http_codes[] = {
     34const int http_codes[KNOWN_CODES] = {
     35        200,
    3736        200,
    3837        400,
     
    5352        while(len > 0) {
    5453                // Call write
    55                 int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p);
    56                 // int ret = write(fd, it, len);
     54                int ret = cfa_send(fd, it, len, 0, CFA_IO_LAZY);
    5755                if( ret < 0 ) {
    5856                        if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
     
    7270        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
    7371        int idx = (int)code;
    74         return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) );
     72        return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
    7573}
    7674
    7775int answer_header( int fd, size_t size ) {
    78         const char * fmt = http_msgs[OK200];
    79         int len = 200;
    80         char buffer[len];
    81         len = snprintf(buffer, len, fmt, date, 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);
    8282        return answer( fd, buffer, len );
    8383}
    8484
    85 int answer_plain( int fd, char buffer[], size_t size ) {
    86         int ret = answer_header(fd, size);
     85#if defined(PLAINTEXT_NOCOPY)
     86int 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)
     90int 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
     104int answer_plaintext( int fd ) {
     105        char text[] = "Hello, World!\n";
     106        int ret = answer_header(fd, sizeof(text));
    87107        if( ret < 0 ) return ret;
    88         return answer(fd, buffer, size);
    89 }
     108        return answer(fd, text, sizeof(text));
     109}
     110#endif
    90111
    91112int answer_empty( int fd ) {
     
    94115
    95116
    96 [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) {
     117[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
    97118        char * it = buffer;
    98119        size_t count = len - 1;
     
    100121        READ:
    101122        for() {
    102                 int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p);
     123                int ret = cfa_recv(fd, (void*)it, count, 0, CFA_IO_LAZY);
    103124                // int ret = read(fd, (void*)it, count);
    104125                if(ret == 0 ) return [OK200, true, 0, 0];
     
    139160        ssize_t ret;
    140161        SPLICE1: while(count > 0) {
    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);
     162                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, CFA_IO_LAZY);
    143163                if( ret < 0 ) {
    144164                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
     
    152172                size_t in_pipe = ret;
    153173                SPLICE2: while(in_pipe > 0) {
    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);
     174                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, CFA_IO_LAZY);
    156175                        if( ret < 0 ) {
    157176                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
     
    173192#include <thread.hfa>
    174193
     194const 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
    175205struct date_buffer {
    176         char buff[100];
     206        https_msg_str strs[KNOWN_CODES];
    177207};
    178208
     
    183213
    184214void ?{}( DateFormater & this ) {
    185         ((thread&)this){ "Server Date Thread", *options.clopts.instance };
     215        ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
    186216        this.idx = 0;
    187         memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) );
    188         memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) );
     217        memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
     218        memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
    189219}
    190220
     
    196226                or else {}
    197227
     228
     229                char buff[100];
    198230                Time now = getTimeNsec();
    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);
     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                }
    204243                this.idx = (this.idx + 1) % 2;
     244
     245                sout | "Date thread sleeping";
    205246
    206247                sleep(1`s);
Note: See TracChangeset for help on using the changeset viewer.