mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-14 04:57:21 +01:00
124 lines
3.0 KiB
C++
124 lines
3.0 KiB
C++
#include "http_server.h"
|
|
|
|
#include "request.h"
|
|
#include "application.h"
|
|
|
|
#define LOG_VERBOSE 0
|
|
|
|
void HTTPServer::http_callback_handler(Request *request) {
|
|
Application::handle_request(request);
|
|
}
|
|
|
|
void HTTPServer::httpEnterCallbackDefault(const HTTPParser &httpParser, const HttpSession::Ptr &session) {
|
|
Request *request = RequestPool::get_request();
|
|
|
|
HttpSession *s = session.get();
|
|
|
|
_request_map[s] = request;
|
|
|
|
request->http_parser = std::make_shared<HTTPParser>(httpParser);
|
|
request->session = session;
|
|
|
|
request->setup_url_stack();
|
|
|
|
#if LOG_VERBOSE
|
|
std::cout << "method:" << http_method_str(static_cast<http_method>(httpParser.method())) << std::endl;
|
|
#endif
|
|
|
|
http_callback_handler(request);
|
|
}
|
|
|
|
void HTTPServer::wsEnterCallbackDefault(const HttpSession::Ptr &httpSession, WebSocketFormat::WebSocketFrameType opcode, const std::string &payload) {
|
|
|
|
std::cout << "frame enter of type:" << int(opcode) << std::endl;
|
|
std::cout << "payload is:" << payload << std::endl;
|
|
// echo frame
|
|
//auto frame = std::make_shared<std::string>();
|
|
|
|
//WebSocketFormat::wsFrameBuild(payload.c_str(), payload.size(), *frame, WebSocketFormat::WebSocketFrameType::TEXT_FRAME, true, false);
|
|
|
|
//httpSession->send(frame);
|
|
}
|
|
|
|
void HTTPServer::closedCallbackDefault(const HttpSession::Ptr &session) {
|
|
HttpSession *s = session.get();
|
|
|
|
Request *r = _request_map[s];
|
|
|
|
if (r == nullptr) {
|
|
printf("Error HTTPServer::closedCallbackDefault: r == nullptr!");
|
|
_request_map.erase(s);
|
|
return;
|
|
}
|
|
|
|
r->connection_closed = true;
|
|
|
|
_request_map.erase(s);
|
|
}
|
|
|
|
void HTTPServer::configure() {
|
|
}
|
|
|
|
void HTTPServer::initialize() {
|
|
if (service)
|
|
return;
|
|
|
|
configure();
|
|
|
|
service = TcpService::Create();
|
|
service->startWorkerThread(threads);
|
|
|
|
int p_port = port;
|
|
|
|
//!
|
|
if (listenBuilder)
|
|
delete listenBuilder;
|
|
|
|
listenBuilder = new wrapper::HttpListenerBuilder();
|
|
listenBuilder->configureService(service);
|
|
|
|
listenBuilder->configureSocketOptions({
|
|
[](TcpSocket &socket) {
|
|
socket.setNodelay();
|
|
},
|
|
});
|
|
|
|
listenBuilder->configureConnectionOptions({ AddSocketOption::WithMaxRecvBufferSize(1024) });
|
|
|
|
listenBuilder->configureListen([p_port](wrapper::BuildListenConfig builder) {
|
|
builder.setAddr(false, "0.0.0.0", p_port);
|
|
});
|
|
|
|
listenBuilder->configureEnterCallback([](const HttpSession::Ptr &httpSession, HttpSessionHandlers &handlers) {
|
|
handlers.setHttpCallback(HTTPServer::httpEnterCallbackDefault);
|
|
handlers.setClosedCallback(HTTPServer::closedCallbackDefault);
|
|
handlers.setWSCallback(HTTPServer::wsEnterCallbackDefault);
|
|
});
|
|
|
|
listenBuilder->asyncRun();
|
|
}
|
|
|
|
void HTTPServer::main_loop() {
|
|
while (true) {
|
|
//std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
if (brynet::base::app_kbhit()) {
|
|
break;
|
|
}
|
|
|
|
Application::get_instance()->update();
|
|
}
|
|
}
|
|
|
|
HTTPServer::HTTPServer() {
|
|
port = 80;
|
|
threads = 4;
|
|
listenBuilder = nullptr;
|
|
}
|
|
|
|
HTTPServer::~HTTPServer() {
|
|
delete listenBuilder;
|
|
}
|
|
|
|
std::map<HttpSession*, Request*> HTTPServer::_request_map; |