table of Contents
- 00 Project Overview
- 01 thread synchronization mechanism packaging
- 02 semi-synchronous / semi-reactor thread pool (on)
- 03 semi-synchronous / semi thread pool reactors (under)
- 04 http connection processing (a)
- 05 http connection processing (in)
- 06 http connection processing (lower)
- 07 timer processing inactive connections (on)
- Inactive timer 08 connected to the processing (lower)
- 09 log system (on)
- Logging system 10 (lower)
- Data connection pool 11
- 12 registration and login verification
- 13 test server
- 14 project encountered problems and solutions
- 15 common interview questions involved in the project
Features
- Linux, C ++ lightweight Web server-based C / C ++ language and B / S model
- Implement web-side registration and login, access the server through the database connection pool
- Realize the function of synchronous and asynchronous logging system to record the state of the server running
- Test the server by Webbench, you can achieve tens of thousands of concurrent connections
- Response requests per second: 552852 pages / min
- The amount of data transmitted per second: 1031990 bytes / sec
- All connections are successful visit
frame
- Item frame is divided into I / O processing unit, a logic processing unit and a storage unit three modules
- I / O processing unit and a logic processing unit corresponding to the semi-synchronous / semi thread pool reactor
- Logic processing unit and a storage unit corresponding to a database connection pooling and logging system
- Semi-synchronous / semi thread pool reactors the web server side and establishing a communication
- Implement processing http request response message
- Timer completes processing of inactive connections
- Database connection pool to avoid frequent access to the database, login and check functions to achieve
- Log system to achieve synchronous and asynchronous two ways to record the state of the server running
work process
A request comes to specific process introduction project workflow, including web client and server to establish a connection, access the database server to complete the login and registration process is completed by a timer and inactive connections, servers running state last recorded by the logging system .
- and a web server to establish a connection end
- 采用epoll的边缘触发模式同时监听多个文件描述符,采用同步I/O模拟proactor模式处理事件,主线程负责监听客户端是否发起请求
- 当web端发起http请求时,主线程接收请求报文,然后将任务插入请求队列,由工作线程通过竞争从请求队列中获取任务
- 通过http类中的主从状态机对请求报文进行分析,根据请求报文对客户端进行http响应,然后由主线程给客户端发送响应报文。
- 连接数据库
- 单例模式创建数据库连接池,避免频繁建立连接,用于后续web端登录和注册校验访问服务器数据库
- 实现web端的登录和注册
- web访问的欢迎界面为GET请求,登录和注册界面是POST请求。
- 欢迎界面有新用户(0)和已有账号(1)两个选项,若选择新用户,会跳转注册(3)界面,注册成功或选择已有账号,跳转登录(2)界面,注册或登录失败会提示失败,成功和失败为0,1
- 同步/异步日志系统,记录服务器运行状态
- 同步的方式下,工作线程直接写入日志文件
- 异步会另外创建一个写线程,工作线程将要写的内容push进请求队列,通过写线程写入文件
- 日志文件支持按日期分类,和超过最大行数自动创建新文件
- 非活动连接的处理
- 由于非活跃连接占用了连接资源,严重影响服务器的性能,通过实现一个服务器定时器,处理这种非活跃连接,释放连接资源。
- 利用alarm函数周期性地触发SIGALRM信号,该信号的信号处理函数利用管道通知主循环执行定时器链表上的定时任务.
web端测试
测试前确认已安装MySQL数据库
//建立yourdb库 create database yourdb set utf8; //创建users表 USE yourdb; CREATE TABLE users( username char(50) NULL, passwd char(50) NULL )ENGINE=InnoDB; //添加数据 INSERT INTO users(username, passwd) VALUES('name', 'passwd');
修改main.c中的数据库初始化信息
//root root为服务器数据库的登录名和密码 connection_pool *connPool=connection_pool::GetInstance("localhost","root","root","yourdb",3306,5);
修改http_conn.cpp中的数据库初始化信息
//root root为服务器数据库的登录名和密码 connection_pool *connPool=connection_pool::GetInstance("localhost","root","root","yourdb",3306,5);
修改http_conn.cpp中的root路径
const char* doc_root="/home/qgy/github/ini_tinywebserver/root";
- CGI多进程注册/登录校验
打开http_conn.cpp中CGI,关闭同步线程
380 //同步线程登录校验 381 //#if 0 423 //#endif 425 //CGI多进程登录校验 426 #if 0 495 #endif
修改sign.cpp中的数据库初始化信息
//root root为服务器数据库的登录名和密码 connection_pool *connPool=connection_pool::GetInstance("localhost","root","root","yourdb",3306,5);
生成check.cgi
make check.cgi
将生成的check.cgi放到root文件夹
cp ./check.cgi ./root
- 同步线程注册/登录校验
关闭http_conn.cpp中CGI,打开同步线程
380 //同步线程登录校验 381 //#if 0 423 //#endif 425 //CGI多进程登录校验 426 #if 0 495 #endif
生成server
make server
启动server
./server port
浏览器端
ip:port
更多资料
- 关注公众号【两猿社】,进入公众号
- 带你丰富互联网相关项目经验,轻松应对校招!!!
- 项目模块详细讲解,在公众号内持续更新!!!