Ignore:
Timestamp:
Jan 14, 2021, 12:23:14 PM (4 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
8e4aa05
Parents:
4468a70 (diff), ec19b21 (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

    r4468a70 r342af53  
    1818#include "options.hfa"
    1919
     20const char * volatile date = 0p;
     21
    2022const char * http_msgs[] = {
    21         "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: %zu\n\n",
    22         "HTTP/1.1 400 Bad Request\nContent-Type: text/plain\nContent-Length: 0\n\n",
    23         "HTTP/1.1 404 Not Found\nContent-Type: text/plain\nContent-Length: 0\n\n",
    24         "HTTP/1.1 413 Payload Too Large\nContent-Type: text/plain\nContent-Length: 0\n\n",
    25         "HTTP/1.1 414 URI Too Long\nContent-Type: text/plain\nContent-Length: 0\n\n",
     23        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: %zu \n\n",
     24        "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     25        "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     26        "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     27        "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    2628};
    2729
     
    4547        while(len > 0) {
    4648                // Call write
    47                 int ret = write(fd, it, len);
     49                int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p);
     50                // int ret = write(fd, it, len);
    4851                if( ret < 0 ) { if( errno != EAGAIN && errno != EWOULDBLOCK) abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) ); }
    4952
     
    6366int answer_header( int fd, size_t size ) {
    6467        const char * fmt = http_msgs[OK200];
    65         int len = 100;
     68        int len = 200;
    6669        char buffer[len];
    67         len = snprintf(buffer, len, fmt, size);
     70        len = snprintf(buffer, len, fmt, date, size);
    6871        return answer( fd, buffer, len );
    6972}
    7073
    71 [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
     74int answer_plain( int fd, char buffer[], size_t size ) {
     75        int ret = answer_header(fd, size);
     76        if( ret < 0 ) return ret;
     77        return answer(fd, buffer, size);
     78}
     79
     80int answer_empty( int fd ) {
     81        return answer_header(fd, 0);
     82}
     83
     84
     85[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) {
    7286        char * it = buffer;
    7387        size_t count = len - 1;
     
    7589        READ:
    7690        for() {
    77                 int ret = cfa_read(fd, (void*)it, count, 0, -1`s, 0p, 0p);
     91                int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p);
     92                // int ret = read(fd, (void*)it, count);
    7893                if(ret == 0 ) return [OK200, true, 0, 0];
    7994                if(ret < 0 ) {
    8095                        if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
     96                        // if( errno == EINVAL ) return [E400, true, 0, 0];
    8197                        abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
    8298                }
     
    92108        }
    93109
    94         printf("%.*s\n", rlen, buffer);
     110        if( options.log ) printf("%.*s\n", rlen, buffer);
    95111
    96112        it = buffer;
     
    104120
    105121void sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
     122        unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
    106123        off_t offset = 0;
    107124        ssize_t ret;
    108125        SPLICE1: while(count > 0) {
    109                 ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p);
     126                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, 0, -1`s, 0p, 0p);
     127                // ret = splice(ans_fd, &offset, pipe[1], 0p, count, sflags);
    110128                if( ret < 0 ) {
    111129                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
     
    117135                size_t in_pipe = ret;
    118136                SPLICE2: while(in_pipe > 0) {
    119                         ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, SPLICE_F_MOVE | SPLICE_F_MORE, 0, -1`s, 0p, 0p);
     137                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, 0, -1`s, 0p, 0p);
     138                        // ret = splice(pipe[0], 0p, fd, 0p, in_pipe, sflags);
    120139                        if( ret < 0 ) {
    121140                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
     
    127146        }
    128147}
     148
     149//=============================================================================================
     150
     151#include <clock.hfa>
     152#include <time.hfa>
     153#include <thread.hfa>
     154
     155struct date_buffer {
     156        char buff[100];
     157};
     158
     159thread DateFormater {
     160        int idx;
     161        date_buffer buffers[2];
     162};
     163
     164void ?{}( DateFormater & this ) {
     165        ((thread&)this){ "Server Date Thread", *options.clopts.instance };
     166        this.idx = 0;
     167        memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) );
     168        memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) );
     169}
     170
     171void main(DateFormater & this) {
     172        LOOP: for() {
     173                waitfor( ^?{} : this) {
     174                        break LOOP;
     175                }
     176                or else {}
     177
     178                Time now = getTimeNsec();
     179
     180                strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
     181
     182                char * next = this.buffers[this.idx].buff;
     183                __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST);
     184                this.idx = (this.idx + 1) % 2;
     185
     186                sleep(1`s);
     187        }
     188}
     189
     190//=============================================================================================
     191DateFormater * the_date_formatter;
     192
     193void init_protocol(void) {
     194        the_date_formatter = alloc();
     195        (*the_date_formatter){};
     196}
     197
     198void deinit_protocol(void) {
     199        ^(*the_date_formatter){};
     200        free( the_date_formatter );
     201}
Note: See TracChangeset for help on using the changeset viewer.