multithreaded_restful_server.c 1.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2015 Cesanta Software Limited
// All rights reserved

// This example shows how to handle long, blocking requests by
// handing off computation to different threads. Here, each
// request spawns a new thread. In a production scenario, a thread
// pools can be used for efficiency, if required.
// Long computation is simulated by sleeping for a random interval.

#include "mongoose.h"

static const char *s_http_port = "8000";

static void ev_handler(struct mg_connection *c, int ev, void *p) {
15
  if (ev == MG_EV_HTTP_REQUEST) {
16 17 18 19 20 21 22
    struct http_message *hm = (struct http_message *) p;
    char reply[100];

    /* Simulate long calculation */
    sleep(3);

    /* Send the reply */
rojer's avatar
rojer committed
23 24 25 26
    snprintf(reply, sizeof(reply), "{ \"uri\": \"%.*s\" }\n", (int) hm->uri.len,
             hm->uri.p);
    mg_printf(c,
              "HTTP/1.1 200 OK\r\n"
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
              "Content-Type: application/json\r\n"
              "Content-Length: %d\r\n"
              "\r\n"
              "%s",
              (int) strlen(reply), reply);
  }
}

int main(void) {
  struct mg_mgr mgr;
  struct mg_connection *nc;

  mg_mgr_init(&mgr, NULL);
  nc = mg_bind(&mgr, s_http_port, ev_handler);
  mg_set_protocol_http_websocket(nc);

  /* For each new connection, execute ev_handler in a separate thread */
  mg_enable_multithreading(nc);

  printf("Starting multi-threaded server on port %s\n", s_http_port);
  for (;;) {
    mg_mgr_poll(&mgr, 3000);
  }
  mg_mgr_free(&mgr);

  return 0;
}