forked from parihaaraka/cpp2tnt
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtntevloop.cpp
More file actions
108 lines (82 loc) · 2.61 KB
/
tntevloop.cpp
File metadata and controls
108 lines (82 loc) · 2.61 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
#include "tntevloop.h"
#include <functional>
#include <iostream>
using namespace std;
namespace tnt
{
TntEvLoop::TntEvLoop()
{
AddOnOpened(bind(&TntEvLoop::OnConnected, this));
}
TntEvLoop::~TntEvLoop()
{
isStoped_ = true;
connection::close(false, false);
}
void TntEvLoop::Attach(struct ev_loop* loop)
{
loop_ = loop;
socketWatcher_.data = this;
ev_init(&socketWatcher_, OnSocketEvent_);
socketWatcher_.events = 0;
on_socket_watcher_request([this](int mode)noexcept{this->OnSocketWatcherRequest(mode);});
ev_async_init(&asyncNotifier_, OnAsyncNotifier_);
asyncNotifier_.data = this;
ev_async_start(loop, &asyncNotifier_);
ev_timer_init(&timer_, OnTimer_, 0, 1);
timer_.data = this;
ev_timer_start(loop, &timer_);
on_notify_request(bind(ev_async_send, loop, &asyncNotifier_));
}
void TntEvLoop::OnSocketWatcherRequest(int mode) noexcept
{
if (isStoped_)
return;
int events = (mode & tnt::socket_state::read ? EV_READ : EV_NONE);
events |= (mode & tnt::socket_state::write ? EV_WRITE : EV_NONE);
if ((socketWatcher_.events & (EV_READ | EV_WRITE)) != events)
{
if (ev_is_active(&socketWatcher_))
ev_io_stop(loop_, &socketWatcher_);
ev_io_set(&socketWatcher_, socket_handle(), events);
if (events)
ev_io_start(loop_, &socketWatcher_);
}
}
void TntEvLoop::OnSocketEvent_(struct ev_loop* loop, ev_io* w, int revents)
{
((TntEvLoop*)w->data)->OnSocketEvent(loop, w, revents);
}
void TntEvLoop::OnSocketEvent(struct ev_loop* loop, ev_io* w, int revents)
{
// if (isStoped_)
// return;
(void)loop;(void)w;
if (revents & EV_ERROR)
connection::handle_error("EV_ERROR soket state received");
if (revents & EV_WRITE)
connection::write();
if (revents & EV_READ)
connection::read();
}
void TntEvLoop::OnAsyncNotifier_(struct ev_loop* loop, ev_async* w, int revents)
{
((TntEvLoop*)w->data)->OnAsyncNotifier(loop, w, revents);
}
void TntEvLoop::OnAsyncNotifier(struct ev_loop* loop, ev_async* w, int revents)
{
(void)loop;(void)w;(void)revents;
acquire_notifications();
}
void TntEvLoop::OnConnected() noexcept
{
}
void TntEvLoop::OnTimer_(struct ev_loop* loop, ev_timer* w, int revents)
{
((TntEvLoop*)w->data)->OnTimer(loop, w, revents);
}
void TntEvLoop::OnTimer(struct ev_loop*, ev_timer*, int)
{
tick_1sec();
}
}