2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/utils/port/SocketFd.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2019-07-23 00:50:12 +02:00
|
|
|
#include "td/utils/format.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
2020-06-15 02:50:38 +02:00
|
|
|
#include "td/utils/port/detail/skip_eintr.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/port/PollFlags.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
#if TD_PORT_WINDOWS
|
|
|
|
#include "td/utils/buffer.h"
|
2018-09-11 16:43:43 +02:00
|
|
|
#include "td/utils/port/detail/Iocp.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/SpinLock.h"
|
|
|
|
#include "td/utils/VectorQueue.h"
|
2021-04-04 00:08:53 +02:00
|
|
|
|
|
|
|
#include <limits>
|
2018-09-07 02:41:21 +02:00
|
|
|
#endif
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#if TD_PORT_POSIX
|
2020-06-07 17:14:52 +02:00
|
|
|
#include <cerrno>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/tcp.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2018-09-10 03:08:15 +02:00
|
|
|
#include <atomic>
|
2018-09-07 02:41:21 +02:00
|
|
|
#include <cstring>
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
2018-08-13 19:15:09 +02:00
|
|
|
namespace detail {
|
|
|
|
#if TD_PORT_WINDOWS
|
2021-07-04 04:58:54 +02:00
|
|
|
class SocketFdImpl final : private Iocp::Callback {
|
2018-08-13 19:15:09 +02:00
|
|
|
public:
|
2021-12-10 11:07:55 +01:00
|
|
|
explicit SocketFdImpl(NativeFd native_fd) : info_(std::move(native_fd)) {
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " create from native_fd";
|
2018-08-13 19:15:09 +02:00
|
|
|
get_poll_info().add_flags(PollFlags::Write());
|
2018-09-11 16:43:43 +02:00
|
|
|
Iocp::get()->subscribe(get_native_fd(), this);
|
2018-08-13 19:15:09 +02:00
|
|
|
is_read_active_ = true;
|
|
|
|
notify_iocp_connected();
|
|
|
|
}
|
2018-09-10 15:52:27 +02:00
|
|
|
|
2021-12-10 11:07:55 +01:00
|
|
|
SocketFdImpl(NativeFd native_fd, const IPAddress &addr) : info_(std::move(native_fd)) {
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " create from native_fd and connect";
|
2018-08-13 19:15:09 +02:00
|
|
|
get_poll_info().add_flags(PollFlags::Write());
|
2018-09-11 16:43:43 +02:00
|
|
|
Iocp::get()->subscribe(get_native_fd(), this);
|
2018-08-13 19:15:09 +02:00
|
|
|
LPFN_CONNECTEX ConnectExPtr = nullptr;
|
|
|
|
GUID guid = WSAID_CONNECTEX;
|
|
|
|
DWORD numBytes;
|
|
|
|
auto error =
|
|
|
|
::WSAIoctl(get_native_fd().socket(), SIO_GET_EXTENSION_FUNCTION_POINTER, static_cast<void *>(&guid),
|
|
|
|
sizeof(guid), static_cast<void *>(&ConnectExPtr), sizeof(ConnectExPtr), &numBytes, nullptr, nullptr);
|
|
|
|
if (error) {
|
|
|
|
on_error(OS_SOCKET_ERROR("WSAIoctl failed"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::memset(&read_overlapped_, 0, sizeof(read_overlapped_));
|
|
|
|
inc_refcnt();
|
|
|
|
is_read_active_ = true;
|
|
|
|
auto status = ConnectExPtr(get_native_fd().socket(), addr.get_sockaddr(), narrow_cast<int>(addr.get_sockaddr_len()),
|
|
|
|
nullptr, 0, nullptr, &read_overlapped_);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-10 19:49:54 +02:00
|
|
|
if (status == TRUE || !check_status("Failed to connect")) {
|
2018-08-13 19:15:09 +02:00
|
|
|
is_read_active_ = false;
|
|
|
|
dec_refcnt();
|
|
|
|
}
|
|
|
|
}
|
2018-09-10 15:52:27 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void close() {
|
2021-12-16 21:38:44 +01:00
|
|
|
if (!is_write_waiting_ && is_connected_) {
|
2021-06-14 18:06:23 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " will close after ongoing write";
|
|
|
|
auto lock = lock_.lock();
|
2021-12-16 21:38:44 +01:00
|
|
|
if (!is_write_waiting_) {
|
|
|
|
need_close_after_write_ = true;
|
|
|
|
return;
|
|
|
|
}
|
2021-06-14 18:06:23 +02:00
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
notify_iocp_close();
|
|
|
|
}
|
2018-09-10 15:52:27 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
PollableFdInfo &get_poll_info() {
|
2021-12-10 11:07:55 +01:00
|
|
|
return info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
const PollableFdInfo &get_poll_info() const {
|
2021-12-10 11:07:55 +01:00
|
|
|
return info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const NativeFd &get_native_fd() const {
|
2021-12-10 11:07:55 +01:00
|
|
|
return info_.native_fd();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-09-10 15:52:27 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Result<size_t> write(Slice data) {
|
2019-07-01 15:18:28 +02:00
|
|
|
// LOG(ERROR) << "Write: " << format::as_hex_dump<0>(data);
|
2018-08-13 19:15:09 +02:00
|
|
|
output_writer_.append(data);
|
2019-07-06 13:29:15 +02:00
|
|
|
return write_finish(data.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> writev(Span<IoSlice> slices) {
|
|
|
|
size_t total_size = 0;
|
|
|
|
for (auto io_slice : slices) {
|
2021-04-03 23:40:37 +02:00
|
|
|
auto size = as_slice(io_slice).size();
|
|
|
|
CHECK(size <= std::numeric_limits<size_t>::max() - total_size);
|
|
|
|
total_size += size;
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto left_size = total_size;
|
|
|
|
for (auto io_slice : slices) {
|
|
|
|
auto slice = as_slice(io_slice);
|
|
|
|
output_writer_.append(slice, left_size);
|
|
|
|
left_size -= slice.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
return write_finish(total_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> write_finish(size_t total_size) {
|
2018-08-13 19:15:09 +02:00
|
|
|
if (is_write_waiting_) {
|
|
|
|
auto lock = lock_.lock();
|
|
|
|
is_write_waiting_ = false;
|
2019-07-06 13:29:15 +02:00
|
|
|
lock.reset();
|
2018-08-13 19:15:09 +02:00
|
|
|
notify_iocp_write();
|
|
|
|
}
|
2019-07-06 13:29:15 +02:00
|
|
|
return total_size;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-09-10 15:52:27 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Result<size_t> read(MutableSlice slice) {
|
2020-07-21 17:29:39 +02:00
|
|
|
if (get_poll_info().get_flags_local().has_pending_error()) {
|
2018-08-13 19:15:09 +02:00
|
|
|
TRY_STATUS(get_pending_error());
|
|
|
|
}
|
|
|
|
input_reader_.sync_with_writer();
|
2018-09-11 18:31:15 +02:00
|
|
|
auto res = input_reader_.advance(td::min(slice.size(), input_reader_.size()), slice);
|
2018-08-13 19:15:09 +02:00
|
|
|
if (res == 0) {
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
2019-07-01 15:18:28 +02:00
|
|
|
} else {
|
|
|
|
// LOG(ERROR) << "Read: " << format::as_hex_dump<0>(Slice(slice.substr(0, res)));
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2018-09-10 15:52:27 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Status get_pending_error() {
|
|
|
|
Status res;
|
|
|
|
{
|
|
|
|
auto lock = lock_.lock();
|
|
|
|
if (!pending_errors_.empty()) {
|
|
|
|
res = pending_errors_.pop();
|
|
|
|
}
|
|
|
|
if (res.is_ok()) {
|
|
|
|
get_poll_info().clear_flags(PollFlags::Error());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-12-10 11:07:55 +01:00
|
|
|
PollableFdInfo info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
SpinLock lock_;
|
|
|
|
|
|
|
|
std::atomic<int> refcnt_{1};
|
|
|
|
bool close_flag_{false};
|
2021-06-14 18:06:23 +02:00
|
|
|
bool need_close_after_write_{false};
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2021-12-16 21:38:44 +01:00
|
|
|
std::atomic<bool> is_connected_{false};
|
2018-08-13 19:15:09 +02:00
|
|
|
bool is_read_active_{false};
|
|
|
|
ChainBufferWriter input_writer_;
|
|
|
|
ChainBufferReader input_reader_ = input_writer_.extract_reader();
|
2018-09-10 19:49:54 +02:00
|
|
|
WSAOVERLAPPED read_overlapped_;
|
2018-08-13 19:15:09 +02:00
|
|
|
VectorQueue<Status> pending_errors_;
|
|
|
|
|
|
|
|
bool is_write_active_{false};
|
|
|
|
std::atomic<bool> is_write_waiting_{false};
|
|
|
|
ChainBufferWriter output_writer_;
|
|
|
|
ChainBufferReader output_reader_ = output_writer_.extract_reader();
|
2018-09-10 19:49:54 +02:00
|
|
|
WSAOVERLAPPED write_overlapped_;
|
2018-08-13 19:15:09 +02:00
|
|
|
|
|
|
|
char close_overlapped_;
|
|
|
|
|
2018-09-10 19:49:54 +02:00
|
|
|
bool check_status(Slice message) {
|
2018-09-07 02:41:21 +02:00
|
|
|
auto last_error = WSAGetLastError();
|
2018-08-13 19:15:09 +02:00
|
|
|
if (last_error == ERROR_IO_PENDING) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
on_error(OS_SOCKET_ERROR(message));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop_read() {
|
|
|
|
CHECK(is_connected_);
|
|
|
|
CHECK(!is_read_active_);
|
2021-06-14 18:06:23 +02:00
|
|
|
if (close_flag_ || need_close_after_write_) {
|
2018-08-13 19:15:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::memset(&read_overlapped_, 0, sizeof(read_overlapped_));
|
|
|
|
auto dest = input_writer_.prepare_append();
|
2018-09-10 19:49:54 +02:00
|
|
|
WSABUF buf;
|
|
|
|
buf.len = narrow_cast<ULONG>(dest.size());
|
|
|
|
buf.buf = dest.data();
|
|
|
|
DWORD flags = 0;
|
|
|
|
int status = WSARecv(get_native_fd().socket(), &buf, 1, nullptr, &flags, &read_overlapped_, nullptr);
|
|
|
|
if (status == 0 || check_status("Failed to read from connection")) {
|
2018-08-13 19:15:09 +02:00
|
|
|
inc_refcnt();
|
|
|
|
is_read_active_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop_write() {
|
|
|
|
CHECK(is_connected_);
|
|
|
|
CHECK(!is_write_active_);
|
|
|
|
|
|
|
|
output_reader_.sync_with_writer();
|
|
|
|
auto to_write = output_reader_.prepare_read();
|
|
|
|
if (to_write.empty()) {
|
|
|
|
auto lock = lock_.lock();
|
2019-07-06 13:29:15 +02:00
|
|
|
output_reader_.sync_with_writer();
|
2018-08-13 19:15:09 +02:00
|
|
|
to_write = output_reader_.prepare_read();
|
|
|
|
if (to_write.empty()) {
|
|
|
|
is_write_waiting_ = true;
|
2021-06-14 18:06:23 +02:00
|
|
|
if (need_close_after_write_) {
|
|
|
|
notify_iocp_close();
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (to_write.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
std::memset(&write_overlapped_, 0, sizeof(write_overlapped_));
|
2020-06-22 01:40:05 +02:00
|
|
|
constexpr size_t BUF_SIZE = 20;
|
|
|
|
WSABUF buf[BUF_SIZE];
|
2019-07-06 13:29:15 +02:00
|
|
|
auto it = output_reader_.clone();
|
|
|
|
size_t buf_i;
|
2020-06-22 01:40:05 +02:00
|
|
|
for (buf_i = 0; buf_i < BUF_SIZE; buf_i++) {
|
2019-07-06 13:29:15 +02:00
|
|
|
auto src = it.prepare_read();
|
|
|
|
if (src.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buf[buf_i].len = narrow_cast<ULONG>(src.size());
|
|
|
|
buf[buf_i].buf = const_cast<CHAR *>(src.data());
|
|
|
|
it.confirm_read(src.size());
|
|
|
|
}
|
|
|
|
int status =
|
|
|
|
WSASend(get_native_fd().socket(), buf, narrow_cast<DWORD>(buf_i), nullptr, 0, &write_overlapped_, nullptr);
|
2018-09-10 19:49:54 +02:00
|
|
|
if (status == 0 || check_status("Failed to write to connection")) {
|
2018-08-13 19:15:09 +02:00
|
|
|
inc_refcnt();
|
|
|
|
is_write_active_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_iocp(Result<size_t> r_size, WSAOVERLAPPED *overlapped) final {
|
2018-08-13 19:15:09 +02:00
|
|
|
// called from other thread
|
|
|
|
if (dec_refcnt() || close_flag_) {
|
2018-12-17 17:12:47 +01:00
|
|
|
VLOG(fd) << "Ignore IOCP (socket is closing)";
|
2018-08-13 19:15:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (r_size.is_error()) {
|
2018-12-17 17:12:47 +01:00
|
|
|
return on_error(get_socket_pending_error(get_native_fd(), overlapped, r_size.move_as_error()));
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!is_connected_ && overlapped == &read_overlapped_) {
|
|
|
|
return on_connected();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto size = r_size.move_as_ok();
|
|
|
|
if (overlapped == &write_overlapped_) {
|
|
|
|
return on_write(size);
|
|
|
|
}
|
|
|
|
if (overlapped == nullptr) {
|
|
|
|
CHECK(size == 0);
|
|
|
|
return on_write(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlapped == &read_overlapped_) {
|
|
|
|
return on_read(size);
|
|
|
|
}
|
2018-09-10 19:49:54 +02:00
|
|
|
if (overlapped == reinterpret_cast<WSAOVERLAPPED *>(&close_overlapped_)) {
|
2018-08-13 19:15:09 +02:00
|
|
|
return on_close();
|
|
|
|
}
|
2021-12-10 11:27:24 +01:00
|
|
|
LOG(ERROR) << this << ' ' << overlapped << ' ' << &read_overlapped_ << ' ' << &write_overlapped_ << ' '
|
|
|
|
<< reinterpret_cast<WSAOVERLAPPED *>(&close_overlapped_) << ' ' << size;
|
|
|
|
LOG(FATAL) << get_native_fd() << ' ' << info_.get_flags_local() << ' ' << refcnt_.load() << ' ' << close_flag_
|
|
|
|
<< ' ' << need_close_after_write_ << ' ' << is_connected_ << ' ' << is_read_active_ << ' '
|
|
|
|
<< is_write_active_ << ' ' << is_write_waiting_.load() << ' ' << input_reader_.size() << ' '
|
|
|
|
<< output_reader_.size();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) {
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " on error " << status;
|
2018-08-13 19:15:09 +02:00
|
|
|
{
|
|
|
|
auto lock = lock_.lock();
|
|
|
|
pending_errors_.push(std::move(status));
|
|
|
|
}
|
|
|
|
get_poll_info().add_flags_from_poll(PollFlags::Error());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_connected() {
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " on connected";
|
2018-08-13 19:15:09 +02:00
|
|
|
CHECK(!is_connected_);
|
|
|
|
CHECK(is_read_active_);
|
|
|
|
is_connected_ = true;
|
|
|
|
is_read_active_ = false;
|
|
|
|
loop_read();
|
|
|
|
loop_write();
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_read(size_t size) {
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " on read " << size;
|
2018-08-13 19:15:09 +02:00
|
|
|
CHECK(is_read_active_);
|
|
|
|
is_read_active_ = false;
|
2018-09-11 18:31:15 +02:00
|
|
|
if (size == 0) {
|
|
|
|
get_poll_info().add_flags_from_poll(PollFlags::Close());
|
|
|
|
return;
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
input_writer_.confirm_append(size);
|
|
|
|
get_poll_info().add_flags_from_poll(PollFlags::Read());
|
|
|
|
loop_read();
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_write(size_t size) {
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " on write " << size;
|
2018-08-13 19:15:09 +02:00
|
|
|
if (size == 0) {
|
|
|
|
if (is_write_active_) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
is_write_active_ = true;
|
|
|
|
}
|
|
|
|
CHECK(is_write_active_);
|
|
|
|
is_write_active_ = false;
|
2019-07-06 13:29:15 +02:00
|
|
|
output_reader_.advance(size);
|
2018-08-13 19:15:09 +02:00
|
|
|
loop_write();
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_close() {
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " on close";
|
2018-08-13 19:15:09 +02:00
|
|
|
close_flag_ = true;
|
2021-12-10 11:07:55 +01:00
|
|
|
info_.set_native_fd({});
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
bool dec_refcnt() {
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " dec_refcnt from " << refcnt_;
|
2018-08-13 19:15:09 +02:00
|
|
|
if (--refcnt_ == 0) {
|
|
|
|
delete this;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
void inc_refcnt() {
|
|
|
|
CHECK(refcnt_ != 0);
|
|
|
|
refcnt_++;
|
2018-09-11 18:44:37 +02:00
|
|
|
VLOG(fd) << get_native_fd() << " inc_refcnt to " << refcnt_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void notify_iocp_write() {
|
|
|
|
inc_refcnt();
|
2018-09-11 16:43:43 +02:00
|
|
|
Iocp::get()->post(0, this, nullptr);
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
void notify_iocp_close() {
|
2018-09-11 16:43:43 +02:00
|
|
|
Iocp::get()->post(0, this, reinterpret_cast<WSAOVERLAPPED *>(&close_overlapped_));
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
void notify_iocp_connected() {
|
|
|
|
inc_refcnt();
|
2018-09-11 16:43:43 +02:00
|
|
|
Iocp::get()->post(0, this, &read_overlapped_);
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void SocketFdImplDeleter::operator()(SocketFdImpl *impl) {
|
|
|
|
impl->close();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
class InitWSA {
|
|
|
|
public:
|
|
|
|
InitWSA() {
|
|
|
|
/* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */
|
|
|
|
WORD wVersionRequested = MAKEWORD(2, 2);
|
|
|
|
WSADATA wsaData;
|
|
|
|
if (WSAStartup(wVersionRequested, &wsaData) != 0) {
|
|
|
|
auto error = OS_SOCKET_ERROR("Failed to init WSA");
|
|
|
|
LOG(FATAL) << error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
static InitWSA init_wsa;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
#else
|
|
|
|
class SocketFdImpl {
|
|
|
|
public:
|
2021-12-10 11:07:55 +01:00
|
|
|
PollableFdInfo info_;
|
|
|
|
explicit SocketFdImpl(NativeFd fd) : info_(std::move(fd)) {
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
PollableFdInfo &get_poll_info() {
|
2021-12-10 11:07:55 +01:00
|
|
|
return info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
const PollableFdInfo &get_poll_info() const {
|
2021-12-10 11:07:55 +01:00
|
|
|
return info_;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
const NativeFd &get_native_fd() const {
|
2021-12-10 11:07:55 +01:00
|
|
|
return info_.native_fd();
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2020-10-18 17:12:32 +02:00
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
Result<size_t> writev(Span<IoSlice> slices) {
|
|
|
|
int native_fd = get_native_fd().socket();
|
2020-10-18 17:12:32 +02:00
|
|
|
TRY_RESULT(slices_size, narrow_cast_safe<int>(slices.size()));
|
|
|
|
auto write_res = detail::skip_eintr([&] {
|
2021-04-08 13:55:54 +02:00
|
|
|
// sendmsg can erroneously return 2^32 - 1 on Android 5.1 and Android 6.0, so it must not be used there
|
2021-04-08 21:17:55 +02:00
|
|
|
#if defined(MSG_NOSIGNAL) && !TD_ANDROID
|
2020-10-18 17:12:32 +02:00
|
|
|
msghdr msg;
|
2020-11-01 22:43:57 +01:00
|
|
|
std::memset(&msg, 0, sizeof(msg));
|
2020-10-18 17:12:32 +02:00
|
|
|
msg.msg_iov = const_cast<iovec *>(slices.begin());
|
|
|
|
msg.msg_iovlen = slices_size;
|
|
|
|
return sendmsg(native_fd, &msg, MSG_NOSIGNAL);
|
|
|
|
#else
|
|
|
|
return ::writev(native_fd, slices.begin(), slices_size);
|
|
|
|
#endif
|
|
|
|
});
|
2021-04-04 00:08:53 +02:00
|
|
|
if (write_res >= 0) {
|
|
|
|
auto result = narrow_cast<size_t>(write_res);
|
|
|
|
auto left = result;
|
|
|
|
for (const auto &slice : slices) {
|
|
|
|
if (left <= slice.iov_len) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
left -= slice.iov_len;
|
|
|
|
}
|
2021-04-08 13:55:54 +02:00
|
|
|
LOG(FATAL) << "Receive " << write_res << " as writev response, but tried to write only " << result - left
|
2021-04-04 00:08:53 +02:00
|
|
|
<< " bytes";
|
|
|
|
}
|
|
|
|
return write_finish();
|
2019-07-06 13:29:15 +02:00
|
|
|
}
|
2020-10-18 17:12:32 +02:00
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
Result<size_t> write(Slice slice) {
|
2018-09-11 15:27:20 +02:00
|
|
|
int native_fd = get_native_fd().socket();
|
2020-10-18 17:12:32 +02:00
|
|
|
auto write_res = detail::skip_eintr([&] {
|
|
|
|
return
|
|
|
|
#ifdef MSG_NOSIGNAL
|
|
|
|
send(native_fd, slice.begin(), slice.size(), MSG_NOSIGNAL);
|
|
|
|
#else
|
|
|
|
::write(native_fd, slice.begin(), slice.size());
|
|
|
|
#endif
|
|
|
|
});
|
2018-08-13 19:15:09 +02:00
|
|
|
if (write_res >= 0) {
|
2021-04-04 00:08:53 +02:00
|
|
|
auto result = narrow_cast<size_t>(write_res);
|
2021-04-08 13:55:54 +02:00
|
|
|
LOG_CHECK(result <= slice.size()) << "Receive " << write_res << " as write response, but tried to write only "
|
2021-04-04 00:08:53 +02:00
|
|
|
<< slice.size() << " bytes";
|
|
|
|
return result;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2021-04-04 00:08:53 +02:00
|
|
|
return write_finish();
|
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2021-04-04 00:08:53 +02:00
|
|
|
Result<size_t> write_finish() {
|
|
|
|
auto write_errno = errno;
|
2018-08-13 19:15:09 +02:00
|
|
|
if (write_errno == EAGAIN
|
|
|
|
#if EAGAIN != EWOULDBLOCK
|
|
|
|
|| write_errno == EWOULDBLOCK
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
get_poll_info().clear_flags(PollFlags::Write());
|
|
|
|
return 0;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-07 01:47:50 +02:00
|
|
|
auto error = Status::PosixError(write_errno, PSLICE() << "Write to " << get_native_fd() << " has failed");
|
2018-08-13 19:15:09 +02:00
|
|
|
switch (write_errno) {
|
|
|
|
case EBADF:
|
|
|
|
case ENXIO:
|
|
|
|
case EFAULT:
|
|
|
|
case EINVAL:
|
|
|
|
LOG(FATAL) << error;
|
|
|
|
UNREACHABLE();
|
|
|
|
default:
|
|
|
|
LOG(WARNING) << error;
|
|
|
|
// fallthrough
|
|
|
|
case ECONNRESET:
|
|
|
|
case EDQUOT:
|
|
|
|
case EFBIG:
|
|
|
|
case EIO:
|
|
|
|
case ENETDOWN:
|
|
|
|
case ENETUNREACH:
|
|
|
|
case ENOSPC:
|
|
|
|
case EPIPE:
|
|
|
|
get_poll_info().clear_flags(PollFlags::Write());
|
|
|
|
get_poll_info().add_flags(PollFlags::Close());
|
|
|
|
return std::move(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Result<size_t> read(MutableSlice slice) {
|
2020-07-21 17:29:39 +02:00
|
|
|
if (get_poll_info().get_flags_local().has_pending_error()) {
|
2018-08-13 19:15:09 +02:00
|
|
|
TRY_STATUS(get_pending_error());
|
|
|
|
}
|
2018-09-11 15:27:20 +02:00
|
|
|
int native_fd = get_native_fd().socket();
|
2021-10-18 14:37:21 +02:00
|
|
|
CHECK(!slice.empty());
|
2018-09-10 01:16:42 +02:00
|
|
|
auto read_res = detail::skip_eintr([&] { return ::read(native_fd, slice.begin(), slice.size()); });
|
2018-08-13 19:15:09 +02:00
|
|
|
auto read_errno = errno;
|
|
|
|
if (read_res >= 0) {
|
|
|
|
if (read_res == 0) {
|
|
|
|
errno = 0;
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
get_poll_info().add_flags(PollFlags::Close());
|
|
|
|
}
|
2021-04-04 00:08:53 +02:00
|
|
|
auto result = narrow_cast<size_t>(read_res);
|
|
|
|
CHECK(result <= slice.size());
|
|
|
|
return result;
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
if (read_errno == EAGAIN
|
|
|
|
#if EAGAIN != EWOULDBLOCK
|
|
|
|
|| read_errno == EWOULDBLOCK
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
return 0;
|
|
|
|
}
|
2018-10-07 01:47:50 +02:00
|
|
|
auto error = Status::PosixError(read_errno, PSLICE() << "Read from " << get_native_fd() << " has failed");
|
2018-08-13 19:15:09 +02:00
|
|
|
switch (read_errno) {
|
|
|
|
case EISDIR:
|
|
|
|
case EBADF:
|
|
|
|
case ENXIO:
|
|
|
|
case EFAULT:
|
|
|
|
case EINVAL:
|
|
|
|
LOG(FATAL) << error;
|
|
|
|
UNREACHABLE();
|
|
|
|
default:
|
|
|
|
LOG(WARNING) << error;
|
|
|
|
// fallthrough
|
|
|
|
case ENOTCONN:
|
|
|
|
case EIO:
|
|
|
|
case ENOBUFS:
|
|
|
|
case ENOMEM:
|
|
|
|
case ECONNRESET:
|
|
|
|
case ETIMEDOUT:
|
|
|
|
get_poll_info().clear_flags(PollFlags::Read());
|
|
|
|
get_poll_info().add_flags(PollFlags::Close());
|
|
|
|
return std::move(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Status get_pending_error() {
|
2020-07-21 17:29:39 +02:00
|
|
|
if (!get_poll_info().get_flags_local().has_pending_error()) {
|
2018-08-13 19:15:09 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
TRY_STATUS(detail::get_socket_pending_error(get_native_fd()));
|
|
|
|
get_poll_info().clear_flags(PollFlags::Error());
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void SocketFdImplDeleter::operator()(SocketFdImpl *impl) {
|
|
|
|
delete impl;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
|
2018-12-17 17:12:47 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
2018-08-13 19:15:09 +02:00
|
|
|
Status get_socket_pending_error(const NativeFd &fd) {
|
|
|
|
int error = 0;
|
|
|
|
socklen_t errlen = sizeof(error);
|
|
|
|
if (getsockopt(fd.socket(), SOL_SOCKET, SO_ERROR, static_cast<void *>(&error), &errlen) == 0) {
|
|
|
|
if (error == 0) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2018-12-17 17:12:47 +01:00
|
|
|
return Status::PosixError(error, PSLICE() << "Error on " << fd);
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
2018-10-07 01:47:50 +02:00
|
|
|
auto status = OS_SOCKET_ERROR(PSLICE() << "Can't load error on socket " << fd);
|
2018-08-13 19:15:09 +02:00
|
|
|
LOG(INFO) << "Can't load pending socket error: " << status;
|
|
|
|
return status;
|
|
|
|
}
|
2018-12-17 17:12:47 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
Status get_socket_pending_error(const NativeFd &fd, WSAOVERLAPPED *overlapped, Status iocp_error) {
|
|
|
|
// We need to call WSAGetOverlappedResult() just so WSAGetLastError() will return the correct error. See
|
|
|
|
// https://stackoverflow.com/questions/28925003/calling-wsagetlasterror-from-an-iocp-thread-return-incorrect-result
|
|
|
|
DWORD num_bytes = 0;
|
|
|
|
DWORD flags = 0;
|
2019-04-26 04:15:26 +02:00
|
|
|
BOOL success = WSAGetOverlappedResult(fd.socket(), overlapped, &num_bytes, false, &flags);
|
2018-12-17 17:12:47 +01:00
|
|
|
if (success) {
|
|
|
|
LOG(ERROR) << "WSAGetOverlappedResult succeded after " << iocp_error;
|
|
|
|
return iocp_error;
|
|
|
|
}
|
|
|
|
return OS_SOCKET_ERROR(PSLICE() << "Error on " << fd);
|
|
|
|
}
|
2018-09-10 01:51:58 +02:00
|
|
|
#endif
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-10 01:51:58 +02:00
|
|
|
Status init_socket_options(NativeFd &native_fd) {
|
2018-09-10 16:05:12 +02:00
|
|
|
TRY_STATUS(native_fd.set_is_blocking_unsafe(false));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-10 01:51:58 +02:00
|
|
|
auto sock = native_fd.socket();
|
2018-12-31 20:04:05 +01:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
int flags = 1;
|
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
BOOL flags = TRUE;
|
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<const char *>(&flags), sizeof(flags));
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast<const char *>(&flags), sizeof(flags));
|
|
|
|
setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, reinterpret_cast<const char *>(&flags), sizeof(flags));
|
2020-10-18 17:12:32 +02:00
|
|
|
#if TD_PORT_POSIX
|
|
|
|
#ifndef MSG_NOSIGNAL // Darwin
|
|
|
|
|
|
|
|
#ifdef SO_NOSIGPIPE
|
|
|
|
setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, reinterpret_cast<const char *>(&flags), sizeof(flags));
|
|
|
|
#else
|
|
|
|
#warning "Failed to suppress SIGPIPE signals. Use signal(SIGPIPE, SIG_IGN) to suppress them."
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#endif
|
2018-08-13 19:15:09 +02:00
|
|
|
// TODO: SO_REUSEADDR, SO_KEEPALIVE, TCP_NODELAY, SO_SNDBUF, SO_RCVBUF, TCP_QUICKACK, SO_LINGER
|
|
|
|
|
2018-09-10 01:51:58 +02:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
SocketFd::SocketFd() = default;
|
2021-10-18 13:36:15 +02:00
|
|
|
SocketFd::SocketFd(SocketFd &&) noexcept = default;
|
|
|
|
SocketFd &SocketFd::operator=(SocketFd &&) noexcept = default;
|
2018-09-10 01:51:58 +02:00
|
|
|
SocketFd::~SocketFd() = default;
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
SocketFd::SocketFd(unique_ptr<detail::SocketFdImpl> impl) : impl_(impl.release()) {
|
2018-09-10 01:51:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<SocketFd> SocketFd::from_native_fd(NativeFd fd) {
|
|
|
|
TRY_STATUS(detail::init_socket_options(fd));
|
2018-09-27 03:19:03 +02:00
|
|
|
return SocketFd(make_unique<detail::SocketFdImpl>(std::move(fd)));
|
2018-08-13 19:15:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<SocketFd> SocketFd::open(const IPAddress &address) {
|
2021-09-24 12:41:22 +02:00
|
|
|
#if TD_DARWIN_WATCH_OS
|
2021-03-26 16:04:01 +01:00
|
|
|
return SocketFd{};
|
|
|
|
#endif
|
|
|
|
|
2018-12-17 20:09:01 +01:00
|
|
|
NativeFd native_fd{socket(address.get_address_family(), SOCK_STREAM, IPPROTO_TCP)};
|
2018-08-13 19:15:09 +02:00
|
|
|
if (!native_fd) {
|
|
|
|
return OS_SOCKET_ERROR("Failed to create a socket");
|
|
|
|
}
|
2018-09-10 01:51:58 +02:00
|
|
|
TRY_STATUS(detail::init_socket_options(native_fd));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
#if TD_PORT_POSIX
|
2018-09-11 16:43:43 +02:00
|
|
|
int e_connect =
|
|
|
|
connect(native_fd.socket(), address.get_sockaddr(), narrow_cast<socklen_t>(address.get_sockaddr_len()));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (e_connect == -1) {
|
|
|
|
auto connect_errno = errno;
|
|
|
|
if (connect_errno != EINPROGRESS) {
|
|
|
|
return Status::PosixError(connect_errno, PSLICE() << "Failed to connect to " << address);
|
|
|
|
}
|
|
|
|
}
|
2018-09-27 03:19:03 +02:00
|
|
|
return SocketFd(make_unique<detail::SocketFdImpl>(std::move(native_fd)));
|
2018-12-31 20:04:05 +01:00
|
|
|
#elif TD_PORT_WINDOWS
|
|
|
|
auto bind_addr = address.get_any_addr();
|
2018-09-10 01:51:58 +02:00
|
|
|
auto e_bind = bind(native_fd.socket(), bind_addr.get_sockaddr(), narrow_cast<int>(bind_addr.get_sockaddr_len()));
|
2018-12-31 20:04:05 +01:00
|
|
|
if (e_bind != 0) {
|
|
|
|
return OS_SOCKET_ERROR("Failed to bind a socket");
|
|
|
|
}
|
2018-09-27 03:19:03 +02:00
|
|
|
return SocketFd(make_unique<detail::SocketFdImpl>(std::move(native_fd), address));
|
2018-12-31 20:04:05 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
void SocketFd::close() {
|
|
|
|
impl_.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
bool SocketFd::empty() const {
|
|
|
|
return !impl_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
PollableFdInfo &SocketFd::get_poll_info() {
|
2021-03-27 09:02:32 +01:00
|
|
|
CHECK(!empty());
|
2018-08-13 19:15:09 +02:00
|
|
|
return impl_->get_poll_info();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-08-13 19:15:09 +02:00
|
|
|
const PollableFdInfo &SocketFd::get_poll_info() const {
|
2021-03-27 09:02:32 +01:00
|
|
|
CHECK(!empty());
|
2018-08-13 19:15:09 +02:00
|
|
|
return impl_->get_poll_info();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 19:15:09 +02:00
|
|
|
const NativeFd &SocketFd::get_native_fd() const {
|
2021-03-27 09:02:32 +01:00
|
|
|
CHECK(!empty());
|
2018-08-13 19:15:09 +02:00
|
|
|
return impl_->get_native_fd();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Status SocketFd::get_pending_error() {
|
2021-03-27 09:02:32 +01:00
|
|
|
CHECK(!empty());
|
2018-08-13 19:15:09 +02:00
|
|
|
return impl_->get_pending_error();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result<size_t> SocketFd::write(Slice slice) {
|
2021-03-27 09:02:32 +01:00
|
|
|
CHECK(!empty());
|
2018-08-13 19:15:09 +02:00
|
|
|
return impl_->write(slice);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2019-07-06 13:29:15 +02:00
|
|
|
Result<size_t> SocketFd::writev(Span<IoSlice> slices) {
|
2021-03-27 09:02:32 +01:00
|
|
|
CHECK(!empty());
|
2019-07-06 13:29:15 +02:00
|
|
|
return impl_->writev(slices);
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
Result<size_t> SocketFd::read(MutableSlice slice) {
|
2021-03-27 09:02:32 +01:00
|
|
|
CHECK(!empty());
|
2018-08-13 19:15:09 +02:00
|
|
|
return impl_->read(slice);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|