MCPcopy Create free account
hub / github.com/IronsDu/brynet / main

Function main

examples/HttpServer.cpp:15–81  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13using namespace brynet::net::http;
14
15int main(int argc, char** argv)
16{
17 if (argc != 3)
18 {
19 fprintf(stderr, "Usage: <listen port> <net work thread num>\n");
20 exit(-1);
21 }
22
23 const auto port = std::atoi(argv[1]);
24 auto service = IOThreadTcpService::Create();
25 service->startWorkerThread(atoi(argv[2]));
26
27 auto httpEnterCallback = [](const HTTPParser& httpParser,
28 const HttpSession::Ptr& session) {
29 (void) httpParser;
30 HttpResponse response;
31 //std::cout << "method:" << http_method_str(static_cast<http_method>(httpParser.method())) << std::endl;
32 response.setBody(std::string("<html>hello world </html>"));
33 if (httpParser.isKeepAlive())
34 {
35 response.addHeadValue("Connection", "Keep-Alive");
36 session->send(response.getResult());
37 }
38 else
39 {
40 response.addHeadValue("Connection", "Close");
41 session->send(response.getResult(), [session]() {
42 session->postShutdown();
43 });
44 }
45 };
46
47 auto wsEnterCallback = [](const HttpSession::Ptr& httpSession,
48 WebSocketFormat::WebSocketFrameType opcode,
49 const std::string& payload) {
50 std::cout << "frame enter of type:" << int(opcode) << std::endl;
51 std::cout << "payload is:" << payload << std::endl;
52 // echo frame
53 httpSession->send(WebSocketFormat::wsFrameBuild(payload));
54 };
55
56 wrapper::HttpListenerBuilder listenBuilder;
57 listenBuilder
58 .WithService(service)
59 .AddSocketProcess([](TcpSocket& socket) {
60 socket.setNodelay();
61 })
62 .WithMaxRecvBufferSize(1024)
63 .WithAddr(false, "0.0.0.0", port)
64 .WithReusePort()
65 .WithEnterCallback([httpEnterCallback, wsEnterCallback](const HttpSession::Ptr& httpSession, HttpSessionHandlers& handlers) {
66 handlers.setHttpEndCallback(httpEnterCallback);
67 handlers.setWSCallback(wsEnterCallback);
68 })
69 .asyncRun();
70
71 while (true)
72 {

Callers

nothing calls this directly

Calls 12

app_kbhitFunction · 0.85
isKeepAliveMethod · 0.80
setNodelayMethod · 0.80
setHttpEndCallbackMethod · 0.80
startWorkerThreadMethod · 0.45
setBodyMethod · 0.45
addHeadValueMethod · 0.45
sendMethod · 0.45
getResultMethod · 0.45
postShutdownMethod · 0.45
asyncRunMethod · 0.45
setWSCallbackMethod · 0.45

Tested by

no test coverage detected