From fd90bf435e0e04f267a4d8faf788d69a8e80da97 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 7 Sep 2018 03:41:21 +0300 Subject: [PATCH] A lot of fixes. GitOrigin-RevId: c7c16991da51e09a685537a444385852e8e93af4 --- benchmark/bench_http_server_cheat.cpp | 2 +- benchmark/bench_http_server_fast.cpp | 2 +- td/mtproto/HandshakeConnection.h | 2 +- td/mtproto/HttpTransport.h | 2 +- td/mtproto/IStreamTransport.h | 2 +- td/mtproto/PingConnection.h | 2 +- td/mtproto/RawConnection.h | 2 +- td/mtproto/SessionConnection.h | 2 +- td/mtproto/TcpTransport.h | 2 +- td/telegram/Client.cpp | 2 +- td/telegram/cli.cpp | 3 +- td/telegram/files/FileHashUploader.cpp | 2 +- tdactor/td/actor/impl/ConcurrentScheduler.cpp | 21 ++++--- tdactor/td/actor/impl/Scheduler-decl.h | 2 +- tdactor/td/actor/impl/Scheduler.h | 4 +- tdactor/test/actors_simple.cpp | 6 +- tddb/td/db/binlog/Binlog.cpp | 1 - tdnet/td/net/HttpConnectionBase.cpp | 2 +- tdnet/td/net/TcpListener.cpp | 2 +- tdnet/td/net/TransparentProxy.cpp | 2 +- tdutils/CMakeLists.txt | 13 ++-- tdutils/td/utils/BufferedUdp.cpp | 6 +- tdutils/td/utils/BufferedUdp.h | 34 ++++++----- tdutils/td/utils/Context.h | 1 + tdutils/td/utils/DecTree.h | 59 ++++++++++--------- tdutils/td/utils/Destructor.h | 6 ++ tdutils/td/utils/FileLog.cpp | 2 +- tdutils/td/utils/MpscPollableQueue.h | 5 +- tdutils/td/utils/Random.cpp | 21 +++---- tdutils/td/utils/Random.h | 2 +- tdutils/td/utils/SharedObjectPool.h | 4 +- tdutils/td/utils/Slice.h | 2 +- tdutils/td/utils/Span.h | 20 ++++--- tdutils/td/utils/VectorQueue.h | 16 +++-- tdutils/td/utils/buffer.h | 6 +- tdutils/td/utils/int_types.h | 25 ++++---- tdutils/td/utils/invoke.h | 5 -- tdutils/td/utils/logging.cpp | 8 +-- tdutils/td/utils/logging.h | 22 +++---- tdutils/td/utils/misc.h | 8 ++- tdutils/td/utils/overloaded.h | 4 +- tdutils/td/utils/port/Fd.h | 1 - tdutils/td/utils/port/FileFd.cpp | 11 ++-- tdutils/td/utils/port/FileFd.h | 8 +-- tdutils/td/utils/port/IPAddress.cpp | 3 +- tdutils/td/utils/port/IPAddress.h | 2 +- tdutils/td/utils/port/PollBase.h | 1 - tdutils/td/utils/port/ServerSocketFd.cpp | 26 ++++---- tdutils/td/utils/port/ServerSocketFd.h | 3 +- tdutils/td/utils/port/SocketFd.cpp | 23 ++++---- tdutils/td/utils/port/SocketFd.h | 4 +- tdutils/td/utils/port/StdStreams.cpp | 4 +- tdutils/td/utils/port/StdStreams.h | 1 + tdutils/td/utils/port/UdpSocketFd.cpp | 54 ++++++----------- tdutils/td/utils/port/UdpSocketFd.h | 7 ++- tdutils/td/utils/port/detail/Epoll.h | 2 +- tdutils/td/utils/port/detail/EventFdBsd.cpp | 3 +- tdutils/td/utils/port/detail/EventFdBsd.h | 1 + tdutils/td/utils/port/detail/EventFdLinux.cpp | 5 +- tdutils/td/utils/port/detail/EventFdLinux.h | 2 +- tdutils/td/utils/port/detail/EventFdWindows.h | 3 +- tdutils/td/utils/port/detail/KQueue.h | 2 +- tdutils/td/utils/port/detail/NativeFd.cpp | 27 ++++++--- tdutils/td/utils/port/detail/NativeFd.h | 15 +++-- tdutils/td/utils/port/detail/Poll.h | 2 +- tdutils/td/utils/port/detail/PollableFd.h | 14 +++-- tdutils/td/utils/port/detail/Select.h | 1 + tdutils/td/utils/port/detail/ThreadPthread.h | 1 + tdutils/td/utils/port/detail/WineventPoll.cpp | 8 +-- tdutils/td/utils/port/detail/WineventPoll.h | 5 +- tdutils/td/utils/port/path.cpp | 2 - tdutils/td/utils/port/thread_local.h | 2 +- tdutils/td/utils/type_traits.h | 6 +- tdutils/test/buffer.cpp | 1 - tdutils/test/misc.cpp | 2 +- tdutils/test/port.cpp | 4 +- test/http.cpp | 2 +- 77 files changed, 318 insertions(+), 274 deletions(-) diff --git a/benchmark/bench_http_server_cheat.cpp b/benchmark/bench_http_server_cheat.cpp index c096261ee..0c9824b3e 100644 --- a/benchmark/bench_http_server_cheat.cpp +++ b/benchmark/bench_http_server_cheat.cpp @@ -12,7 +12,7 @@ #include "td/utils/buffer.h" #include "td/utils/logging.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/SocketFd.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" diff --git a/benchmark/bench_http_server_fast.cpp b/benchmark/bench_http_server_fast.cpp index c3511c046..c97d978ef 100644 --- a/benchmark/bench_http_server_fast.cpp +++ b/benchmark/bench_http_server_fast.cpp @@ -14,7 +14,7 @@ #include "td/utils/buffer.h" #include "td/utils/BufferedFd.h" #include "td/utils/logging.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/SocketFd.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" diff --git a/td/mtproto/HandshakeConnection.h b/td/mtproto/HandshakeConnection.h index 08fe98cdc..f01f52e3f 100644 --- a/td/mtproto/HandshakeConnection.h +++ b/td/mtproto/HandshakeConnection.h @@ -17,7 +17,7 @@ #include "td/utils/common.h" #include "td/utils/format.h" #include "td/utils/logging.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/Status.h" namespace td { diff --git a/td/mtproto/HttpTransport.h b/td/mtproto/HttpTransport.h index e625fe35e..3afc6d029 100644 --- a/td/mtproto/HttpTransport.h +++ b/td/mtproto/HttpTransport.h @@ -12,7 +12,7 @@ #include "td/net/HttpReader.h" #include "td/utils/buffer.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/Status.h" namespace td { diff --git a/td/mtproto/IStreamTransport.h b/td/mtproto/IStreamTransport.h index e69c7724f..c38ab4e05 100644 --- a/td/mtproto/IStreamTransport.h +++ b/td/mtproto/IStreamTransport.h @@ -7,7 +7,7 @@ #pragma once #include "td/utils/buffer.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/Status.h" namespace td { diff --git a/td/mtproto/PingConnection.h b/td/mtproto/PingConnection.h index 88a0f904a..c880066f5 100644 --- a/td/mtproto/PingConnection.h +++ b/td/mtproto/PingConnection.h @@ -12,7 +12,7 @@ #include "td/mtproto/utils.h" #include "td/utils/buffer.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/Random.h" #include "td/utils/Status.h" #include "td/utils/Time.h" diff --git a/td/mtproto/RawConnection.h b/td/mtproto/RawConnection.h index 84d715b74..39ba72ca4 100644 --- a/td/mtproto/RawConnection.h +++ b/td/mtproto/RawConnection.h @@ -11,7 +11,7 @@ #include "td/utils/buffer.h" #include "td/utils/BufferedFd.h" #include "td/utils/common.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/SocketFd.h" #include "td/utils/Status.h" diff --git a/td/mtproto/SessionConnection.h b/td/mtproto/SessionConnection.h index 4024d5e64..07bfa351e 100644 --- a/td/mtproto/SessionConnection.h +++ b/td/mtproto/SessionConnection.h @@ -13,7 +13,7 @@ #include "td/utils/buffer.h" #include "td/utils/format.h" #include "td/utils/Named.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" #include "td/utils/StringBuilder.h" diff --git a/td/mtproto/TcpTransport.h b/td/mtproto/TcpTransport.h index 7cfd82351..a63733f72 100644 --- a/td/mtproto/TcpTransport.h +++ b/td/mtproto/TcpTransport.h @@ -13,7 +13,7 @@ #include "td/utils/ByteFlow.h" #include "td/utils/common.h" #include "td/utils/crypto.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/Status.h" namespace td { diff --git a/td/telegram/Client.cpp b/td/telegram/Client.cpp index 792071b6d..43d76504d 100644 --- a/td/telegram/Client.cpp +++ b/td/telegram/Client.cpp @@ -13,7 +13,7 @@ #include "td/utils/crypto.h" #include "td/utils/logging.h" #include "td/utils/MpscPollableQueue.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/Poll.h" #include "td/utils/port/thread.h" diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 2cb41b6fc..8e8dd6c35 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -26,9 +26,10 @@ #include "td/utils/JsonBuilder.h" #include "td/utils/logging.h" #include "td/utils/misc.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/FileFd.h" #include "td/utils/port/signals.h" +#include "td/utils/port/SocketFd.h" #include "td/utils/port/Stat.h" #include "td/utils/port/StdStreams.h" #include "td/utils/port/thread_local.h" diff --git a/td/telegram/files/FileHashUploader.cpp b/td/telegram/files/FileHashUploader.cpp index e2389a75a..ef242f41b 100644 --- a/td/telegram/files/FileHashUploader.cpp +++ b/td/telegram/files/FileHashUploader.cpp @@ -18,7 +18,7 @@ #include "td/utils/MimeType.h" #include "td/utils/misc.h" #include "td/utils/PathView.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/FileFd.h" #include "td/utils/Status.h" diff --git a/tdactor/td/actor/impl/ConcurrentScheduler.cpp b/tdactor/td/actor/impl/ConcurrentScheduler.cpp index 98fd36c1e..46a726975 100644 --- a/tdactor/td/actor/impl/ConcurrentScheduler.cpp +++ b/tdactor/td/actor/impl/ConcurrentScheduler.cpp @@ -14,6 +14,10 @@ #include "td/utils/MpscPollableQueue.h" #include "td/utils/port/thread_local.h" +#if TD_PORT_WINDOWS +#include "td/utils/port/detail/WineventPoll.h" +#endif + #include namespace td { @@ -24,19 +28,18 @@ void ConcurrentScheduler::init(int32 threads_n) { #endif threads_n++; std::vector>> outbound(threads_n); +#if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED for (int32 i = 0; i < threads_n; i++) { -#if TD_THREAD_UNSUPPORTED || TD_EVENTFD_UNSUPPORTED -#else auto queue = std::make_shared>(); queue->init(); outbound[i] = queue; -#endif } +#endif // +1 for extra scheduler for IOCP and send_closure from unrelated threads // It will know about other schedulers - // Other schedulers will have no idea about its existance - int extra_scheduler = 1; + // Other schedulers will have no idea about its existence + int32 extra_scheduler = 1; #if TD_THREAD_UNSUPPORTED || TD_EVENTFD_UNSUPPORTED extra_scheduler = 0; #endif @@ -46,11 +49,13 @@ void ConcurrentScheduler::init(int32 threads_n) { auto &sched = schedulers_[i]; sched = make_unique(); +#if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED if (i >= threads_n) { auto queue = std::make_shared>(); queue->init(); outbound.push_back(std::move(queue)); } +#endif sched->init(i, outbound, static_cast(this)); } @@ -88,12 +93,12 @@ void ConcurrentScheduler::start() { } })); } -#endif #if TD_PORT_WINDOWS - iocp_thread_ = td::thread([this] { + iocp_thread_ = td::thread([this] { auto guard = this->get_send_guard(); - this->iocp_->loop(); + this->iocp_->loop(); }); +#endif #endif state_ = State::Run; diff --git a/tdactor/td/actor/impl/Scheduler-decl.h b/tdactor/td/actor/impl/Scheduler-decl.h index fb8dc3135..df1a30d68 100644 --- a/tdactor/td/actor/impl/Scheduler-decl.h +++ b/tdactor/td/actor/impl/Scheduler-decl.h @@ -16,8 +16,8 @@ #include "td/utils/MovableValue.h" #include "td/utils/MpscPollableQueue.h" #include "td/utils/ObjectPool.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/EventFd.h" -#include "td/utils/port/Fd.h" #include "td/utils/port/Poll.h" #include "td/utils/port/thread_local.h" #include "td/utils/Slice.h" diff --git a/tdactor/td/actor/impl/Scheduler.h b/tdactor/td/actor/impl/Scheduler.h index 1b70a63b3..1b21bfffe 100644 --- a/tdactor/td/actor/impl/Scheduler.h +++ b/tdactor/td/actor/impl/Scheduler.h @@ -14,7 +14,7 @@ #include "td/utils/logging.h" #include "td/utils/MpscPollableQueue.h" #include "td/utils/ObjectPool.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/Slice.h" #include @@ -190,10 +190,8 @@ inline void Scheduler::inc_wait_generation() { template void Scheduler::send_impl(const ActorId<> &actor_id, const RunFuncT &run_func, const EventFuncT &event_func) { - //CHECK(has_guard_ || ); ActorInfo *actor_info = actor_id.get_actor_info(); if (unlikely(actor_info == nullptr || close_flag_)) { - // LOG(ERROR) << "Invalid actor id"; return; } diff --git a/tdactor/test/actors_simple.cpp b/tdactor/test/actors_simple.cpp index 121df540c..d56aced85 100644 --- a/tdactor/test/actors_simple.cpp +++ b/tdactor/test/actors_simple.cpp @@ -15,10 +15,12 @@ #include "td/utils/logging.h" #include "td/utils/Observer.h" #include "td/utils/port/FileFd.h" +#include "td/utils/port/thread.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" #include "td/utils/StringBuilder.h" +#include #include REGISTER_TESTS(actors_simple) @@ -621,6 +623,7 @@ TEST(Actors, always_wait_for_mailbox) { scheduler.finish(); } +#if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED TEST(Actors, send_from_other_threads) { SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR)); ConcurrentScheduler scheduler; @@ -628,7 +631,7 @@ TEST(Actors, send_from_other_threads) { int thread_n = 10; class Listener : public Actor { public: - Listener(int cnt) : cnt_(cnt) { + explicit Listener(int cnt) : cnt_(cnt) { } void dec() { if (--cnt_ == 0) { @@ -656,3 +659,4 @@ TEST(Actors, send_from_other_threads) { } scheduler.finish(); } +#endif diff --git a/tddb/td/db/binlog/Binlog.cpp b/tddb/td/db/binlog/Binlog.cpp index 5c5b8fdc4..8089854f3 100644 --- a/tddb/td/db/binlog/Binlog.cpp +++ b/tddb/td/db/binlog/Binlog.cpp @@ -14,7 +14,6 @@ #include "td/utils/logging.h" #include "td/utils/misc.h" #include "td/utils/port/Clocks.h" -#include "td/utils/port/Fd.h" #include "td/utils/port/path.h" #include "td/utils/port/Stat.h" #include "td/utils/Random.h" diff --git a/tdnet/td/net/HttpConnectionBase.cpp b/tdnet/td/net/HttpConnectionBase.cpp index 8e99e2aa4..7975f2009 100644 --- a/tdnet/td/net/HttpConnectionBase.cpp +++ b/tdnet/td/net/HttpConnectionBase.cpp @@ -10,7 +10,7 @@ #include "td/utils/logging.h" #include "td/utils/misc.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" namespace td { namespace detail { diff --git a/tdnet/td/net/TcpListener.cpp b/tdnet/td/net/TcpListener.cpp index fde2fd19b..b98b4c727 100644 --- a/tdnet/td/net/TcpListener.cpp +++ b/tdnet/td/net/TcpListener.cpp @@ -7,7 +7,7 @@ #include "td/net/TcpListener.h" #include "td/utils/logging.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" namespace td { // TcpListener implementation diff --git a/tdnet/td/net/TransparentProxy.cpp b/tdnet/td/net/TransparentProxy.cpp index f9ee64def..b9881e985 100644 --- a/tdnet/td/net/TransparentProxy.cpp +++ b/tdnet/td/net/TransparentProxy.cpp @@ -7,7 +7,7 @@ #include "td/net/TransparentProxy.h" #include "td/utils/logging.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" namespace td { diff --git a/tdutils/CMakeLists.txt b/tdutils/CMakeLists.txt index 5e11dbb42..0e98a4d0d 100644 --- a/tdutils/CMakeLists.txt +++ b/tdutils/CMakeLists.txt @@ -44,6 +44,7 @@ set(TDUTILS_SOURCE td/utils/port/Stat.cpp td/utils/port/StdStreams.cpp td/utils/port/thread_local.cpp + td/utils/port/UdpSocketFd.cpp td/utils/port/wstring_convert.cpp td/utils/port/detail/Epoll.cpp @@ -51,11 +52,11 @@ set(TDUTILS_SOURCE td/utils/port/detail/EventFdLinux.cpp td/utils/port/detail/EventFdWindows.cpp td/utils/port/detail/KQueue.cpp + td/utils/port/detail/NativeFd.cpp td/utils/port/detail/Poll.cpp td/utils/port/detail/PollableFd.cpp td/utils/port/detail/Select.cpp td/utils/port/detail/ThreadIdGuard.cpp - td/utils/port/UdpSocketFd.cpp td/utils/port/detail/WineventPoll.cpp ${TDMIME_AUTO} @@ -117,7 +118,6 @@ set(TDUTILS_SOURCE td/utils/port/detail/EventFdWindows.h td/utils/port/detail/KQueue.h td/utils/port/detail/NativeFd.h - td/utils/port/detail/NativeFd.cpp td/utils/port/detail/Poll.h td/utils/port/detail/PollableFd.h td/utils/port/detail/Select.h @@ -131,9 +131,9 @@ set(TDUTILS_SOURCE td/utils/benchmark.h td/utils/BigNum.h td/utils/buffer.h - td/utils/BufferedUdp.h td/utils/BufferedFd.h td/utils/BufferedReader.h + td/utils/BufferedUdp.h td/utils/ByteFlow.h td/utils/ChangesProcessor.h td/utils/Closure.h @@ -141,8 +141,9 @@ set(TDUTILS_SOURCE td/utils/Container.h td/utils/Context.h td/utils/crypto.h - td/utils/Enumerator.h + td/utils/DecTree.h td/utils/Destructor.h + td/utils/Enumerator.h td/utils/FileLog.h td/utils/filesystem.h td/utils/find_boundary.h @@ -209,12 +210,12 @@ set(TDUTILS_TEST_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/test/buffer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/crypto.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/Enumerator.cpp - #${CMAKE_CURRENT_SOURCE_DIR}/test/filesystem.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test/filesystem.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/gzip.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/HazardPointers.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/heap.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/json.cpp - #${CMAKE_CURRENT_SOURCE_DIR}/test/misc.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test/misc.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/MpmcQueue.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/MpmcWaiter.cpp ${CMAKE_CURRENT_SOURCE_DIR}/test/MpscLinkQueue.cpp diff --git a/tdutils/td/utils/BufferedUdp.cpp b/tdutils/td/utils/BufferedUdp.cpp index ff5ee8fa5..fe6892a40 100644 --- a/tdutils/td/utils/BufferedUdp.cpp +++ b/tdutils/td/utils/BufferedUdp.cpp @@ -6,8 +6,12 @@ // #include "td/utils/BufferedUdp.h" +char disable_linker_warning_about_empty_file_buffered_udp_cpp TD_UNUSED; + namespace td { + #if TD_PORT_POSIX -TD_THREAD_LOCAL detail::UdpReader* BufferedUdp::udp_reader_; +TD_THREAD_LOCAL detail::UdpReader *BufferedUdp::udp_reader_; #endif + } // namespace td diff --git a/tdutils/td/utils/BufferedUdp.h b/tdutils/td/utils/BufferedUdp.h index 78d88d175..e3acbd2af 100644 --- a/tdutils/td/utils/BufferedUdp.h +++ b/tdutils/td/utils/BufferedUdp.h @@ -5,21 +5,27 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once -#include "td/utils/port/UdpSocketFd.h" #include "td/utils/buffer.h" #include "td/utils/optional.h" +#include "td/utils/port/detail/PollableFd.h" +#include "td/utils/port/thread_local.h" +#include "td/utils/port/UdpSocketFd.h" +#include "td/utils/Span.h" #include "td/utils/VectorQueue.h" +#include + namespace td { + #if TD_PORT_POSIX namespace detail { class UdpWriter { public: - static Status write_once(UdpSocketFd& fd, VectorQueue& queue) TD_WARN_UNUSED_RESULT { + static Status write_once(UdpSocketFd &fd, VectorQueue &queue) TD_WARN_UNUSED_RESULT { std::array messages; auto to_send = queue.as_span(); - size_t to_send_n = std::min(messages.size(), to_send.size()); + size_t to_send_n = td::min(messages.size(), to_send.size()); to_send.truncate(to_send_n); for (size_t i = 0; i < to_send_n; i++) { messages[i].to = &to_send[i].address; @@ -35,7 +41,7 @@ class UdpWriter { class UdpReaderHelper { public: - void init_inbound_message(UdpSocketFd::InboundMessage& message) { + void init_inbound_message(UdpSocketFd::InboundMessage &message) { message.from = &message_.address; message.error = &message_.error; if (buffer_.size() < MAX_PACKET_SIZE) { @@ -45,7 +51,7 @@ class UdpReaderHelper { message.data = buffer_.as_slice().truncate(MAX_PACKET_SIZE); } - UdpMessage extract_udp_message(UdpSocketFd::InboundMessage& message) { + UdpMessage extract_udp_message(UdpSocketFd::InboundMessage &message) { message_.data = buffer_.from_slice(message.data); auto size = message_.data.size(); size = (size + 7) & ~7; @@ -60,7 +66,7 @@ class UdpReaderHelper { BufferSlice buffer_; }; -//One for thread is enough +// One for thread is enough class UdpReader { public: UdpReader() { @@ -68,7 +74,7 @@ class UdpReader { helpers_[i].init_inbound_message(messages_[i]); } } - Status read_once(UdpSocketFd& fd, VectorQueue& queue) TD_WARN_UNUSED_RESULT { + Status read_once(UdpSocketFd &fd, VectorQueue &queue) TD_WARN_UNUSED_RESULT { for (size_t i = 0; i < messages_.size(); i++) { CHECK(messages_[i].data.size() == 2048); } @@ -81,7 +87,6 @@ class UdpReader { for (size_t i = cnt; i < messages_.size(); i++) { CHECK(messages_[i].data.size() == 2048) << " cnt = " << cnt << " i = " << i << " size = " << messages_[i].data.size() << " status = " << status; - ; } if (status.is_error() && !UdpSocketFd::is_critical_read_error(status)) { queue.push(UdpMessage{{}, {}, std::move(status)}); @@ -101,7 +106,7 @@ class UdpReader { class BufferedUdp : public UdpSocketFd { public: - BufferedUdp(UdpSocketFd fd) : UdpSocketFd(std::move(fd)) { + explicit BufferedUdp(UdpSocketFd fd) : UdpSocketFd(std::move(fd)) { } #if TD_PORT_POSIX @@ -132,8 +137,8 @@ class BufferedUdp : public UdpSocketFd { return std::move(as_fd()); } - UdpSocketFd& as_fd() { - return *static_cast(this); + UdpSocketFd &as_fd() { + return *static_cast(this); } private: @@ -141,10 +146,10 @@ class BufferedUdp : public UdpSocketFd { VectorQueue input_; VectorQueue output_; - VectorQueue& input() { + VectorQueue &input() { return input_; } - VectorQueue& output() { + VectorQueue &output() { return output_; } @@ -157,7 +162,8 @@ class BufferedUdp : public UdpSocketFd { return udp_reader_->read_once(as_fd(), input_); } - static TD_THREAD_LOCAL detail::UdpReader* udp_reader_; + static TD_THREAD_LOCAL detail::UdpReader *udp_reader_; #endif }; + } // namespace td diff --git a/tdutils/td/utils/Context.h b/tdutils/td/utils/Context.h index 9bf2b31ff..c62108d97 100644 --- a/tdutils/td/utils/Context.h +++ b/tdutils/td/utils/Context.h @@ -9,6 +9,7 @@ #include "td/utils/port/thread_local.h" namespace td { + template class Context { public: diff --git a/tdutils/td/utils/DecTree.h b/tdutils/td/utils/DecTree.h index 9a73e5f29..b44f7a0c1 100644 --- a/tdutils/td/utils/DecTree.h +++ b/tdutils/td/utils/DecTree.h @@ -6,17 +6,17 @@ // #pragma once +#include "td/utils/int_types.h" +#include "td/utils/Random.h" + +#include #include #include -#include "int_types.h" -#include "Random.h" - namespace td { template > class DecTree { - private: struct Node { std::unique_ptr left_; std::unique_ptr right_; @@ -35,15 +35,16 @@ class DecTree { } } - Node(KeyType key, ValueType value, uint32 y) : key_(std::move(key)), value_(std::move(value)), y_(y) { - size_ = 1; + Node(KeyType key, ValueType value, uint32 y) : size_(1), key_(std::move(key)), value_(std::move(value)), y_(y) { } }; std::unique_ptr root_; - std::unique_ptr create_node(KeyType key, ValueType value, uint32 y) { + + static std::unique_ptr create_node(KeyType key, ValueType value, uint32 y) { return std::make_unique(std::move(key), std::move(value), y); } - std::unique_ptr insert_node(std::unique_ptr Tree, KeyType key, ValueType value, uint32 y) { + + static std::unique_ptr insert_node(std::unique_ptr Tree, KeyType key, ValueType value, uint32 y) { if (Tree == nullptr) { return create_node(std::move(key), std::move(value), y); } @@ -63,9 +64,10 @@ class DecTree { // ?? assert } Tree->relax(); - return std::move(Tree); + return Tree; } - std::unique_ptr remove_node(std::unique_ptr Tree, KeyType &key) { + + static std::unique_ptr remove_node(std::unique_ptr Tree, const KeyType &key) { if (Tree == nullptr) { // ?? assert return nullptr; @@ -80,10 +82,10 @@ class DecTree { if (Tree != nullptr) { Tree->relax(); } - return std::move(Tree); + return Tree; } - ValueType *get_node(std::unique_ptr &Tree, KeyType &key) { + static ValueType *get_node(std::unique_ptr &Tree, const KeyType &key) { if (Tree == nullptr) { return nullptr; } @@ -95,7 +97,8 @@ class DecTree { return &Tree->value_; } } - ValueType *get_node_by_idx(std::unique_ptr &Tree, size_t idx) { + + static ValueType *get_node_by_idx(std::unique_ptr &Tree, size_t idx) { CHECK(Tree != nullptr); auto s = (Tree->left_ != nullptr) ? Tree->left_->size_ : 0; if (idx < s) { @@ -106,46 +109,46 @@ class DecTree { return get_node_by_idx(Tree->right_, idx - s - 1); } } - std::pair, std::unique_ptr> split_node(std::unique_ptr Tree, KeyType &key) { + + static std::pair, std::unique_ptr> split_node(std::unique_ptr Tree, + const KeyType &key) { if (Tree == nullptr) { - return std::pair, std::unique_ptr>(nullptr, nullptr); + return {nullptr, nullptr}; } if (Compare()(key, Tree->key_)) { auto P = split_node(std::move(Tree->left_), key); Tree->left_ = std::move(P.second); Tree->relax(); P.second = std::move(Tree); - return std::move(P); + return P; } else { auto P = split_node(std::move(Tree->right_), key); Tree->right_ = std::move(P.first); Tree->relax(); P.first = std::move(Tree); - return std::move(P); + return P; } } - std::unique_ptr merge_node(std::unique_ptr left, std::unique_ptr right) { + + static std::unique_ptr merge_node(std::unique_ptr left, std::unique_ptr right) { if (left == nullptr) { - return std::move(right); + return right; } if (right == nullptr) { - return std::move(left); + return left; } if (left->y_ < right->y_) { right->left_ = merge_node(std::move(left), std::move(right->left_)); right->relax(); - return std::move(right); + return right; } else { left->right_ = merge_node(std::move(left->right_), std::move(right)); left->relax(); - return std::move(left); + return left; } } public: - DecTree() { - } - size_t size() const { if (root_ == nullptr) { return 0; @@ -156,10 +159,10 @@ class DecTree { void insert(KeyType key, ValueType value) { root_ = insert_node(std::move(root_), std::move(key), std::move(value), td::Random::fast_uint32()); } - void remove(KeyType &key) { + void remove(const KeyType &key) { root_ = remove_node(std::move(root_), key); } - ValueType *get(KeyType &key) { + ValueType *get(const KeyType &key) { return get_node(root_, key); } ValueType *get_random() { @@ -169,7 +172,7 @@ class DecTree { return get_node_by_idx(root_, td::Random::fast_uint32() % size()); } } - bool exists(KeyType &key) { + bool exists(const KeyType &key) const { return get_node(root_, key) != nullptr; } }; diff --git a/tdutils/td/utils/Destructor.h b/tdutils/td/utils/Destructor.h index bbfc8bebb..574368f4b 100644 --- a/tdutils/td/utils/Destructor.h +++ b/tdutils/td/utils/Destructor.h @@ -4,8 +4,13 @@ // 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) // +#pragma once + #include "td/utils/common.h" +#include +#include + namespace td { class Destructor { @@ -43,4 +48,5 @@ template auto create_shared_destructor(F &&f) { return std::make_shared>(std::forward(f)); } + } // namespace td diff --git a/tdutils/td/utils/FileLog.cpp b/tdutils/td/utils/FileLog.cpp index 3a45888f7..2cd0b63fc 100644 --- a/tdutils/td/utils/FileLog.cpp +++ b/tdutils/td/utils/FileLog.cpp @@ -8,9 +8,9 @@ #include "td/utils/common.h" #include "td/utils/logging.h" -#include "td/utils/port/Fd.h" #include "td/utils/port/FileFd.h" #include "td/utils/port/path.h" +#include "td/utils/port/StdStreams.h" #include "td/utils/Slice.h" #include diff --git a/tdutils/td/utils/MpscPollableQueue.h b/tdutils/td/utils/MpscPollableQueue.h index b1603cf64..251d4428a 100644 --- a/tdutils/td/utils/MpscPollableQueue.h +++ b/tdutils/td/utils/MpscPollableQueue.h @@ -10,6 +10,9 @@ #include "td/utils/port/EventFd.h" #if !TD_EVENTFD_UNSUPPORTED + +#include "td/utils/SpinLock.h" + #if !TD_WINDOWS #include #include @@ -17,8 +20,6 @@ #include -#include "td/utils/SpinLock.h" - namespace td { // interface like in PollableQueue template diff --git a/tdutils/td/utils/Random.cpp b/tdutils/td/utils/Random.cpp index 1b1e17815..debe6d1c5 100644 --- a/tdutils/td/utils/Random.cpp +++ b/tdutils/td/utils/Random.cpp @@ -92,7 +92,7 @@ uint32 Random::fast_uint32() { if (!gen) { auto &rg = rand_device_helper; std::seed_seq seq{rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg()}; - init_thread_local(gen); + init_thread_local(gen, seq); } return static_cast((*gen)()); } @@ -102,7 +102,7 @@ uint64 Random::fast_uint64() { if (!gen) { auto &rg = rand_device_helper; std::seed_seq seq{rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg(), rg()}; - init_thread_local(gen); + init_thread_local(gen, seq); } return static_cast((*gen)()); } @@ -116,12 +116,13 @@ int Random::fast(int min, int max) { return static_cast(min + fast_uint32() % (max - min + 1)); // TODO signed_cast } -Random::Xorshift128plus::Xorshift128plus(uint32 seed) { +Random::Xorshift128plus::Xorshift128plus(uint64 seed) { auto next = [&]() { // splitmix64 - uint64_t z = (seed += UINT64_C(0x9E3779B97F4A7C15)); - z = (z ^ (z >> 30)) * UINT64_C(0xBF58476D1CE4E5B9); - z = (z ^ (z >> 27)) * UINT64_C(0x94D049BB133111EB); + seed += static_cast(0x9E3779B97F4A7C15); + uint64 z = seed; + z = (z ^ (z >> 30)) * static_cast(0xBF58476D1CE4E5B9); + z = (z ^ (z >> 27)) * static_cast(0x94D049BB133111EB); return z ^ (z >> 31); }; seed_[0] = next(); @@ -134,11 +135,11 @@ Random::Xorshift128plus::Xorshift128plus(uint64 seed_a, uint64 seed_b) { } uint64 Random::Xorshift128plus::operator()() { - uint64_t x = seed_[0]; - uint64_t const y = seed_[1]; + uint64 x = seed_[0]; + const uint64 y = seed_[1]; seed_[0] = y; - x ^= x << 23; // a - seed_[1] = x ^ y ^ (x >> 17) ^ (y >> 26); // b, c + x ^= x << 23; + seed_[1] = x ^ y ^ (x >> 17) ^ (y >> 26); return seed_[1] + y; } diff --git a/tdutils/td/utils/Random.h b/tdutils/td/utils/Random.h index 1c1cd141e..326eecc62 100644 --- a/tdutils/td/utils/Random.h +++ b/tdutils/td/utils/Random.h @@ -31,7 +31,7 @@ class Random { class Xorshift128plus { public: - Xorshift128plus(uint32 seed); + explicit Xorshift128plus(uint64 seed); Xorshift128plus(uint64 seed_a, uint64 seed_b); uint64 operator()(); diff --git a/tdutils/td/utils/SharedObjectPool.h b/tdutils/td/utils/SharedObjectPool.h index 6743d8b17..1c867cbf2 100644 --- a/tdutils/td/utils/SharedObjectPool.h +++ b/tdutils/td/utils/SharedObjectPool.h @@ -38,7 +38,9 @@ class AtomicRefCnt { }; template -class SharedPtrRaw : public DeleterT, private MpscLinkQueueImpl::Node { +class SharedPtrRaw + : public DeleterT + , private MpscLinkQueueImpl::Node { public: explicit SharedPtrRaw(DeleterT deleter) : DeleterT(std::move(deleter)), ref_cnt_{0}, option_magic_(Magic) { } diff --git a/tdutils/td/utils/Slice.h b/tdutils/td/utils/Slice.h index 427695ce8..a2f17a507 100644 --- a/tdutils/td/utils/Slice.h +++ b/tdutils/td/utils/Slice.h @@ -281,7 +281,7 @@ inline bool operator!=(const Slice &a, const Slice &b) { } inline bool operator<(const Slice &a, const Slice &b) { - auto x = std::memcmp(a.data(), b.data(), std::min(a.size(), b.size())); + auto x = std::memcmp(a.data(), b.data(), td::min(a.size(), b.size())); if (x == 0) { return a.size() < b.size(); } diff --git a/tdutils/td/utils/Span.h b/tdutils/td/utils/Span.h index 23f47213b..2a609e355 100644 --- a/tdutils/td/utils/Span.h +++ b/tdutils/td/utils/Span.h @@ -5,13 +5,17 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once -#include "td/utils/Status.h" + +#include "td/utils/common.h" +#include "td/utils/logging.h" + +#include namespace td { + namespace detail { template class SpanImpl { - private: InnerT *data_{nullptr}; size_t size_{0}; @@ -21,8 +25,6 @@ class SpanImpl { } SpanImpl(InnerT &data) : SpanImpl(&data, 1) { } - SpanImpl(const SpanImpl &other) = default; - SpanImpl &operator=(const SpanImpl &other) = default; template SpanImpl(const SpanImpl &other) : SpanImpl(other.data(), other.size()) { @@ -34,9 +36,9 @@ class SpanImpl { template SpanImpl(std::array &arr) : SpanImpl(arr.data(), arr.size()) { } - SpanImpl(const std::vector &v) : SpanImpl(v.data(), v.size()) { + SpanImpl(const vector &v) : SpanImpl(v.data(), v.size()) { } - SpanImpl(std::vector &v) : SpanImpl(v.data(), v.size()) { + SpanImpl(vector &v) : SpanImpl(v.data(), v.size()) { } template @@ -69,15 +71,17 @@ class SpanImpl { return *this; } - SpanImpl substr(size_t offset) { + SpanImpl substr(size_t offset) const { CHECK(offset <= size_); return SpanImpl(begin() + offset, size_ - offset); } }; } // namespace detail + template using Span = detail::SpanImpl; template using MutableSpan = detail::SpanImpl; -}; // namespace td + +} // namespace td diff --git a/tdutils/td/utils/VectorQueue.h b/tdutils/td/utils/VectorQueue.h index 24c54b69a..f5c2259ca 100644 --- a/tdutils/td/utils/VectorQueue.h +++ b/tdutils/td/utils/VectorQueue.h @@ -5,15 +5,19 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once + #include "td/utils/Span.h" + +#include #include namespace td { + template class VectorQueue { public: template - void push(S&& s) { + void push(S &&s) { vector_.push_back(std::forward(s)); } T pop() { @@ -24,11 +28,11 @@ class VectorQueue { read_pos_ += n; try_shrink(); } - T& front() { + T &front() { return vector_[read_pos_]; } - T& back() { - vector_.back(); + T &back() { + return vector_.back(); } bool empty() const { return size() == 0; @@ -36,10 +40,10 @@ class VectorQueue { size_t size() const { return vector_.size() - read_pos_; } - T* data() { + T *data() { return vector_.data() + read_pos_; } - const T* data() const { + const T *data() const { return vector_.data() + read_pos_; } Span as_span() const { diff --git a/tdutils/td/utils/buffer.h b/tdutils/td/utils/buffer.h index 5a5c774e2..3516a473c 100644 --- a/tdutils/td/utils/buffer.h +++ b/tdutils/td/utils/buffer.h @@ -8,9 +8,9 @@ #include "td/utils/common.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" #include "td/utils/port/thread_local.h" #include "td/utils/Slice.h" -#include "td/utils/misc.h" #include #include @@ -638,7 +638,7 @@ class ChainBufferWriter { } // legacy - static ChainBufferWriter create_empty(size_t /*size*/ = 0) { + static ChainBufferWriter create_empty() { return ChainBufferWriter(); } @@ -764,6 +764,6 @@ class BufferBuilder { void append_slow(BufferSlice slice); bool prepend_inplace(Slice slice); void prepend_slow(BufferSlice slice); -}; // namespace td +}; } // namespace td diff --git a/tdutils/td/utils/int_types.h b/tdutils/td/utils/int_types.h index 82da073fa..709896bbc 100644 --- a/tdutils/td/utils/int_types.h +++ b/tdutils/td/utils/int_types.h @@ -7,7 +7,6 @@ #pragma once #include "td/utils/port/platform.h" -//#include "td/utils/format.h" #include #include @@ -51,26 +50,26 @@ struct UInt { }; template -inline bool operator==(const UInt &a, const UInt &b) { +bool operator==(const UInt &a, const UInt &b) { return std::memcmp(a.raw, b.raw, sizeof(a.raw)) == 0; } template -inline td::UInt operator^(const UInt &a, const UInt &b) { +bool operator!=(const UInt &a, const UInt &b) { + return !(a == b); +} + +template +td::UInt operator^(const UInt &a, const UInt &b) { td::UInt res; for (size_t i = 0; i * 8 < size; i++) { - res.raw[i] = a.raw[i] ^ b.raw[i]; + res.raw[i] = static_cast(a.raw[i] ^ b.raw[i]); } return res; } template -inline bool operator!=(const UInt &a, const UInt &b) { - return !(a == b); -} - -template -inline bool is_zero(const UInt &a) { +bool is_zero(const UInt &a) { for (size_t i = 0; i * 8 < size; i++) { if (a.raw[i]) { return false; @@ -80,15 +79,15 @@ inline bool is_zero(const UInt &a) { } template -inline int get_kth_bit(const UInt &a, uint32 bit) { +int get_kth_bit(const UInt &a, uint32 bit) { uint8 b = a.raw[bit / 8]; bit &= 7; return (b >> (7 - bit)) & 1; } template -inline bool operator<(const UInt &a, const UInt &b) { - return memcmp(a.raw, b.raw, sizeof(a.raw)) < 0; +bool operator<(const UInt &a, const UInt &b) { + return std::memcmp(a.raw, b.raw, sizeof(a.raw)) < 0; } using UInt128 = UInt<128>; diff --git a/tdutils/td/utils/invoke.h b/tdutils/td/utils/invoke.h index 3dee3444a..157c03170 100644 --- a/tdutils/td/utils/invoke.h +++ b/tdutils/td/utils/invoke.h @@ -175,11 +175,6 @@ void tuple_for_each(const std::tuple &tuple, const F &func) { detail::tuple_for_each_impl(tuple, func, detail::IntRange()); } -template = 0> -auto &&get_nth_argument(Arg &&arg) { - return std::forward(arg); -} - template = 0> auto &&get_nth_argument(Arg &&arg, Args &&... args) { return std::forward(arg); diff --git a/tdutils/td/utils/logging.cpp b/tdutils/td/utils/logging.cpp index a8cef318c..dadfde6f6 100644 --- a/tdutils/td/utils/logging.cpp +++ b/tdutils/td/utils/logging.cpp @@ -55,7 +55,7 @@ Logger::Logger(LogInterface &log, const LogOptions &options, int log_level, Slic if (log_level < 10) { sb_ << ' '; } - sb_ << log_level << "]"; + sb_ << log_level << ']'; // thread id auto thread_id = get_thread_id(); @@ -63,10 +63,10 @@ Logger::Logger(LogInterface &log, const LogOptions &options, int log_level, Slic if (thread_id < 10) { sb_ << ' '; } - sb_ << thread_id << "]"; + sb_ << thread_id << ']'; // timestamp - sb_ << "[" << StringBuilder::FixedDouble(Clocks::system(), 9) << "]"; + sb_ << '[' << StringBuilder::FixedDouble(Clocks::system(), 9) << ']'; // file : line if (!file_name.empty()) { @@ -104,7 +104,7 @@ Logger::~Logger() { slice.back() = '\n'; } while (slice.size() > 1 && slice[slice.size() - 2] == '\n') { - slice.back() = 0; + slice.back() = '\0'; slice = MutableCSlice(slice.begin(), slice.begin() + slice.size() - 1); } log_.append(slice, log_level_); diff --git a/tdutils/td/utils/logging.h b/tdutils/td/utils/logging.h index 28ba84a01..7c4e553b6 100644 --- a/tdutils/td/utils/logging.h +++ b/tdutils/td/utils/logging.h @@ -30,8 +30,6 @@ #include "td/utils/StringBuilder.h" #include -#include -#include #include #define PSTR_IMPL() ::td::Logger(::td::NullLog().ref(), ::td::LogOptions::plain(), 0) @@ -81,10 +79,8 @@ inline bool no_return_func() { } // clang-format off -#ifdef CHECK - #undef CHECK -#endif #define DUMMY_CHECK(condition) LOG_IF(NEVER, !(condition)) + #ifdef TD_DEBUG #if TD_MSVC #define CHECK(condition) \ @@ -96,10 +92,11 @@ inline bool no_return_func() { #else #define CHECK DUMMY_CHECK #endif + #if NDEBUG -#define DCHECK DUMMY_CHECK + #define DCHECK DUMMY_CHECK #else -#define DCHECK CHECK + #define DCHECK CHECK #endif // clang-format on @@ -134,7 +131,12 @@ struct LogOptions { bool add_info{true}; static constexpr LogOptions plain() { - return {0, false, false}; + return LogOptions{0, false, false}; + } + + constexpr LogOptions() = default; + constexpr LogOptions(int level, bool fix_newlines, bool add_info) + : level(level), fix_newlines(fix_newlines), add_info(add_info) { } }; @@ -151,7 +153,7 @@ class LogInterface { virtual void append(CSlice slice) { append(slice, -1); } - virtual void append(CSlice slice, int /*log_level_*/) { + virtual void append(CSlice slice, int /*log_level*/) { append(slice); } virtual void rotate() { @@ -160,7 +162,7 @@ class LogInterface { class NullLog : public LogInterface { public: - void append(CSlice /*slice*/, int /*log_level_*/) override { + void append(CSlice /*slice*/, int /*log_level*/) override { } void rotate() override { } diff --git a/tdutils/td/utils/misc.h b/tdutils/td/utils/misc.h index d137862aa..5c199b483 100644 --- a/tdutils/td/utils/misc.h +++ b/tdutils/td/utils/misc.h @@ -284,8 +284,8 @@ string url_encode(Slice str); namespace detail { template -struct is_same_signedness : public std::integral_constant::value == std::is_signed::value> { -}; +struct is_same_signedness + : public std::integral_constant::value == std::is_signed::value> {}; template struct safe_undeflying_type { @@ -350,6 +350,7 @@ bool is_aligned_pointer(const T *pointer) { return (reinterpret_cast(static_cast(pointer)) & (Alignment - 1)) == 0; } +namespace detail { template struct reversion_wrapper { T &iterable; @@ -364,9 +365,10 @@ template auto end(reversion_wrapper w) { return rend(w.iterable); } +} // namespace detail template -reversion_wrapper reversed(T &&iterable) { +detail::reversion_wrapper reversed(T &iterable) { return {iterable}; } diff --git a/tdutils/td/utils/overloaded.h b/tdutils/td/utils/overloaded.h index 11c22008c..6c6186f6a 100644 --- a/tdutils/td/utils/overloaded.h +++ b/tdutils/td/utils/overloaded.h @@ -20,7 +20,9 @@ struct overload : public F { }; template -struct overload : public overload, overload { +struct overload + : public overload + , overload { overload(F f, Fs... fs) : overload(f), overload(fs...) { } using overload::operator(); diff --git a/tdutils/td/utils/port/Fd.h b/tdutils/td/utils/port/Fd.h index f730891f1..2a7653752 100644 --- a/tdutils/td/utils/port/Fd.h +++ b/tdutils/td/utils/port/Fd.h @@ -11,7 +11,6 @@ #include "td/utils/common.h" #include "td/utils/Slice.h" -#include "td/utils/format.h" #include "td/utils/Status.h" #include "td/utils/port/IPAddress.h" diff --git a/tdutils/td/utils/port/FileFd.cpp b/tdutils/td/utils/port/FileFd.cpp index 646381322..715d9c99d 100644 --- a/tdutils/td/utils/port/FileFd.cpp +++ b/tdutils/td/utils/port/FileFd.cpp @@ -15,11 +15,10 @@ #include "td/utils/logging.h" #include "td/utils/misc.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/sleep.h" #include "td/utils/StringBuilder.h" -#include "td/utils/port/detail/PollableFd.h" - #include #if TD_PORT_POSIX @@ -134,7 +133,6 @@ Result FileFd::open(CSlice filepath, int32 flags, int32 mode) { if (native_fd < 0) { return OS_ERROR(PSLICE() << "File \"" << filepath << "\" can't be " << PrintFlags{flags}); } - return from_native_fd(NativeFd(native_fd)); #elif TD_PORT_WINDOWS // TODO: support modes @@ -187,15 +185,14 @@ Result FileFd::open(CSlice filepath, int32 flags, int32 mode) { offset.QuadPart = 0; auto set_pointer_res = SetFilePointerEx(handle, offset, nullptr, FILE_END); if (!set_pointer_res) { - auto res = OS_ERROR(PSLICE() << "Failed to seek to the end of file \"" << filepath << "\""); - return res; + return OS_ERROR(PSLICE() << "Failed to seek to the end of file \"" << filepath << "\""); } } return from_native_fd(std::move(native_fd)); #endif } -Result FileFd::from_native_fd(NativeFd native_fd) { +FileFd FileFd::from_native_fd(NativeFd native_fd) { auto impl = std::make_unique(); impl->info.set_native_fd(std::move(native_fd)); impl->info.add_flags(PollFlags::Write()); @@ -508,9 +505,11 @@ Status FileFd::truncate_to_current_position(int64 current_position) { return Status::OK(); } PollableFdInfo &FileFd::get_poll_info() { + CHECK(!empty()); return impl_->info; } const PollableFdInfo &FileFd::get_poll_info() const { + CHECK(!empty()); return impl_->info; } diff --git a/tdutils/td/utils/port/FileFd.h b/tdutils/td/utils/port/FileFd.h index 121d99791..b9fd6dfa2 100644 --- a/tdutils/td/utils/port/FileFd.h +++ b/tdutils/td/utils/port/FileFd.h @@ -9,9 +9,9 @@ #include "td/utils/port/config.h" #include "td/utils/common.h" -#include "td/utils/port/Fd.h" -#include "td/utils/port/Stat.h" +#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/PollableFd.h" +#include "td/utils/port/Stat.h" #include "td/utils/Slice.h" #include "td/utils/Status.h" @@ -32,7 +32,7 @@ class FileFd { enum Flags : int32 { Write = 1, Read = 2, Truncate = 4, Create = 8, Append = 16, CreateNew = 32 }; static Result open(CSlice filepath, int32 flags, int32 mode = 0600) TD_WARN_UNUSED_RESULT; - static Result from_native_fd(NativeFd fd) TD_WARN_UNUSED_RESULT; + static FileFd from_native_fd(NativeFd fd) TD_WARN_UNUSED_RESULT; Result write(Slice slice) TD_WARN_UNUSED_RESULT; Result read(MutableSlice slice) TD_WARN_UNUSED_RESULT; @@ -64,7 +64,7 @@ class FileFd { private: std::unique_ptr impl_; - FileFd(std::unique_ptr impl); + explicit FileFd(std::unique_ptr impl); }; } // namespace td diff --git a/tdutils/td/utils/port/IPAddress.cpp b/tdutils/td/utils/port/IPAddress.cpp index b81966abf..2e5583ac8 100644 --- a/tdutils/td/utils/port/IPAddress.cpp +++ b/tdutils/td/utils/port/IPAddress.cpp @@ -369,16 +369,15 @@ Status IPAddress::init_sockaddr(sockaddr *addr, socklen_t len) { if (addr->sa_family == AF_INET6) { CHECK(len == sizeof(ipv6_addr_)); std::memcpy(&ipv6_addr_, reinterpret_cast(addr), sizeof(ipv6_addr_)); - LOG(DEBUG) << "Have ipv6 address " << get_ip_str() << " with port " << get_port(); } else if (addr->sa_family == AF_INET) { CHECK(len == sizeof(ipv4_addr_)); std::memcpy(&ipv4_addr_, reinterpret_cast(addr), sizeof(ipv4_addr_)); - LOG(DEBUG) << "Have ipv4 address " << get_ip_str() << " with port " << get_port(); } else { return Status::Error(PSLICE() << "Unknown " << tag("sa_family", addr->sa_family)); } is_valid_ = true; + LOG(INFO) << "Have address " << get_ip_str() << " with port " << get_port(); return Status::OK(); } diff --git a/tdutils/td/utils/port/IPAddress.h b/tdutils/td/utils/port/IPAddress.h index a4720601d..39b4015a2 100644 --- a/tdutils/td/utils/port/IPAddress.h +++ b/tdutils/td/utils/port/IPAddress.h @@ -67,7 +67,7 @@ class IPAddress { sockaddr_in ipv4_addr_; sockaddr_in6 ipv6_addr_; }; - socklen_t storage_size() { + static constexpr socklen_t storage_size() { return sizeof(ipv6_addr_); } bool is_valid_; diff --git a/tdutils/td/utils/port/PollBase.h b/tdutils/td/utils/port/PollBase.h index 602fd3b40..49fd369ff 100644 --- a/tdutils/td/utils/port/PollBase.h +++ b/tdutils/td/utils/port/PollBase.h @@ -6,7 +6,6 @@ // #pragma once -#include "td/utils/port/Fd.h" #include "td/utils/port/detail/PollableFd.h" namespace td { diff --git a/tdutils/td/utils/port/ServerSocketFd.cpp b/tdutils/td/utils/port/ServerSocketFd.cpp index f14114df2..0d3b6f691 100644 --- a/tdutils/td/utils/port/ServerSocketFd.cpp +++ b/tdutils/td/utils/port/ServerSocketFd.cpp @@ -29,13 +29,15 @@ #include "td/utils/VectorQueue.h" #endif +#include + namespace td { namespace detail { #if TD_PORT_WINDOWS class ServerSocketFdImpl : private IOCP::Callback { public: - ServerSocketFdImpl(NativeFd fd, int socket_family) : info(std::move(fd)), socket_family_(socket_family) { + ServerSocketFdImpl(NativeFd fd, int socket_family) : info_(std::move(fd)), socket_family_(socket_family) { VLOG(fd) << get_native_fd().io_handle() << " create ServerSocketFd"; IOCP::get()->subscribe(get_native_fd(), this); notify_iocp_read(); @@ -44,14 +46,14 @@ class ServerSocketFdImpl : private IOCP::Callback { notify_iocp_close(); } PollableFdInfo &get_poll_info() { - return info; + return info_; } const PollableFdInfo &get_poll_info() const { - return info; + return info_; } const NativeFd &get_native_fd() const { - return info.native_fd(); + return info_.native_fd(); } Result accept() { @@ -78,7 +80,7 @@ class ServerSocketFdImpl : private IOCP::Callback { } private: - PollableFdInfo info; + PollableFdInfo info_; SpinLock lock_; VectorQueue accepted_; @@ -98,7 +100,7 @@ class ServerSocketFdImpl : private IOCP::Callback { void on_close() { close_flag_ = true; - info.set_native_fd({}); + info_.set_native_fd({}); } void on_read() { VLOG(fd) << get_native_fd().io_handle() << " on_read"; @@ -133,7 +135,7 @@ class ServerSocketFdImpl : private IOCP::Callback { if (status == 0) { return true; } - auto last_error = GetLastError(); + auto last_error = WSAGetLastError(); if (last_error == ERROR_IO_PENDING) { return true; } @@ -197,17 +199,17 @@ void ServerSocketFdImplDeleter::operator()(ServerSocketFdImpl *impl) { #elif TD_PORT_POSIX class ServerSocketFdImpl { public: - ServerSocketFdImpl(NativeFd fd) : info(std::move(fd)) { + explicit ServerSocketFdImpl(NativeFd fd) : info_(std::move(fd)) { } PollableFdInfo &get_poll_info() { - return info; + return info_; } const PollableFdInfo &get_poll_info() const { - return info; + return info_; } const NativeFd &get_native_fd() const { - return info.native_fd(); + return info_.native_fd(); } Result accept() { sockaddr_storage addr; @@ -260,7 +262,7 @@ class ServerSocketFdImpl { } private: - PollableFdInfo info; + PollableFdInfo info_; }; void ServerSocketFdImplDeleter::operator()(ServerSocketFdImpl *impl) { delete impl; diff --git a/tdutils/td/utils/port/ServerSocketFd.h b/tdutils/td/utils/port/ServerSocketFd.h index fc5d01503..cbc342ac2 100644 --- a/tdutils/td/utils/port/ServerSocketFd.h +++ b/tdutils/td/utils/port/ServerSocketFd.h @@ -6,7 +6,8 @@ // #pragma once -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/NativeFd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/SocketFd.h" #include "td/utils/Slice.h" diff --git a/tdutils/td/utils/port/SocketFd.cpp b/tdutils/td/utils/port/SocketFd.cpp index 6fb880125..67ee91f14 100644 --- a/tdutils/td/utils/port/SocketFd.cpp +++ b/tdutils/td/utils/port/SocketFd.cpp @@ -7,9 +7,15 @@ #include "td/utils/port/SocketFd.h" #include "td/utils/logging.h" - #include "td/utils/misc.h" +#if TD_PORT_WINDOWS +#include "td/utils/buffer.h" +#include "td/utils/port/detail/WineventPoll.h" +#include "td/utils/SpinLock.h" +#include "td/utils/VectorQueue.h" +#endif + #if TD_PORT_POSIX #include #include @@ -20,19 +26,14 @@ #include #endif -#if TD_PORT_WINDOWS -#include "td/utils/buffer.h" -#include "td/utils/port/detail/WineventPoll.h" -#include "td/utils/SpinLock.h" -#include "td/utils/VectorQueue.h" -#endif +#include namespace td { namespace detail { #if TD_PORT_WINDOWS class SocketFdImpl : private IOCP::Callback { public: - SocketFdImpl(NativeFd native_fd) : info(std::move(native_fd)) { + explicit SocketFdImpl(NativeFd native_fd) : info(std::move(native_fd)) { VLOG(fd) << get_native_fd().io_handle() << " create from native_fd"; get_poll_info().add_flags(PollFlags::Write()); IOCP::get()->subscribe(get_native_fd(), this); @@ -95,7 +96,6 @@ class SocketFdImpl : private IOCP::Callback { if (res == 0) { get_poll_info().clear_flags(PollFlags::Read()); } - LOG(ERROR) << "GOT " << res; return res; } Status get_pending_error() { @@ -138,7 +138,7 @@ class SocketFdImpl : private IOCP::Callback { if (status == 0) { return true; } - auto last_error = GetLastError(); + auto last_error = WSAGetLastError(); if (last_error == ERROR_IO_PENDING) { return true; } @@ -271,7 +271,6 @@ class SocketFdImpl : private IOCP::Callback { } bool dec_refcnt() { if (--refcnt_ == 0) { - LOG(ERROR) << "DELETE"; delete this; return true; } @@ -318,7 +317,7 @@ static InitWSA init_wsa; class SocketFdImpl { public: PollableFdInfo info; - SocketFdImpl(NativeFd fd) : info(std::move(fd)) { + explicit SocketFdImpl(NativeFd fd) : info(std::move(fd)) { } PollableFdInfo &get_poll_info() { return info; diff --git a/tdutils/td/utils/port/SocketFd.h b/tdutils/td/utils/port/SocketFd.h index 73db484ec..933c93688 100644 --- a/tdutils/td/utils/port/SocketFd.h +++ b/tdutils/td/utils/port/SocketFd.h @@ -8,9 +8,10 @@ #include "td/utils/port/config.h" +#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/IPAddress.h" - +#include "td/utils/Slice.h" #include "td/utils/Status.h" namespace td { @@ -23,6 +24,7 @@ class SocketFdImplDeleter { }; class EventFdBsd; } // namespace detail + class SocketFd { public: SocketFd(); diff --git a/tdutils/td/utils/port/StdStreams.cpp b/tdutils/td/utils/port/StdStreams.cpp index 2160364b4..800d64702 100644 --- a/tdutils/td/utils/port/StdStreams.cpp +++ b/tdutils/td/utils/port/StdStreams.cpp @@ -6,12 +6,14 @@ // #include "td/utils/port/StdStreams.h" +#include "td/utils/port/detail/NativeFd.h" + namespace td { namespace { template FileFd create(T handle) { - return FileFd::from_native_fd(NativeFd(handle, true)).move_as_ok(); + return FileFd::from_native_fd(NativeFd(handle, true)); } } // namespace FileFd &Stdin() { diff --git a/tdutils/td/utils/port/StdStreams.h b/tdutils/td/utils/port/StdStreams.h index 36e213942..612c4ee12 100644 --- a/tdutils/td/utils/port/StdStreams.h +++ b/tdutils/td/utils/port/StdStreams.h @@ -5,6 +5,7 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once + #include "td/utils/port/FileFd.h" namespace td { diff --git a/tdutils/td/utils/port/UdpSocketFd.cpp b/tdutils/td/utils/port/UdpSocketFd.cpp index 9576457ca..4de2c2d6b 100644 --- a/tdutils/td/utils/port/UdpSocketFd.cpp +++ b/tdutils/td/utils/port/UdpSocketFd.cpp @@ -6,10 +6,16 @@ // #include "td/utils/port/UdpSocketFd.h" -#include "td/utils/port/SocketFd.h" -#include "td/utils/logging.h" +#include "td/utils/common.h" #include "td/utils/format.h" +#include "td/utils/logging.h" #include "td/utils/misc.h" +#include "td/utils/port/SocketFd.h" +#include "td/utils/VectorQueue.h" + +#if TD_PORT_WINDOWS +#include "td/utils/port/detail/WineventPoll.h" +#endif #if TD_PORT_POSIX #include @@ -25,11 +31,8 @@ #endif #endif // TD_PORT_POSIX -#if TD_PORT_WINDOWS -#include -#include "td/utils/port/Poll.h" -#include "td/utils/VectorQueue.h" -#endif +#include +#include namespace td { namespace detail { @@ -64,7 +67,7 @@ class UdpSocketReceiveHelper { message.data.truncate(message_size); CHECK(message_size == message.data.size()); if (message_size >= 1500) { - LOG(ERROR) << "received datagram of size " << message_size; + LOG(ERROR) << "Received datagram of size " << message_size; } } @@ -94,7 +97,7 @@ class UdpSocketSendHelper { class UdpSocketFdImpl : private IOCP::Callback { public: - UdpSocketFdImpl(NativeFd fd) : info(std::move(fd)) { + explicit UdpSocketFdImpl(NativeFd fd) : info(std::move(fd)) { get_poll_info().add_flags(PollFlags::Write()); IOCP::get()->subscribe(get_native_fd(), this); is_receive_active_ = true; @@ -176,7 +179,7 @@ class UdpSocketFdImpl : private IOCP::Callback { if (status == 0) { return true; } - auto last_error = GetLastError(); + auto last_error = WSAGetLastError(); if (last_error == ERROR_IO_PENDING) { return true; } @@ -296,7 +299,7 @@ class UdpSocketFdImpl : private IOCP::Callback { receive_buffer_.confirm_read((to_receive_.data.size() + 7) & ~7); { auto lock = lock_.lock(); - LOG(ERROR) << format::escaped(to_receive_.data.as_slice()); + // LOG(ERROR) << format::escaped(to_receive_.data.as_slice()); receive_queue_.push(std::move(to_receive_)); } get_poll_info().add_flags_from_poll(PollFlags::Read()); @@ -324,7 +327,6 @@ class UdpSocketFdImpl : private IOCP::Callback { bool dec_refcnt() { if (--refcnt_ == 0) { - LOG(ERROR) << "DELETE"; delete this; return true; } @@ -465,7 +467,7 @@ class UdpSocketSendHelper { class UdpSocketFdImpl { public: - UdpSocketFdImpl(NativeFd fd) : info(std::move(fd)) { + explicit UdpSocketFdImpl(NativeFd fd) : info(std::move(fd)) { } PollableFdInfo &get_poll_info() { return info; @@ -581,11 +583,11 @@ class UdpSocketFdImpl { case EMSGSIZE: case EPERM: LOG(WARNING) << "Silently drop packet :( " << error; - //TODO: get errors from MSG_ERRQUEUE is possible + //TODO: get errors from MSG_ERRQUEUE is possible is_sent = true; return error; - // Some general problems, wich may be fixed in future + // Some general problems, which may be fixed in future case ENOMEM: case EDQUOT: case EFBIG: @@ -658,7 +660,7 @@ class UdpSocketFdImpl { //}; struct std::array helpers; struct std::array headers; - size_t to_send = std::min(messages.size(), headers.size()); + size_t to_send = td::min(messages.size(), headers.size()); for (size_t i = 0; i < to_send; i++) { helpers[i].to_native(messages[i], headers[i].msg_hdr); headers[i].msg_len = 0; @@ -709,7 +711,7 @@ class UdpSocketFdImpl { //}; struct std::array helpers; struct std::array headers; - size_t to_receive = std::min(messages.size(), headers.size()); + size_t to_receive = td::min(messages.size(), headers.size()); for (size_t i = 0; i < to_receive; i++) { helpers[i].to_native(messages[i], headers[i].msg_hdr); headers[i].msg_len = 0; @@ -751,24 +753,6 @@ const PollableFdInfo &UdpSocketFd::get_poll_info() const { return impl_->get_poll_info(); } -//Result UdpSocketFd::from_native_fd(int fd) { -//auto fd_guard = ScopeExit() + [fd]() { ::close(fd); }; - -//TRY_STATUS(detail::set_native_socket_is_blocking(fd, false)); - -//// TODO remove copypaste -//int flags = 1; -//setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast(&flags), sizeof(flags)); -//setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, reinterpret_cast(&flags), sizeof(flags)); -//// TODO: SO_REUSEADDR, SO_KEEPALIVE, TCP_NODELAY, SO_SNDBUF, SO_RCVBUF, TCP_QUICKACK, SO_LINGER - -//fd_guard.dismiss(); - -//UdpSocketFd socket; -//socket.fd_ = Fd(fd, Fd::Mode::Owner); -//return std::move(socket); -//} - Result UdpSocketFd::open(const IPAddress &address) { NativeFd native_fd{socket(address.get_address_family(), SOCK_DGRAM, IPPROTO_UDP)}; if (!native_fd) { diff --git a/tdutils/td/utils/port/UdpSocketFd.h b/tdutils/td/utils/port/UdpSocketFd.h index 26f8cdf1d..81e23930f 100644 --- a/tdutils/td/utils/port/UdpSocketFd.h +++ b/tdutils/td/utils/port/UdpSocketFd.h @@ -8,13 +8,14 @@ #include "td/utils/port/config.h" +#include "td/utils/buffer.h" +#include "td/utils/optional.h" +#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/IPAddress.h" #include "td/utils/Slice.h" -#include "td/utils/Status.h" -#include "td/utils/buffer.h" #include "td/utils/Span.h" -#include "td/utils/optional.h" +#include "td/utils/Status.h" namespace td { // Udp and errors diff --git a/tdutils/td/utils/port/detail/Epoll.h b/tdutils/td/utils/port/detail/Epoll.h index 36ae3c604..a6e024d2f 100644 --- a/tdutils/td/utils/port/detail/Epoll.h +++ b/tdutils/td/utils/port/detail/Epoll.h @@ -11,7 +11,7 @@ #ifdef TD_POLL_EPOLL #include "td/utils/common.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/PollBase.h" #include diff --git a/tdutils/td/utils/port/detail/EventFdBsd.cpp b/tdutils/td/utils/port/detail/EventFdBsd.cpp index 53768733e..e310666cb 100644 --- a/tdutils/td/utils/port/detail/EventFdBsd.cpp +++ b/tdutils/td/utils/port/detail/EventFdBsd.cpp @@ -11,8 +11,9 @@ char disable_linker_warning_about_empty_file_event_fd_bsd_cpp TD_UNUSED; #ifdef TD_EVENTFD_BSD #include "td/utils/logging.h" -#include "td/utils/Slice.h" +#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/SocketFd.h" +#include "td/utils/Slice.h" #include #include diff --git a/tdutils/td/utils/port/detail/EventFdBsd.h b/tdutils/td/utils/port/detail/EventFdBsd.h index 2fae5a21f..2f511dfb8 100644 --- a/tdutils/td/utils/port/detail/EventFdBsd.h +++ b/tdutils/td/utils/port/detail/EventFdBsd.h @@ -11,6 +11,7 @@ #ifdef TD_EVENTFD_BSD #include "td/utils/common.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/EventFdBase.h" #include "td/utils/port/SocketFd.h" #include "td/utils/Status.h" diff --git a/tdutils/td/utils/port/detail/EventFdLinux.cpp b/tdutils/td/utils/port/detail/EventFdLinux.cpp index b89f8cbc1..af43ed12a 100644 --- a/tdutils/td/utils/port/detail/EventFdLinux.cpp +++ b/tdutils/td/utils/port/detail/EventFdLinux.cpp @@ -6,14 +6,13 @@ // #include "td/utils/port/detail/EventFdLinux.h" -#include "td/utils/misc.h" - char disable_linker_warning_about_empty_file_event_fd_linux_cpp TD_UNUSED; #ifdef TD_EVENTFD_LINUX -#include "td/utils/port/detail/PollableFd.h" #include "td/utils/logging.h" +#include "td/utils/misc.h" +#include "td/utils/port/detail/NativeFd.h" #include "td/utils/Slice.h" #include diff --git a/tdutils/td/utils/port/detail/EventFdLinux.h b/tdutils/td/utils/port/detail/EventFdLinux.h index 72b2f21d8..933922c9c 100644 --- a/tdutils/td/utils/port/detail/EventFdLinux.h +++ b/tdutils/td/utils/port/detail/EventFdLinux.h @@ -11,8 +11,8 @@ #ifdef TD_EVENTFD_LINUX #include "td/utils/common.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/EventFdBase.h" -#include "td/utils/port/SocketFd.h" #include "td/utils/Status.h" namespace td { diff --git a/tdutils/td/utils/port/detail/EventFdWindows.h b/tdutils/td/utils/port/detail/EventFdWindows.h index 4dee01812..085c30346 100644 --- a/tdutils/td/utils/port/detail/EventFdWindows.h +++ b/tdutils/td/utils/port/detail/EventFdWindows.h @@ -11,8 +11,9 @@ #ifdef TD_EVENTFD_WINDOWS #include "td/utils/common.h" -#include "td/utils/port/EventFdBase.h" +#include "td/utils/port/detail/NativeFd.h" #include "td/utils/port/detail/PollableFd.h" +#include "td/utils/port/EventFdBase.h" #include "td/utils/Status.h" namespace td { diff --git a/tdutils/td/utils/port/detail/KQueue.h b/tdutils/td/utils/port/detail/KQueue.h index 6521736d1..0e26df16b 100644 --- a/tdutils/td/utils/port/detail/KQueue.h +++ b/tdutils/td/utils/port/detail/KQueue.h @@ -11,7 +11,7 @@ #ifdef TD_POLL_KQUEUE #include "td/utils/common.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/PollBase.h" #include diff --git a/tdutils/td/utils/port/detail/NativeFd.cpp b/tdutils/td/utils/port/detail/NativeFd.cpp index 85043a0ad..60db785e4 100644 --- a/tdutils/td/utils/port/detail/NativeFd.cpp +++ b/tdutils/td/utils/port/detail/NativeFd.cpp @@ -5,45 +5,54 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #include "td/utils/port/detail/NativeFd.h" -#include "td/utils/logging.h" -#include "td/utils/Status.h" #include "td/utils/format.h" +#include "td/utils/logging.h" +#include "td/utils/Status.h" #if TD_PORT_POSIX #include #endif namespace td { -NativeFd::NativeFd(NativeFd::Raw raw) : fd_(raw) { + +NativeFd::NativeFd(Raw raw) : fd_(raw) { VLOG(fd) << *this << " create"; } -NativeFd::NativeFd(NativeFd::Raw raw, bool nolog) : fd_(raw) { + +NativeFd::NativeFd(Raw raw, bool nolog) : fd_(raw) { } + #if TD_PORT_WINDOWS NativeFd::NativeFd(SOCKET raw) : fd_(reinterpret_cast(raw)), is_socket_(true) { VLOG(fd) << *this << " create"; } #endif + NativeFd::~NativeFd() { close(); } + NativeFd::operator bool() const { return fd_.get() != empty_raw(); } -constexpr NativeFd::Raw NativeFd::empty_raw() { + +NativeFd::Raw NativeFd::empty_raw() { #if TD_PORT_POSIX return -1; #elif TD_PORT_WINDOWS return INVALID_HANDLE_VALUE; #endif } + NativeFd::Raw NativeFd::raw() const { return fd_.get(); } + NativeFd::Raw NativeFd::fd() const { return raw(); } + #if TD_PORT_WINDOWS NativeFd::Raw NativeFd::io_handle() const { return raw(); @@ -57,13 +66,14 @@ NativeFd::Raw NativeFd::socket() const { return raw(); } #endif + void NativeFd::close() { if (!*this) { return; } VLOG(fd) << *this << " close"; #if TD_PORT_WINDOWS - if (!CloseHandle(io_handle())) { + if (is_socket_ ? closesocket(socket()) : !CloseHandle(io_handle())) { #elif TD_PORT_POSIX if (::close(fd()) < 0) { #endif @@ -72,6 +82,7 @@ void NativeFd::close() { } fd_ = {}; } + NativeFd::Raw NativeFd::release() { VLOG(fd) << *this << " release"; auto res = fd_.get(); @@ -80,7 +91,7 @@ NativeFd::Raw NativeFd::release() { } StringBuilder &operator<<(StringBuilder &sb, const NativeFd &fd) { - sb << tag("fd", fd.raw()); - return sb; + return sb << tag("fd", fd.raw()); } + } // namespace td diff --git a/tdutils/td/utils/port/detail/NativeFd.h b/tdutils/td/utils/port/detail/NativeFd.h index 1637035ae..4f80e9cda 100644 --- a/tdutils/td/utils/port/detail/NativeFd.h +++ b/tdutils/td/utils/port/detail/NativeFd.h @@ -5,14 +5,14 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once -#include "td/utils/port/config.h" -#include "td/utils/common.h" +#include "td/utils/port/config.h" + +#include "td/utils/common.h" #include "td/utils/MovableValue.h" + namespace td { -class StringBuilder; -} -namespace td { + class NativeFd { public: #if TD_PORT_POSIX @@ -24,6 +24,7 @@ class NativeFd { NativeFd(NativeFd &&) = default; NativeFd &operator=(NativeFd &&) = default; explicit NativeFd(Raw raw); + NativeFd &operator=(const NativeFd &) = delete; NativeFd(Raw raw, bool nolog); #if TD_PORT_WINDOWS explicit NativeFd(SOCKET raw); @@ -31,7 +32,7 @@ class NativeFd { ~NativeFd(); explicit operator bool() const; - static constexpr Raw empty_raw(); + static Raw empty_raw(); Raw raw() const; Raw fd() const; #if TD_PORT_WINDOWS @@ -52,5 +53,7 @@ class NativeFd { #endif }; +class StringBuilder; StringBuilder &operator<<(StringBuilder &sb, const NativeFd &fd); + } // namespace td diff --git a/tdutils/td/utils/port/detail/Poll.h b/tdutils/td/utils/port/detail/Poll.h index 7f6e28ac7..61d92b9aa 100644 --- a/tdutils/td/utils/port/detail/Poll.h +++ b/tdutils/td/utils/port/detail/Poll.h @@ -11,7 +11,7 @@ #ifdef TD_POLL_POLL #include "td/utils/common.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/PollBase.h" #include diff --git a/tdutils/td/utils/port/detail/PollableFd.h b/tdutils/td/utils/port/detail/PollableFd.h index defb08669..0392f1285 100644 --- a/tdutils/td/utils/port/detail/PollableFd.h +++ b/tdutils/td/utils/port/detail/PollableFd.h @@ -5,16 +5,17 @@ // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #pragma once -#include "td/utils/Status.h" -#include "td/utils/List.h" -#include "td/utils/Observer.h" -#include "td/utils/MovableValue.h" -#include "td/utils/SpinLock.h" -#include "td/utils/format.h" +#include "td/utils/format.h" +#include "td/utils/List.h" +#include "td/utils/MovableValue.h" +#include "td/utils/Observer.h" #include "td/utils/port/detail/NativeFd.h" +#include "td/utils/SpinLock.h" +#include "td/utils/Status.h" #include + namespace td { class ObserverBase; @@ -321,6 +322,7 @@ inline PollFlags PollableFd::get_flags_unsafe() const { inline const NativeFd &PollableFd::native_fd() const { return fd_info_->native_fd(); } + #if TD_PORT_POSIX template auto skip_eintr(F &&f) { diff --git a/tdutils/td/utils/port/detail/Select.h b/tdutils/td/utils/port/detail/Select.h index dcd7d5c96..ae303ff41 100644 --- a/tdutils/td/utils/port/detail/Select.h +++ b/tdutils/td/utils/port/detail/Select.h @@ -11,6 +11,7 @@ #ifdef TD_POLL_SELECT #include "td/utils/common.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/PollBase.h" #include diff --git a/tdutils/td/utils/port/detail/ThreadPthread.h b/tdutils/td/utils/port/detail/ThreadPthread.h index edb93ae09..11199dca5 100644 --- a/tdutils/td/utils/port/detail/ThreadPthread.h +++ b/tdutils/td/utils/port/detail/ThreadPthread.h @@ -11,6 +11,7 @@ #ifdef TD_THREAD_PTHREAD #include "td/utils/common.h" +#include "td/utils/Destructor.h" #include "td/utils/invoke.h" #include "td/utils/MovableValue.h" #include "td/utils/port/detail/ThreadIdGuard.h" diff --git a/tdutils/td/utils/port/detail/WineventPoll.cpp b/tdutils/td/utils/port/detail/WineventPoll.cpp index a7fc24587..f23cfb334 100644 --- a/tdutils/td/utils/port/detail/WineventPoll.cpp +++ b/tdutils/td/utils/port/detail/WineventPoll.cpp @@ -13,9 +13,7 @@ char disable_linker_warning_about_empty_file_wineventpoll_cpp TD_UNUSED; #include "td/utils/common.h" #include "td/utils/logging.h" #include "td/utils/misc.h" -#include "td/utils/port/Fd.h" #include "td/utils/port/PollBase.h" -#include "td/utils/port/sleep.h" #include "td/utils/Status.h" #include @@ -32,14 +30,14 @@ void IOCP::loop() { DWORD bytes = 0; ULONG_PTR key = 0; OVERLAPPED *overlapped = nullptr; - bool ok = GetQueuedCompletionStatus(iocp_handle_.io_handle(), &bytes, &key, &overlapped, 1000); + BOOL ok = GetQueuedCompletionStatus(iocp_handle_.io_handle(), &bytes, &key, &overlapped, 1000); if (bytes || key || overlapped) { - LOG(ERROR) << "Got iocp " << bytes << " " << key << " " << overlapped; + // LOG(ERROR) << "Got iocp " << bytes << " " << key << " " << overlapped; } if (ok) { auto callback = reinterpret_cast(key); if (callback == nullptr) { - LOG(ERROR) << "Interrupt IOCP loop"; + // LOG(ERROR) << "Interrupt IOCP loop"; return; } callback->on_iocp(bytes, overlapped); diff --git a/tdutils/td/utils/port/detail/WineventPoll.h b/tdutils/td/utils/port/detail/WineventPoll.h index 62ca8bc22..3e7a205ae 100644 --- a/tdutils/td/utils/port/detail/WineventPoll.h +++ b/tdutils/td/utils/port/detail/WineventPoll.h @@ -12,7 +12,8 @@ #include "td/utils/common.h" #include "td/utils/Context.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/NativeFd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/PollBase.h" #include "td/utils/port/thread.h" @@ -70,8 +71,6 @@ class WineventPoll final : public PollBase { static bool is_edge_triggered() { return true; } - - private: }; } // namespace detail diff --git a/tdutils/td/utils/port/path.cpp b/tdutils/td/utils/port/path.cpp index d0147ab2b..60be43823 100644 --- a/tdutils/td/utils/port/path.cpp +++ b/tdutils/td/utils/port/path.cpp @@ -6,8 +6,6 @@ // #include "td/utils/port/path.h" -#include "td/utils/port/Fd.h" - #if TD_WINDOWS #include "td/utils/Random.h" #endif diff --git a/tdutils/td/utils/port/thread_local.h b/tdutils/td/utils/port/thread_local.h index 38d921ff0..4c7621c03 100644 --- a/tdutils/td/utils/port/thread_local.h +++ b/tdutils/td/utils/port/thread_local.h @@ -50,7 +50,7 @@ void do_init_thread_local(P &raw_ptr, ArgsT &&... args) { ptr.reset(); raw_ptr = nullptr; })); -} // namespace detail +} } // namespace detail template diff --git a/tdutils/td/utils/type_traits.h b/tdutils/td/utils/type_traits.h index 0511fa03a..e91f41cbb 100644 --- a/tdutils/td/utils/type_traits.h +++ b/tdutils/td/utils/type_traits.h @@ -14,7 +14,7 @@ struct member_function_class; template struct member_function_class { using type = Type; - constexpr static size_t arguments_count() { + static constexpr size_t argument_count() { return sizeof...(Args); } }; @@ -23,8 +23,8 @@ template using member_function_class_t = typename member_function_class::type; template -constexpr size_t member_function_arguments_count() { - return member_function_class::arguments_count(); +constexpr size_t member_function_argument_count() { + return member_function_class::argument_count(); } } // namespace td diff --git a/tdutils/test/buffer.cpp b/tdutils/test/buffer.cpp index ab2e5c4d9..a63467270 100644 --- a/tdutils/test/buffer.cpp +++ b/tdutils/test/buffer.cpp @@ -28,7 +28,6 @@ TEST(Buffer, buffer_builder) { builder.append(" B"); ASSERT_EQ(builder.extract().as_slice(), "A hello B"); } - //Slice slice(std::string()); { std::string str = rand_string('a', 'z', 10000); auto splitted_str = rand_split(str); diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index 6286977a4..84cbf9529 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -7,9 +7,9 @@ #include "td/utils/base64.h" #include "td/utils/BigNum.h" #include "td/utils/HttpUrl.h" +#include "td/utils/invoke.h" #include "td/utils/logging.h" #include "td/utils/misc.h" -#include "td/utils/invoke.h" #include "td/utils/port/EventFd.h" #include "td/utils/port/FileFd.h" #include "td/utils/port/IPAddress.h" diff --git a/tdutils/test/port.cpp b/tdutils/test/port.cpp index 6da6d8f68..99b0a1470 100644 --- a/tdutils/test/port.cpp +++ b/tdutils/test/port.cpp @@ -4,9 +4,11 @@ // 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/tests.h" +#include "td/utils/common.h" #include "td/utils/port/FileFd.h" #include "td/utils/port/path.h" +#include "td/utils/Slice.h" +#include "td/utils/tests.h" using namespace td; diff --git a/test/http.cpp b/test/http.cpp index eaa6d5f8c..6d79c3ca9 100644 --- a/test/http.cpp +++ b/test/http.cpp @@ -22,7 +22,7 @@ #include "td/utils/GzipByteFlow.h" #include "td/utils/logging.h" #include "td/utils/misc.h" -#include "td/utils/port/Fd.h" +#include "td/utils/port/detail/PollableFd.h" #include "td/utils/port/FileFd.h" #include "td/utils/port/path.h" #include "td/utils/port/thread_local.h"