16#include <trantor/exports.h>
18#include <trantor/net/InetAddress.h>
20#include <trantor/net/callbacks.h>
52 bool reUseAddr =
true,
53 bool reUsePort =
true);
78 loopPoolPtr_ = std::make_shared<EventLoopThreadPool>(num);
79 loopPoolPtr_->start();
80 ioLoops_ = loopPoolPtr_->getLoops();
81 numIoLoops_ = ioLoops_.size();
93 assert(pool->size() > 0);
96 loopPoolPtr_->start();
97 ioLoops_ = loopPoolPtr_->getLoops();
98 numIoLoops_ = ioLoops_.size();
109 void setIoLoops(
const std::vector<trantor::EventLoop *> &ioLoops)
111 assert(!ioLoops.empty());
114 numIoLoops_ = ioLoops_.size();
115 loopPoolPtr_.reset();
126 recvMessageCallback_ = cb;
128 void setRecvMessageCallback(RecvMessageCallback &&cb)
130 recvMessageCallback_ = std::move(cb);
141 connectionCallback_ = cb;
143 void setConnectionCallback(ConnectionCallback &&cb)
145 connectionCallback_ = std::move(cb);
156 writeCompleteCallback_ = cb;
158 void setWriteCompleteCallback(WriteCompleteCallback &&cb)
160 writeCompleteCallback_ = std::move(cb);
181 const std::string &
name()
const
228 loop_->runInLoop([
this, timeout]() {
230 idleTimeout_ = timeout;
247 [[deprecated(
"Use enableSSL(TLSPolicyPtr) instead")]]
void enableSSL(
248 const std::string &certPath,
249 const std::string &keyPath,
250 bool useOldTLS =
false,
251 const std::vector<std::pair<std::string, std::string>> &sslConfCmds =
253 const std::string &caPath =
"");
259 policyPtr_ = std::move(policy);
260 sslContextPtr_ = newSSLContext(*policyPtr_,
true);
272 void handleCloseInLoop(
const TcpConnectionPtr &connectionPtr);
273 void newConnection(
int fd,
const InetAddress &peer);
274 void connectionClosed(
const TcpConnectionPtr &connectionPtr);
277 std::unique_ptr<Acceptor> acceptorPtr_;
278 std::string serverName_;
279 std::set<TcpConnectionPtr> connSet_;
281 RecvMessageCallback recvMessageCallback_;
282 ConnectionCallback connectionCallback_;
283 WriteCompleteCallback writeCompleteCallback_;
285 size_t idleTimeout_{0};
286 std::map<EventLoop *, std::shared_ptr<TimingWheel>> timingWheelMap_;
291 std::shared_ptr<EventLoopThreadPool> loopPoolPtr_;
295 std::vector<EventLoop *> ioLoops_;
296 size_t nextLoopIdx_{0};
297 size_t numIoLoops_{0};
305 ::signal(SIGPIPE, SIG_IGN);
306 LOG_TRACE <<
"Ignore SIGPIPE";
310 IgnoreSigPipe initObj;
312 bool started_{
false};
313 TLSPolicyPtr policyPtr_{
nullptr};
314 SSLContextPtr sslContextPtr_{
nullptr};
As the name implies, this class represents an event loop that runs in a particular thread....
Definition EventLoop.h:56
Wrapper of sockaddr_in. This is an POD interface class.
Definition InetAddress.h:46
void enableSSL(const std::string &certPath, const std::string &keyPath, bool useOldTLS=false, const std::vector< std::pair< std::string, std::string > > &sslConfCmds={}, const std::string &caPath="")
Enable SSL encryption.
void setIoLoopThreadPool(const std::shared_ptr< EventLoopThreadPool > &pool)
Set the event loops pool in which the I/O of connections to the server is handled....
Definition TcpServer.h:91
void setIoLoops(const std::vector< trantor::EventLoop * > &ioLoops)
Set the event loops in which the I/O of connections to the server is handled. The loops are managed b...
Definition TcpServer.h:109
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
Set the write complete callback.
Definition TcpServer.h:154
void kickoffIdleConnections(size_t timeout)
An idle connection is a connection that has no read or write, kick off it after timeout seconds.
Definition TcpServer.h:226
void enableSSL(TLSPolicyPtr policy)
Enable SSL encryption.
Definition TcpServer.h:257
void setAfterAcceptSockOptCallback(SockOptCallback cb)
Set the after accept setsockopt callback.
void setIoLoopNum(size_t num)
Set the number of event loops in which the I/O of connections to the server is handled....
Definition TcpServer.h:75
std::string ipPort() const
Get the IP and port string of the server.
void setConnectionCallback(const ConnectionCallback &cb)
Set the connection callback.
Definition TcpServer.h:139
EventLoop * getLoop() const
Get the event loop of the server.
Definition TcpServer.h:205
void setRecvMessageCallback(const RecvMessageCallback &cb)
Set the message callback.
Definition TcpServer.h:124
const trantor::InetAddress & address() const
Get the address of the server.
void setBeforeListenSockOptCallback(SockOptCallback cb)
Set the before listen setsockopt callback.
std::vector< EventLoop * > getIoLoops() const
Get the I/O event loops of the server.
Definition TcpServer.h:215
const std::string & name() const
Get the name of the server.
Definition TcpServer.h:181
void stop()
Stop the server.
void start()
Start the server.
TcpServer(EventLoop *loop, const InetAddress &address, std::string name, bool reUseAddr=true, bool reUsePort=true)
Construct a new TCP server instance.
void reloadSSL()
Reload the SSL context.
Definition EventLoop.h:34