-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_ws_ex.hpp
58 lines (48 loc) · 1.43 KB
/
server_ws_ex.hpp
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
//
// Created by assasin on 17.06.17.
//
#pragma once
#include "simple_wss/server_wss.hpp"
#include "algo.hpp"
#include "logger.hpp"
class WebSocketServerEx : public SimpleWeb::SocketServer<SimpleWeb::WS> {
public:
WebSocketServerEx() : SimpleWeb::SocketServer<SimpleWeb::WS>() {
io_service = std::make_shared<boost::asio::io_service>();
internal_io_service = true;
}
void runWithTimeout(int msec, std::function<void()> func) {
using namespace boost;
using namespace boost::asio;
auto timer = std::make_shared<deadline_timer>(*io_service);
std::function<void(const system::error_code& ec)> f;
f = [func, timer](const system::error_code& ec) {
if(!ec) {
func();
}
else {
Logger::error("Timer error ", ec.value(), ": ", ec.message());
}
};
timer->expires_from_now(posix_time::millisec(msec));
timer->async_wait(f);
}
void runWithInterval(int msec, std::function<void()> func) {
using namespace boost;
using namespace boost::asio;
auto timer = std::make_shared<deadline_timer>(*io_service);
auto f = std::make_shared<std::function<void(const system::error_code& ec)>>();
*f = [=](const system::error_code& ec) {
if(!ec) {
func();
timer->expires_from_now(posix_time::millisec(msec));
timer->async_wait(*f);
}
else {
Logger::error("Timer error ", ec.value(), ": ", ec.message());
}
};
timer->expires_from_now(posix_time::millisec(msec));
timer->async_wait(*f);
}
};