Ignore:
Timestamp:
Mar 4, 2021, 7:40:25 PM (5 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:
77d601f
Parents:
342af53 (diff), a5040fe (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/worker.cfa

    r342af53 r8e4aa05  
    66#include <unistd.h>
    77
     8#include <fstream.hfa>
    89#include <iofwd.hfa>
    910
     
    1617//=============================================================================================
    1718void ?{}( Worker & this ) {
    18         ((thread&)this){ "Server Worker Thread", *options.clopts.instance };
     19        size_t cli = rand() % options.clopts.cltr_cnt;
     20        ((thread&)this){ "Server Worker Thread", *options.clopts.instance[cli] };
     21        options.clopts.thrd_cnt[cli]++;
    1922        this.pipe[0] = -1;
    2023        this.pipe[1] = -1;
     
    3336        CONNECTION:
    3437        for() {
    35                 if( options.log ) printf("=== Accepting connection ===\n");
    36                 int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], 0, -1`s, &this.cancel, 0p );
    37                 // int fd = accept4( this.[sockfd, addr, addrlen, flags] );
     38                if( options.log ) sout | "=== Accepting connection ===";
     39                int fd = cfa_accept4( this.[sockfd, addr, addrlen, flags], CFA_IO_LAZY );
    3840                if(fd < 0) {
    3941                        if( errno == ECONNABORTED ) break;
    40                         if( errno == EINVAL && this.done ) break;
     42                        if( this.done && (errno == EINVAL || errno == EBADF) ) break;
    4143                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
    4244                }
     45                if(this.done) break;
    4346
    44                 if( options.log ) printf("=== New connection %d, waiting for requests ===\n", fd);
     47                if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
    4548                REQUEST:
    4649                for() {
     
    5356                        size_t len = options.socket.buflen;
    5457                        char buffer[len];
    55                         if( options.log ) printf("=== Reading request ===\n");
    56                         [code, closed, file, name_size] = http_read(fd, buffer, len, &this.cancel);
     58                        if( options.log ) sout | "=== Reading request ===";
     59                        [code, closed, file, name_size] = http_read(fd, buffer, len);
    5760
    5861                        // if we are done, break out of the loop
    59                         if( closed ) {
    60                                 if( options.log ) printf("=== Connection closed ===\n");
    61                                 close(fd);
    62                                 continue CONNECTION;
    63                         }
     62                        if( closed ) break REQUEST;
    6463
    6564                        // If this wasn't a request retrun 400
    6665                        if( code != OK200 ) {
    67                                 printf("=== Invalid Request : %d ===\n", code_val(code));
     66                                sout | "=== Invalid Request :" | code_val(code) | "===";
    6867                                answer_error(fd, code);
    6968                                continue REQUEST;
     
    7170
    7271                        if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) {
    73                                 if( options.log ) printf("=== Request for /plaintext ===\n");
     72                                if( options.log ) sout | "=== Request for /plaintext ===";
    7473
    75                                 char text[] = "Hello, World!\n";
     74                                int ret = answer_plaintext(fd);
     75                                if( ret == -ECONNRESET ) break REQUEST;
    7676
    77                                 // Send the header
    78                                 answer_plain(fd, text, sizeof(text));
    79 
    80                                 if( options.log ) printf("=== Answer sent ===\n");
     77                                if( options.log ) sout | "=== Answer sent ===";
    8178                                continue REQUEST;
    8279                        }
    8380
    8481                        if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) {
    85                                 if( options.log ) printf("=== Request for /ping ===\n");
     82                                if( options.log ) sout | "=== Request for /ping ===";
    8683
    8784                                // Send the header
    88                                 answer_empty(fd);
     85                                int ret = answer_empty(fd);
     86                                if( ret == -ECONNRESET ) break REQUEST;
    8987
    90                                 if( options.log ) printf("=== Answer sent ===\n");
     88                                if( options.log ) sout | "=== Answer sent ===";
    9189                                continue REQUEST;
    9290                        }
    9391
    94                         if( options.log ) printf("=== Request for file %.*s ===\n", (int)name_size, file);
     92                        if( options.log ) {
     93                                sout | "=== Request for file " | nonl;
     94                                write(sout, file, name_size);
     95                                sout | " ===";
     96                        }
     97
     98                        if( !options.file_cache.path ) {
     99                                if( options.log ) {
     100                                        sout | "=== File Not Found (" | nonl;
     101                                        write(sout, file, name_size);
     102                                        sout | ") ===";
     103                                }
     104                                answer_error(fd, E405);
     105                                continue REQUEST;
     106                        }
    95107
    96108                        // Get the fd from the file cache
     
    101113                        // If we can't find the file, return 404
    102114                        if( ans_fd < 0 ) {
    103                                 printf("=== File Not Found ===\n");
     115                                if( options.log ) {
     116                                        sout | "=== File Not Found (" | nonl;
     117                                        write(sout, file, name_size);
     118                                        sout | ") ===";
     119                                }
    104120                                answer_error(fd, E404);
    105121                                continue REQUEST;
     
    107123
    108124                        // Send the header
    109                         answer_header(fd, count);
     125                        int ret = answer_header(fd, count);
     126                        if( ret == -ECONNRESET ) break REQUEST;
    110127
    111128                        // Send the desired file
    112                         sendfile( this.pipe, fd, ans_fd, count);
     129                        ret = sendfile( this.pipe, fd, ans_fd, count);
     130                        if( ret == -ECONNRESET ) break REQUEST;
    113131
    114                         if( options.log ) printf("=== Answer sent ===\n");
     132                        if( options.log ) sout | "=== Answer sent ===";
    115133                }
     134
     135                if( options.log ) sout | "=== Connection closed ===";
     136                close(fd);
     137                continue CONNECTION;
    116138        }
    117139}
Note: See TracChangeset for help on using the changeset viewer.