rcpp_framework/core/http_server.cpp

98 lines
2.3 KiB
C++
Raw Normal View History

2020-11-25 00:20:41 +01:00
#include "http_server.h"
#include "request.h"
#include "application.h"
2020-11-25 00:20:41 +01:00
#define LOG_VERBOSE 0
void HTTPServer::http_callback_handler(Request *request) {
Application::handle_request(request);
2020-11-25 00:20:41 +01:00
}
void HTTPServer::httpEnterCallbackDefault(const HTTPParser &httpParser, const HttpSession::Ptr &session) {
2020-11-25 00:20:41 +01:00
Request *request = RequestPool::get_request();
request->http_parser = &httpParser;
request->session = &session;
2020-11-25 00:20:41 +01:00
request->setup_url_stack();
2020-11-25 00:20:41 +01:00
#if LOG_VERBOSE
std::cout << "method:" << http_method_str(static_cast<http_method>(httpParser.method())) << std::endl;
#endif
http_callback_handler(request);
2020-11-25 00:20:41 +01:00
}
void HTTPServer::wsEnterCallbackDefault(const HttpSession::Ptr &httpSession, WebSocketFormat::WebSocketFrameType opcode, const std::string &payload) {
2020-11-25 00:20:41 +01:00
std::cout << "frame enter of type:" << int(opcode) << std::endl;
std::cout << "payload is:" << payload << std::endl;
// echo frame
2021-01-29 17:26:17 +01:00
//auto frame = std::make_shared<std::string>();
2020-11-25 00:20:41 +01:00
2021-01-29 17:26:17 +01:00
//WebSocketFormat::wsFrameBuild(payload.c_str(), payload.size(), *frame, WebSocketFormat::WebSocketFrameType::TEXT_FRAME, true, false);
2020-11-25 00:20:41 +01:00
2021-01-29 17:26:17 +01:00
//httpSession->send(frame);
2020-11-25 00:20:41 +01:00
}
void HTTPServer::configure() {
2020-11-25 00:20:41 +01:00
}
void HTTPServer::initialize() {
2020-11-25 00:20:41 +01:00
if (service)
return;
configure();
2020-11-25 00:20:41 +01:00
service = TcpService::Create();
2020-11-25 00:20:41 +01:00
service->startWorkerThread(threads);
int p_port = port;
//!
2020-11-25 00:20:41 +01:00
if (listenBuilder)
delete listenBuilder;
listenBuilder = new wrapper::HttpListenerBuilder();
2020-11-25 00:20:41 +01:00
listenBuilder->configureService(service);
listenBuilder->configureSocketOptions({
[](TcpSocket &socket) {
2020-11-25 00:20:41 +01:00
socket.setNodelay();
},
});
listenBuilder->configureConnectionOptions({ AddSocketOption::WithMaxRecvBufferSize(1024) });
2020-11-25 00:20:41 +01:00
listenBuilder->configureListen([p_port](wrapper::BuildListenConfig builder) {
2020-11-25 00:20:41 +01:00
builder.setAddr(false, "0.0.0.0", p_port);
});
listenBuilder->configureEnterCallback([](const HttpSession::Ptr &httpSession, HttpSessionHandlers &handlers) {
2020-11-25 00:20:41 +01:00
handlers.setHttpCallback(HTTPServer::httpEnterCallbackDefault);
handlers.setWSCallback(HTTPServer::wsEnterCallbackDefault);
});
listenBuilder->asyncRun();
}
void HTTPServer::main_loop() {
2020-11-25 00:20:41 +01:00
while (true) {
std::this_thread::sleep_for(std::chrono::seconds(1));
2020-11-25 00:20:41 +01:00
if (brynet::base::app_kbhit()) {
break;
}
}
}
HTTPServer::HTTPServer() {
port = 80;
threads = 4;
listenBuilder = nullptr;
2020-11-25 00:20:41 +01:00
}
HTTPServer::~HTTPServer() {
delete listenBuilder;
}