trantor
Non-blocking I/O cross-platform TCP network library, using C++14
Loading...
Searching...
No Matches
TcpClient.h
1// taken from muduo
2// Copyright 2010, Shuo Chen. All rights reserved.
3// http://code.google.com/p/muduo/
4//
5// Use of this source code is governed by a BSD-style license
6// that can be found in the License file.
7
8// Author: Shuo Chen (chenshuo at chenshuo dot com)
9//
10// This is a public header file, it must only include public header files.
11
12// Copyright 2016, Tao An. All rights reserved.
13//
14// Use of this source code is governed by a BSD-style license
15// that can be found in the License file.
16
17// Author: Tao An
18
19#pragma once
20#include <trantor/net/EventLoop.h>
21#include <trantor/net/InetAddress.h>
24#include <trantor/exports.h>
25#include <functional>
26#include <thread>
27#include <atomic>
28#include <signal.h>
29namespace trantor
30{
31class Connector;
32using ConnectorPtr = std::shared_ptr<Connector>;
37class TRANTOR_EXPORT TcpClient : NonCopyable,
38 public std::enable_shared_from_this<TcpClient>
39{
40 public:
49 const InetAddress &serverAddr,
50 const std::string &nameArg);
51 ~TcpClient();
52
57 void connect();
58
63 void disconnect();
64
69 void stop();
70
76 TcpConnectionPtr connection() const
77 {
78 std::lock_guard<std::mutex> lock(mutex_);
79 return connection_;
80 }
81
88 {
89 return loop_;
90 }
91
98 bool retry() const
99 {
100 return retry_;
101 }
102
108 {
109 retry_ = true;
110 }
111
117 const std::string &name() const
118 {
119 return name_;
120 }
121
128 void setConnectionCallback(const ConnectionCallback &cb)
129 {
130 connectionCallback_ = cb;
131 }
132 void setConnectionCallback(ConnectionCallback &&cb)
133 {
134 connectionCallback_ = std::move(cb);
135 }
136
143 void setConnectionErrorCallback(const ConnectionErrorCallback &cb)
144 {
145 connectionErrorCallback_ = cb;
146 }
147
154 void setMessageCallback(const RecvMessageCallback &cb)
155 {
156 messageCallback_ = cb;
157 }
158 void setMessageCallback(RecvMessageCallback &&cb)
159 {
160 messageCallback_ = std::move(cb);
161 }
164
171 void setWriteCompleteCallback(const WriteCompleteCallback &cb)
172 {
173 writeCompleteCallback_ = cb;
174 }
175 void setWriteCompleteCallback(WriteCompleteCallback &&cb)
176 {
177 writeCompleteCallback_ = std::move(cb);
178 }
179
184 void setSSLErrorCallback(const SSLErrorCallback &cb)
185 {
186 sslErrorCallback_ = cb;
187 }
188 void setSSLErrorCallback(SSLErrorCallback &&cb)
189 {
190 sslErrorCallback_ = std::move(cb);
191 }
192
197 void setSockOptCallback(const SockOptCallback &cb);
198 void setSockOptCallback(SockOptCallback &&cb);
199
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 =
221 {},
222 const std::string &certPath = "",
223 const std::string &keyPath = "",
224 const std::string &caPath = "");
228 void enableSSL(TLSPolicyPtr policy)
229 {
230 tlsPolicyPtr_ = std::move(policy);
231 sslContextPtr_ = newSSLContext(*tlsPolicyPtr_, false);
232 }
233
234 private:
236 void newConnection(int sockfd);
238 void removeConnection(const TcpConnectionPtr &conn);
239
240 EventLoop *loop_;
241 ConnectorPtr connector_; // avoid revealing 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_; // atomic
249 std::atomic_bool connect_; // atomic
250 // always in loop thread
251 mutable std::mutex mutex_;
252 TcpConnectionPtr connection_; // @GuardedBy mutex_
253 TLSPolicyPtr tlsPolicyPtr_;
254 SSLContextPtr sslContextPtr_;
255 bool validateCert_{false};
256
257#ifndef _WIN32
258 class IgnoreSigPipe
259 {
260 public:
261 IgnoreSigPipe()
262 {
263 ::signal(SIGPIPE, SIG_IGN);
264 }
265 };
266
267 static IgnoreSigPipe initObj;
268#endif
269};
270
271} // 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(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