[项目][WebServer][整体框架设计]详细讲解

发布于:2024-09-18 ⋅ 阅读:(135) ⋅ 点赞:(0)


0.框架 && 前言

  • 程序main()函数如下
    void Usage(std::string proc)
    {
        std::cout << "Usage:\n\t" << proc << " Port" << std::endl;
    }
    
    int main(int argc, char *argv[])
    {
        if(argc != 2)
        {
            Usage(argv[0]);
            exit(4);
        }
    
        std::unique_ptr<HttpServer> httpServer(new HttpServer(atoi(argv[1])));
        httpServer->Init();
        httpServer->Loop();
    
        return 0;
    }
    
  • 本项目将分模块设计,主要包含以下模块
  • 注意:本篇只放函数声明

1.TcpServer类

1.功能

  • 主要负责底层的TCP通信
  • 将其设计为单例模式,作为一个组件,置入HTTPServer中

2.类设计

static const uint16_t PORT = 8090;
static const int BACKLOG = 128;

// 单例 -- 饿汉模式
class TcpServer
{
public:
    static TcpServer* GetInstance(uint16_t port = PORT)
    {}

    void Init()
    {}

    void Socket()
    {}

    void Bind()
    {}

    void Listen()
    {}

    int Sock()
    {}

    ~TcpServer()
    {}
private:
    TcpServer(uint16_t port)
        : _port(port)
        , _listenSock(-1)
    {}

    TcpServer(const TcpServer&) = delete;
private:
    uint16_t _port;
    int _listenSock;
    static TcpServer* svr;
};

TcpServer* TcpServer::svr = nullptr;

2.HttpServer类

1.功能

  • 主要负责HTTP协议的通信
  • 其中主要包括以下模块
    • TcpServer
    • ThreadPool
    • TaskQueue

2.类设计

class HttpServer
{
public:
    HttpServer(int port = PORT)
        : _port(port)
        , _stop(false)
    {}

    void Init()
    {}

    void Loop(int threadNum = THREAD_POOL_NUM)
    {}
private:
    uint16_t _port;
    bool _stop;
};

3.Request类 && Response类

1.功能

  • Request类负责存储接收到的请求及解析结果
  • ReSponse类用来构建响应

2.Request类设计

struct HttpRequest
{
    std::string request_line;
    std::vector<std::string> request_header;
    std::string blank;
    std::string request_body;

    // 解析结果
    std::string method;
    std::string uri;
    std::string version;
    std::unordered_map<std::string, std::string> headerMap;
    size_t content_length;
    std::string path;
    std::string suffix;
    std::string arg;

    bool cgi;

    HttpRequest()
        : content_length(0)
        , cgi(false)
    {}
};

3.Response类设计

struct HttpResponse
{
    std::string status_line;
    std::vector<std::string> response_header;
    std::string blank;
    std::string response_body;

    int status_code;
    int fd;
    int fSize;

    HttpResponse()
        : status_code(OK)
        , fd(-1)
        , blank(LINE_END)
    {}
};

4.EndPoint类

1.功能

  • 负责两端业务处理,主要包括以下功能
    • 读取请求、分析请求、构建响应、IO通信
    • 该类为本项目主要设计方向

2.类设计

class EndPoint
{
public:
    EndPoint(int sock)
        : _sock(sock)
        , _stop(false)
    {}

    ~EndPoint()
    {}

    void RecvRequest()
    {}

    void BuildResponse()
    {}

    void SendResponse()
    {}

    bool IsStop()
    {}

private:
    bool RecvRequestLine()
    {}

    bool RecvRequestHeader()
    {}

    void ParseRequestLine()
    {}

    void ParseRequestHeader()
    {}

    bool IsRecvRequestBody()
    {}
    
    bool RecvRequestBody()
    {}

    int ProcessNonCgi()
    {}

    int ProcessCgi()
    {}

    void BuildResponseHelper()
    {}

    void BuildOKResponse()
    {}

    void HandlerError(std::string page)
    {}

private:
    int _sock;
    bool _stop;
    HttpRequest _request;
    HttpResponse _response;
};

5.Task类

1.功能

  • 将HTTP请求构建成一个任务,以便ThreadPool管理任务队列,一定程度上和ThreadPool一起缓解了服务器压力

2.类设计

class Task
{
public:
    void ProcessOn()
    {}
    
    Task()
    {}

    Task(int sock)
        : _sock(sock)
    {}

    ~Task()
    {}
private:
    int _sock;
    CallBack _handler; // 设置回调
};

6.ThreadPool类

1.功能

  • 线程池,分配任务给各线程,使其处理请求
  • 主要解决问题
    • 大量链接过来导致服务器内部进程或者线程暴增,进而导致服务器效率严重降低或者挂掉
    • 节省链接请求到来时,创建线程的时间成本
      • 提前创建好了一批线程,来任务时处理任务,没有任务时,让线程休眠
    • 让服务器的效率在一个恒定的稳定区间内
      • 线程个数不增多,CPU调度成本基本不变

2.类设计

static const int THREAD_POOL_NUM = 10;

// 单例模式
class ThreadPool
{
public:
    static ThreadPool *GetInstance(int num = THREAD_POOL_NUM)
    {}

    static void *ThreadRoutine(void *args)
    {}

    bool Init()
    {}

    void Push(const Task& task) // in
    {}

    void Pop(Task& task) // out
    {}

    void ThreadWait()
    {}

    void ThreadWakeUp()
    {}

    bool TaskQueueIsEmpty()
    {}

    void Lock()
    {}

    void Unlock()
    {}

    bool IsStop()
    {}

    ~ThreadPool()
    {}
private:
    ThreadPool(int num = THREAD_POOL_NUM)
        : _num(num), _stop(false)
    {}

    ThreadPool(const ThreadPool &) = delete;
private:
    int _num;
    bool _stop;
    std::queue<Task> _taskQueue;
    pthread_mutex_t _mtx;
    pthread_cond_t _cond;
    static ThreadPool *_tp;
};

ThreadPool* ThreadPool::_tp = nullptr;


网站公告

今日签到

点亮在社区的每一天
去签到