forked from andreasbuhr/cppcoro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket_disconnect_operation.cpp
More file actions
147 lines (131 loc) · 4.01 KB
/
Copy pathsocket_disconnect_operation.cpp
File metadata and controls
147 lines (131 loc) · 4.01 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
///////////////////////////////////////////////////////////////////////////////
// Copyright (c) Lewis Baker
// Licenced under MIT license. See LICENSE.txt for details.
///////////////////////////////////////////////////////////////////////////////
#include <cppcoro/net/socket_disconnect_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>
bool cppcoro::net::socket_disconnect_operation_impl::try_start(
cppcoro::detail::win32_overlapped_operation_base& operation) noexcept
{
// Lookup the address of the DisconnectEx function pointer for this socket.
LPFN_DISCONNECTEX disconnectExPtr;
{
GUID disconnectExGuid = WSAID_DISCONNECTEX;
DWORD byteCount = 0;
const int result = ::WSAIoctl(
m_socket.native_handle(),
SIO_GET_EXTENSION_FUNCTION_POINTER,
static_cast<void*>(&disconnectExGuid),
sizeof(disconnectExGuid),
static_cast<void*>(&disconnectExPtr),
sizeof(disconnectExPtr),
&byteCount,
nullptr,
nullptr);
if (result == SOCKET_ERROR)
{
operation.m_errorCode = static_cast<DWORD>(::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();
// Need to add TF_REUSE_SOCKET to these flags if we want to allow reusing
// a socket for subsequent connections once the disconnect operation
// completes.
const DWORD flags = 0;
const BOOL ok = disconnectExPtr(
m_socket.native_handle(),
operation.get_overlapped(),
flags,
0);
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_disconnect_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_disconnect_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(),
"Disconnect operation failed: DisconnectEx"
};
}
}
#elif CPPCORO_OS_LINUX
# include <sys/socket.h>
# include <netinet/in.h>
# include <netinet/tcp.h>
# include <netinet/udp.h>
bool cppcoro::net::socket_disconnect_operation_impl::try_start(
cppcoro::detail::linux_async_operation_base& operation) noexcept
{
operation.m_completeFunc = [=]() {
operation.m_mq->remove_fd_watch(m_socket.native_handle());
int res = m_socket.close();
return res;
};
operation.m_mq->add_fd_watch(m_socket.native_handle(), reinterpret_cast<void*>(&operation), EPOLLOUT);
return true;
}
void cppcoro::net::socket_disconnect_operation_impl::cancel(
cppcoro::detail::linux_async_operation_base& operation) noexcept
{
operation.m_mq->remove_fd_watch(m_socket.native_handle());
}
void cppcoro::net::socket_disconnect_operation_impl::get_result(
cppcoro::detail::linux_async_operation_base& operation)
{
if (operation.m_res < 0)
{
if (operation.m_res == -cppcoro::detail::error_operation_cancelled)
{
throw operation_cancelled{};
}
throw std::system_error{
static_cast<int>(-operation.m_res),
std::system_category(),
"Disconnect operation failed: disconnect"
};
}
}
#endif