Remove trantor NonCopyable inheritances from core.

This commit is contained in:
Relintai 2022-02-10 10:05:10 +01:00
parent b8e0579a2a
commit 3534817831
12 changed files with 683 additions and 710 deletions

View File

@ -18,6 +18,7 @@
#ifdef __linux__
#include <sys/prctl.h>
#endif
using namespace trantor;
ConcurrentTaskQueue::ConcurrentTaskQueue(size_t threadNum,
const std::string &name) :

View File

@ -17,20 +17,20 @@
#include "core/containers/task_queue.h"
#include <list>
#include <memory>
#include <vector>
#include <queue>
#include <string>
#include <vector>
namespace trantor
{
namespace trantor {
/**
* @brief This class implements a task queue running in parallel. Basically this
* can be called a threads pool.
*
*/
class ConcurrentTaskQueue : public TaskQueue
{
class ConcurrentTaskQueue : public TaskQueue {
public:
ConcurrentTaskQueue() {}
/**
* @brief Construct a new concurrent task queue instance.
*
@ -52,8 +52,7 @@ class ConcurrentTaskQueue : public TaskQueue
*
* @return std::string
*/
virtual std::string getName() const
{
virtual std::string getName() const {
return queueName_;
};

View File

@ -14,10 +14,10 @@
#pragma once
#include <assert.h>
#include <trantor/utils/NonCopyable.h>
#include <atomic>
#include <memory>
#include <type_traits>
namespace trantor {
/**
* @brief This class template represents a lock-free multiple producers single

View File

@ -14,24 +14,29 @@
#pragma once
#include "NonCopyable.h"
#include <functional>
#include <future>
#include <string>
namespace trantor
{
namespace trantor {
/**
* @brief This class is a pure virtual class that can be implemented as a
* SerialTaskQueue or a ConcurrentTaskQueue.
*
*/
class TaskQueue : public NonCopyable
{
class TaskQueue {
protected:
TaskQueue(const TaskQueue &) = delete;
TaskQueue &operator=(const TaskQueue &) = delete;
// some uncopyable classes maybe support move constructor....
TaskQueue(TaskQueue &&) noexcept(true) = default;
TaskQueue &operator=(TaskQueue &&) noexcept(true) = default;
public:
TaskQueue() {}
virtual void runTaskInQueue(const std::function<void()> &task) = 0;
virtual void runTaskInQueue(std::function<void()> &&task) = 0;
virtual std::string getName() const
{
virtual std::string getName() const {
return "";
};
@ -41,8 +46,7 @@ class TaskQueue : public NonCopyable
*
* @param task
*/
void syncTaskInQueue(const std::function<void()> &task)
{
void syncTaskInQueue(const std::function<void()> &task) {
std::promise<int> prom;
std::future<int> fut = prom.get_future();
runTaskInQueue([&]() {
@ -51,8 +55,7 @@ class TaskQueue : public NonCopyable
});
fut.get();
};
virtual ~TaskQueue()
{
virtual ~TaskQueue() {
}
};
} // namespace trantor

View File

@ -55,8 +55,8 @@ class Channel;
class Socket;
class TcpServer;
void removeConnection(EventLoop *loop, const TcpConnectionPtr &conn);
class TcpConnectionImpl : public TcpConnection,
public NonCopyable,
public std::enable_shared_from_this<TcpConnectionImpl>
{
friend class TcpServer;
@ -64,6 +64,13 @@ class TcpConnectionImpl : public TcpConnection,
friend void trantor::removeConnection(EventLoop *loop,
const TcpConnectionPtr &conn);
protected:
TcpConnectionImpl(const TcpConnectionImpl &) = delete;
TcpConnectionImpl &operator=(const TcpConnectionImpl &) = delete;
// some uncopyable classes maybe support move constructor....
TcpConnectionImpl(TcpConnectionImpl &&) noexcept(true) = default;
TcpConnectionImpl &operator=(TcpConnectionImpl &&) noexcept(true) = default;
public:
class KickoffEntry
{

View File

@ -19,34 +19,33 @@
#include <atomic>
#include <memory>
namespace trantor
{
class Connector : public NonCopyable,
public std::enable_shared_from_this<Connector>
{
namespace trantor {
class Connector : public std::enable_shared_from_this<Connector> {
protected:
Connector(const Connector &) = delete;
Connector &operator=(const Connector &) = delete;
// some uncopyable classes maybe support move constructor....
Connector(Connector &&) noexcept(true) = default;
Connector &operator=(Connector &&) noexcept(true) = default;
public:
using NewConnectionCallback = std::function<void(int sockfd)>;
using ConnectionErrorCallback = std::function<void()>;
Connector(EventLoop *loop, const InetAddress &addr, bool retry = true);
Connector(EventLoop *loop, InetAddress &&addr, bool retry = true);
void setNewConnectionCallback(const NewConnectionCallback &cb)
{
void setNewConnectionCallback(const NewConnectionCallback &cb) {
newConnectionCallback_ = cb;
}
void setNewConnectionCallback(NewConnectionCallback &&cb)
{
void setNewConnectionCallback(NewConnectionCallback &&cb) {
newConnectionCallback_ = std::move(cb);
}
void setErrorCallback(const ConnectionErrorCallback &cb)
{
void setErrorCallback(const ConnectionErrorCallback &cb) {
errorCallback_ = cb;
}
void setErrorCallback(ConnectionErrorCallback &&cb)
{
void setErrorCallback(ConnectionErrorCallback &&cb) {
errorCallback_ = std::move(cb);
}
const InetAddress &serverAddress() const
{
const InetAddress &serverAddress() const {
return serverAddr_;
}
void start();
@ -56,8 +55,7 @@ class Connector : public NonCopyable,
private:
NewConnectionCallback newConnectionCallback_;
ConnectionErrorCallback errorCallback_;
enum class Status
{
enum class Status {
Disconnected,
Connecting,
Connected

View File

@ -6,43 +6,43 @@
// Author: Tao An
#pragma once
#include "core/net/resolver.h"
#include <trantor/utils/NonCopyable.h>
#include "core/loops/event_loop_thread.h"
#include "core/net/resolver.h"
#include <string.h>
#include <map>
#include <memory>
#include <string.h>
extern "C"
{
extern "C" {
struct hostent;
struct ares_channeldata;
using ares_channel = struct ares_channeldata *;
}
namespace trantor
{
namespace trantor {
class AresResolver : public Resolver,
public NonCopyable,
public std::enable_shared_from_this<AresResolver>
{
public std::enable_shared_from_this<AresResolver> {
protected:
AresResolver(const AresResolver &) = delete;
AresResolver &operator=(const AresResolver &) = delete;
// some uncopyable classes maybe support move constructor....
AresResolver(AresResolver &&) noexcept(true) = default;
AresResolver &operator=(AresResolver &&) noexcept(true) = default;
public:
AresResolver(trantor::EventLoop *loop, size_t timeout);
~AresResolver();
virtual void resolve(const std::string &hostname,
const Callback& cb) override
{
const Callback &cb) override {
bool cached = false;
InetAddress inet;
{
std::lock_guard<std::mutex> lock(globalMutex());
auto iter = globalCache().find(hostname);
if (iter != globalCache().end())
{
if (iter != globalCache().end()) {
auto &cachedAddr = iter->second;
if (timeout_ == 0 ||
cachedAddr.second.after(timeout_) > trantor::Date::date())
{
cachedAddr.second.after(timeout_) > trantor::Date::date()) {
struct sockaddr_in addr;
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
@ -53,17 +53,13 @@ class AresResolver : public Resolver,
}
}
}
if (cached)
{
if (cached) {
cb(inet);
return;
}
if (loop_->isInLoopThread())
{
if (loop_->isInLoopThread()) {
resolveInLoop(hostname, cb);
}
else
{
} else {
loop_->queueInLoop([thisPtr = shared_from_this(), hostname, cb]() {
thisPtr->resolveInLoop(hostname, cb);
});
@ -71,16 +67,14 @@ class AresResolver : public Resolver,
}
private:
struct QueryData
{
struct QueryData {
AresResolver *owner_;
Callback callback_;
std::string hostname_;
QueryData(AresResolver *o,
const Callback &cb,
const std::string& hostname)
: owner_(o), callback_(cb), hostname_(hostname)
{
const std::string &hostname) :
owner_(o), callback_(cb), hostname_(hostname) {
}
};
void resolveInLoop(const std::string &hostname, const Callback &cb);
@ -92,20 +86,17 @@ class AresResolver : public Resolver,
ChannelList channels_;
static std::unordered_map<std::string,
std::pair<struct in_addr, trantor::Date> > &
globalCache()
{
globalCache() {
static std::unordered_map<std::string,
std::pair<struct in_addr, trantor::Date> >
dnsCache;
return dnsCache;
}
static std::mutex& globalMutex()
{
static std::mutex &globalMutex() {
static std::mutex mutex_;
return mutex_;
}
static EventLoop* getLoop()
{
static EventLoop *getLoop() {
static EventLoopThread loopThread;
loopThread.run();
return loopThread.getLoop();
@ -138,8 +129,7 @@ class AresResolver : public Resolver,
#endif
int read,
int write);
struct LibraryInitializer
{
struct LibraryInitializer {
LibraryInitializer();
~LibraryInitializer();
};

View File

@ -6,53 +6,49 @@
// Author: Tao An
#pragma once
#include "core/net/resolver.h"
#include <trantor/utils/NonCopyable.h>
#include "core/containers/concurrent_task_queue.h"
#include "core/net/resolver.h"
#include <memory>
#include <vector>
#include <thread>
#include <vector>
namespace trantor
{
namespace trantor {
constexpr size_t kResolveBufferLength{ 16 * 1024 };
class NormalResolver : public Resolver,
public NonCopyable,
public std::enable_shared_from_this<NormalResolver>
{
public std::enable_shared_from_this<NormalResolver> {
protected:
NormalResolver(const NormalResolver &) = delete;
NormalResolver &operator=(const NormalResolver &) = delete;
// some uncopyable classes maybe support move constructor....
NormalResolver(NormalResolver &&) noexcept(true) = default;
NormalResolver &operator=(NormalResolver &&) noexcept(true) = default;
public:
virtual void resolve(const std::string &hostname,
const Callback &callback) override;
explicit NormalResolver(size_t timeout)
: timeout_(timeout), resolveBuffer_(kResolveBufferLength)
{
explicit NormalResolver(size_t timeout) :
timeout_(timeout), resolveBuffer_(kResolveBufferLength) {
}
virtual ~NormalResolver()
{
virtual ~NormalResolver() {
}
private:
static std::unordered_map<std::string,
std::pair<trantor::InetAddress, trantor::Date> > &
globalCache()
{
globalCache() {
static std::unordered_map<
std::string,
std::pair<trantor::InetAddress, trantor::Date> >
dnsCache_;
return dnsCache_;
}
static std::mutex& globalMutex()
{
static std::mutex &globalMutex() {
static std::mutex mutex_;
return mutex_;
}
static trantor::ConcurrentTaskQueue& concurrentTaskQueue()
{
static trantor::ConcurrentTaskQueue &concurrentTaskQueue() {
static trantor::ConcurrentTaskQueue queue(
std::thread::hardware_concurrency() < 8
? 8
: std::thread::hardware_concurrency(),
std::thread::hardware_concurrency() < 8 ? 8 : std::thread::hardware_concurrency(),
"Dns Queue");
return queue;
}

View File

@ -28,7 +28,6 @@ namespace trantor {
class Socket {
protected:
// NonCopyable
Socket(const Socket &) = delete;
Socket &operator=(const Socket &) = delete;
// some uncopyable classes maybe support move constructor....

View File

@ -21,13 +21,12 @@
#include "core/loops/event_loop.h"
#include "core/net/inet_address.h"
#include "tcp_connection.h"
#include <signal.h>
#include <atomic>
#include <functional>
#include <thread>
#include <atomic>
#include <signal.h>
namespace trantor
{
namespace trantor {
class Connector;
using ConnectorPtr = std::shared_ptr<Connector>;
class SSLContext;
@ -35,8 +34,14 @@ class SSLContext;
* @brief This class represents a TCP client.
*
*/
class TcpClient : NonCopyable
{
class TcpClient {
protected:
TcpClient(const TcpClient &) = delete;
TcpClient &operator=(const TcpClient &) = delete;
// some uncopyable classes maybe support move constructor....
TcpClient(TcpClient &&) noexcept(true) = default;
TcpClient &operator=(TcpClient &&) noexcept(true) = default;
public:
/**
* @brief Construct a new TCP client instance.
@ -73,8 +78,7 @@ class TcpClient : NonCopyable
*
* @return TcpConnectionPtr
*/
TcpConnectionPtr connection() const
{
TcpConnectionPtr connection() const {
std::lock_guard<std::mutex> lock(mutex_);
return connection_;
}
@ -84,8 +88,7 @@ class TcpClient : NonCopyable
*
* @return EventLoop*
*/
EventLoop *getLoop() const
{
EventLoop *getLoop() const {
return loop_;
}
@ -95,8 +98,7 @@ class TcpClient : NonCopyable
* @return true
* @return false
*/
bool retry() const
{
bool retry() const {
return retry_;
}
@ -104,8 +106,7 @@ class TcpClient : NonCopyable
* @brief Enable retrying.
*
*/
void enableRetry()
{
void enableRetry() {
retry_ = true;
}
@ -114,8 +115,7 @@ class TcpClient : NonCopyable
*
* @return const std::string&
*/
const std::string &name() const
{
const std::string &name() const {
return name_;
}
@ -125,12 +125,10 @@ class TcpClient : NonCopyable
* @param cb The callback is called when the connection to the server is
* established or closed.
*/
void setConnectionCallback(const ConnectionCallback &cb)
{
void setConnectionCallback(const ConnectionCallback &cb) {
connectionCallback_ = cb;
}
void setConnectionCallback(ConnectionCallback &&cb)
{
void setConnectionCallback(ConnectionCallback &&cb) {
connectionCallback_ = std::move(cb);
}
@ -140,8 +138,7 @@ class TcpClient : NonCopyable
* @param cb The callback is called when an error occurs during connecting
* to the server.
*/
void setConnectionErrorCallback(const ConnectionErrorCallback &cb)
{
void setConnectionErrorCallback(const ConnectionErrorCallback &cb) {
connectionErrorCallback_ = cb;
}
@ -151,12 +148,10 @@ class TcpClient : NonCopyable
* @param cb The callback is called when some data is received from the
* server.
*/
void setMessageCallback(const RecvMessageCallback &cb)
{
void setMessageCallback(const RecvMessageCallback &cb) {
messageCallback_ = cb;
}
void setMessageCallback(RecvMessageCallback &&cb)
{
void setMessageCallback(RecvMessageCallback &&cb) {
messageCallback_ = std::move(cb);
}
/// Set write complete callback.
@ -168,12 +163,10 @@ class TcpClient : NonCopyable
* @param cb The callback is called when data to send is written to the
* socket.
*/
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
{
void setWriteCompleteCallback(const WriteCompleteCallback &cb) {
writeCompleteCallback_ = cb;
}
void setWriteCompleteCallback(WriteCompleteCallback &&cb)
{
void setWriteCompleteCallback(WriteCompleteCallback &&cb) {
writeCompleteCallback_ = std::move(cb);
}
@ -181,12 +174,10 @@ class TcpClient : NonCopyable
* @brief Set the callback for errors of SSL
* @param cb The callback is called when an SSL error occurs.
*/
void setSSLErrorCallback(const SSLErrorCallback &cb)
{
void setSSLErrorCallback(const SSLErrorCallback &cb) {
sslErrorCallback_ = cb;
}
void setSSLErrorCallback(SSLErrorCallback &&cb)
{
void setSSLErrorCallback(SSLErrorCallback &&cb) {
sslErrorCallback_ = std::move(cb);
}
@ -232,11 +223,9 @@ class TcpClient : NonCopyable
bool validateCert_{ false };
std::string SSLHostName_;
#ifndef _WIN32
class IgnoreSigPipe
{
class IgnoreSigPipe {
public:
IgnoreSigPipe()
{
IgnoreSigPipe() {
::signal(SIGPIPE, SIG_IGN);
}
};

View File

@ -18,7 +18,6 @@
#include "core/loops/callbacks.h"
#include "core/loops/event_loop.h"
#include "core/net/inet_address.h"
#include <trantor/utils/NonCopyable.h>
#include <functional>
#include <memory>
#include <string>

View File

@ -13,27 +13,33 @@
*/
#pragma once
#include "core/loops/callbacks.h"
#include <trantor/utils/NonCopyable.h>
#include "core/log/logger.h"
#include "core/loops/callbacks.h"
#include "core/loops/event_loop_thread_pool.h"
#include "core/loops/timing_wheel.h"
#include "core/net/inet_address.h"
#include "core/net/tcp_connection.h"
#include "core/loops/timing_wheel.h"
#include <string>
#include <signal.h>
#include <memory>
#include <set>
#include <signal.h>
namespace trantor
{
#include <string>
namespace trantor {
class Acceptor;
class SSLContext;
/**
* @brief This class represents a TCP server.
*
*/
class TcpServer : NonCopyable
{
class TcpServer {
protected:
TcpServer(const TcpServer &) = delete;
TcpServer &operator=(const TcpServer &) = delete;
// some uncopyable classes maybe support move constructor....
TcpServer(TcpServer &&) noexcept(true) = default;
TcpServer &operator=(TcpServer &&) noexcept(true) = default;
public:
/**
* @brief Construct a new TCP server instance.
@ -70,8 +76,7 @@ class TcpServer : NonCopyable
*
* @param num
*/
void setIoLoopNum(size_t num)
{
void setIoLoopNum(size_t num) {
assert(!started_);
loopPoolPtr_ = std::make_shared<EventLoopThreadPool>(num);
loopPoolPtr_->start();
@ -83,8 +88,7 @@ class TcpServer : NonCopyable
*
* @param pool
*/
void setIoLoopThreadPool(const std::shared_ptr<EventLoopThreadPool> &pool)
{
void setIoLoopThreadPool(const std::shared_ptr<EventLoopThreadPool> &pool) {
assert(pool->size() > 0);
assert(!started_);
loopPoolPtr_ = pool;
@ -97,12 +101,10 @@ class TcpServer : NonCopyable
* @param cb The callback is called when some data is received on a
* connection to the server.
*/
void setRecvMessageCallback(const RecvMessageCallback &cb)
{
void setRecvMessageCallback(const RecvMessageCallback &cb) {
recvMessageCallback_ = cb;
}
void setRecvMessageCallback(RecvMessageCallback &&cb)
{
void setRecvMessageCallback(RecvMessageCallback &&cb) {
recvMessageCallback_ = std::move(cb);
}
@ -112,12 +114,10 @@ class TcpServer : NonCopyable
* @param cb The callback is called when a connection is established or
* closed.
*/
void setConnectionCallback(const ConnectionCallback &cb)
{
void setConnectionCallback(const ConnectionCallback &cb) {
connectionCallback_ = cb;
}
void setConnectionCallback(ConnectionCallback &&cb)
{
void setConnectionCallback(ConnectionCallback &&cb) {
connectionCallback_ = std::move(cb);
}
@ -127,12 +127,10 @@ class TcpServer : NonCopyable
* @param cb The callback is called when data to send is written to the
* socket of a connection.
*/
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
{
void setWriteCompleteCallback(const WriteCompleteCallback &cb) {
writeCompleteCallback_ = cb;
}
void setWriteCompleteCallback(WriteCompleteCallback &&cb)
{
void setWriteCompleteCallback(WriteCompleteCallback &&cb) {
writeCompleteCallback_ = std::move(cb);
}
@ -141,8 +139,7 @@ class TcpServer : NonCopyable
*
* @return const std::string&
*/
const std::string &name() const
{
const std::string &name() const {
return serverName_;
}
@ -165,8 +162,7 @@ class TcpServer : NonCopyable
*
* @return EventLoop*
*/
EventLoop *getLoop() const
{
EventLoop *getLoop() const {
return loop_;
}
@ -175,8 +171,7 @@ class TcpServer : NonCopyable
*
* @return std::vector<EventLoop *>
*/
std::vector<EventLoop *> getIoLoops() const
{
std::vector<EventLoop *> getIoLoops() const {
return loopPoolPtr_->getLoops();
}
@ -186,8 +181,7 @@ class TcpServer : NonCopyable
*
* @param timeout
*/
void kickoffIdleConnections(size_t timeout)
{
void kickoffIdleConnections(size_t timeout) {
loop_->runInLoop([this, timeout]() {
assert(!started_);
idleTimeout_ = timeout;
@ -228,11 +222,9 @@ class TcpServer : NonCopyable
void connectionClosed(const TcpConnectionPtr &connectionPtr);
std::shared_ptr<EventLoopThreadPool> loopPoolPtr_;
#ifndef _WIN32
class IgnoreSigPipe
{
class IgnoreSigPipe {
public:
IgnoreSigPipe()
{
IgnoreSigPipe() {
::signal(SIGPIPE, SIG_IGN);
LOG_TRACE << "Ignore SIGPIPE";
}