-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp_impl.cpp
More file actions
113 lines (90 loc) · 2.67 KB
/
Copy pathapp_impl.cpp
File metadata and controls
113 lines (90 loc) · 2.67 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// Created by flexf on 12.03.2020.
//
#include "app_impl.h"
#include "http_listener.h"
#include "log.h"
namespace flask4cpp
{
namespace
{
class ListenerCreator
{
LoggerType* m_logger;
boost::asio::io_context* m_ioc;
public:
ListenerCreator(LoggerType* logger, boost::asio::io_context* ioc)
: m_logger(logger)
, m_ioc(ioc)
{
}
std::shared_ptr<HttpListenerBase> operator()(const ListenerParams& l) const
{
auto const address = boost::asio::ip::make_address(l.address);
HttpListener::SocketType::endpoint endpoint{ address, l.port };
return std::make_shared<HttpListener>(m_logger, *m_ioc, endpoint);
}
std::shared_ptr<HttpListenerBase> operator()(const SSLListenerParams& l) const { return std::shared_ptr<HttpListenerBase>{}; }
};
} // namespace
int AppImpl::Run()
{
Start(false);
return WaitForStop();
}
int AppImpl::WaitForStop()
{
{
std::unique_lock<std::mutex> l(m_syncMutex);
m_syncCondVar.wait(l, [this] { return m_currentState == State::Stopped; });
}
return 0;
}
int AppImpl::Start(bool wait)
{
m_currentState = State::Starting;
m_ioContext = std::make_unique<boost::asio::io_context>(m_settings->threadNum);
LOG_DEBUG() << "Start creating listeners";
for (auto& l : m_settings->listeners)
{
LOG_DEBUG() << ">>>>>>>>> 1";
auto listener = nonstd::visit(ListenerCreator(log(), m_ioContext.get()), l);
LOG_DEBUG() << ">>>>>>>>> 2";
if (!listener)
continue;
LOG_DEBUG() << ">>>>>>>>> 3";
if (!listener->IsOpen())
return -1;
LOG_DEBUG() << ">>>>>>>>> 4";
m_listeners.push_back(listener);
LOG_DEBUG() << ">>>>>>>>> 5";
listener->run();
LOG_DEBUG() << ">>>>>>>>> 6";
}
LOG_DEBUG() << "End creating listeners. " << m_listeners.size() << " created";
auto threads = m_settings->threadNum;
m_ioThreads.reserve(threads);
LOG_DEBUG() << "Start creating IO thread pool threads";
std::atomic_int started_threads{ 0 };
for (auto i = threads - 1; i >= 0; --i)
m_ioThreads.emplace_back([this, &started_threads] {
LOG_DEBUG() << "IO thread started";
started_threads++;
m_ioContext->run();
});
LOG_DEBUG() << "End creating IO thread pool threads. " << m_ioThreads.size() << " threads created";
if (wait)
{
LOG_DEBUG() << "Wait for IO threads started";
while (started_threads != threads)
;
LOG_DEBUG() << "IO threads actually started";
}
m_currentState = State::Started;
return 0;
}
int AppImpl::Shutdown()
{
return 0;
}
} // flask4cpp