WebServer

High-performance multi-threaded tcp network server in c++11

View on GitHub

High-performance multi-threaded tcp network server

Introduction

此项目借鉴《muduo网络库》思想,实现了一个网络库轮子web服务器,语言为c++11,并发模型使用Reactor+非阻塞IO+主线程和工作线程的事件循环,思想遵循one loop per thread。可处理静态资源,解析了get、HTTPhead请求,支持HTTP连接,支持管线化请求,并用webBench进行压测。

Environment

- OS:CentOS 7
- complier:g++4.8

Build

./build.sh

Start server

./WebServer [-t thread_numbers] [-p port]

Example main.cpp

    #include "EventLoop.h"
    #include "Server.h"
    #include <getopt.h>
    #include <string>

    int main(int argc, char *argv[])
    {
        int threadNum = 4;
        int port = 8888;
        std::string logPath = "./WebServer.log";

        int opt;
        const char *str = "t:p:";
        while((opt = getopt(argc, argv, str)) != -1)
        {
            switch(opt)
            {
                case 't':
                {
                    threadNum = atoi(optarg);
                    break;
                }
                case 'p':
                {
                    port = atoi(optarg);
                    break;
                }
                default: break;
            }
        }

        #ifndef _PTHREADS
            //LOG << "_PTHREADS is not defined !";
        #endif
        EventLoop mainLoop;
        Server myHTTPServer(&mainLoop, threadNum, port);
        myHTTPServer.start();
        mainLoop.loop();
        return 0;
    }

Technical

WebServer demo

在这里插入图片描述

Code statistics

在这里插入图片描述

Project imagines

Show more details in CSDN my blog