-
Notifications
You must be signed in to change notification settings - Fork 373
/
Copy pathWebMain.C
77 lines (59 loc) · 1.75 KB
/
WebMain.C
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
/*
* Copyright (C) 2008 Emweb bv, Herent, Belgium.
*
* See the LICENSE file for terms of use.
*/
#include "WebMain.h"
#include <Wt/WIOService.h>
#include <Wt/WServer.h>
#include <Wt/AsioWrapper/asio.hpp>
#include "WebController.h"
#include "WebStream.h"
namespace Wt {
LOGGER("WebMain");
WebMain::WebMain(WServer *server, WebStream *stream,
std::string singleSessionId)
: server_(server),
stream_(stream),
singleSessionId_(singleSessionId),
shutdown_(false)
{ }
WebMain::~WebMain()
{ }
void WebMain::shutdown()
{
shutdown_ = true;
controller().shutdown();
}
void WebMain::run()
{
server_->ioService().start();
WebRequest *request = stream_->getNextRequest(10);
if (request)
controller().handleRequest(request);
else
if (!singleSessionId_.empty()) {
LOG_ERROR("no initial request ?");
return;
}
for (;;) {
bool haveMoreSessions = controller().expireSessions();
if (!haveMoreSessions && !singleSessionId_.empty())
break;
WebRequest *request = stream_->getNextRequest(5);
if (shutdown_)
break;
if (request) {
// Attention: WIOService::post() uses a global strand to ensure keeping
// the order in which events were posted. For processing WebRequests,
// this is very bad since at most one handleRequest() will be executed
// simultaneously. Additionally, this breaks recursive event loops.
// Asio's io_service::post does no such thing, so calling the function
// of the superclass if fine.
AsioWrapper::asio::post(static_cast<Wt::AsioWrapper::asio::io_service&>(server_->ioService()),
std::bind(&WebController::handleRequest, &controller(), request));
}
}
server_->ioService().stop();
}
}