2017-09-13 6 views
1

"examples"에서 "restful_server"예제를 실행하려고했습니다. ev_handler 함수는 모든 요청을 하나씩 처리하므로 요청은 이전 요청이 완료 될 때까지 대기해야합니다.몽구스가 다른 요청을 차단하고 하나씩 처리합니다.

/* 
* Copyright (c) 2014 Cesanta Software Limited 
* All rights reserved 
*/ 

#include "mongoose.h" 
#include <unistd.h> 

static const char *s_http_port = "8004"; 
static struct mg_serve_http_opts s_http_server_opts; 

static void handle_sum_call(struct mg_connection *nc, struct http_message *hm) { 
    char n1[100], n2[100]; 
    double result; 

    /* Get form variables */ 
    mg_get_http_var(&hm->body, "n1", n1, sizeof(n1)); 
    mg_get_http_var(&hm->body, "n2", n2, sizeof(n2)); 

    int n2s = (int) strtod(n2, NULL); 
    if(n2s == 100){ 
     while(n2s == 100){ 
      n2s = 100; 
     } 
    } 

    /* Send headers */ 
    mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); 

    /* Compute the result and send it back as a JSON object */ 
    result = strtod(n1, NULL) + strtod(n2, NULL); 
    mg_printf_http_chunk(nc, "{ \"result\": %lf }", result); 
    mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */ 
} 

static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { 
    struct http_message *hm = (struct http_message *) ev_data; 

    switch (ev) { 
    case MG_EV_HTTP_REQUEST: 
     if (mg_vcmp(&hm->uri, "/api/v1/sum") == 0) { 
     handle_sum_call(nc, hm); /* Handle RESTful call */ 
     } else if (mg_vcmp(&hm->uri, "/printcontent") == 0) { 
     char buf[100] = {0}; 
     memcpy(buf, hm->body.p, 
       sizeof(buf) - 1 < hm->body.len ? sizeof(buf) - 1 : hm->body.len); 
     printf("%s\n", buf); 
     } else { 
     mg_serve_http(nc, hm, s_http_server_opts); /* Serve static content */ 
     } 
     break; 
    default: 
     break; 
    } 
} 

int main(int argc, char *argv[]) { 
    struct mg_mgr mgr; 
    struct mg_connection *nc; 
    struct mg_bind_opts bind_opts; 
    int i; 
    char *cp; 
    const char *err_str; 
#if MG_ENABLE_SSL 
    const char *ssl_cert = NULL; 
#endif 

    mg_mgr_init(&mgr, NULL); 

    /* Use current binary directory as document root */ 
    if (argc > 0 && ((cp = strrchr(argv[0], DIRSEP)) != NULL)) { 
    *cp = '\0'; 
    s_http_server_opts.document_root = argv[0]; 
    } 

    /* Process command line options to customize HTTP server */ 
    for (i = 1; i < argc; i++) { 
    if (strcmp(argv[i], "-D") == 0 && i + 1 < argc) { 
     mgr.hexdump_file = argv[++i]; 
    } else if (strcmp(argv[i], "-d") == 0 && i + 1 < argc) { 
     s_http_server_opts.document_root = argv[++i]; 
    } else if (strcmp(argv[i], "-p") == 0 && i + 1 < argc) { 
     s_http_port = argv[++i]; 
    } else if (strcmp(argv[i], "-a") == 0 && i + 1 < argc) { 
     s_http_server_opts.auth_domain = argv[++i]; 
#if MG_ENABLE_JAVASCRIPT 
    } else if (strcmp(argv[i], "-j") == 0 && i + 1 < argc) { 
     const char *init_file = argv[++i]; 
     mg_enable_javascript(&mgr, v7_create(), init_file); 
#endif 
    } else if (strcmp(argv[i], "-P") == 0 && i + 1 < argc) { 
     s_http_server_opts.global_auth_file = argv[++i]; 
    } else if (strcmp(argv[i], "-A") == 0 && i + 1 < argc) { 
     s_http_server_opts.per_directory_auth_file = argv[++i]; 
    } else if (strcmp(argv[i], "-r") == 0 && i + 1 < argc) { 
     s_http_server_opts.url_rewrites = argv[++i]; 
#if MG_ENABLE_HTTP_CGI 
    } else if (strcmp(argv[i], "-i") == 0 && i + 1 < argc) { 
     s_http_server_opts.cgi_interpreter = argv[++i]; 
#endif 
#if MG_ENABLE_SSL 
    } else if (strcmp(argv[i], "-s") == 0 && i + 1 < argc) { 
     ssl_cert = argv[++i]; 
#endif 
    } else { 
     fprintf(stderr, "Unknown option: [%s]\n", argv[i]); 
     exit(1); 
    } 
    } 

    /* Set HTTP server options */ 
    memset(&bind_opts, 0, sizeof(bind_opts)); 
    bind_opts.error_string = &err_str; 
#if MG_ENABLE_SSL 
    if (ssl_cert != NULL) { 
    bind_opts.ssl_cert = ssl_cert; 
    } 
#endif 
    nc = mg_bind_opt(&mgr, s_http_port, ev_handler, bind_opts); 
    if (nc == NULL) { 
    fprintf(stderr, "Error starting server on port %s: %s\n", s_http_port, 
      *bind_opts.error_string); 
    exit(1); 
    } 

    mg_set_protocol_http_websocket(nc); 
    s_http_server_opts.enable_directory_listing = "yes"; 

    printf("Starting RESTful server on port %s, serving %s\n", s_http_port, 
     s_http_server_opts.document_root); 
    for (;;) { 
    mg_mgr_poll(&mgr, 1000); 
    } 
    mg_mgr_free(&mgr); 

    return 0; 
} 

