-
Notifications
You must be signed in to change notification settings - Fork 4
/
mgx_cosocket.cpp
185 lines (159 loc) · 4.35 KB
/
mgx_cosocket.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "mgx_cosocket.h"
#include "mgx_comm.h"
#include "mgx_io.h"
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <poll.h>
Mgx_cosocket::Mgx_cosocket()
{
m_sch = Mgx_coroutine_scheduler::get_instance();
}
int Mgx_cosocket::set_socket_opt(int sockfd)
{
int flags = fcntl(sockfd, F_GETFL);
int ret = fcntl(sockfd, F_SETFL, flags | O_NONBLOCK);
if (ret < 0) {
::close(sockfd);
return ret;
}
int opt = 1;
ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
if (ret < 0)
::close(sockfd);
ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
if (ret < 0)
::close(sockfd);
return ret;
}
int Mgx_cosocket::socket(int domain, int type, int protocol)
{
m_sockfd = ::socket(domain, type, protocol);
if (m_sockfd < 0)
return m_sockfd;
return set_socket_opt(m_sockfd);
}
int Mgx_cosocket::close()
{
return ::close(m_sockfd);
}
int Mgx_cosocket::close(int sockfd)
{
return ::close(sockfd);
}
int Mgx_cosocket::bind(struct sockaddr *my_addr, socklen_t addrlen)
{
return ::bind(m_sockfd, my_addr, addrlen);
}
int Mgx_cosocket::listen(int backlog)
{
return ::listen(m_sockfd, backlog);
}
int Mgx_cosocket::accept(struct sockaddr *addr, socklen_t *addrlen)
{
Mgx_coroutine *co = m_sch->get_current_coroutine();
int sockfd = -1;
for (;;) {
sockfd = ::accept(m_sockfd, addr, addrlen);
if (sockfd >= 0)
break;
if (EAGAIN == errno) {
co->set_wait_fd(m_sockfd);
m_sch->add_event_wait_epoll(co, EPOLLIN);
co->yield(false);
m_sch->remove_event_wait_epoll(co);
} else {
return sockfd;
}
}
int ret = set_socket_opt(sockfd);
if (ret < 0)
return ret;
return sockfd;
}
ssize_t Mgx_cosocket::recv(int sockfd, void *buf, size_t len, int flags)
{
Mgx_coroutine *co = m_sch->get_current_coroutine();
ssize_t ret = -1;
for (;;) {
ret = ::recv(sockfd, buf, len, flags);
if (ret < 0) {
if (EAGAIN == errno) {
co->set_wait_fd(sockfd);
m_sch->add_event_wait_epoll(co, EPOLLIN);
co->yield(false);
m_sch->remove_event_wait_epoll(co);
} else {
return ret;
}
} else {
break;
}
}
return ret;
}
ssize_t Mgx_cosocket::send(int sockfd, const void *buf, size_t len, int flags)
{
Mgx_coroutine *co = m_sch->get_current_coroutine();
ssize_t ret = -1;
for (;;) {
ret = ::send(sockfd, buf, len, flags);
if (ret > 0)
break;
if (EAGAIN == errno) {
co->set_wait_fd(sockfd);
m_sch->add_event_wait_epoll(co, EPOLLOUT);
co->yield(false);
m_sch->remove_event_wait_epoll(co);
} else {
return ret;
}
}
return ret;
}
//#define CONNECT_TIMEOUT_SELECT
//#define CONNECT_TIMEOUT_POLL
int Mgx_cosocket::connect(const struct sockaddr *addr, socklen_t addrlen, unsigned long timeout)
{
int ret = ::connect(m_sockfd, addr, addrlen);
/*
* when call connect() on a non-blocking socket,
* get EINPROGRESS instead of EAGIN before connection handshake completed.
*/
if (EINPROGRESS != errno)
return ret;
Mgx_coroutine *co = m_sch->get_current_coroutine();
if (timeout == 0) {
co->set_wait_fd(m_sockfd);
m_sch->add_event_wait_epoll(co, EPOLLOUT);
co->yield(false);
m_sch->remove_event_wait_epoll(co);
} else {
#if defined(CONNECT_TIMEOUT_SELECT)
ret = mgx_select(m_sockfd, false, true, timeout);
#elif defined(CONNECT_TIMEOUT_POLL)
ret = mgx_poll(m_sockfd, false, true, timeout);
#else
co->msleep(timeout);
ret = mgx_select(m_sockfd, false, true, 0);
#endif
if (ret == 0)
goto _timeout;
else if (ret < 0)
return ret;
}
::getsockopt(m_sockfd, SOL_SOCKET, SO_ERROR, &ret, (socklen_t *)&addrlen);
if (ret == 0)
return 0;
_timeout:
errno = ETIMEDOUT;
return -1;
}
ssize_t Mgx_cosocket::recv(void *buf, size_t len, int flags)
{
return recv(m_sockfd, buf, len, flags);
}
ssize_t Mgx_cosocket::send(const void *buf, size_t len, int flags)
{
return send(m_sockfd, buf, len, flags);
}