mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2025-05-06 17:51:36 +02:00
Remove trantor NonCopyable inheritances from core.
This commit is contained in:
parent
b8e0579a2a
commit
3534817831
@ -18,6 +18,7 @@
|
|||||||
#ifdef __linux__
|
#ifdef __linux__
|
||||||
#include <sys/prctl.h>
|
#include <sys/prctl.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using namespace trantor;
|
using namespace trantor;
|
||||||
ConcurrentTaskQueue::ConcurrentTaskQueue(size_t threadNum,
|
ConcurrentTaskQueue::ConcurrentTaskQueue(size_t threadNum,
|
||||||
const std::string &name) :
|
const std::string &name) :
|
||||||
|
@ -17,20 +17,20 @@
|
|||||||
#include "core/containers/task_queue.h"
|
#include "core/containers/task_queue.h"
|
||||||
#include <list>
|
#include <list>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace trantor
|
namespace trantor {
|
||||||
{
|
|
||||||
/**
|
/**
|
||||||
* @brief This class implements a task queue running in parallel. Basically this
|
* @brief This class implements a task queue running in parallel. Basically this
|
||||||
* can be called a threads pool.
|
* can be called a threads pool.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class ConcurrentTaskQueue : public TaskQueue
|
class ConcurrentTaskQueue : public TaskQueue {
|
||||||
{
|
public:
|
||||||
public:
|
ConcurrentTaskQueue() {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Construct a new concurrent task queue instance.
|
* @brief Construct a new concurrent task queue instance.
|
||||||
*
|
*
|
||||||
@ -52,8 +52,7 @@ class ConcurrentTaskQueue : public TaskQueue
|
|||||||
*
|
*
|
||||||
* @return std::string
|
* @return std::string
|
||||||
*/
|
*/
|
||||||
virtual std::string getName() const
|
virtual std::string getName() const {
|
||||||
{
|
|
||||||
return queueName_;
|
return queueName_;
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -72,11 +71,11 @@ class ConcurrentTaskQueue : public TaskQueue
|
|||||||
|
|
||||||
~ConcurrentTaskQueue();
|
~ConcurrentTaskQueue();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t queueCount_;
|
size_t queueCount_;
|
||||||
std::string queueName_;
|
std::string queueName_;
|
||||||
|
|
||||||
std::queue<std::function<void()>> taskQueue_;
|
std::queue<std::function<void()> > taskQueue_;
|
||||||
std::vector<std::thread> threads_;
|
std::vector<std::thread> threads_;
|
||||||
|
|
||||||
std::mutex taskMutex_;
|
std::mutex taskMutex_;
|
||||||
|
@ -14,10 +14,10 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <trantor/utils/NonCopyable.h>
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
|
||||||
namespace trantor {
|
namespace trantor {
|
||||||
/**
|
/**
|
||||||
* @brief This class template represents a lock-free multiple producers single
|
* @brief This class template represents a lock-free multiple producers single
|
||||||
|
@ -14,24 +14,29 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "NonCopyable.h"
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <future>
|
#include <future>
|
||||||
#include <string>
|
#include <string>
|
||||||
namespace trantor
|
|
||||||
{
|
namespace trantor {
|
||||||
/**
|
/**
|
||||||
* @brief This class is a pure virtual class that can be implemented as a
|
* @brief This class is a pure virtual class that can be implemented as a
|
||||||
* SerialTaskQueue or a ConcurrentTaskQueue.
|
* SerialTaskQueue or a ConcurrentTaskQueue.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class TaskQueue : public NonCopyable
|
class TaskQueue {
|
||||||
{
|
protected:
|
||||||
public:
|
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(const std::function<void()> &task) = 0;
|
||||||
virtual void runTaskInQueue(std::function<void()> &&task) = 0;
|
virtual void runTaskInQueue(std::function<void()> &&task) = 0;
|
||||||
virtual std::string getName() const
|
virtual std::string getName() const {
|
||||||
{
|
|
||||||
return "";
|
return "";
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -41,8 +46,7 @@ class TaskQueue : public NonCopyable
|
|||||||
*
|
*
|
||||||
* @param task
|
* @param task
|
||||||
*/
|
*/
|
||||||
void syncTaskInQueue(const std::function<void()> &task)
|
void syncTaskInQueue(const std::function<void()> &task) {
|
||||||
{
|
|
||||||
std::promise<int> prom;
|
std::promise<int> prom;
|
||||||
std::future<int> fut = prom.get_future();
|
std::future<int> fut = prom.get_future();
|
||||||
runTaskInQueue([&]() {
|
runTaskInQueue([&]() {
|
||||||
@ -51,8 +55,7 @@ class TaskQueue : public NonCopyable
|
|||||||
});
|
});
|
||||||
fut.get();
|
fut.get();
|
||||||
};
|
};
|
||||||
virtual ~TaskQueue()
|
virtual ~TaskQueue() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace trantor
|
} // namespace trantor
|
||||||
|
@ -55,8 +55,8 @@ class Channel;
|
|||||||
class Socket;
|
class Socket;
|
||||||
class TcpServer;
|
class TcpServer;
|
||||||
void removeConnection(EventLoop *loop, const TcpConnectionPtr &conn);
|
void removeConnection(EventLoop *loop, const TcpConnectionPtr &conn);
|
||||||
|
|
||||||
class TcpConnectionImpl : public TcpConnection,
|
class TcpConnectionImpl : public TcpConnection,
|
||||||
public NonCopyable,
|
|
||||||
public std::enable_shared_from_this<TcpConnectionImpl>
|
public std::enable_shared_from_this<TcpConnectionImpl>
|
||||||
{
|
{
|
||||||
friend class TcpServer;
|
friend class TcpServer;
|
||||||
@ -64,6 +64,13 @@ class TcpConnectionImpl : public TcpConnection,
|
|||||||
friend void trantor::removeConnection(EventLoop *loop,
|
friend void trantor::removeConnection(EventLoop *loop,
|
||||||
const TcpConnectionPtr &conn);
|
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:
|
public:
|
||||||
class KickoffEntry
|
class KickoffEntry
|
||||||
{
|
{
|
||||||
|
@ -19,45 +19,43 @@
|
|||||||
#include <atomic>
|
#include <atomic>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
namespace trantor
|
namespace trantor {
|
||||||
{
|
class Connector : public std::enable_shared_from_this<Connector> {
|
||||||
class Connector : public NonCopyable,
|
protected:
|
||||||
public std::enable_shared_from_this<Connector>
|
Connector(const Connector &) = delete;
|
||||||
{
|
Connector &operator=(const Connector &) = delete;
|
||||||
public:
|
// 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 NewConnectionCallback = std::function<void(int sockfd)>;
|
||||||
using ConnectionErrorCallback = std::function<void()>;
|
using ConnectionErrorCallback = std::function<void()>;
|
||||||
Connector(EventLoop *loop, const InetAddress &addr, bool retry = true);
|
Connector(EventLoop *loop, const InetAddress &addr, bool retry = true);
|
||||||
Connector(EventLoop *loop, 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;
|
newConnectionCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setNewConnectionCallback(NewConnectionCallback &&cb)
|
void setNewConnectionCallback(NewConnectionCallback &&cb) {
|
||||||
{
|
|
||||||
newConnectionCallback_ = std::move(cb);
|
newConnectionCallback_ = std::move(cb);
|
||||||
}
|
}
|
||||||
void setErrorCallback(const ConnectionErrorCallback &cb)
|
void setErrorCallback(const ConnectionErrorCallback &cb) {
|
||||||
{
|
|
||||||
errorCallback_ = cb;
|
errorCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setErrorCallback(ConnectionErrorCallback &&cb)
|
void setErrorCallback(ConnectionErrorCallback &&cb) {
|
||||||
{
|
|
||||||
errorCallback_ = std::move(cb);
|
errorCallback_ = std::move(cb);
|
||||||
}
|
}
|
||||||
const InetAddress &serverAddress() const
|
const InetAddress &serverAddress() const {
|
||||||
{
|
|
||||||
return serverAddr_;
|
return serverAddr_;
|
||||||
}
|
}
|
||||||
void start();
|
void start();
|
||||||
void restart();
|
void restart();
|
||||||
void stop();
|
void stop();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
NewConnectionCallback newConnectionCallback_;
|
NewConnectionCallback newConnectionCallback_;
|
||||||
ConnectionErrorCallback errorCallback_;
|
ConnectionErrorCallback errorCallback_;
|
||||||
enum class Status
|
enum class Status {
|
||||||
{
|
|
||||||
Disconnected,
|
Disconnected,
|
||||||
Connecting,
|
Connecting,
|
||||||
Connected
|
Connected
|
||||||
@ -68,11 +66,11 @@ class Connector : public NonCopyable,
|
|||||||
EventLoop *loop_;
|
EventLoop *loop_;
|
||||||
InetAddress serverAddr_;
|
InetAddress serverAddr_;
|
||||||
|
|
||||||
std::atomic_bool connect_{false};
|
std::atomic_bool connect_{ false };
|
||||||
std::atomic<Status> status_{Status::Disconnected};
|
std::atomic<Status> status_{ Status::Disconnected };
|
||||||
|
|
||||||
int retryInterval_{kInitRetryDelayMs};
|
int retryInterval_{ kInitRetryDelayMs };
|
||||||
int maxRetryInterval_{kMaxRetryDelayMs};
|
int maxRetryInterval_{ kMaxRetryDelayMs };
|
||||||
|
|
||||||
bool retry_;
|
bool retry_;
|
||||||
|
|
||||||
|
@ -6,43 +6,43 @@
|
|||||||
// Author: Tao An
|
// Author: Tao An
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "core/net/resolver.h"
|
|
||||||
#include <trantor/utils/NonCopyable.h>
|
|
||||||
#include "core/loops/event_loop_thread.h"
|
#include "core/loops/event_loop_thread.h"
|
||||||
|
#include "core/net/resolver.h"
|
||||||
|
#include <string.h>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
extern "C"
|
extern "C" {
|
||||||
{
|
struct hostent;
|
||||||
struct hostent;
|
struct ares_channeldata;
|
||||||
struct ares_channeldata;
|
using ares_channel = struct ares_channeldata *;
|
||||||
using ares_channel = struct ares_channeldata*;
|
|
||||||
}
|
}
|
||||||
namespace trantor
|
namespace trantor {
|
||||||
{
|
|
||||||
class AresResolver : public Resolver,
|
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;
|
||||||
public:
|
AresResolver &operator=(const AresResolver &) = delete;
|
||||||
AresResolver(trantor::EventLoop* loop, size_t timeout);
|
// 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();
|
~AresResolver();
|
||||||
|
|
||||||
virtual void resolve(const std::string& hostname,
|
virtual void resolve(const std::string &hostname,
|
||||||
const Callback& cb) override
|
const Callback &cb) override {
|
||||||
{
|
|
||||||
bool cached = false;
|
bool cached = false;
|
||||||
InetAddress inet;
|
InetAddress inet;
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(globalMutex());
|
std::lock_guard<std::mutex> lock(globalMutex());
|
||||||
auto iter = globalCache().find(hostname);
|
auto iter = globalCache().find(hostname);
|
||||||
if (iter != globalCache().end())
|
if (iter != globalCache().end()) {
|
||||||
{
|
auto &cachedAddr = iter->second;
|
||||||
auto& cachedAddr = iter->second;
|
|
||||||
if (timeout_ == 0 ||
|
if (timeout_ == 0 ||
|
||||||
cachedAddr.second.after(timeout_) > trantor::Date::date())
|
cachedAddr.second.after(timeout_) > trantor::Date::date()) {
|
||||||
{
|
|
||||||
struct sockaddr_in addr;
|
struct sockaddr_in addr;
|
||||||
memset(&addr, 0, sizeof addr);
|
memset(&addr, 0, sizeof addr);
|
||||||
addr.sin_family = AF_INET;
|
addr.sin_family = AF_INET;
|
||||||
@ -53,84 +53,75 @@ class AresResolver : public Resolver,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (cached)
|
if (cached) {
|
||||||
{
|
|
||||||
cb(inet);
|
cb(inet);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (loop_->isInLoopThread())
|
if (loop_->isInLoopThread()) {
|
||||||
{
|
|
||||||
resolveInLoop(hostname, cb);
|
resolveInLoop(hostname, cb);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
loop_->queueInLoop([thisPtr = shared_from_this(), hostname, cb]() {
|
loop_->queueInLoop([thisPtr = shared_from_this(), hostname, cb]() {
|
||||||
thisPtr->resolveInLoop(hostname, cb);
|
thisPtr->resolveInLoop(hostname, cb);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
struct QueryData
|
struct QueryData {
|
||||||
{
|
AresResolver *owner_;
|
||||||
AresResolver* owner_;
|
|
||||||
Callback callback_;
|
Callback callback_;
|
||||||
std::string hostname_;
|
std::string hostname_;
|
||||||
QueryData(AresResolver* o,
|
QueryData(AresResolver *o,
|
||||||
const Callback& cb,
|
const Callback &cb,
|
||||||
const std::string& hostname)
|
const std::string &hostname) :
|
||||||
: owner_(o), callback_(cb), hostname_(hostname)
|
owner_(o), callback_(cb), hostname_(hostname) {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
void resolveInLoop(const std::string& hostname, const Callback& cb);
|
void resolveInLoop(const std::string &hostname, const Callback &cb);
|
||||||
void init();
|
void init();
|
||||||
trantor::EventLoop* loop_;
|
trantor::EventLoop *loop_;
|
||||||
ares_channel ctx_{nullptr};
|
ares_channel ctx_{ nullptr };
|
||||||
bool timerActive_{false};
|
bool timerActive_{ false };
|
||||||
using ChannelList = std::map<int, std::unique_ptr<trantor::Channel>>;
|
using ChannelList = std::map<int, std::unique_ptr<trantor::Channel> >;
|
||||||
ChannelList channels_;
|
ChannelList channels_;
|
||||||
static std::unordered_map<std::string,
|
static std::unordered_map<std::string,
|
||||||
std::pair<struct in_addr, trantor::Date>>&
|
std::pair<struct in_addr, trantor::Date> > &
|
||||||
globalCache()
|
globalCache() {
|
||||||
{
|
|
||||||
static std::unordered_map<std::string,
|
static std::unordered_map<std::string,
|
||||||
std::pair<struct in_addr, trantor::Date>>
|
std::pair<struct in_addr, trantor::Date> >
|
||||||
dnsCache;
|
dnsCache;
|
||||||
return dnsCache;
|
return dnsCache;
|
||||||
}
|
}
|
||||||
static std::mutex& globalMutex()
|
static std::mutex &globalMutex() {
|
||||||
{
|
|
||||||
static std::mutex mutex_;
|
static std::mutex mutex_;
|
||||||
return mutex_;
|
return mutex_;
|
||||||
}
|
}
|
||||||
static EventLoop* getLoop()
|
static EventLoop *getLoop() {
|
||||||
{
|
|
||||||
static EventLoopThread loopThread;
|
static EventLoopThread loopThread;
|
||||||
loopThread.run();
|
loopThread.run();
|
||||||
return loopThread.getLoop();
|
return loopThread.getLoop();
|
||||||
}
|
}
|
||||||
const size_t timeout_{60};
|
const size_t timeout_{ 60 };
|
||||||
|
|
||||||
void onRead(int sockfd);
|
void onRead(int sockfd);
|
||||||
void onTimer();
|
void onTimer();
|
||||||
void onQueryResult(int status,
|
void onQueryResult(int status,
|
||||||
struct hostent* result,
|
struct hostent *result,
|
||||||
const std::string& hostname,
|
const std::string &hostname,
|
||||||
const Callback& callback);
|
const Callback &callback);
|
||||||
void onSockCreate(int sockfd, int type);
|
void onSockCreate(int sockfd, int type);
|
||||||
void onSockStateChange(int sockfd, bool read, bool write);
|
void onSockStateChange(int sockfd, bool read, bool write);
|
||||||
|
|
||||||
static void ares_hostcallback_(void* data,
|
static void ares_hostcallback_(void *data,
|
||||||
int status,
|
int status,
|
||||||
int timeouts,
|
int timeouts,
|
||||||
struct hostent* hostent);
|
struct hostent *hostent);
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
static int ares_sock_createcallback_(SOCKET sockfd, int type, void* data);
|
static int ares_sock_createcallback_(SOCKET sockfd, int type, void *data);
|
||||||
#else
|
#else
|
||||||
static int ares_sock_createcallback_(int sockfd, int type, void* data);
|
static int ares_sock_createcallback_(int sockfd, int type, void *data);
|
||||||
#endif
|
#endif
|
||||||
static void ares_sock_statecallback_(void* data,
|
static void ares_sock_statecallback_(void *data,
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
SOCKET sockfd,
|
SOCKET sockfd,
|
||||||
#else
|
#else
|
||||||
@ -138,8 +129,7 @@ class AresResolver : public Resolver,
|
|||||||
#endif
|
#endif
|
||||||
int read,
|
int read,
|
||||||
int write);
|
int write);
|
||||||
struct LibraryInitializer
|
struct LibraryInitializer {
|
||||||
{
|
|
||||||
LibraryInitializer();
|
LibraryInitializer();
|
||||||
~LibraryInitializer();
|
~LibraryInitializer();
|
||||||
};
|
};
|
||||||
|
@ -6,53 +6,49 @@
|
|||||||
// Author: Tao An
|
// Author: Tao An
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "core/net/resolver.h"
|
|
||||||
#include <trantor/utils/NonCopyable.h>
|
|
||||||
#include "core/containers/concurrent_task_queue.h"
|
#include "core/containers/concurrent_task_queue.h"
|
||||||
|
#include "core/net/resolver.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace trantor
|
namespace trantor {
|
||||||
{
|
constexpr size_t kResolveBufferLength{ 16 * 1024 };
|
||||||
constexpr size_t kResolveBufferLength{16 * 1024};
|
|
||||||
class NormalResolver : public Resolver,
|
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;
|
||||||
public:
|
NormalResolver &operator=(const NormalResolver &) = delete;
|
||||||
virtual void resolve(const std::string& hostname,
|
// some uncopyable classes maybe support move constructor....
|
||||||
const Callback& callback) override;
|
NormalResolver(NormalResolver &&) noexcept(true) = default;
|
||||||
explicit NormalResolver(size_t timeout)
|
NormalResolver &operator=(NormalResolver &&) noexcept(true) = default;
|
||||||
: timeout_(timeout), resolveBuffer_(kResolveBufferLength)
|
|
||||||
{
|
public:
|
||||||
|
virtual void resolve(const std::string &hostname,
|
||||||
|
const Callback &callback) override;
|
||||||
|
explicit NormalResolver(size_t timeout) :
|
||||||
|
timeout_(timeout), resolveBuffer_(kResolveBufferLength) {
|
||||||
}
|
}
|
||||||
virtual ~NormalResolver()
|
virtual ~NormalResolver() {
|
||||||
{
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::unordered_map<std::string,
|
static std::unordered_map<std::string,
|
||||||
std::pair<trantor::InetAddress, trantor::Date>>&
|
std::pair<trantor::InetAddress, trantor::Date> > &
|
||||||
globalCache()
|
globalCache() {
|
||||||
{
|
|
||||||
static std::unordered_map<
|
static std::unordered_map<
|
||||||
std::string,
|
std::string,
|
||||||
std::pair<trantor::InetAddress, trantor::Date>>
|
std::pair<trantor::InetAddress, trantor::Date> >
|
||||||
dnsCache_;
|
dnsCache_;
|
||||||
return dnsCache_;
|
return dnsCache_;
|
||||||
}
|
}
|
||||||
static std::mutex& globalMutex()
|
static std::mutex &globalMutex() {
|
||||||
{
|
|
||||||
static std::mutex mutex_;
|
static std::mutex mutex_;
|
||||||
return mutex_;
|
return mutex_;
|
||||||
}
|
}
|
||||||
static trantor::ConcurrentTaskQueue& concurrentTaskQueue()
|
static trantor::ConcurrentTaskQueue &concurrentTaskQueue() {
|
||||||
{
|
|
||||||
static trantor::ConcurrentTaskQueue queue(
|
static trantor::ConcurrentTaskQueue queue(
|
||||||
std::thread::hardware_concurrency() < 8
|
std::thread::hardware_concurrency() < 8 ? 8 : std::thread::hardware_concurrency(),
|
||||||
? 8
|
|
||||||
: std::thread::hardware_concurrency(),
|
|
||||||
"Dns Queue");
|
"Dns Queue");
|
||||||
return queue;
|
return queue;
|
||||||
}
|
}
|
||||||
|
@ -28,7 +28,6 @@ namespace trantor {
|
|||||||
class Socket {
|
class Socket {
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
// NonCopyable
|
|
||||||
Socket(const Socket &) = delete;
|
Socket(const Socket &) = delete;
|
||||||
Socket &operator=(const Socket &) = delete;
|
Socket &operator=(const Socket &) = delete;
|
||||||
// some uncopyable classes maybe support move constructor....
|
// some uncopyable classes maybe support move constructor....
|
||||||
|
@ -21,13 +21,12 @@
|
|||||||
#include "core/loops/event_loop.h"
|
#include "core/loops/event_loop.h"
|
||||||
#include "core/net/inet_address.h"
|
#include "core/net/inet_address.h"
|
||||||
#include "tcp_connection.h"
|
#include "tcp_connection.h"
|
||||||
|
#include <signal.h>
|
||||||
|
#include <atomic>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <atomic>
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
namespace trantor
|
namespace trantor {
|
||||||
{
|
|
||||||
class Connector;
|
class Connector;
|
||||||
using ConnectorPtr = std::shared_ptr<Connector>;
|
using ConnectorPtr = std::shared_ptr<Connector>;
|
||||||
class SSLContext;
|
class SSLContext;
|
||||||
@ -35,9 +34,15 @@ class SSLContext;
|
|||||||
* @brief This class represents a TCP client.
|
* @brief This class represents a TCP client.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class TcpClient : NonCopyable
|
class TcpClient {
|
||||||
{
|
protected:
|
||||||
public:
|
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.
|
* @brief Construct a new TCP client instance.
|
||||||
*
|
*
|
||||||
@ -73,8 +78,7 @@ class TcpClient : NonCopyable
|
|||||||
*
|
*
|
||||||
* @return TcpConnectionPtr
|
* @return TcpConnectionPtr
|
||||||
*/
|
*/
|
||||||
TcpConnectionPtr connection() const
|
TcpConnectionPtr connection() const {
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lock(mutex_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
return connection_;
|
return connection_;
|
||||||
}
|
}
|
||||||
@ -84,8 +88,7 @@ class TcpClient : NonCopyable
|
|||||||
*
|
*
|
||||||
* @return EventLoop*
|
* @return EventLoop*
|
||||||
*/
|
*/
|
||||||
EventLoop *getLoop() const
|
EventLoop *getLoop() const {
|
||||||
{
|
|
||||||
return loop_;
|
return loop_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,8 +98,7 @@ class TcpClient : NonCopyable
|
|||||||
* @return true
|
* @return true
|
||||||
* @return false
|
* @return false
|
||||||
*/
|
*/
|
||||||
bool retry() const
|
bool retry() const {
|
||||||
{
|
|
||||||
return retry_;
|
return retry_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,8 +106,7 @@ class TcpClient : NonCopyable
|
|||||||
* @brief Enable retrying.
|
* @brief Enable retrying.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
void enableRetry()
|
void enableRetry() {
|
||||||
{
|
|
||||||
retry_ = true;
|
retry_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -114,8 +115,7 @@ class TcpClient : NonCopyable
|
|||||||
*
|
*
|
||||||
* @return const std::string&
|
* @return const std::string&
|
||||||
*/
|
*/
|
||||||
const std::string &name() const
|
const std::string &name() const {
|
||||||
{
|
|
||||||
return name_;
|
return name_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -125,12 +125,10 @@ class TcpClient : NonCopyable
|
|||||||
* @param cb The callback is called when the connection to the server is
|
* @param cb The callback is called when the connection to the server is
|
||||||
* established or closed.
|
* established or closed.
|
||||||
*/
|
*/
|
||||||
void setConnectionCallback(const ConnectionCallback &cb)
|
void setConnectionCallback(const ConnectionCallback &cb) {
|
||||||
{
|
|
||||||
connectionCallback_ = cb;
|
connectionCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setConnectionCallback(ConnectionCallback &&cb)
|
void setConnectionCallback(ConnectionCallback &&cb) {
|
||||||
{
|
|
||||||
connectionCallback_ = std::move(cb);
|
connectionCallback_ = std::move(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -140,8 +138,7 @@ class TcpClient : NonCopyable
|
|||||||
* @param cb The callback is called when an error occurs during connecting
|
* @param cb The callback is called when an error occurs during connecting
|
||||||
* to the server.
|
* to the server.
|
||||||
*/
|
*/
|
||||||
void setConnectionErrorCallback(const ConnectionErrorCallback &cb)
|
void setConnectionErrorCallback(const ConnectionErrorCallback &cb) {
|
||||||
{
|
|
||||||
connectionErrorCallback_ = cb;
|
connectionErrorCallback_ = cb;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -151,12 +148,10 @@ class TcpClient : NonCopyable
|
|||||||
* @param cb The callback is called when some data is received from the
|
* @param cb The callback is called when some data is received from the
|
||||||
* server.
|
* server.
|
||||||
*/
|
*/
|
||||||
void setMessageCallback(const RecvMessageCallback &cb)
|
void setMessageCallback(const RecvMessageCallback &cb) {
|
||||||
{
|
|
||||||
messageCallback_ = cb;
|
messageCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setMessageCallback(RecvMessageCallback &&cb)
|
void setMessageCallback(RecvMessageCallback &&cb) {
|
||||||
{
|
|
||||||
messageCallback_ = std::move(cb);
|
messageCallback_ = std::move(cb);
|
||||||
}
|
}
|
||||||
/// Set write complete callback.
|
/// 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
|
* @param cb The callback is called when data to send is written to the
|
||||||
* socket.
|
* socket.
|
||||||
*/
|
*/
|
||||||
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
|
void setWriteCompleteCallback(const WriteCompleteCallback &cb) {
|
||||||
{
|
|
||||||
writeCompleteCallback_ = cb;
|
writeCompleteCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setWriteCompleteCallback(WriteCompleteCallback &&cb)
|
void setWriteCompleteCallback(WriteCompleteCallback &&cb) {
|
||||||
{
|
|
||||||
writeCompleteCallback_ = std::move(cb);
|
writeCompleteCallback_ = std::move(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -181,12 +174,10 @@ class TcpClient : NonCopyable
|
|||||||
* @brief Set the callback for errors of SSL
|
* @brief Set the callback for errors of SSL
|
||||||
* @param cb The callback is called when an SSL error occurs.
|
* @param cb The callback is called when an SSL error occurs.
|
||||||
*/
|
*/
|
||||||
void setSSLErrorCallback(const SSLErrorCallback &cb)
|
void setSSLErrorCallback(const SSLErrorCallback &cb) {
|
||||||
{
|
|
||||||
sslErrorCallback_ = cb;
|
sslErrorCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setSSLErrorCallback(SSLErrorCallback &&cb)
|
void setSSLErrorCallback(SSLErrorCallback &&cb) {
|
||||||
{
|
|
||||||
sslErrorCallback_ = std::move(cb);
|
sslErrorCallback_ = std::move(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -206,10 +197,10 @@ class TcpClient : NonCopyable
|
|||||||
void enableSSL(bool useOldTLS = false,
|
void enableSSL(bool useOldTLS = false,
|
||||||
bool validateCert = true,
|
bool validateCert = true,
|
||||||
std::string hostname = "",
|
std::string hostname = "",
|
||||||
const std::vector<std::pair<std::string, std::string>>
|
const std::vector<std::pair<std::string, std::string> >
|
||||||
&sslConfCmds = {});
|
&sslConfCmds = {});
|
||||||
|
|
||||||
private:
|
private:
|
||||||
/// Not thread safe, but in loop
|
/// Not thread safe, but in loop
|
||||||
void newConnection(int sockfd);
|
void newConnection(int sockfd);
|
||||||
/// Not thread safe, but in loop
|
/// Not thread safe, but in loop
|
||||||
@ -229,14 +220,12 @@ class TcpClient : NonCopyable
|
|||||||
mutable std::mutex mutex_;
|
mutable std::mutex mutex_;
|
||||||
TcpConnectionPtr connection_; // @GuardedBy mutex_
|
TcpConnectionPtr connection_; // @GuardedBy mutex_
|
||||||
std::shared_ptr<SSLContext> sslCtxPtr_;
|
std::shared_ptr<SSLContext> sslCtxPtr_;
|
||||||
bool validateCert_{false};
|
bool validateCert_{ false };
|
||||||
std::string SSLHostName_;
|
std::string SSLHostName_;
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
class IgnoreSigPipe
|
class IgnoreSigPipe {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
IgnoreSigPipe()
|
IgnoreSigPipe() {
|
||||||
{
|
|
||||||
::signal(SIGPIPE, SIG_IGN);
|
::signal(SIGPIPE, SIG_IGN);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
#include "core/loops/callbacks.h"
|
#include "core/loops/callbacks.h"
|
||||||
#include "core/loops/event_loop.h"
|
#include "core/loops/event_loop.h"
|
||||||
#include "core/net/inet_address.h"
|
#include "core/net/inet_address.h"
|
||||||
#include <trantor/utils/NonCopyable.h>
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -13,28 +13,34 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "core/loops/callbacks.h"
|
|
||||||
#include <trantor/utils/NonCopyable.h>
|
|
||||||
#include "core/log/logger.h"
|
#include "core/log/logger.h"
|
||||||
|
#include "core/loops/callbacks.h"
|
||||||
#include "core/loops/event_loop_thread_pool.h"
|
#include "core/loops/event_loop_thread_pool.h"
|
||||||
|
#include "core/loops/timing_wheel.h"
|
||||||
#include "core/net/inet_address.h"
|
#include "core/net/inet_address.h"
|
||||||
#include "core/net/tcp_connection.h"
|
#include "core/net/tcp_connection.h"
|
||||||
#include "core/loops/timing_wheel.h"
|
#include <signal.h>
|
||||||
#include <string>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <signal.h>
|
#include <string>
|
||||||
namespace trantor
|
|
||||||
{
|
namespace trantor {
|
||||||
class Acceptor;
|
class Acceptor;
|
||||||
class SSLContext;
|
class SSLContext;
|
||||||
/**
|
/**
|
||||||
* @brief This class represents a TCP server.
|
* @brief This class represents a TCP server.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
class TcpServer : NonCopyable
|
class TcpServer {
|
||||||
{
|
protected:
|
||||||
public:
|
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.
|
* @brief Construct a new TCP server instance.
|
||||||
*
|
*
|
||||||
@ -70,8 +76,7 @@ class TcpServer : NonCopyable
|
|||||||
*
|
*
|
||||||
* @param num
|
* @param num
|
||||||
*/
|
*/
|
||||||
void setIoLoopNum(size_t num)
|
void setIoLoopNum(size_t num) {
|
||||||
{
|
|
||||||
assert(!started_);
|
assert(!started_);
|
||||||
loopPoolPtr_ = std::make_shared<EventLoopThreadPool>(num);
|
loopPoolPtr_ = std::make_shared<EventLoopThreadPool>(num);
|
||||||
loopPoolPtr_->start();
|
loopPoolPtr_->start();
|
||||||
@ -83,8 +88,7 @@ class TcpServer : NonCopyable
|
|||||||
*
|
*
|
||||||
* @param pool
|
* @param pool
|
||||||
*/
|
*/
|
||||||
void setIoLoopThreadPool(const std::shared_ptr<EventLoopThreadPool> &pool)
|
void setIoLoopThreadPool(const std::shared_ptr<EventLoopThreadPool> &pool) {
|
||||||
{
|
|
||||||
assert(pool->size() > 0);
|
assert(pool->size() > 0);
|
||||||
assert(!started_);
|
assert(!started_);
|
||||||
loopPoolPtr_ = pool;
|
loopPoolPtr_ = pool;
|
||||||
@ -97,12 +101,10 @@ class TcpServer : NonCopyable
|
|||||||
* @param cb The callback is called when some data is received on a
|
* @param cb The callback is called when some data is received on a
|
||||||
* connection to the server.
|
* connection to the server.
|
||||||
*/
|
*/
|
||||||
void setRecvMessageCallback(const RecvMessageCallback &cb)
|
void setRecvMessageCallback(const RecvMessageCallback &cb) {
|
||||||
{
|
|
||||||
recvMessageCallback_ = cb;
|
recvMessageCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setRecvMessageCallback(RecvMessageCallback &&cb)
|
void setRecvMessageCallback(RecvMessageCallback &&cb) {
|
||||||
{
|
|
||||||
recvMessageCallback_ = std::move(cb);
|
recvMessageCallback_ = std::move(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,12 +114,10 @@ class TcpServer : NonCopyable
|
|||||||
* @param cb The callback is called when a connection is established or
|
* @param cb The callback is called when a connection is established or
|
||||||
* closed.
|
* closed.
|
||||||
*/
|
*/
|
||||||
void setConnectionCallback(const ConnectionCallback &cb)
|
void setConnectionCallback(const ConnectionCallback &cb) {
|
||||||
{
|
|
||||||
connectionCallback_ = cb;
|
connectionCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setConnectionCallback(ConnectionCallback &&cb)
|
void setConnectionCallback(ConnectionCallback &&cb) {
|
||||||
{
|
|
||||||
connectionCallback_ = std::move(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
|
* @param cb The callback is called when data to send is written to the
|
||||||
* socket of a connection.
|
* socket of a connection.
|
||||||
*/
|
*/
|
||||||
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
|
void setWriteCompleteCallback(const WriteCompleteCallback &cb) {
|
||||||
{
|
|
||||||
writeCompleteCallback_ = cb;
|
writeCompleteCallback_ = cb;
|
||||||
}
|
}
|
||||||
void setWriteCompleteCallback(WriteCompleteCallback &&cb)
|
void setWriteCompleteCallback(WriteCompleteCallback &&cb) {
|
||||||
{
|
|
||||||
writeCompleteCallback_ = std::move(cb);
|
writeCompleteCallback_ = std::move(cb);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -141,8 +139,7 @@ class TcpServer : NonCopyable
|
|||||||
*
|
*
|
||||||
* @return const std::string&
|
* @return const std::string&
|
||||||
*/
|
*/
|
||||||
const std::string &name() const
|
const std::string &name() const {
|
||||||
{
|
|
||||||
return serverName_;
|
return serverName_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,8 +162,7 @@ class TcpServer : NonCopyable
|
|||||||
*
|
*
|
||||||
* @return EventLoop*
|
* @return EventLoop*
|
||||||
*/
|
*/
|
||||||
EventLoop *getLoop() const
|
EventLoop *getLoop() const {
|
||||||
{
|
|
||||||
return loop_;
|
return loop_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -175,8 +171,7 @@ class TcpServer : NonCopyable
|
|||||||
*
|
*
|
||||||
* @return std::vector<EventLoop *>
|
* @return std::vector<EventLoop *>
|
||||||
*/
|
*/
|
||||||
std::vector<EventLoop *> getIoLoops() const
|
std::vector<EventLoop *> getIoLoops() const {
|
||||||
{
|
|
||||||
return loopPoolPtr_->getLoops();
|
return loopPoolPtr_->getLoops();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,8 +181,7 @@ class TcpServer : NonCopyable
|
|||||||
*
|
*
|
||||||
* @param timeout
|
* @param timeout
|
||||||
*/
|
*/
|
||||||
void kickoffIdleConnections(size_t timeout)
|
void kickoffIdleConnections(size_t timeout) {
|
||||||
{
|
|
||||||
loop_->runInLoop([this, timeout]() {
|
loop_->runInLoop([this, timeout]() {
|
||||||
assert(!started_);
|
assert(!started_);
|
||||||
idleTimeout_ = timeout;
|
idleTimeout_ = timeout;
|
||||||
@ -209,10 +203,10 @@ class TcpServer : NonCopyable
|
|||||||
void enableSSL(const std::string &certPath,
|
void enableSSL(const std::string &certPath,
|
||||||
const std::string &keyPath,
|
const std::string &keyPath,
|
||||||
bool useOldTLS = false,
|
bool useOldTLS = false,
|
||||||
const std::vector<std::pair<std::string, std::string>>
|
const std::vector<std::pair<std::string, std::string> >
|
||||||
&sslConfCmds = {});
|
&sslConfCmds = {});
|
||||||
|
|
||||||
private:
|
private:
|
||||||
EventLoop *loop_;
|
EventLoop *loop_;
|
||||||
std::unique_ptr<Acceptor> acceptorPtr_;
|
std::unique_ptr<Acceptor> acceptorPtr_;
|
||||||
void newConnection(int fd, const InetAddress &peer);
|
void newConnection(int fd, const InetAddress &peer);
|
||||||
@ -223,16 +217,14 @@ class TcpServer : NonCopyable
|
|||||||
ConnectionCallback connectionCallback_;
|
ConnectionCallback connectionCallback_;
|
||||||
WriteCompleteCallback writeCompleteCallback_;
|
WriteCompleteCallback writeCompleteCallback_;
|
||||||
|
|
||||||
size_t idleTimeout_{0};
|
size_t idleTimeout_{ 0 };
|
||||||
std::map<EventLoop *, std::shared_ptr<TimingWheel>> timingWheelMap_;
|
std::map<EventLoop *, std::shared_ptr<TimingWheel> > timingWheelMap_;
|
||||||
void connectionClosed(const TcpConnectionPtr &connectionPtr);
|
void connectionClosed(const TcpConnectionPtr &connectionPtr);
|
||||||
std::shared_ptr<EventLoopThreadPool> loopPoolPtr_;
|
std::shared_ptr<EventLoopThreadPool> loopPoolPtr_;
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
class IgnoreSigPipe
|
class IgnoreSigPipe {
|
||||||
{
|
|
||||||
public:
|
public:
|
||||||
IgnoreSigPipe()
|
IgnoreSigPipe() {
|
||||||
{
|
|
||||||
::signal(SIGPIPE, SIG_IGN);
|
::signal(SIGPIPE, SIG_IGN);
|
||||||
LOG_TRACE << "Ignore SIGPIPE";
|
LOG_TRACE << "Ignore SIGPIPE";
|
||||||
}
|
}
|
||||||
@ -240,7 +232,7 @@ class TcpServer : NonCopyable
|
|||||||
|
|
||||||
IgnoreSigPipe initObj;
|
IgnoreSigPipe initObj;
|
||||||
#endif
|
#endif
|
||||||
bool started_{false};
|
bool started_{ false };
|
||||||
|
|
||||||
// OpenSSL SSL context Object;
|
// OpenSSL SSL context Object;
|
||||||
std::shared_ptr<SSLContext> sslCtxPtr_;
|
std::shared_ptr<SSLContext> sslCtxPtr_;
|
||||||
|
Loading…
Reference in New Issue
Block a user