-
Notifications
You must be signed in to change notification settings - Fork 240
/
PromiseReceive.cpp
86 lines (76 loc) · 2.97 KB
/
PromiseReceive.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <brynet/base/AppStatus.hpp>
#include <brynet/net/EventLoop.hpp>
#include <brynet/net/PromiseReceive.hpp>
#include <brynet/net/TcpService.hpp>
#include <brynet/net/http/HttpFormat.hpp>
#include <brynet/net/wrapper/ServiceBuilder.hpp>
#include <iostream>
#include <mutex>
using namespace brynet;
using namespace brynet::net;
using namespace brynet::net::http;
int main(int argc, char** argv)
{
if (argc != 3)
{
fprintf(stderr, "Usage: <listen port> <net work thread num>\n");
exit(-1);
}
auto service = IOThreadTcpService::Create();
service->startWorkerThread(atoi(argv[2]));
auto enterCallback = [](const TcpConnection::Ptr& session) {
auto promiseReceive = setupPromiseReceive(session);
auto contentLength = std::make_shared<size_t>();
promiseReceive
->receiveUntil("\r\n", [](const char* buffer, size_t len) {
auto headline = std::string(buffer, len);
std::cout << headline << std::endl;
return false;
})
->receiveUntil("\r\n", [promiseReceive, contentLength](const char* buffer, size_t len) {
auto headerValue = std::string(buffer, len);
std::cout << headerValue << std::endl;
if (len > 2)
{
const static std::string ContentLenghtFlag = "Content-Length: ";
auto pos = headerValue.find(ContentLenghtFlag);
if (pos != std::string::npos)
{
auto lenStr = headerValue.substr(pos + ContentLenghtFlag.size(), headerValue.size());
*contentLength = std::stoi(lenStr);
}
return true;
}
return false;
})
->receive(contentLength, [session](const char* buffer, size_t len) {
(void) buffer;
(void) len;
HttpResponse response;
response.setStatus(HttpResponse::HTTP_RESPONSE_STATUS::OK);
response.setContentType("text/html; charset=utf-8");
response.setBody("<html>hello world </html>");
session->send(response.getResult());
session->postShutdown();
return false;
});
};
wrapper::ListenerBuilder listener;
listener.WithService(service)
.AddSocketProcess({[](TcpSocket& socket) {
socket.setNodelay();
}})
.WithMaxRecvBufferSize(1024 * 1024)
.AddEnterCallback(enterCallback)
.WithAddr(false, "0.0.0.0", atoi(argv[1]))
.asyncRun();
while (true)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
if (brynet::base::app_kbhit())
{
break;
}
}
return 0;
}