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>
34using SSLContextPtr = std::shared_ptr<SSLContext>;
40class TRANTOR_EXPORT TcpConnection
43 friend class TcpServer;
44 friend class TcpConnectionImpl;
45 friend class TcpClient;
47 TcpConnection() =
default;
48 virtual ~TcpConnection(){};
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;
62 virtual void send(
const std::shared_ptr<std::string> &msgPtr) = 0;
63 virtual void send(
const std::shared_ptr<MsgBuffer> &msgPtr) = 0;
74 long long length = 0) = 0;
84 long long length = 0) = 0;
95 virtual void sendStream(std::function<std::size_t(
char *, std::size_t)>
188 contextPtr_ = context;
190 void setContext(std::shared_ptr<void> &&context)
192 contextPtr_ = std::move(context);
194 virtual std::string applicationProtocol()
const = 0;
202 template <
typename T>
205 return std::static_pointer_cast<T>(contextPtr_);
216 return (
bool)contextPtr_;
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 =
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));
317 void setValidationPolicy(TLSPolicy &&policy)
319 tlsPolicy_ = std::move(policy);
322 void setRecvMsgCallback(
const RecvMessageCallback &cb)
324 recvMsgCallback_ = cb;
326 void setRecvMsgCallback(RecvMessageCallback &&cb)
328 recvMsgCallback_ = std::move(cb);
330 void setConnectionCallback(
const ConnectionCallback &cb)
332 connectionCallback_ = cb;
334 void setConnectionCallback(ConnectionCallback &&cb)
336 connectionCallback_ = std::move(cb);
338 void setWriteCompleteCallback(
const WriteCompleteCallback &cb)
340 writeCompleteCallback_ = cb;
342 void setWriteCompleteCallback(WriteCompleteCallback &&cb)
344 writeCompleteCallback_ = std::move(cb);
346 void setCloseCallback(
const CloseCallback &cb)
350 void setCloseCallback(CloseCallback &&cb)
352 closeCallback_ = std::move(cb);
354 CloseCallback getCloseCallback()
const
356 return closeCallback_;
358 void setSSLErrorCallback(
const SSLErrorCallback &cb)
360 sslErrorCallback_ = cb;
362 void setSSLErrorCallback(SSLErrorCallback &&cb)
364 sslErrorCallback_ = std::move(cb);
368 virtual void connectEstablished() = 0;
369 virtual void connectDestroyed() = 0;
370 virtual void enableKickingOff(
372 const std::shared_ptr<TimingWheel> &timingWheel) = 0;
374 virtual void forwardToTLSBuffer(MsgBuffer *buffer) = 0;
378 RecvMessageCallback recvMsgCallback_;
379 ConnectionCallback connectionCallback_;
380 CloseCallback closeCallback_;
381 WriteCompleteCallback writeCompleteCallback_;
382 HighWaterMarkCallback highWaterMarkCallback_;
383 SSLErrorCallback sslErrorCallback_;
384 TLSPolicy tlsPolicy_;
387 std::shared_ptr<void> contextPtr_;
389TRANTOR_EXPORT SSLContextPtr newSSLContext(
const TLSPolicy &policy,
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