예제 #1
0
파일: worker.c 프로젝트: alemic/wheatserver
static void handleRequest(struct evcenter *center, int fd, void *data, int mask)
{
    struct client *client;
    struct conn *conn;
    ssize_t nread, ret;
    struct timeval start, end;
    long time_use;
    struct slice slice;
    size_t parsed;

    parsed = 0;
    ret = 0;
    client = data;

    gettimeofday(&start, NULL);
    nread = WorkerProcess->worker->recvData(client);
    if (!isClientValid(client)) {
        freeClient(client);
        return ;
    }

    if (msgGetSize(client->req_buf) > getStatVal(StatBufferSize)) {
        getStatVal(StatBufferSize) = msgGetSize(client->req_buf);
    }

    while (msgCanRead(client->req_buf)) {
        conn = connGet(client);

        msgRead(client->req_buf, &slice);
        ret = client->protocol->parser(conn, &slice, &parsed);

        if (ret == WHEAT_WRONG) {
            wheatLog(WHEAT_NOTICE, "parse data failed");
            msgSetReaded(client->req_buf, 0);
            setClientUnvalid(client);
            break;
        } else if (ret == WHEAT_OK) {
            msgSetReaded(client->req_buf, parsed);
            getStatVal(StatTotalRequest)++;
            client->pending = NULL;
            ret = client->protocol->spotAppAndCall(conn);
            if (ret != WHEAT_OK) {
                getStatVal(StatFailedRequest)++;
                client->should_close = 1;
                wheatLog(WHEAT_NOTICE, "app failed");
                break;
            }
        } else if (ret == 1) {
            client->pending = conn;
            msgSetReaded(client->req_buf, parsed);
            continue;
        }
    }
    tryFreeClient(client);
    gettimeofday(&end, NULL);
    time_use = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
    getStatVal(StatRunTime) += time_use;
}
예제 #2
0
int main(int argc, char** argv)
{
    (void)argc;
    (void)argv;

    if ( 0 )
    {
        char* test = strformat( "Hello, %s", argv[0] );
        printf( "%s\n", test );
        strfree( test );
    }

    if ( 1 )
    {
        disruptorKill( "benchmark" );
    }

    {
        int fd;
        bool child;

        if ( 1 )
            fd = fork();
        else
            fd = 0;
        child = (fd == 0);

        if ( 1 )
        {
            disruptorMsg m;
            disruptor* d = disruptorCreate( "benchmark", (child ? "clientB" : "clientA"), 16*1024 );

            disruptorPrintf( d, "hello, world!" );
            disruptorPrintf( d, "hello again, world!" );

            while ( (m = disruptorRecv( d )) )
            {
                int64_t time = msgGetTimestamp( d, m );
                int senderId = msgGetSenderId( d, m );
                const char* sender = msgGetSender( d, m );
                size_t size = msgGetSize( d, m );
                char* msg = msgGetData( d, m );
                printf( "received time=%lld sender=%s size=%d msg=%s\n",
                        time, sender, (int)size, msg );
            }

            disruptorRelease( d );
        }

        if ( !child )
        {
            int signal;
            wait( &signal );
        }
    }

    return 0;
}