trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
TcpConnection.h
Go to the documentation of this file.
1
14
15#pragma once
16#include <trantor/exports.h>
17#include <trantor/net/EventLoop.h>
18#include <trantor/net/InetAddress.h>
21#include <trantor/net/callbacks.h>
22#include <trantor/net/Certificate.h>
23#include <trantor/net/TLSPolicy.h>
25#include <memory>
26#include <functional>
27#include <string>
28
29namespace trantor
30{
31class TimingWheel;
32
33struct SSLContext;
34using SSLContextPtr = std::shared_ptr<SSLContext>;
35
40class TRANTOR_EXPORT TcpConnection
41{
42 public:
43 friend class TcpServer;
44 friend class TcpConnectionImpl;
45 friend class TcpClient;
46
47 TcpConnection() = default;
48 virtual ~TcpConnection(){};
49
56 virtual void send(const char *msg, size_t len) = 0;
57 virtual void send(const void *msg, size_t len) = 0;
58 virtual void send(const std::string &msg) = 0;
59 virtual void send(std::string &&msg) = 0;
60 virtual void send(const MsgBuffer &buffer) = 0;
61 virtual void send(MsgBuffer &&buffer) = 0;
62 virtual void send(const std::shared_ptr<std::string> &msgPtr) = 0;
63 virtual void send(const std::shared_ptr<MsgBuffer> &msgPtr) = 0;
64
72 virtual void sendFile(const char *fileName,
73 long long offset = 0,
74 long long length = 0) = 0;
82 virtual void sendFile(const wchar_t *fileName,
83 long long offset = 0,
84 long long length = 0) = 0;
95 virtual void sendStream(std::function<std::size_t(char *, std::size_t)>
96 callback) = 0; // (buffer, buffer size) -> size
97 // of data put in buffer
98
106 virtual AsyncStreamPtr sendAsyncStream(bool disableKickoff = false) = 0;
112
113 virtual const InetAddress &localAddr() const = 0;
114
120 virtual const InetAddress &peerAddr() const = 0;
121
128 virtual bool connected() const = 0;
129
136 virtual bool disconnected() const = 0;
137
138 /* *
139 * @brief Get the buffer in which the received data stored.
140 *
141 * @return MsgBuffer*
142 */
143 // virtual MsgBuffer *getRecvBuffer() = 0;
144
152 virtual void setHighWaterMarkCallback(const HighWaterMarkCallback &cb,
153 size_t markLen) = 0;
154
160 virtual void setTcpNoDelay(bool on) = 0;
161
166 virtual void shutdown() = 0;
167
172 virtual void forceClose() = 0;
173
179 virtual EventLoop *getLoop() = 0;
180
186 void setContext(const std::shared_ptr<void> &context)
187 {
188 contextPtr_ = context;
189 }
190 void setContext(std::shared_ptr<void> &&context)
191 {
192 contextPtr_ = std::move(context);
193 }
194 virtual std::string applicationProtocol() const = 0;
195
202 template <typename T>
203 std::shared_ptr<T> getContext() const
204 {
205 return std::static_pointer_cast<T>(contextPtr_);
206 }
207
214 bool hasContext() const
215 {
216 return (bool)contextPtr_;
217 }
218
224 {
225 contextPtr_.reset();
226 }
227
233 virtual void keepAlive() = 0;
234
241 virtual bool isKeepAlive() = 0;
242
248 virtual size_t bytesSent() const = 0;
249
255 virtual size_t bytesReceived() const = 0;
256
263 virtual bool isSSLConnection() const = 0;
264
268 virtual MsgBuffer *getRecvBuffer() = 0;
269
276 virtual CertificatePtr peerCertificate() const = 0;
277
284 virtual std::string sniName() const = 0;
285
292 virtual void startEncryption(TLSPolicyPtr policy,
293 bool isServer,
294 std::function<void(const TcpConnectionPtr &)>
295 upgradeCallback = nullptr) = 0;
300 [[deprecated("Use startEncryption(TLSPolicyPtr) instead")]] void
302 std::function<void(const TcpConnectionPtr &)> &&callback,
303 bool useOldTLS = false,
304 bool validateCert = true,
305 const std::string &hostname = "",
306 const std::vector<std::pair<std::string, std::string>> &sslConfCmds =
307 {})
308 {
309 auto policy = TLSPolicy::defaultClientPolicy();
310 policy->setUseOldTLS(useOldTLS)
311 .setValidate(validateCert)
312 .setHostname(hostname)
313 .setConfCmds(sslConfCmds);
314 startEncryption(std::move(policy), false, std::move(callback));
315 }
316
317 void setValidationPolicy(TLSPolicy &&policy)
318 {
319 tlsPolicy_ = std::move(policy);
320 }
321
322 void setRecvMsgCallback(const RecvMessageCallback &cb)
323 {
324 recvMsgCallback_ = cb;
325 }
326 void setRecvMsgCallback(RecvMessageCallback &&cb)
327 {
328 recvMsgCallback_ = std::move(cb);
329 }
330 void setConnectionCallback(const ConnectionCallback &cb)
331 {
332 connectionCallback_ = cb;
333 }
334 void setConnectionCallback(ConnectionCallback &&cb)
335 {
336 connectionCallback_ = std::move(cb);
337 }
338 void setWriteCompleteCallback(const WriteCompleteCallback &cb)
339 {
340 writeCompleteCallback_ = cb;
341 }
342 void setWriteCompleteCallback(WriteCompleteCallback &&cb)
343 {
344 writeCompleteCallback_ = std::move(cb);
345 }
346 void setCloseCallback(const CloseCallback &cb)
347 {
348 closeCallback_ = cb;
349 }
350 void setCloseCallback(CloseCallback &&cb)
351 {
352 closeCallback_ = std::move(cb);
353 }
354 CloseCallback getCloseCallback() const
355 {
356 return closeCallback_;
357 }
358 void setSSLErrorCallback(const SSLErrorCallback &cb)
359 {
360 sslErrorCallback_ = cb;
361 }
362 void setSSLErrorCallback(SSLErrorCallback &&cb)
363 {
364 sslErrorCallback_ = std::move(cb);
365 }
366
367 // TODO: These should be internal APIs
368 virtual void connectEstablished() = 0;
369 virtual void connectDestroyed() = 0;
370 virtual void enableKickingOff(
371 size_t timeout,
372 const std::shared_ptr<TimingWheel> &timingWheel) = 0;
373
374 virtual void forwardToTLSBuffer(MsgBuffer *buffer) = 0;
375
376 protected:
377 // callbacks
378 RecvMessageCallback recvMsgCallback_;
379 ConnectionCallback connectionCallback_;
380 CloseCallback closeCallback_;
381 WriteCompleteCallback writeCompleteCallback_;
382 HighWaterMarkCallback highWaterMarkCallback_;
383 SSLErrorCallback sslErrorCallback_;
384 TLSPolicy tlsPolicy_;
385
386 private:
387 std::shared_ptr<void> contextPtr_;
388};
389TRANTOR_EXPORT SSLContextPtr newSSLContext(const TLSPolicy &policy,
390 bool server);
391
392} // 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
This class represents a memory buffer used for sending and receiving data.
Definition MsgBuffer.h:40
virtual void startEncryption(TLSPolicyPtr policy, bool isServer, std::function< void(const TcpConnectionPtr &)> upgradeCallback=nullptr)=0
Start TLS. If the connection is specified as a server, the connection will be upgraded to a TLS serve...
virtual void setHighWaterMarkCallback(const HighWaterMarkCallback &cb, size_t markLen)=0
Set the high water mark callback.
virtual size_t bytesReceived() const =0
Return the number of bytes received.
void startClientEncryption(std::function< void(const TcpConnectionPtr &)> &&callback, bool useOldTLS=false, bool validateCert=true, const std::string &hostname="", const std::vector< std::pair< std::string, std::string > > &sslConfCmds={})
Start TLS as a client.
Definition TcpConnection.h:301
void clearContext()
Clear the custom data.
Definition TcpConnection.h:223
virtual CertificatePtr peerCertificate() const =0
Get peer certificate (if any).
virtual std::string sniName() const =0
Get the SNI name (for server connections only).
virtual void forceClose()=0
Close the connection forcefully.
virtual void sendFile(const char *fileName, long long offset=0, long long length=0)=0
Send a file to the peer.
virtual void sendFile(const wchar_t *fileName, long long offset=0, long long length=0)=0
Send a file to the peer.
virtual bool isSSLConnection() const =0
Check whether the connection is SSL encrypted.
virtual bool disconnected() const =0
Return false if the connection is established.
std::shared_ptr< T > getContext() const
Get the custom data from the connection.
Definition TcpConnection.h:203
virtual size_t bytesSent() const =0
Return the number of bytes sent.
virtual MsgBuffer * getRecvBuffer()=0
Get buffer of unprompted data.
bool hasContext() const
Return true if the custom data is set by user.
Definition TcpConnection.h:214
virtual const InetAddress & localAddr() const =0
Get the local address of the connection.
virtual EventLoop * getLoop()=0
Get the event loop in which the connection I/O is handled.
virtual bool isKeepAlive()=0
Return true if the keepAlive() method is called.
virtual AsyncStreamPtr sendAsyncStream(bool disableKickoff=false)=0
Send a stream to the peer asynchronously.
virtual bool connected() const =0
Return true if the connection is established.
virtual void setTcpNoDelay(bool on)=0
Set the TCP_NODELAY option to the socket.
virtual void shutdown()=0
Shutdown the connection.
virtual const InetAddress & peerAddr() const =0
Get the remote address of the connection.
virtual void keepAlive()=0
Call this method to avoid being kicked off by TcpServer, refer to the kickoffIdleConnections method i...
virtual void sendStream(std::function< std::size_t(char *, std::size_t)> callback)=0
Send a stream to the peer.
void setContext(const std::shared_ptr< void > &context)
Set the custom data on the connection.
Definition TcpConnection.h:186
virtual void send(const char *msg, size_t len)=0
Send some data to the peer.
This class implements a timer strategy with high performance and low accuracy. This is usually used i...
Definition TimingWheel.h:46
Definition EventLoop.h:34
Definition TLSPolicy.h:12