Http服务器

Stella981
• 阅读 538

项目github   https://github.com/dreamyouxi/LiteHttp

文件ThreadPool.cpp

#include "ThreadPool.h"
#include "winsock.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include <fstream>
#include <iostream>
#include <string>
#include <atomic>
#include "Defs.h"
#include "FileCache.h"
#include "HttpRequest.h"
#include "HttpRespone.h"

using namespace std;


void SendThread(HttpRespone* rep)
{
    send(rep->sock, (const char*)rep->header, rep->header_size, 0);
    send(rep->sock, (const char*)rep->buffer,rep->size, 0);

}

class ThreadCounterRAII
{
public:
    ThreadCounterRAII()
    {
        _count.fetch_add(1);
    }

    static atomic<int > _count;
    ~ThreadCounterRAII()
    {
        _count.fetch_sub(1);
    }

};

atomic<int>  ThreadCounterRAII::_count = 0;

void ProcessRequestThread(HttpRequest*request )
{
    ThreadCounterRAII counter;
    ..........
}


ThreadPool * ThreadPool::getInstance()
{
    static ThreadPool * ins = nullptr;
    if (!ins)ins = new ThreadPool;
    return ins;
}



void ThreadPool::WorkThread()
{
    while (true)
    {
        this->_mutex.lock();
        while(this->works.empty())
        {
            this->_cond.wait(this->_mutex);
        }
        HttpRequest * req =  this->works.front();
        this->works.pop();
        this->_mutex.unlock();
        ProcessRequestThread(req);
    }
}


void  ThreadPool::addTask(HttpRequest*work)
{
    this->_mutex.lock();
    this->works.push(work);
    this->_mutex.unlock();

    this->_cond.notify_one();
}

ThreadPool::ThreadPool()
{
    int MAX_THREADS = std::thread::hardware_concurrency();

    while (MAX_THREADS--)
    {
        std::thread t(std::bind(&ThreadPool::WorkThread, this));

        t.detach();
        this->workers.push_back(std::move(t));
    }
}

程序初始化时启动 10个工作线程,主线程接收到的TCP链接请求全部加入请求队列,工作线程处理

主线程:Application.cpp

    while (true)
    {
        int sock_client = accept(sock, (sockaddr *)&client_ipaddr, &length);
        if (sock_client == SOCKET_ERROR)
        {
            return;
        }
        HttpRequest*req = new HttpRequest;
        req->sock = sock_client;
        ThreadPool::getInstance()->addTask(req);
    }
点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解
Opencv中Mat矩阵相乘——点乘、dot、mul运算详解2016年09月02日00:00:36 \牧野(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fme.csdn.net%2Fdcrmg) 阅读数:59593
Wesley13 Wesley13
3年前
P2P技术揭秘.P2P网络技术原理与典型系统开发
Modular.Java(2009.06)\.Craig.Walls.文字版.pdf:http://www.t00y.com/file/59501950(https://www.oschina.net/action/GoToLink?urlhttp%3A%2F%2Fwww.t00y.com%2Ffile%2F59501950)\More.E
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法
Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法参考文章:(1)Google地球出现“无法连接到登录服务器(错误代码:c00a0194)”解决方法(https://www.oschina.net/action/GoToLink?urlhttps%3A%2F%2Fwww.codeprj.com%2Fblo
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这