trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
TcpServer.h
Go to the documentation of this file.
1
14
15#pragma once
16#include <trantor/exports.h>
18#include <trantor/net/InetAddress.h>
20#include <trantor/net/callbacks.h>
24#include <csignal>
25#include <memory>
26#include <set>
27#include <string>
28
29namespace trantor
30{
31class Acceptor;
36class TRANTOR_EXPORT TcpServer : NonCopyable
37{
38 public:
50 const InetAddress &address,
51 std::string name,
52 bool reUseAddr = true,
53 bool reUsePort = true);
54 ~TcpServer();
55
60 void start();
61
66 void stop();
67
75 void setIoLoopNum(size_t num)
76 {
77 assert(!started_);
78 loopPoolPtr_ = std::make_shared<EventLoopThreadPool>(num);
79 loopPoolPtr_->start();
80 ioLoops_ = loopPoolPtr_->getLoops();
81 numIoLoops_ = ioLoops_.size();
82 }
83
91 void setIoLoopThreadPool(const std::shared_ptr<EventLoopThreadPool> &pool)
92 {
93 assert(pool->size() > 0);
94 assert(!started_);
95 loopPoolPtr_ = pool;
96 loopPoolPtr_->start(); // TODO: should not start by TcpServer
97 ioLoops_ = loopPoolPtr_->getLoops();
98 numIoLoops_ = ioLoops_.size();
99 }
100
109 void setIoLoops(const std::vector<trantor::EventLoop *> &ioLoops)
110 {
111 assert(!ioLoops.empty());
112 assert(!started_);
113 ioLoops_ = ioLoops;
114 numIoLoops_ = ioLoops_.size();
115 loopPoolPtr_.reset();
116 }
117
124 void setRecvMessageCallback(const RecvMessageCallback &cb)
125 {
126 recvMessageCallback_ = cb;
127 }
128 void setRecvMessageCallback(RecvMessageCallback &&cb)
129 {
130 recvMessageCallback_ = std::move(cb);
131 }
132
139 void setConnectionCallback(const ConnectionCallback &cb)
140 {
141 connectionCallback_ = cb;
142 }
143 void setConnectionCallback(ConnectionCallback &&cb)
144 {
145 connectionCallback_ = std::move(cb);
146 }
147
154 void setWriteCompleteCallback(const WriteCompleteCallback &cb)
155 {
156 writeCompleteCallback_ = cb;
157 }
158 void setWriteCompleteCallback(WriteCompleteCallback &&cb)
159 {
160 writeCompleteCallback_ = std::move(cb);
161 }
162
168 void setBeforeListenSockOptCallback(SockOptCallback cb);
174 void setAfterAcceptSockOptCallback(SockOptCallback cb);
175
181 const std::string &name() const
182 {
183 return serverName_;
184 }
185
191 std::string ipPort() const;
192
199
206 {
207 return loop_;
208 }
209
215 std::vector<EventLoop *> getIoLoops() const
216 {
217 return ioLoops_;
218 }
219
226 void kickoffIdleConnections(size_t timeout)
227 {
228 loop_->runInLoop([this, timeout]() {
229 assert(!started_);
230 idleTimeout_ = timeout;
231 });
232 }
233
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 =
252 {},
253 const std::string &caPath = "");
257 void enableSSL(TLSPolicyPtr policy)
258 {
259 policyPtr_ = std::move(policy);
260 sslContextPtr_ = newSSLContext(*policyPtr_, true);
261 }
262
269 void reloadSSL();
270
271 private:
272 void handleCloseInLoop(const TcpConnectionPtr &connectionPtr);
273 void newConnection(int fd, const InetAddress &peer);
274 void connectionClosed(const TcpConnectionPtr &connectionPtr);
275
276 EventLoop *loop_;
277 std::unique_ptr<Acceptor> acceptorPtr_;
278 std::string serverName_;
279 std::set<TcpConnectionPtr> connSet_;
280
281 RecvMessageCallback recvMessageCallback_;
282 ConnectionCallback connectionCallback_;
283 WriteCompleteCallback writeCompleteCallback_;
284
285 size_t idleTimeout_{0};
286 std::map<EventLoop *, std::shared_ptr<TimingWheel>> timingWheelMap_;
287
288 // `loopPoolPtr_` may and may not hold the internal thread pool.
289 // We should not access it directly in codes.
290 // Instead, we should use its delegation variable `ioLoops_`.
291 std::shared_ptr<EventLoopThreadPool> loopPoolPtr_;
292 // If one of `setIoLoopNum()`, `setIoLoopThreadPool()` and `setIoLoops()` is
293 // called, `ioLoops_` will hold the loops passed in.
294 // Otherwise, it should contain only one element, which is `loop_`.
295 std::vector<EventLoop *> ioLoops_;
296 size_t nextLoopIdx_{0};
297 size_t numIoLoops_{0};
298
299#ifndef _WIN32
300 class IgnoreSigPipe
301 {
302 public:
303 IgnoreSigPipe()
304 {
305 ::signal(SIGPIPE, SIG_IGN);
306 LOG_TRACE << "Ignore SIGPIPE";
307 }
308 };
309
310 IgnoreSigPipe initObj;
311#endif
312 bool started_{false};
313 TLSPolicyPtr policyPtr_{nullptr};
314 SSLContextPtr sslContextPtr_{nullptr};
315};
316
317} // namespace trantor
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