You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
2.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#pragma once
#include"TcpServer.h"
#include"ThreadPool.h"
#include<string>
#include"HttpRequest.h"
#include"HttpConnection.h"
#include"HttpResponse.h"
#include"MYSQLConnectionPool.h"
#include"HttpContext.h"
class TcpServer;
class ThreadPool;
using upContext=std::unique_ptr<HttpContext>;//HttpContext指针
using httpHandler=std::function<void(upContext)>;
// using upMYSQLPool=std::unique_ptr<MYSQLConnectionPool>;
class HttpServer
{
private:
std::unordered_map<std::string,httpHandler>GetHandler_; //get方法处理
std::unordered_map<std::string,httpHandler>PostHandler_; //post方法处理
TcpServer tcpServer_; //tcpServer层
//底层
void Close(int _fd); //根据fd关闭Connectionmap中也删除
void HandleMessage(spConnection _connection,std::string _message); //处理消息
//业务层
void NewConnection(Socket *clieSocket); //处理新连接将fd交给Connection处理客户端ip和port
void SetGetHandler(); //设置Get处理
void SetPostHandler(); //设置post处理
public:
HttpServer(const std::string& _ip,uint16_t _port,int _eventLoopNum,int _workNum,
//mysql配置
int _mysqlPort,const std::string& _user,const std::string& _passwd,//端口3306用户密码
const std::string& _dataBase,size_t _maxSize,size_t _minSize,//数据库,最大总连接数,最小总连接数/最大空闲连接数
int _idleCheckInterval,int _connectionTimeOut);//空闲连接检查间隔,空闲连接超时时间
HttpServer(const std::string& _ip,uint16_t _port,int _eventLoopNum);
~HttpServer();
void Start(); //处理事件
void Stop(); //停止线程池和tcpServer
void SetSignUp(httpHandler _fun); //设置用户注册回调函数
void SetLogIn(httpHandler _fun); //设置用户登录回调函数
void SetPublishPost(httpHandler _fun); //设置发帖回调函数
void SetDeletePost(httpHandler _fun); //设置删帖回调函数
void SetCheckMyPosts(httpHandler _fun); //设置查看自己的贴子回调函数
};