-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_iomanager.cc
74 lines (62 loc) · 2.07 KB
/
test_iomanager.cc
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
#include "src/jujimeizuo.h"
#include "src/iomanager.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <iostream>
#include <sys/epoll.h>
jujimeizuo::Logger::ptr g_logger = JUJIMEIZUO_LOG_ROOT();
int sock = 0;
void test_fiber() {
JUJIMEIZUO_LOG_INFO(g_logger) << "test_fiber sock=" << sock;
//sleep(3);
//close(sock);
//jujimeizuo::IOManager::GetThis()->cancelAll(sock);
sock = socket(AF_INET, SOCK_STREAM, 0);
fcntl(sock, F_SETFL, O_NONBLOCK);
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(80);
inet_pton(AF_INET, "115.239.210.27", &addr.sin_addr.s_addr);
if(!connect(sock, (const sockaddr*)&addr, sizeof(addr))) {
} else if(errno == EINPROGRESS) {
JUJIMEIZUO_LOG_INFO(g_logger) << "add event errno=" << errno << " " << strerror(errno);
jujimeizuo::IOManager::GetThis()->addEvent(sock, jujimeizuo::IOManager::READ, [](){
JUJIMEIZUO_LOG_INFO(g_logger) << "read callback";
});
jujimeizuo::IOManager::GetThis()->addEvent(sock, jujimeizuo::IOManager::WRITE, [](){
JUJIMEIZUO_LOG_INFO(g_logger) << "write callback";
//close(sock);
jujimeizuo::IOManager::GetThis()->cancelEvent(sock, jujimeizuo::IOManager::READ);
close(sock);
});
} else {
JUJIMEIZUO_LOG_INFO(g_logger) << "else " << errno << " " << strerror(errno);
}
}
void test1() {
std::cout << "EPOLLIN=" << EPOLLIN
<< " EPOLLOUT=" << EPOLLOUT << std::endl;
jujimeizuo::IOManager iom(2, false);
iom.schedule(&test_fiber);
}
jujimeizuo::Timer::ptr s_timer;
void test_timer() {
jujimeizuo::IOManager iom(2);
s_timer = iom.addTimer(1000, [](){
static int i = 0;
JUJIMEIZUO_LOG_INFO(g_logger) << "hello timer i=" << i;
if(++i == 3) {
s_timer->reset(2000, true);
//s_timer->cancel();
}
}, true);
}
int main(int argc, char** argv) {
//test1();
test_timer();
return 0;
}