-
Notifications
You must be signed in to change notification settings - Fork 6
/
EventLoop.cpp
168 lines (146 loc) · 3.17 KB
/
EventLoop.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
Copyright © 2017-2019, orcaer@yeah.net All rights reserved.
Author: orcaer@yeah.net
Last modified: 2019-9-11
Description: https://github.com/wlgq2/uv-cpp
Memo:changed something by robin 2021-07-02
*/
#include "../include/Async.h"
#include "../include/EventLoop.h"
using namespace robin;
EventLoop::EventLoop() : EventLoop(EventLoop::Mode::New)
{
}
EventLoop::EventLoop(EventLoop::Mode mode):loop(nullptr),async(nullptr),status(NotRun)
{
if (mode == EventLoop::Mode::New)
{
loop = new uv_loop_t();
uv_loop_init(loop);
}
else
{
loop = uv_default_loop();
}
async = std::make_shared<Async>(this);
}
EventLoop::~EventLoop()
{
// new loop
if (loop != uv_default_loop())
{
uv_loop_close(loop);
delete loop;
}
else // default static loop in libuv
{
uv_loop_close(loop);
}
}
EventLoop* EventLoop::DefaultLoop()
{
static EventLoop defaultLoop(EventLoop::Mode::Default);
return &defaultLoop;
}
uv_loop_t* EventLoop::handle()
{
return loop;
}
// normally you should call this run() in yours work thread
// and should call stop() to
int EventLoop::run()
{
if (status == Status::NotRun || status == Status::Stop)
{
async->init();
loopThreadId = std::this_thread::get_id();
status = Status::Running;
int ret = ::uv_run(loop, UV_RUN_DEFAULT);
status = Status::Stop;
return ret;
}
return -1;
}
int EventLoop::runNoWait()
{
if (status == Status::NotRun)
{
async->init();
loopThreadId = std::this_thread::get_id();
status = Status::Running;
auto rst = ::uv_run(loop, UV_RUN_NOWAIT);
status = Status::NotRun;
return rst;
}
return -1;
}
// should close async first
// uv_stop must called in loop thread
int EventLoop::stop()
{
if (status == Status::Running)
{
// after sync, the close
runInLoop([this]()
{
async->close([](Async* ptr)
{
//printf("asyncall close\n");
::uv_stop(ptr->Loop()->handle());
});
});
/*runInLoop([this]()
{
uv_stop(loop);
});*/
return 0;
}
return -1;
}
bool EventLoop::isStoped()
{
return status == Status::Stop;
}
EventLoop::Status EventLoop::getStatus()
{
return status;
}
bool EventLoop::isRunInLoopThread()
{
if (status == Status::Running)
{
return std::this_thread::get_id() == loopThreadId;
}
//EventLoop isn't running
return false;
}
// since loop is not running, something will not callback, like uv_write
bool EventLoop::runInLoop(const DefaultCallback func)
{
if (nullptr == func)
return false;
if (isRunInLoopThread() || isStoped())
{
//LOG_DEBUG("run direct...");
func();
return false;
}
async->runInLoop(func);
return true;
}
bool EventLoop::runInLoopEn(const DefaultCallback func)
{
if (nullptr == func)
return false;
async->runInLoop(func);
return true;
}
//const char* EventLoop::GetErrorMessage(int status)
//{
// if (WriteInfo::Disconnected == status)
// {
// static char info[] = "the connection is disconnected";
// return info;
// }
// return uv_strerror(status);
//}