Skip to content

Commit 35592d5

Browse files
authored
Add setsockopt to TcpClient (#255)
1 parent 8abea09 commit 35592d5

File tree

6 files changed

+60
-0
lines changed

6 files changed

+60
-0
lines changed

trantor/net/TcpClient.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ void TcpClient::stop()
133133
connector_->stop();
134134
}
135135

136+
void TcpClient::setSockOptCallback(SockOptCallback &&cb)
137+
{
138+
connector_->setSockOptCallback(std::move(cb));
139+
}
140+
141+
void TcpClient::setSockOptCallback(const SockOptCallback &cb)
142+
{
143+
connector_->setSockOptCallback(cb);
144+
}
145+
136146
void TcpClient::newConnection(int sockfd)
137147
{
138148
loop_->assertInLoopThread();

trantor/net/TcpClient.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,13 @@ class TRANTOR_EXPORT TcpClient : NonCopyable,
190190
sslErrorCallback_ = std::move(cb);
191191
}
192192

193+
/**
194+
* @brief Set the callback for set socket option
195+
* @param cb The callback is called, before connect
196+
*/
197+
void setSockOptCallback(const SockOptCallback &cb);
198+
void setSockOptCallback(SockOptCallback &&cb);
199+
193200
/**
194201
* @brief Enable SSL encryption.
195202
* @param useOldTLS If true, the TLS 1.0 and 1.1 are supported by the

trantor/net/callbacks.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@ using WriteCompleteCallback = std::function<void(const TcpConnectionPtr &)>;
4040
using HighWaterMarkCallback =
4141
std::function<void(const TcpConnectionPtr &, const size_t)>;
4242
using SSLErrorCallback = std::function<void(SSLError)>;
43+
using SockOptCallback = std::function<void(int)>;
4344

4445
} // namespace trantor

trantor/net/inner/Connector.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ void Connector::connect()
7979
{
8080
socketHanded_ = false;
8181
fd_ = Socket::createNonblockingSocketOrDie(serverAddr_.family());
82+
if (sockOptCallback_)
83+
sockOptCallback_(fd_);
8284
errno = 0;
8385
int ret = Socket::connect(fd_, serverAddr_);
8486
int savedErrno = (ret == 0) ? 0 : errno;

trantor/net/inner/Connector.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class Connector : public NonCopyable,
2828
public:
2929
using NewConnectionCallback = std::function<void(int sockfd)>;
3030
using ConnectionErrorCallback = std::function<void()>;
31+
using SockOptCallback = std::function<void(int sockfd)>;
3132
Connector(EventLoop *loop, const InetAddress &addr, bool retry = true);
3233
Connector(EventLoop *loop, InetAddress &&addr, bool retry = true);
3334
~Connector();
@@ -47,6 +48,14 @@ class Connector : public NonCopyable,
4748
{
4849
errorCallback_ = std::move(cb);
4950
}
51+
void setSockOptCallback(const SockOptCallback &cb)
52+
{
53+
sockOptCallback_ = cb;
54+
}
55+
void setSockOptCallback(SockOptCallback &&cb)
56+
{
57+
sockOptCallback_ = std::move(cb);
58+
}
5059
const InetAddress &serverAddress() const
5160
{
5261
return serverAddr_;
@@ -58,6 +67,7 @@ class Connector : public NonCopyable,
5867
private:
5968
NewConnectionCallback newConnectionCallback_;
6069
ConnectionErrorCallback errorCallback_;
70+
SockOptCallback sockOptCallback_;
6171
enum class Status
6272
{
6373
Disconnected,

trantor/tests/TcpClientTest.cc

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
#include <string>
55
#include <iostream>
66
#include <atomic>
7+
#ifdef _WIN32
8+
#include <ws2tcpip.h>
9+
#else
10+
#include <sys/socket.h>
11+
#include <netinet/tcp.h>
12+
#endif
13+
714
using namespace trantor;
815
#define USE_IPV6 0
916
int main()
@@ -24,6 +31,29 @@ int main()
2431
client[i] = std::make_shared<trantor::TcpClient>(&loop,
2532
serverAddr,
2633
"tcpclienttest");
34+
client[i]->setSockOptCallback([](int fd) {
35+
LOG_DEBUG << "setSockOptCallback!";
36+
#ifdef _WIN32
37+
#elif __linux__
38+
int optval = 10;
39+
::setsockopt(fd,
40+
SOL_TCP,
41+
TCP_KEEPCNT,
42+
&optval,
43+
static_cast<socklen_t>(sizeof optval));
44+
::setsockopt(fd,
45+
SOL_TCP,
46+
TCP_KEEPIDLE,
47+
&optval,
48+
static_cast<socklen_t>(sizeof optval));
49+
::setsockopt(fd,
50+
SOL_TCP,
51+
TCP_KEEPINTVL,
52+
&optval,
53+
static_cast<socklen_t>(sizeof optval));
54+
#else
55+
#endif
56+
});
2757
client[i]->setConnectionCallback(
2858
[i, &loop, &connCount](const TcpConnectionPtr &conn) {
2959
if (conn->connected())

0 commit comments

Comments
 (0)