mongoose analytical sample code frame (a)

mongoose analytical sample code frame (a)


reference:

  1. Mongoose Networking Library Documentation(Server)
  2. Mongoose Networking Library Documentation(Client)

Preface:

OK, thanks finished benefactor, take a look at today's text it ~ By the way, yesterday after a lapse of nine years to write again encountered the blog top half love it, give yourself some time to stimulate. Here also put a link, yes, I am very happy about this skin! A preliminary understanding of the compilation process under mac C source code of the blog -CSDN blog _qq_31433709

I have to say, a name really is a science, as well as the name of the method parameter names of these two source files are perfect glance!

All in all, I C environment on the vim still not get it, the process of this code line and not enough feeling really happy! At night the way to get it! If you do not find (although this is quite unrealistic), it is the way to learn how to write plug it = =!


text

A server-side:

Source File: simplest_web_server.c

// Copyright (c) 2015 Cesanta Software Limited
// All rights reserved

#include "mongoose.h"

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

static void ev_handler(struct mg_connection *nc, int ev, void *p) {
  if (ev == MG_EV_HTTP_REQUEST) {
    mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
  }
}

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

  mg_mgr_init(&mgr, NULL);
  printf("Starting web server on port %s\n", s_http_port);
  nc = mg_bind(&mgr, s_http_port, ev_handler);
  if (nc == NULL) {
    printf("Failed to create listener\n");
    return 1;
  }

  // Set up HTTP server parameters
  mg_set_protocol_http_websocket(nc);
  s_http_server_opts.document_root = ".";  // Serve current directory
  s_http_server_opts.enable_directory_listing = "yes";

  for (;;) {
    mg_mgr_poll(&mgr, 1000);
  }
  mg_mgr_free(&mgr);

  return 0;
}

(1) preliminary first glance:

Hum, a lot has been closed into the frame of method calls, constants and structures:

  1. Structure:
  • mg_mgr
  • mg_connection
  • mg_serve_http_opts
  1. Method call:
  • mg_mgr_init
  • mg_bind
  • mg_serve_http
  • mg_set_protocol_http_websocket
  • mg_mgr_pool
  • mg_mgr_free
  1. constant:
  • MG_EV_HTTP_REQUEST

Import only one header file, go there to look chanting = =, wow, 6277 OK, ok, kids, do you have questions?
Header files can be acquired only I feel the most constant and define the implementation and structure of the method, I still feel from .c files to obtain.

(2) it would look at the spectacle file

vim global search, the result MG_EV_HTTP_REQUEST to find definitions on the line, the rest of the Well, had wanted to be listed, but for the length of simplicity, or omitted better, personal feeling (probably aiming a bit ~), and then I found that both structure or method call or is constant, there are very specific explanation in the header file inside, and even sample code, it is proposed that direct their own search mongoose.h file and parameters mg_mgr_pool for 1000, or the need to look .c file the specific implementation

Then this is the reason I generally results in a circle, after all, was the first to write content in this area, is the difference of points, but after all, is for self-improvement (self-comfort), and the total process from poor to good Well !

// Copyright (c) 2015 Cesanta Software Limited
// All rights reserved

//导入mongoose所依赖的必须头文件
#include "mongoose.h"

//指定默认的服务器http端口号
static const char *s_http_port = "8000";
//指定http链接的各种选项
static struct mg_serve_http_opts s_http_server_opts;

//ev_handler指定的event handler,即事件处理,mg指代的是mongoose
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
  if (ev == MG_EV_HTTP_REQUEST) {
    mg_serve_http(nc, (struct http_message *) p, s_http_server_opts);
  }
}

//总入口,一切的开始
int main(void) {
  struct mg_mgr mgr;
  struct mg_connection *nc;

  //初始化mg_mar变量
  mg_mgr_init(&mgr, NULL);
  printf("Starting web server on port %s\n", s_http_port);
  //这里有点函数式的感觉,传入的不是对象,而是方法指针
  //将mgr和其中的mgr_connection以及对应的端口号还有对应的不同event的处理器所绑定,并返回对应的绑定后的mg_connection对象
  nc = mg_bind(&mgr, s_http_port, ev_handler);

  if (nc == NULL) {
    printf("Failed to create listener\n");
    return 1;
  }

  // Set up HTTP server parameters
  mg_set_protocol_http_websocket(nc);
  //设置客户端访问服务器的初始路径,这里设置为"."即为可执行文件的当前路径
  s_http_server_opts.document_root = ".";  // Serve current directory
  //同时设置是否支持显示当前目录下的文件信息
  s_http_server_opts.enable_directory_listing = "yes";

  for (;;) {
    //这里的1000是什么意思呢?
    //翻译一下文档中对这个方法的描述:
    //这个方法执行实际上的IO操作并且必须在一个循环中被调用(一个事件循环)。
    //它返回了生成的用户事件数目(除了POLL)。
    //第二个参数是以毫秒为单位的睡眠最大时间。
    //该方法为IO操作检查了所有的连接(connection)。如果至少有一个连接已经准备好IO操作,此方法将切换到对应的事件处理器然后返回。
    mg_mgr_poll(&mgr, 1000);
    //所以说,1000到底是什么意思?看来光看头文件比较难啊。
    //ok,大概看了看实现,1000是设置的请求超时时间
  }
  //解构mg_mgr,该方法中应该同时解构了mg_connection,并释放了对应占用的内存,道理的确是这样,但是什么情况下会跳出这个死循环呢?如果我直接通过信号,貌似不会走这步,并没有看见输出stopped的信息
  mg_mgr_free(&mgr);
    printf("stopped mongoose server");
  return 0;
}


summary:

OK, almost feeling today to see this program, but also appropriate to see some details, the process server is basically the reason in a circle. Zaoshuizaoqi good ~

Published 306 original articles · won praise 26 · views 120 000 +

Guess you like

Origin blog.csdn.net/qq_31433709/article/details/105352282