forked from lewissbaker/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket_connect_operation.cpp
More file actions
178 lines (161 loc) · 4.86 KB
/
Copy pathsocket_connect_operation.cpp
File metadata and controls
178 lines (161 loc) · 4.86 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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/net/socket_connect_operation.hpp>
#include <cppcoro/net/socket.hpp>
#include <cppcoro/operation_cancelled.hpp>
#include "socket_helpers.hpp"
#include <cassert>
#include <system_error>
#if CPPCORO_OS_WINNT
# include <WinSock2.h>
# include <WS2tcpip.h>
# include <MSWSock.h>
# include <Windows.h>
bool cppcoro::net::socket_connect_operation_impl::try_start(
cppcoro::detail::win32_overlapped_operation_base& operation) noexcept
{
// Lookup the address of the ConnectEx function pointer for this socket.
LPFN_CONNECTEX connectExPtr;
{
GUID connectExGuid = WSAID_CONNECTEX;
DWORD byteCount = 0;
int result = ::WSAIoctl(
m_socket.native_handle(),
SIO_GET_EXTENSION_FUNCTION_POINTER,
static_cast<void*>(&connectExGuid),
sizeof(connectExGuid),
static_cast<void*>(&connectExPtr),
sizeof(connectExPtr),
&byteCount,
nullptr,
nullptr);
if (result == SOCKET_ERROR)
{
operation.m_errorCode = ::WSAGetLastError();
return false;
}
}
// 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_socket.skip_completion_on_success();
SOCKADDR_STORAGE remoteSockaddrStorage;
const int sockaddrNameLength = cppcoro::net::detail::ip_endpoint_to_sockaddr(
m_remoteEndPoint,
std::ref(remoteSockaddrStorage));
DWORD bytesSent = 0;
const BOOL ok = connectExPtr(
m_socket.native_handle(),
reinterpret_cast<const SOCKADDR*>(&remoteSockaddrStorage),
sockaddrNameLength,
nullptr, // send buffer
0, // size of send buffer
&bytesSent,
operation.get_overlapped());
if (!ok)
{
const int errorCode = ::WSAGetLastError();
if (errorCode != ERROR_IO_PENDING)
{
// Failed synchronously.
operation.m_errorCode = static_cast<DWORD>(errorCode);
return false;
}
}
else if (skipCompletionOnSuccess)
{
// Successfully completed synchronously and no completion event
// will be posted to an I/O thread so we can return without suspending.
operation.m_errorCode = ERROR_SUCCESS;
return false;
}
return true;
}
void cppcoro::net::socket_connect_operation_impl::cancel(
cppcoro::detail::win32_overlapped_operation_base& operation) noexcept
{
(void)::CancelIoEx(
reinterpret_cast<HANDLE>(m_socket.native_handle()),
operation.get_overlapped());
}
void cppcoro::net::socket_connect_operation_impl::get_result(
cppcoro::detail::win32_overlapped_operation_base& operation)
{
if (operation.m_errorCode != ERROR_SUCCESS)
{
if (operation.m_errorCode == ERROR_OPERATION_ABORTED)
{
throw operation_cancelled{};
}
throw std::system_error{
static_cast<int>(operation.m_errorCode),
std::system_category(),
"Connect operation failed: ConnectEx"
};
}
// We need to call setsockopt() to update the socket state with information
// about the connection now that it has been successfully connected.
{
const int result = ::setsockopt(
m_socket.native_handle(),
SOL_SOCKET,
SO_UPDATE_CONNECT_CONTEXT,
nullptr,
0);
if (result == SOCKET_ERROR)
{
// This shouldn't fail, but just in case it does we fall back to
// setting the remote address as specified in the call to Connect().
//
// Don't really want to throw an exception here since the connection
// has actually been established.
m_socket.m_remoteEndPoint = m_remoteEndPoint;
return;
}
}
{
SOCKADDR_STORAGE localSockaddr;
int nameLength = sizeof(localSockaddr);
const int result = ::getsockname(
m_socket.native_handle(),
reinterpret_cast<SOCKADDR*>(&localSockaddr),
&nameLength);
if (result == 0)
{
m_socket.m_localEndPoint = cppcoro::net::detail::sockaddr_to_ip_endpoint(
*reinterpret_cast<const SOCKADDR*>(&localSockaddr));
}
else
{
// Failed to get the updated local-end-point
// Just leave m_localEndPoint set to whatever bind() left it as.
//
// TODO: Should we be throwing an exception here instead?
}
}
{
SOCKADDR_STORAGE remoteSockaddr;
int nameLength = sizeof(remoteSockaddr);
const int result = ::getpeername(
m_socket.native_handle(),
reinterpret_cast<SOCKADDR*>(&remoteSockaddr),
&nameLength);
if (result == 0)
{
m_socket.m_remoteEndPoint = cppcoro::net::detail::sockaddr_to_ip_endpoint(
*reinterpret_cast<const SOCKADDR*>(&remoteSockaddr));
}
else
{
// Failed to get the actual remote end-point so just fall back to
// remembering the actual end-point that was passed to connect().
//
// TODO: Should we be throwing an exception here instead?
m_socket.m_remoteEndPoint = m_remoteEndPoint;
}
}
}
#endif