Erlang -- cowboy source code analysis

Source: Internet
Author: User

It's time to start writing this article.ArticleAnd stick to yesterday's promise.

Cowboy is an application that is a small, fast, and well-modularized HTTP server. Among them, the good clean module allows us to expand to a variety of network protocols. Cowboy comes with TCP and SSL, and some people provide extensions such as SMTP. In the next project, I plan to use cowboy as the framework to complete a server to provide services for the mobile Internet... The features of the mobile Internet have been covered in the previous chapter.

Features of cowboy:

1.CodeLess.
2. Fast.
3. High modularization. both transport and protocol can be easily replaced.
4. Use the binary syntax to implement the HTTP service, which is faster and smaller.
5. Easy to embed other applications.
6. A dispatcher can be embedded in FastCGI PHP or Ruby.
7. No process dictionary and the code is clean.

Cowboy_app.erl starts initialization, and then creates a cowboy_sup (supervisor) process, which uses cache for fast time computing. Then, there will be no more. This is all the processes started by cowboy. Code clean, less dependency... Take a look at the cowboy_example instance. The official developer provides an instance, and then I found an instance of the more complex application cowboy on the Internet: bigwig. Next, let's take a look at the official example.

After downloading cowboy_example,

Rebar get-deps compile.

After startup, create cowboy_example.erl (application) and run the following code:

Start (_ type, _ ARGs)->
Dispatch = [
{'_',[
{[<"Websocket">], websocket_handler, []},
{[<"Eventsource">], eventsource_handler, []},
{[<"Eventsource" >,< <"live">], eventsource_emitter, []},
{'_', Default_handler, []}
]}
],
Cowboy: start_listener (my_http_listener, 100,
Cowboy_tcp_transport, [{port, 8080}],
Cowboy_http_protocol, [{dispatch, dispatch}]
),
Cowboys: start_listener (my_https_listener, 100,
Cowboy_ssl_transport ,[
{Port, 8443}, {certfile, "priv/SSL/cert. pem "},
{Keyfile, "priv/SSL/key. pem"}, {password, "Cowboy"}],
Cowboy_http_protocol, [{dispatch, dispatch}]
),
Cowboy_examples_sup: start_link ().

We can see that there is a dispatcher, and then two listening processes are created below, and finally the invigilation process is started. In dispatcher, you can convert it to another processing method, such as PHP. In the final cowboy_examples_sup.erl, You can monitor the process services you need, such as maintaining the status. This refreshing module is really refreshing.

Next, refer to the transport and protocol of cowboy to process the network protocol you need, and finally create your own logic in Sup. This is done...

I will introduce the specific Protocol Module Based on my own practical experience.

I found a new cowboy source code analysis series written by a friend, which is very detailed. I hope you can continue to ask me if you have any questions. Thank you.

Http://www.cnblogs.com/yourihua/archive/2012/05/24/2515380.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.