20#include <trantor/net/EventLoop.h>
21#include <trantor/net/InetAddress.h>
24#include <trantor/exports.h>
32using ConnectorPtr = std::shared_ptr<Connector>;
38 public std::enable_shared_from_this<TcpClient>
50 const std::string &nameArg);
78 std::lock_guard<std::mutex> lock(mutex_);
117 const std::string &
name()
const
130 connectionCallback_ = cb;
132 void setConnectionCallback(ConnectionCallback &&cb)
134 connectionCallback_ = std::move(cb);
145 connectionErrorCallback_ = cb;
156 messageCallback_ = cb;
158 void setMessageCallback(RecvMessageCallback &&cb)
160 messageCallback_ = std::move(cb);
173 writeCompleteCallback_ = cb;
175 void setWriteCompleteCallback(WriteCompleteCallback &&cb)
177 writeCompleteCallback_ = std::move(cb);
186 sslErrorCallback_ = cb;
188 void setSSLErrorCallback(SSLErrorCallback &&cb)
190 sslErrorCallback_ = std::move(cb);
216 [[deprecated(
"Use enableSSL(TLSPolicyPtr policy) instead")]]
void enableSSL(
217 bool useOldTLS =
false,
218 bool validateCert =
true,
219 std::string hostname =
"",
220 const std::vector<std::pair<std::string, std::string>> &sslConfCmds =
222 const std::string &certPath =
"",
223 const std::string &keyPath =
"",
224 const std::string &caPath =
"");
230 tlsPolicyPtr_ = std::move(policy);
231 sslContextPtr_ = newSSLContext(*tlsPolicyPtr_,
false);
236 void newConnection(
int sockfd);
238 void removeConnection(
const TcpConnectionPtr &conn);
241 ConnectorPtr connector_;
242 const std::string name_;
243 ConnectionCallback connectionCallback_;
244 ConnectionErrorCallback connectionErrorCallback_;
245 RecvMessageCallback messageCallback_;
246 WriteCompleteCallback writeCompleteCallback_;
247 SSLErrorCallback sslErrorCallback_;
248 std::atomic_bool retry_;
249 std::atomic_bool connect_;
251 mutable std::mutex mutex_;
252 TcpConnectionPtr connection_;
253 TLSPolicyPtr tlsPolicyPtr_;
254 SSLContextPtr sslContextPtr_;
255 bool validateCert_{
false};
263 ::signal(SIGPIPE, SIG_IGN);
267 static IgnoreSigPipe initObj;
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(bool useOldTLS=false, bool validateCert=true, std::string hostname="", const std::vector< std::pair< std::string, std::string > > &sslConfCmds={}, const std::string &certPath="", const std::string &keyPath="", const std::string &caPath="")
Enable SSL encryption.
bool retry() const
Check whether the client re-connect to the server.
Definition TcpClient.h:98
EventLoop * getLoop() const
Get the event loop.
Definition TcpClient.h:87
void setMessageCallback(const RecvMessageCallback &cb)
Set the message callback.
Definition TcpClient.h:154
void enableRetry()
Enable retrying.
Definition TcpClient.h:107
const std::string & name() const
Get the name of the client.
Definition TcpClient.h:117
void disconnect()
Disconnect from the server.
TcpClient(EventLoop *loop, const InetAddress &serverAddr, const std::string &nameArg)
Construct a new TCP client instance.
void setSSLErrorCallback(const SSLErrorCallback &cb)
Set the callback for errors of SSL.
Definition TcpClient.h:184
void connect()
Connect to the server.
void setWriteCompleteCallback(const WriteCompleteCallback &cb)
Set the write complete callback.
Definition TcpClient.h:171
void setConnectionCallback(const ConnectionCallback &cb)
Set the connection callback.
Definition TcpClient.h:128
void enableSSL(TLSPolicyPtr policy)
Enable SSL encryption.
Definition TcpClient.h:228
void setSockOptCallback(const SockOptCallback &cb)
Set the callback for set socket option.
void setConnectionErrorCallback(const ConnectionErrorCallback &cb)
Set the connection error callback.
Definition TcpClient.h:143
void stop()
Stop connecting to the server.
TcpConnectionPtr connection() const
Get the TCP connection to the server.
Definition TcpClient.h:76
Definition EventLoop.h:34