mirror of
https://github.com/Relintai/rcpp_framework.git
synced 2024-11-10 00:52:11 +01:00
Remove trantor NonCopyable inheritances from core.
This commit is contained in:
parent
b8e0579a2a
commit
3534817831
@ -18,6 +18,7 @@
|
||||
#ifdef __linux__
|
||||
#include <sys/prctl.h>
|
||||
#endif
|
||||
|
||||
using namespace trantor;
|
||||
ConcurrentTaskQueue::ConcurrentTaskQueue(size_t threadNum,
|
||||
const std::string &name) :
|
||||
|
@ -17,72 +17,71 @@
|
||||
#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
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new concurrent task queue instance.
|
||||
*
|
||||
* @param threadNum The number of threads in the queue.
|
||||
* @param name The name of the queue.
|
||||
*/
|
||||
ConcurrentTaskQueue(size_t threadNum, const std::string &name);
|
||||
class ConcurrentTaskQueue : public TaskQueue {
|
||||
public:
|
||||
ConcurrentTaskQueue() {}
|
||||
|
||||
/**
|
||||
* @brief Run a task in the queue.
|
||||
*
|
||||
* @param task
|
||||
*/
|
||||
virtual void runTaskInQueue(const std::function<void()> &task);
|
||||
virtual void runTaskInQueue(std::function<void()> &&task);
|
||||
/**
|
||||
* @brief Construct a new concurrent task queue instance.
|
||||
*
|
||||
* @param threadNum The number of threads in the queue.
|
||||
* @param name The name of the queue.
|
||||
*/
|
||||
ConcurrentTaskQueue(size_t threadNum, const std::string &name);
|
||||
|
||||
/**
|
||||
* @brief Get the name of the queue.
|
||||
*
|
||||
* @return std::string
|
||||
*/
|
||||
virtual std::string getName() const
|
||||
{
|
||||
return queueName_;
|
||||
};
|
||||
/**
|
||||
* @brief Run a task in the queue.
|
||||
*
|
||||
* @param task
|
||||
*/
|
||||
virtual void runTaskInQueue(const std::function<void()> &task);
|
||||
virtual void runTaskInQueue(std::function<void()> &&task);
|
||||
|
||||
/**
|
||||
* @brief Get the number of tasks to be executed in the queue.
|
||||
*
|
||||
* @return size_t
|
||||
*/
|
||||
size_t getTaskCount();
|
||||
/**
|
||||
* @brief Get the name of the queue.
|
||||
*
|
||||
* @return std::string
|
||||
*/
|
||||
virtual std::string getName() const {
|
||||
return queueName_;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Stop all threads in the queue.
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
/**
|
||||
* @brief Get the number of tasks to be executed in the queue.
|
||||
*
|
||||
* @return size_t
|
||||
*/
|
||||
size_t getTaskCount();
|
||||
|
||||
~ConcurrentTaskQueue();
|
||||
/**
|
||||
* @brief Stop all threads in the queue.
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
|
||||
private:
|
||||
size_t queueCount_;
|
||||
std::string queueName_;
|
||||
~ConcurrentTaskQueue();
|
||||
|
||||
std::queue<std::function<void()>> taskQueue_;
|
||||
std::vector<std::thread> threads_;
|
||||
private:
|
||||
size_t queueCount_;
|
||||
std::string queueName_;
|
||||
|
||||
std::mutex taskMutex_;
|
||||
std::condition_variable taskCond_;
|
||||
std::atomic_bool stop_;
|
||||
void queueFunc(int queueNum);
|
||||
std::queue<std::function<void()> > taskQueue_;
|
||||
std::vector<std::thread> threads_;
|
||||
|
||||
std::mutex taskMutex_;
|
||||
std::condition_variable taskCond_;
|
||||
std::atomic_bool stop_;
|
||||
void queueFunc(int queueNum);
|
||||
};
|
||||
|
||||
} // namespace trantor
|
||||
} // namespace trantor
|
||||
|
@ -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
|
||||
|
@ -14,45 +14,48 @@
|
||||
|
||||
#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
|
||||
{
|
||||
public:
|
||||
virtual void runTaskInQueue(const std::function<void()> &task) = 0;
|
||||
virtual void runTaskInQueue(std::function<void()> &&task) = 0;
|
||||
virtual std::string getName() const
|
||||
{
|
||||
return "";
|
||||
};
|
||||
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;
|
||||
|
||||
/**
|
||||
* @brief Run a task in the queue sychronously. This means that the task is
|
||||
* executed before the method returns.
|
||||
*
|
||||
* @param task
|
||||
*/
|
||||
void syncTaskInQueue(const std::function<void()> &task)
|
||||
{
|
||||
std::promise<int> prom;
|
||||
std::future<int> fut = prom.get_future();
|
||||
runTaskInQueue([&]() {
|
||||
task();
|
||||
prom.set_value(1);
|
||||
});
|
||||
fut.get();
|
||||
};
|
||||
virtual ~TaskQueue()
|
||||
{
|
||||
}
|
||||
public:
|
||||
TaskQueue() {}
|
||||
virtual void runTaskInQueue(const std::function<void()> &task) = 0;
|
||||
virtual void runTaskInQueue(std::function<void()> &&task) = 0;
|
||||
virtual std::string getName() const {
|
||||
return "";
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Run a task in the queue sychronously. This means that the task is
|
||||
* executed before the method returns.
|
||||
*
|
||||
* @param task
|
||||
*/
|
||||
void syncTaskInQueue(const std::function<void()> &task) {
|
||||
std::promise<int> prom;
|
||||
std::future<int> fut = prom.get_future();
|
||||
runTaskInQueue([&]() {
|
||||
task();
|
||||
prom.set_value(1);
|
||||
});
|
||||
fut.get();
|
||||
};
|
||||
virtual ~TaskQueue() {
|
||||
}
|
||||
};
|
||||
} // namespace trantor
|
||||
} // namespace trantor
|
||||
|
@ -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
|
||||
{
|
||||
|
@ -19,70 +19,68 @@
|
||||
#include <atomic>
|
||||
#include <memory>
|
||||
|
||||
namespace trantor
|
||||
{
|
||||
class Connector : public NonCopyable,
|
||||
public std::enable_shared_from_this<Connector>
|
||||
{
|
||||
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)
|
||||
{
|
||||
newConnectionCallback_ = cb;
|
||||
}
|
||||
void setNewConnectionCallback(NewConnectionCallback &&cb)
|
||||
{
|
||||
newConnectionCallback_ = std::move(cb);
|
||||
}
|
||||
void setErrorCallback(const ConnectionErrorCallback &cb)
|
||||
{
|
||||
errorCallback_ = cb;
|
||||
}
|
||||
void setErrorCallback(ConnectionErrorCallback &&cb)
|
||||
{
|
||||
errorCallback_ = std::move(cb);
|
||||
}
|
||||
const InetAddress &serverAddress() const
|
||||
{
|
||||
return serverAddr_;
|
||||
}
|
||||
void start();
|
||||
void restart();
|
||||
void stop();
|
||||
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;
|
||||
|
||||
private:
|
||||
NewConnectionCallback newConnectionCallback_;
|
||||
ConnectionErrorCallback errorCallback_;
|
||||
enum class Status
|
||||
{
|
||||
Disconnected,
|
||||
Connecting,
|
||||
Connected
|
||||
};
|
||||
static constexpr int kMaxRetryDelayMs = 30 * 1000;
|
||||
static constexpr int kInitRetryDelayMs = 500;
|
||||
std::shared_ptr<Channel> channelPtr_;
|
||||
EventLoop *loop_;
|
||||
InetAddress serverAddr_;
|
||||
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) {
|
||||
newConnectionCallback_ = cb;
|
||||
}
|
||||
void setNewConnectionCallback(NewConnectionCallback &&cb) {
|
||||
newConnectionCallback_ = std::move(cb);
|
||||
}
|
||||
void setErrorCallback(const ConnectionErrorCallback &cb) {
|
||||
errorCallback_ = cb;
|
||||
}
|
||||
void setErrorCallback(ConnectionErrorCallback &&cb) {
|
||||
errorCallback_ = std::move(cb);
|
||||
}
|
||||
const InetAddress &serverAddress() const {
|
||||
return serverAddr_;
|
||||
}
|
||||
void start();
|
||||
void restart();
|
||||
void stop();
|
||||
|
||||
std::atomic_bool connect_{false};
|
||||
std::atomic<Status> status_{Status::Disconnected};
|
||||
private:
|
||||
NewConnectionCallback newConnectionCallback_;
|
||||
ConnectionErrorCallback errorCallback_;
|
||||
enum class Status {
|
||||
Disconnected,
|
||||
Connecting,
|
||||
Connected
|
||||
};
|
||||
static constexpr int kMaxRetryDelayMs = 30 * 1000;
|
||||
static constexpr int kInitRetryDelayMs = 500;
|
||||
std::shared_ptr<Channel> channelPtr_;
|
||||
EventLoop *loop_;
|
||||
InetAddress serverAddr_;
|
||||
|
||||
int retryInterval_{kInitRetryDelayMs};
|
||||
int maxRetryInterval_{kMaxRetryDelayMs};
|
||||
std::atomic_bool connect_{ false };
|
||||
std::atomic<Status> status_{ Status::Disconnected };
|
||||
|
||||
bool retry_;
|
||||
int retryInterval_{ kInitRetryDelayMs };
|
||||
int maxRetryInterval_{ kMaxRetryDelayMs };
|
||||
|
||||
void startInLoop();
|
||||
void connect();
|
||||
void connecting(int sockfd);
|
||||
int removeAndResetChannel();
|
||||
void handleWrite();
|
||||
void handleError();
|
||||
void retry(int sockfd);
|
||||
bool retry_;
|
||||
|
||||
void startInLoop();
|
||||
void connect();
|
||||
void connecting(int sockfd);
|
||||
int removeAndResetChannel();
|
||||
void handleWrite();
|
||||
void handleError();
|
||||
void retry(int sockfd);
|
||||
};
|
||||
|
||||
} // namespace trantor
|
||||
} // namespace trantor
|
||||
|
@ -6,143 +6,133 @@
|
||||
// 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"
|
||||
{
|
||||
struct hostent;
|
||||
struct ares_channeldata;
|
||||
using ares_channel = struct ares_channeldata*;
|
||||
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:
|
||||
AresResolver(trantor::EventLoop* loop, size_t timeout);
|
||||
~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;
|
||||
|
||||
virtual void resolve(const std::string& hostname,
|
||||
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())
|
||||
{
|
||||
auto& cachedAddr = iter->second;
|
||||
if (timeout_ == 0 ||
|
||||
cachedAddr.second.after(timeout_) > trantor::Date::date())
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
memset(&addr, 0, sizeof addr);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = 0;
|
||||
addr.sin_addr = cachedAddr.first;
|
||||
inet = InetAddress(addr);
|
||||
cached = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cached)
|
||||
{
|
||||
cb(inet);
|
||||
return;
|
||||
}
|
||||
if (loop_->isInLoopThread())
|
||||
{
|
||||
resolveInLoop(hostname, cb);
|
||||
}
|
||||
else
|
||||
{
|
||||
loop_->queueInLoop([thisPtr = shared_from_this(), hostname, cb]() {
|
||||
thisPtr->resolveInLoop(hostname, cb);
|
||||
});
|
||||
}
|
||||
}
|
||||
public:
|
||||
AresResolver(trantor::EventLoop *loop, size_t timeout);
|
||||
~AresResolver();
|
||||
|
||||
private:
|
||||
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)
|
||||
{
|
||||
}
|
||||
};
|
||||
void resolveInLoop(const std::string& hostname, const Callback& cb);
|
||||
void init();
|
||||
trantor::EventLoop* loop_;
|
||||
ares_channel ctx_{nullptr};
|
||||
bool timerActive_{false};
|
||||
using ChannelList = std::map<int, std::unique_ptr<trantor::Channel>>;
|
||||
ChannelList channels_;
|
||||
static std::unordered_map<std::string,
|
||||
std::pair<struct in_addr, trantor::Date>>&
|
||||
globalCache()
|
||||
{
|
||||
static std::unordered_map<std::string,
|
||||
std::pair<struct in_addr, trantor::Date>>
|
||||
dnsCache;
|
||||
return dnsCache;
|
||||
}
|
||||
static std::mutex& globalMutex()
|
||||
{
|
||||
static std::mutex mutex_;
|
||||
return mutex_;
|
||||
}
|
||||
static EventLoop* getLoop()
|
||||
{
|
||||
static EventLoopThread loopThread;
|
||||
loopThread.run();
|
||||
return loopThread.getLoop();
|
||||
}
|
||||
const size_t timeout_{60};
|
||||
virtual void resolve(const std::string &hostname,
|
||||
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()) {
|
||||
auto &cachedAddr = iter->second;
|
||||
if (timeout_ == 0 ||
|
||||
cachedAddr.second.after(timeout_) > trantor::Date::date()) {
|
||||
struct sockaddr_in addr;
|
||||
memset(&addr, 0, sizeof addr);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = 0;
|
||||
addr.sin_addr = cachedAddr.first;
|
||||
inet = InetAddress(addr);
|
||||
cached = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (cached) {
|
||||
cb(inet);
|
||||
return;
|
||||
}
|
||||
if (loop_->isInLoopThread()) {
|
||||
resolveInLoop(hostname, cb);
|
||||
} else {
|
||||
loop_->queueInLoop([thisPtr = shared_from_this(), hostname, cb]() {
|
||||
thisPtr->resolveInLoop(hostname, cb);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void onRead(int sockfd);
|
||||
void onTimer();
|
||||
void onQueryResult(int status,
|
||||
struct hostent* result,
|
||||
const std::string& hostname,
|
||||
const Callback& callback);
|
||||
void onSockCreate(int sockfd, int type);
|
||||
void onSockStateChange(int sockfd, bool read, bool write);
|
||||
private:
|
||||
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) {
|
||||
}
|
||||
};
|
||||
void resolveInLoop(const std::string &hostname, const Callback &cb);
|
||||
void init();
|
||||
trantor::EventLoop *loop_;
|
||||
ares_channel ctx_{ nullptr };
|
||||
bool timerActive_{ false };
|
||||
using ChannelList = std::map<int, std::unique_ptr<trantor::Channel> >;
|
||||
ChannelList channels_;
|
||||
static std::unordered_map<std::string,
|
||||
std::pair<struct in_addr, trantor::Date> > &
|
||||
globalCache() {
|
||||
static std::unordered_map<std::string,
|
||||
std::pair<struct in_addr, trantor::Date> >
|
||||
dnsCache;
|
||||
return dnsCache;
|
||||
}
|
||||
static std::mutex &globalMutex() {
|
||||
static std::mutex mutex_;
|
||||
return mutex_;
|
||||
}
|
||||
static EventLoop *getLoop() {
|
||||
static EventLoopThread loopThread;
|
||||
loopThread.run();
|
||||
return loopThread.getLoop();
|
||||
}
|
||||
const size_t timeout_{ 60 };
|
||||
|
||||
static void ares_hostcallback_(void* data,
|
||||
int status,
|
||||
int timeouts,
|
||||
struct hostent* hostent);
|
||||
void onRead(int sockfd);
|
||||
void onTimer();
|
||||
void onQueryResult(int status,
|
||||
struct hostent *result,
|
||||
const std::string &hostname,
|
||||
const Callback &callback);
|
||||
void onSockCreate(int sockfd, int type);
|
||||
void onSockStateChange(int sockfd, bool read, bool write);
|
||||
|
||||
static void ares_hostcallback_(void *data,
|
||||
int status,
|
||||
int timeouts,
|
||||
struct hostent *hostent);
|
||||
#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
|
||||
static int ares_sock_createcallback_(int sockfd, int type, void* data);
|
||||
static int ares_sock_createcallback_(int sockfd, int type, void *data);
|
||||
#endif
|
||||
static void ares_sock_statecallback_(void* data,
|
||||
static void ares_sock_statecallback_(void *data,
|
||||
#ifdef _WIN32
|
||||
SOCKET sockfd,
|
||||
SOCKET sockfd,
|
||||
#else
|
||||
int sockfd,
|
||||
int sockfd,
|
||||
#endif
|
||||
int read,
|
||||
int write);
|
||||
struct LibraryInitializer
|
||||
{
|
||||
LibraryInitializer();
|
||||
~LibraryInitializer();
|
||||
};
|
||||
static LibraryInitializer libraryInitializer_;
|
||||
int read,
|
||||
int write);
|
||||
struct LibraryInitializer {
|
||||
LibraryInitializer();
|
||||
~LibraryInitializer();
|
||||
};
|
||||
static LibraryInitializer libraryInitializer_;
|
||||
};
|
||||
} // namespace trantor
|
||||
} // namespace trantor
|
||||
|
@ -6,57 +6,53 @@
|
||||
// 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
|
||||
{
|
||||
constexpr size_t kResolveBufferLength{16 * 1024};
|
||||
namespace trantor {
|
||||
constexpr size_t kResolveBufferLength{ 16 * 1024 };
|
||||
class NormalResolver : public Resolver,
|
||||
public NonCopyable,
|
||||
public std::enable_shared_from_this<NormalResolver>
|
||||
{
|
||||
public:
|
||||
virtual void resolve(const std::string& hostname,
|
||||
const Callback& callback) override;
|
||||
explicit NormalResolver(size_t timeout)
|
||||
: timeout_(timeout), resolveBuffer_(kResolveBufferLength)
|
||||
{
|
||||
}
|
||||
virtual ~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;
|
||||
|
||||
private:
|
||||
static std::unordered_map<std::string,
|
||||
std::pair<trantor::InetAddress, trantor::Date>>&
|
||||
globalCache()
|
||||
{
|
||||
static std::unordered_map<
|
||||
std::string,
|
||||
std::pair<trantor::InetAddress, trantor::Date>>
|
||||
dnsCache_;
|
||||
return dnsCache_;
|
||||
}
|
||||
static std::mutex& globalMutex()
|
||||
{
|
||||
static std::mutex mutex_;
|
||||
return mutex_;
|
||||
}
|
||||
static trantor::ConcurrentTaskQueue& concurrentTaskQueue()
|
||||
{
|
||||
static trantor::ConcurrentTaskQueue queue(
|
||||
std::thread::hardware_concurrency() < 8
|
||||
? 8
|
||||
: std::thread::hardware_concurrency(),
|
||||
"Dns Queue");
|
||||
return queue;
|
||||
}
|
||||
const size_t timeout_;
|
||||
std::vector<char> resolveBuffer_;
|
||||
public:
|
||||
virtual void resolve(const std::string &hostname,
|
||||
const Callback &callback) override;
|
||||
explicit NormalResolver(size_t timeout) :
|
||||
timeout_(timeout), resolveBuffer_(kResolveBufferLength) {
|
||||
}
|
||||
virtual ~NormalResolver() {
|
||||
}
|
||||
|
||||
private:
|
||||
static std::unordered_map<std::string,
|
||||
std::pair<trantor::InetAddress, trantor::Date> > &
|
||||
globalCache() {
|
||||
static std::unordered_map<
|
||||
std::string,
|
||||
std::pair<trantor::InetAddress, trantor::Date> >
|
||||
dnsCache_;
|
||||
return dnsCache_;
|
||||
}
|
||||
static std::mutex &globalMutex() {
|
||||
static std::mutex mutex_;
|
||||
return mutex_;
|
||||
}
|
||||
static trantor::ConcurrentTaskQueue &concurrentTaskQueue() {
|
||||
static trantor::ConcurrentTaskQueue queue(
|
||||
std::thread::hardware_concurrency() < 8 ? 8 : std::thread::hardware_concurrency(),
|
||||
"Dns Queue");
|
||||
return queue;
|
||||
}
|
||||
const size_t timeout_;
|
||||
std::vector<char> resolveBuffer_;
|
||||
};
|
||||
} // namespace trantor
|
||||
} // namespace trantor
|
@ -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....
|
||||
|
@ -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,214 +34,204 @@ class SSLContext;
|
||||
* @brief This class represents a TCP client.
|
||||
*
|
||||
*/
|
||||
class TcpClient : NonCopyable
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new TCP client instance.
|
||||
*
|
||||
* @param loop The event loop in which the client runs.
|
||||
* @param serverAddr The address of the server.
|
||||
* @param nameArg The name of the client.
|
||||
*/
|
||||
TcpClient(EventLoop *loop,
|
||||
const InetAddress &serverAddr,
|
||||
const std::string &nameArg);
|
||||
~TcpClient();
|
||||
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;
|
||||
|
||||
/**
|
||||
* @brief Connect to the server.
|
||||
*
|
||||
*/
|
||||
void connect();
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new TCP client instance.
|
||||
*
|
||||
* @param loop The event loop in which the client runs.
|
||||
* @param serverAddr The address of the server.
|
||||
* @param nameArg The name of the client.
|
||||
*/
|
||||
TcpClient(EventLoop *loop,
|
||||
const InetAddress &serverAddr,
|
||||
const std::string &nameArg);
|
||||
~TcpClient();
|
||||
|
||||
/**
|
||||
* @brief Disconnect from the server.
|
||||
*
|
||||
*/
|
||||
void disconnect();
|
||||
/**
|
||||
* @brief Connect to the server.
|
||||
*
|
||||
*/
|
||||
void connect();
|
||||
|
||||
/**
|
||||
* @brief Stop connecting to the server.
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
/**
|
||||
* @brief Disconnect from the server.
|
||||
*
|
||||
*/
|
||||
void disconnect();
|
||||
|
||||
/**
|
||||
* @brief Get the TCP connection to the server.
|
||||
*
|
||||
* @return TcpConnectionPtr
|
||||
*/
|
||||
TcpConnectionPtr connection() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return connection_;
|
||||
}
|
||||
/**
|
||||
* @brief Stop connecting to the server.
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* @brief Get the event loop.
|
||||
*
|
||||
* @return EventLoop*
|
||||
*/
|
||||
EventLoop *getLoop() const
|
||||
{
|
||||
return loop_;
|
||||
}
|
||||
/**
|
||||
* @brief Get the TCP connection to the server.
|
||||
*
|
||||
* @return TcpConnectionPtr
|
||||
*/
|
||||
TcpConnectionPtr connection() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return connection_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check whether the client re-connect to the server.
|
||||
*
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool retry() const
|
||||
{
|
||||
return retry_;
|
||||
}
|
||||
/**
|
||||
* @brief Get the event loop.
|
||||
*
|
||||
* @return EventLoop*
|
||||
*/
|
||||
EventLoop *getLoop() const {
|
||||
return loop_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable retrying.
|
||||
*
|
||||
*/
|
||||
void enableRetry()
|
||||
{
|
||||
retry_ = true;
|
||||
}
|
||||
/**
|
||||
* @brief Check whether the client re-connect to the server.
|
||||
*
|
||||
* @return true
|
||||
* @return false
|
||||
*/
|
||||
bool retry() const {
|
||||
return retry_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the name of the client.
|
||||
*
|
||||
* @return const std::string&
|
||||
*/
|
||||
const std::string &name() const
|
||||
{
|
||||
return name_;
|
||||
}
|
||||
/**
|
||||
* @brief Enable retrying.
|
||||
*
|
||||
*/
|
||||
void enableRetry() {
|
||||
retry_ = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the connection callback.
|
||||
*
|
||||
* @param cb The callback is called when the connection to the server is
|
||||
* established or closed.
|
||||
*/
|
||||
void setConnectionCallback(const ConnectionCallback &cb)
|
||||
{
|
||||
connectionCallback_ = cb;
|
||||
}
|
||||
void setConnectionCallback(ConnectionCallback &&cb)
|
||||
{
|
||||
connectionCallback_ = std::move(cb);
|
||||
}
|
||||
/**
|
||||
* @brief Get the name of the client.
|
||||
*
|
||||
* @return const std::string&
|
||||
*/
|
||||
const std::string &name() const {
|
||||
return name_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the connection error callback.
|
||||
*
|
||||
* @param cb The callback is called when an error occurs during connecting
|
||||
* to the server.
|
||||
*/
|
||||
void setConnectionErrorCallback(const ConnectionErrorCallback &cb)
|
||||
{
|
||||
connectionErrorCallback_ = cb;
|
||||
}
|
||||
/**
|
||||
* @brief Set the connection callback.
|
||||
*
|
||||
* @param cb The callback is called when the connection to the server is
|
||||
* established or closed.
|
||||
*/
|
||||
void setConnectionCallback(const ConnectionCallback &cb) {
|
||||
connectionCallback_ = cb;
|
||||
}
|
||||
void setConnectionCallback(ConnectionCallback &&cb) {
|
||||
connectionCallback_ = std::move(cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the message callback.
|
||||
*
|
||||
* @param cb The callback is called when some data is received from the
|
||||
* server.
|
||||
*/
|
||||
void setMessageCallback(const RecvMessageCallback &cb)
|
||||
{
|
||||
messageCallback_ = cb;
|
||||
}
|
||||
void setMessageCallback(RecvMessageCallback &&cb)
|
||||
{
|
||||
messageCallback_ = std::move(cb);
|
||||
}
|
||||
/// Set write complete callback.
|
||||
/// Not thread safe.
|
||||
/**
|
||||
* @brief Set the connection error callback.
|
||||
*
|
||||
* @param cb The callback is called when an error occurs during connecting
|
||||
* to the server.
|
||||
*/
|
||||
void setConnectionErrorCallback(const ConnectionErrorCallback &cb) {
|
||||
connectionErrorCallback_ = cb;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the write complete callback.
|
||||
*
|
||||
* @param cb The callback is called when data to send is written to the
|
||||
* socket.
|
||||
*/
|
||||
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
|
||||
{
|
||||
writeCompleteCallback_ = cb;
|
||||
}
|
||||
void setWriteCompleteCallback(WriteCompleteCallback &&cb)
|
||||
{
|
||||
writeCompleteCallback_ = std::move(cb);
|
||||
}
|
||||
/**
|
||||
* @brief Set the message callback.
|
||||
*
|
||||
* @param cb The callback is called when some data is received from the
|
||||
* server.
|
||||
*/
|
||||
void setMessageCallback(const RecvMessageCallback &cb) {
|
||||
messageCallback_ = cb;
|
||||
}
|
||||
void setMessageCallback(RecvMessageCallback &&cb) {
|
||||
messageCallback_ = std::move(cb);
|
||||
}
|
||||
/// Set write complete callback.
|
||||
/// Not thread safe.
|
||||
|
||||
/**
|
||||
* @brief Set the callback for errors of SSL
|
||||
* @param cb The callback is called when an SSL error occurs.
|
||||
*/
|
||||
void setSSLErrorCallback(const SSLErrorCallback &cb)
|
||||
{
|
||||
sslErrorCallback_ = cb;
|
||||
}
|
||||
void setSSLErrorCallback(SSLErrorCallback &&cb)
|
||||
{
|
||||
sslErrorCallback_ = std::move(cb);
|
||||
}
|
||||
/**
|
||||
* @brief Set the write complete callback.
|
||||
*
|
||||
* @param cb The callback is called when data to send is written to the
|
||||
* socket.
|
||||
*/
|
||||
void setWriteCompleteCallback(const WriteCompleteCallback &cb) {
|
||||
writeCompleteCallback_ = cb;
|
||||
}
|
||||
void setWriteCompleteCallback(WriteCompleteCallback &&cb) {
|
||||
writeCompleteCallback_ = std::move(cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable SSL encryption.
|
||||
* @param useOldTLS If true, the TLS 1.0 and 1.1 are supported by the
|
||||
* client.
|
||||
* @param validateCert If true, we try to validate if the peer's SSL cert
|
||||
* is valid.
|
||||
* @param hostname The server hostname for SNI. If it is empty, the SNI is
|
||||
* not used.
|
||||
* @param sslConfCmds The commands used to call the SSL_CONF_cmd function in
|
||||
* OpenSSL.
|
||||
* @note It's well known that TLS 1.0 and 1.1 are not considered secure in
|
||||
* 2020. And it's a good practice to only use TLS 1.2 and above.
|
||||
*/
|
||||
void enableSSL(bool useOldTLS = false,
|
||||
bool validateCert = true,
|
||||
std::string hostname = "",
|
||||
const std::vector<std::pair<std::string, std::string>>
|
||||
&sslConfCmds = {});
|
||||
/**
|
||||
* @brief Set the callback for errors of SSL
|
||||
* @param cb The callback is called when an SSL error occurs.
|
||||
*/
|
||||
void setSSLErrorCallback(const SSLErrorCallback &cb) {
|
||||
sslErrorCallback_ = cb;
|
||||
}
|
||||
void setSSLErrorCallback(SSLErrorCallback &&cb) {
|
||||
sslErrorCallback_ = std::move(cb);
|
||||
}
|
||||
|
||||
private:
|
||||
/// Not thread safe, but in loop
|
||||
void newConnection(int sockfd);
|
||||
/// Not thread safe, but in loop
|
||||
void removeConnection(const TcpConnectionPtr &conn);
|
||||
/**
|
||||
* @brief Enable SSL encryption.
|
||||
* @param useOldTLS If true, the TLS 1.0 and 1.1 are supported by the
|
||||
* client.
|
||||
* @param validateCert If true, we try to validate if the peer's SSL cert
|
||||
* is valid.
|
||||
* @param hostname The server hostname for SNI. If it is empty, the SNI is
|
||||
* not used.
|
||||
* @param sslConfCmds The commands used to call the SSL_CONF_cmd function in
|
||||
* OpenSSL.
|
||||
* @note It's well known that TLS 1.0 and 1.1 are not considered secure in
|
||||
* 2020. And it's a good practice to only use TLS 1.2 and above.
|
||||
*/
|
||||
void enableSSL(bool useOldTLS = false,
|
||||
bool validateCert = true,
|
||||
std::string hostname = "",
|
||||
const std::vector<std::pair<std::string, std::string> >
|
||||
&sslConfCmds = {});
|
||||
|
||||
EventLoop *loop_;
|
||||
ConnectorPtr connector_; // avoid revealing Connector
|
||||
const std::string name_;
|
||||
ConnectionCallback connectionCallback_;
|
||||
ConnectionErrorCallback connectionErrorCallback_;
|
||||
RecvMessageCallback messageCallback_;
|
||||
WriteCompleteCallback writeCompleteCallback_;
|
||||
SSLErrorCallback sslErrorCallback_;
|
||||
std::atomic_bool retry_; // atomic
|
||||
std::atomic_bool connect_; // atomic
|
||||
// always in loop thread
|
||||
mutable std::mutex mutex_;
|
||||
TcpConnectionPtr connection_; // @GuardedBy mutex_
|
||||
std::shared_ptr<SSLContext> sslCtxPtr_;
|
||||
bool validateCert_{false};
|
||||
std::string SSLHostName_;
|
||||
private:
|
||||
/// Not thread safe, but in loop
|
||||
void newConnection(int sockfd);
|
||||
/// Not thread safe, but in loop
|
||||
void removeConnection(const TcpConnectionPtr &conn);
|
||||
|
||||
EventLoop *loop_;
|
||||
ConnectorPtr connector_; // avoid revealing Connector
|
||||
const std::string name_;
|
||||
ConnectionCallback connectionCallback_;
|
||||
ConnectionErrorCallback connectionErrorCallback_;
|
||||
RecvMessageCallback messageCallback_;
|
||||
WriteCompleteCallback writeCompleteCallback_;
|
||||
SSLErrorCallback sslErrorCallback_;
|
||||
std::atomic_bool retry_; // atomic
|
||||
std::atomic_bool connect_; // atomic
|
||||
// always in loop thread
|
||||
mutable std::mutex mutex_;
|
||||
TcpConnectionPtr connection_; // @GuardedBy mutex_
|
||||
std::shared_ptr<SSLContext> sslCtxPtr_;
|
||||
bool validateCert_{ false };
|
||||
std::string SSLHostName_;
|
||||
#ifndef _WIN32
|
||||
class IgnoreSigPipe
|
||||
{
|
||||
public:
|
||||
IgnoreSigPipe()
|
||||
{
|
||||
::signal(SIGPIPE, SIG_IGN);
|
||||
}
|
||||
};
|
||||
class IgnoreSigPipe {
|
||||
public:
|
||||
IgnoreSigPipe() {
|
||||
::signal(SIGPIPE, SIG_IGN);
|
||||
}
|
||||
};
|
||||
|
||||
static IgnoreSigPipe initObj;
|
||||
static IgnoreSigPipe initObj;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace trantor
|
||||
} // namespace trantor
|
||||
|
@ -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>
|
||||
|
@ -13,237 +13,229 @@
|
||||
*/
|
||||
|
||||
#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
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new TCP server instance.
|
||||
*
|
||||
* @param loop The event loop in which the acceptor of the server is
|
||||
* handled.
|
||||
* @param address The address of the server.
|
||||
* @param name The name of the server.
|
||||
* @param reUseAddr The SO_REUSEADDR option.
|
||||
* @param reUsePort The SO_REUSEPORT option.
|
||||
*/
|
||||
TcpServer(EventLoop *loop,
|
||||
const InetAddress &address,
|
||||
const std::string &name,
|
||||
bool reUseAddr = true,
|
||||
bool reUsePort = true);
|
||||
~TcpServer();
|
||||
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;
|
||||
|
||||
/**
|
||||
* @brief Start the server.
|
||||
*
|
||||
*/
|
||||
void start();
|
||||
public:
|
||||
/**
|
||||
* @brief Construct a new TCP server instance.
|
||||
*
|
||||
* @param loop The event loop in which the acceptor of the server is
|
||||
* handled.
|
||||
* @param address The address of the server.
|
||||
* @param name The name of the server.
|
||||
* @param reUseAddr The SO_REUSEADDR option.
|
||||
* @param reUsePort The SO_REUSEPORT option.
|
||||
*/
|
||||
TcpServer(EventLoop *loop,
|
||||
const InetAddress &address,
|
||||
const std::string &name,
|
||||
bool reUseAddr = true,
|
||||
bool reUsePort = true);
|
||||
~TcpServer();
|
||||
|
||||
/**
|
||||
* @brief Stop the server.
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
/**
|
||||
* @brief Start the server.
|
||||
*
|
||||
*/
|
||||
void start();
|
||||
|
||||
/**
|
||||
* @brief Set the number of event loops in which the I/O of connections to
|
||||
* the server is handled.
|
||||
*
|
||||
* @param num
|
||||
*/
|
||||
void setIoLoopNum(size_t num)
|
||||
{
|
||||
assert(!started_);
|
||||
loopPoolPtr_ = std::make_shared<EventLoopThreadPool>(num);
|
||||
loopPoolPtr_->start();
|
||||
}
|
||||
/**
|
||||
* @brief Stop the server.
|
||||
*
|
||||
*/
|
||||
void stop();
|
||||
|
||||
/**
|
||||
* @brief Set the event loops pool in which the I/O of connections to
|
||||
* the server is handled.
|
||||
*
|
||||
* @param pool
|
||||
*/
|
||||
void setIoLoopThreadPool(const std::shared_ptr<EventLoopThreadPool> &pool)
|
||||
{
|
||||
assert(pool->size() > 0);
|
||||
assert(!started_);
|
||||
loopPoolPtr_ = pool;
|
||||
loopPoolPtr_->start();
|
||||
}
|
||||
/**
|
||||
* @brief Set the number of event loops in which the I/O of connections to
|
||||
* the server is handled.
|
||||
*
|
||||
* @param num
|
||||
*/
|
||||
void setIoLoopNum(size_t num) {
|
||||
assert(!started_);
|
||||
loopPoolPtr_ = std::make_shared<EventLoopThreadPool>(num);
|
||||
loopPoolPtr_->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the message callback.
|
||||
*
|
||||
* @param cb The callback is called when some data is received on a
|
||||
* connection to the server.
|
||||
*/
|
||||
void setRecvMessageCallback(const RecvMessageCallback &cb)
|
||||
{
|
||||
recvMessageCallback_ = cb;
|
||||
}
|
||||
void setRecvMessageCallback(RecvMessageCallback &&cb)
|
||||
{
|
||||
recvMessageCallback_ = std::move(cb);
|
||||
}
|
||||
/**
|
||||
* @brief Set the event loops pool in which the I/O of connections to
|
||||
* the server is handled.
|
||||
*
|
||||
* @param pool
|
||||
*/
|
||||
void setIoLoopThreadPool(const std::shared_ptr<EventLoopThreadPool> &pool) {
|
||||
assert(pool->size() > 0);
|
||||
assert(!started_);
|
||||
loopPoolPtr_ = pool;
|
||||
loopPoolPtr_->start();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the connection callback.
|
||||
*
|
||||
* @param cb The callback is called when a connection is established or
|
||||
* closed.
|
||||
*/
|
||||
void setConnectionCallback(const ConnectionCallback &cb)
|
||||
{
|
||||
connectionCallback_ = cb;
|
||||
}
|
||||
void setConnectionCallback(ConnectionCallback &&cb)
|
||||
{
|
||||
connectionCallback_ = std::move(cb);
|
||||
}
|
||||
/**
|
||||
* @brief Set the message callback.
|
||||
*
|
||||
* @param cb The callback is called when some data is received on a
|
||||
* connection to the server.
|
||||
*/
|
||||
void setRecvMessageCallback(const RecvMessageCallback &cb) {
|
||||
recvMessageCallback_ = cb;
|
||||
}
|
||||
void setRecvMessageCallback(RecvMessageCallback &&cb) {
|
||||
recvMessageCallback_ = std::move(cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Set the write complete callback.
|
||||
*
|
||||
* @param cb The callback is called when data to send is written to the
|
||||
* socket of a connection.
|
||||
*/
|
||||
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
|
||||
{
|
||||
writeCompleteCallback_ = cb;
|
||||
}
|
||||
void setWriteCompleteCallback(WriteCompleteCallback &&cb)
|
||||
{
|
||||
writeCompleteCallback_ = std::move(cb);
|
||||
}
|
||||
/**
|
||||
* @brief Set the connection callback.
|
||||
*
|
||||
* @param cb The callback is called when a connection is established or
|
||||
* closed.
|
||||
*/
|
||||
void setConnectionCallback(const ConnectionCallback &cb) {
|
||||
connectionCallback_ = cb;
|
||||
}
|
||||
void setConnectionCallback(ConnectionCallback &&cb) {
|
||||
connectionCallback_ = std::move(cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the name of the server.
|
||||
*
|
||||
* @return const std::string&
|
||||
*/
|
||||
const std::string &name() const
|
||||
{
|
||||
return serverName_;
|
||||
}
|
||||
/**
|
||||
* @brief Set the write complete callback.
|
||||
*
|
||||
* @param cb The callback is called when data to send is written to the
|
||||
* socket of a connection.
|
||||
*/
|
||||
void setWriteCompleteCallback(const WriteCompleteCallback &cb) {
|
||||
writeCompleteCallback_ = cb;
|
||||
}
|
||||
void setWriteCompleteCallback(WriteCompleteCallback &&cb) {
|
||||
writeCompleteCallback_ = std::move(cb);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the IP and port string of the server.
|
||||
*
|
||||
* @return const std::string
|
||||
*/
|
||||
const std::string ipPort() const;
|
||||
/**
|
||||
* @brief Get the name of the server.
|
||||
*
|
||||
* @return const std::string&
|
||||
*/
|
||||
const std::string &name() const {
|
||||
return serverName_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get the address of the server.
|
||||
*
|
||||
* @return const trantor::InetAddress&
|
||||
*/
|
||||
const trantor::InetAddress &address() const;
|
||||
/**
|
||||
* @brief Get the IP and port string of the server.
|
||||
*
|
||||
* @return const std::string
|
||||
*/
|
||||
const std::string ipPort() const;
|
||||
|
||||
/**
|
||||
* @brief Get the event loop of the server.
|
||||
*
|
||||
* @return EventLoop*
|
||||
*/
|
||||
EventLoop *getLoop() const
|
||||
{
|
||||
return loop_;
|
||||
}
|
||||
/**
|
||||
* @brief Get the address of the server.
|
||||
*
|
||||
* @return const trantor::InetAddress&
|
||||
*/
|
||||
const trantor::InetAddress &address() const;
|
||||
|
||||
/**
|
||||
* @brief Get the I/O event loops of the server.
|
||||
*
|
||||
* @return std::vector<EventLoop *>
|
||||
*/
|
||||
std::vector<EventLoop *> getIoLoops() const
|
||||
{
|
||||
return loopPoolPtr_->getLoops();
|
||||
}
|
||||
/**
|
||||
* @brief Get the event loop of the server.
|
||||
*
|
||||
* @return EventLoop*
|
||||
*/
|
||||
EventLoop *getLoop() const {
|
||||
return loop_;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief An idle connection is a connection that has no read or write, kick
|
||||
* off it after timeout seconds.
|
||||
*
|
||||
* @param timeout
|
||||
*/
|
||||
void kickoffIdleConnections(size_t timeout)
|
||||
{
|
||||
loop_->runInLoop([this, timeout]() {
|
||||
assert(!started_);
|
||||
idleTimeout_ = timeout;
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @brief Get the I/O event loops of the server.
|
||||
*
|
||||
* @return std::vector<EventLoop *>
|
||||
*/
|
||||
std::vector<EventLoop *> getIoLoops() const {
|
||||
return loopPoolPtr_->getLoops();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enable SSL encryption.
|
||||
*
|
||||
* @param certPath The path of the certificate file.
|
||||
* @param keyPath The path of the private key file.
|
||||
* @param useOldTLS If true, the TLS 1.0 and 1.1 are supported by the
|
||||
* server.
|
||||
* @param sslConfCmds The commands used to call the SSL_CONF_cmd function in
|
||||
* OpenSSL.
|
||||
* @note It's well known that TLS 1.0 and 1.1 are not considered secure in
|
||||
* 2020. And it's a good practice to only use TLS 1.2 and above.
|
||||
*/
|
||||
void enableSSL(const std::string &certPath,
|
||||
const std::string &keyPath,
|
||||
bool useOldTLS = false,
|
||||
const std::vector<std::pair<std::string, std::string>>
|
||||
&sslConfCmds = {});
|
||||
/**
|
||||
* @brief An idle connection is a connection that has no read or write, kick
|
||||
* off it after timeout seconds.
|
||||
*
|
||||
* @param timeout
|
||||
*/
|
||||
void kickoffIdleConnections(size_t timeout) {
|
||||
loop_->runInLoop([this, timeout]() {
|
||||
assert(!started_);
|
||||
idleTimeout_ = timeout;
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
EventLoop *loop_;
|
||||
std::unique_ptr<Acceptor> acceptorPtr_;
|
||||
void newConnection(int fd, const InetAddress &peer);
|
||||
std::string serverName_;
|
||||
std::set<TcpConnectionPtr> connSet_;
|
||||
/**
|
||||
* @brief Enable SSL encryption.
|
||||
*
|
||||
* @param certPath The path of the certificate file.
|
||||
* @param keyPath The path of the private key file.
|
||||
* @param useOldTLS If true, the TLS 1.0 and 1.1 are supported by the
|
||||
* server.
|
||||
* @param sslConfCmds The commands used to call the SSL_CONF_cmd function in
|
||||
* OpenSSL.
|
||||
* @note It's well known that TLS 1.0 and 1.1 are not considered secure in
|
||||
* 2020. And it's a good practice to only use TLS 1.2 and above.
|
||||
*/
|
||||
void enableSSL(const std::string &certPath,
|
||||
const std::string &keyPath,
|
||||
bool useOldTLS = false,
|
||||
const std::vector<std::pair<std::string, std::string> >
|
||||
&sslConfCmds = {});
|
||||
|
||||
RecvMessageCallback recvMessageCallback_;
|
||||
ConnectionCallback connectionCallback_;
|
||||
WriteCompleteCallback writeCompleteCallback_;
|
||||
private:
|
||||
EventLoop *loop_;
|
||||
std::unique_ptr<Acceptor> acceptorPtr_;
|
||||
void newConnection(int fd, const InetAddress &peer);
|
||||
std::string serverName_;
|
||||
std::set<TcpConnectionPtr> connSet_;
|
||||
|
||||
size_t idleTimeout_{0};
|
||||
std::map<EventLoop *, std::shared_ptr<TimingWheel>> timingWheelMap_;
|
||||
void connectionClosed(const TcpConnectionPtr &connectionPtr);
|
||||
std::shared_ptr<EventLoopThreadPool> loopPoolPtr_;
|
||||
RecvMessageCallback recvMessageCallback_;
|
||||
ConnectionCallback connectionCallback_;
|
||||
WriteCompleteCallback writeCompleteCallback_;
|
||||
|
||||
size_t idleTimeout_{ 0 };
|
||||
std::map<EventLoop *, std::shared_ptr<TimingWheel> > timingWheelMap_;
|
||||
void connectionClosed(const TcpConnectionPtr &connectionPtr);
|
||||
std::shared_ptr<EventLoopThreadPool> loopPoolPtr_;
|
||||
#ifndef _WIN32
|
||||
class IgnoreSigPipe
|
||||
{
|
||||
public:
|
||||
IgnoreSigPipe()
|
||||
{
|
||||
::signal(SIGPIPE, SIG_IGN);
|
||||
LOG_TRACE << "Ignore SIGPIPE";
|
||||
}
|
||||
};
|
||||
class IgnoreSigPipe {
|
||||
public:
|
||||
IgnoreSigPipe() {
|
||||
::signal(SIGPIPE, SIG_IGN);
|
||||
LOG_TRACE << "Ignore SIGPIPE";
|
||||
}
|
||||
};
|
||||
|
||||
IgnoreSigPipe initObj;
|
||||
IgnoreSigPipe initObj;
|
||||
#endif
|
||||
bool started_{false};
|
||||
bool started_{ false };
|
||||
|
||||
// OpenSSL SSL context Object;
|
||||
std::shared_ptr<SSLContext> sslCtxPtr_;
|
||||
// OpenSSL SSL context Object;
|
||||
std::shared_ptr<SSLContext> sslCtxPtr_;
|
||||
};
|
||||
|
||||
} // namespace trantor
|
||||
} // namespace trantor
|
||||
|
Loading…
Reference in New Issue
Block a user