forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket_accept_operation.cpp
More file actions
183 lines (158 loc) · 5.38 KB
/
Copy pathsocket_accept_operation.cpp
File metadata and controls
183 lines (158 loc) · 5.38 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
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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/net/socket_accept_operation.hpp>
#include <cppcoro/net/socket.hpp>
#include "socket_helpers.hpp"
#include <system_error>
#if CPPCORO_OS_WINNT
# include <winsock2.h>
# include <ws2tcpip.h>
# include <mswsock.h>
# include <windows.h>
// TODO: Eliminate duplication of implementation between socket_accept_operation
// and socket_accept_operation_cancellable.
bool cppcoro::net::socket_accept_operation_impl::try_start(
cppcoro::detail::win32_overlapped_operation_base& operation) noexcept
{
static_assert(
(sizeof(m_addressBuffer) / 2) >= (16 + sizeof(SOCKADDR_IN)) &&
(sizeof(m_addressBuffer) / 2) >= (16 + sizeof(SOCKADDR_IN6)),
"AcceptEx requires address buffer to be at least 16 bytes more than largest address.");
// Need to read this flag before starting the operation, otherwise
// it may be possible that the operation will complete immediately
// on another thread and then destroy the socket before we get a
// chance to read it.
const bool skipCompletionOnSuccess = m_listeningSocket.skip_completion_on_success();
DWORD bytesReceived = 0;
BOOL ok = ::AcceptEx(
m_listeningSocket.native_handle(),
m_acceptingSocket.native_handle(),
m_addressBuffer,
0,
sizeof(m_addressBuffer) / 2,
sizeof(m_addressBuffer) / 2,
&bytesReceived,
operation.get_overlapped());
if (!ok)
{
int errorCode = ::WSAGetLastError();
if (errorCode != ERROR_IO_PENDING)
{
operation.m_errorCode = static_cast<DWORD>(errorCode);
return false;
}
}
else if (skipCompletionOnSuccess)
{
operation.m_errorCode = ERROR_SUCCESS;
return false;
}
return true;
}
void cppcoro::net::socket_accept_operation_impl::cancel(
cppcoro::detail::win32_overlapped_operation_base& operation) noexcept
{
(void)::CancelIoEx(
reinterpret_cast<HANDLE>(m_listeningSocket.native_handle()),
operation.get_overlapped());
}
void cppcoro::net::socket_accept_operation_impl::get_result(
cppcoro::detail::win32_overlapped_operation_base& operation)
{
if (operation.m_errorCode != ERROR_SUCCESS)
{
throw std::system_error{
static_cast<int>(operation.m_errorCode),
std::system_category(),
"Accepting a connection failed: AcceptEx"
};
}
sockaddr* localSockaddr = nullptr;
sockaddr* remoteSockaddr = nullptr;
INT localSockaddrLength;
INT remoteSockaddrLength;
::GetAcceptExSockaddrs(
m_addressBuffer,
0,
sizeof(m_addressBuffer) / 2,
sizeof(m_addressBuffer) / 2,
&localSockaddr,
&localSockaddrLength,
&remoteSockaddr,
&remoteSockaddrLength);
m_acceptingSocket.m_localEndPoint =
detail::sockaddr_to_ip_endpoint(*localSockaddr);
m_acceptingSocket.m_remoteEndPoint =
detail::sockaddr_to_ip_endpoint(*remoteSockaddr);
{
// Need to set SO_UPDATE_ACCEPT_CONTEXT after the accept completes
// to ensure that ::shutdown() and ::setsockopt() calls work on the
// accepted socket.
SOCKET listenSocket = m_listeningSocket.native_handle();
const int result = ::setsockopt(
m_acceptingSocket.native_handle(),
SOL_SOCKET,
SO_UPDATE_ACCEPT_CONTEXT,
(const char*)&listenSocket,
sizeof(SOCKET));
if (result == SOCKET_ERROR)
{
const int errorCode = ::WSAGetLastError();
throw std::system_error{
errorCode,
std::system_category(),
"Socket accept operation failed: setsockopt(SO_UPDATE_ACCEPT_CONTEXT)"
};
}
}
}
#elif CPPCORO_OS_LINUX
# include <sys/socket.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# include <netinet/udp.h>
bool cppcoro::net::socket_accept_operation_impl::try_start(
cppcoro::detail::linux_async_operation_base& operation) noexcept
{
static_assert(
(sizeof(m_addressBuffer) / 2) >= (16 + sizeof(sockaddr_in)) &&
(sizeof(m_addressBuffer) / 2) >= (16 + sizeof(sockaddr_in6)),
"AcceptEx requires address buffer to be at least 16 bytes more than largest address.");
operation.m_completeFunc = [=]() {
socklen_t len = sizeof(m_addressBuffer) / 2;
int res = accept(m_listeningSocket.native_handle(), reinterpret_cast<sockaddr*>(m_addressBuffer), &len);
operation.m_mq->remove_fd_watch(m_listeningSocket.native_handle());
return res;
};
operation.m_mq->add_fd_watch(m_listeningSocket.native_handle(), reinterpret_cast<void*>(&operation), EPOLLIN);
return true;
}
void cppcoro::net::socket_accept_operation_impl::cancel(
cppcoro::detail::linux_async_operation_base& operation) noexcept
{
operation.m_mq->remove_fd_watch(m_listeningSocket.native_handle());
}
void cppcoro::net::socket_accept_operation_impl::get_result(
cppcoro::detail::linux_async_operation_base& operation)
{
if (operation.m_res < 0)
{
throw std::system_error{
static_cast<int>(-operation.m_res),
std::system_category(),
"Accepting a connection failed: accept"
};
}
m_acceptingSocket = socket(operation.m_res, m_acceptingSocket.m_mq);
sockaddr* remoteSockaddr = reinterpret_cast<sockaddr*>(m_addressBuffer);
sockaddr* localSockaddr = reinterpret_cast<sockaddr*>(m_addressBuffer + sizeof(m_addressBuffer)/2);
socklen_t len = sizeof(m_addressBuffer) / 2;
getsockname(m_acceptingSocket.native_handle(), localSockaddr, &len);
m_acceptingSocket.m_localEndPoint =
detail::sockaddr_to_ip_endpoint(*localSockaddr);
m_acceptingSocket.m_remoteEndPoint =
detail::sockaddr_to_ip_endpoint(*remoteSockaddr);
}
#endif