또한 sleep_function을 handle_sum_call에 추가하여이 문제를 해결했습니다.

static void handle_sum_call(struct mg_connection *nc, struct http_message *hm) { 
    char n1[100], n2[100]; 
    double result; 

    /* Get form variables */ 
    mg_get_http_var(&hm->body, "n1", n1, sizeof(n1)); 
    mg_get_http_var(&hm->body, "n2", n2, sizeof(n2)); 

    int n2s = (int) strtod(n2, NULL); 
    if(n2s == 100){ 
     sleep(6000); 
    } 

    /* Send headers */ 
    mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); 

    /* Compute the result and send it back as a JSON object */ 
    result = strtod(n1, NULL) + strtod(n2, NULL); 
    mg_printf_http_chunk(nc, "{ \"result\": %lf }", result); 
    mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */ 
} 

나는 10 ~ 20 사용자 (너무 많이)에 의해 사용되는 간단한 웹 응용 프로그램을 만드는 몽구스를 사용할 계획했다. 그것은 비동기 및 비 차단이라고? 내가 뭔가 잘못하고 있는거야?

답변

0

NULL, ... 우리는 모듈

{ 를 초기화하는 후 (굵게 표시) 아래 문을 호출 할 필요가 멀티 스레딩을 기반으로 웹 서버

에 대한

ns_mgr_init (& MGR을 https://github.com/cesanta/fossa을 확인하시기 바랍니다);

nc = ns_bind (&mgr, s_http_port, ev_handler);

ns_set_protocol_http_websocket (nc);

ns_enable_multithreading (nc);

...}

그것은 포사가 다시 몽구스에 병합 말한다 컴파일 플래그

+0

의 정의 NS_ENABLE_THREADS을 가능하게하는 것을 잊지 마십시오. – Nayfe

+0

이 링크가 질문에 대답 할 수 있지만 여기에 답변의 핵심 부분을 포함시키고 참조 용 링크를 제공하는 것이 좋습니다. 연결된 페이지가 바뀌면 링크 전용 답변이 무효화 될 수 있습니다. –

+0

좋아, 대답을 편집했습니다. 지적 해 주셔서 고맙습니다. – Shunmuga