Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setSocketErrorCallback #351

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions trantor/net/TcpClient.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ TcpClient::~TcpClient()
return;
}
assert(loop_ == connection_->getLoop());
auto conn =
std::atomic_load_explicit(&connection_, std::memory_order_relaxed);
auto conn = connection_;
loop_->runInLoop([conn = std::move(conn)]() {
conn->setCloseCallback([](const TcpConnectionPtr &connPtr) mutable {
connPtr->getLoop()->queueInLoop(
Expand Down Expand Up @@ -113,6 +112,9 @@ void TcpClient::connect()
ptr->connectionErrorCallback_();
}
});
if (socketErrorCallback_)
connector_->setSockErrorCallback(socketErrorCallback_);

connect_ = true;
connector_->start();
}
Expand Down
11 changes: 11 additions & 0 deletions trantor/net/TcpClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ class TRANTOR_EXPORT TcpClient : NonCopyable,
connectionErrorCallback_ = cb;
}

/**
* @brief Set the create socket error callback.
*
* @param cb The callback is called when an error occurs create socket
*/
void setSocketErrorCallback(const SocketErrorCallback &cb)
{
socketErrorCallback_ = cb;
}

/**
* @brief Set the message callback.
*
Expand Down Expand Up @@ -245,6 +255,7 @@ class TRANTOR_EXPORT TcpClient : NonCopyable,
RecvMessageCallback messageCallback_;
WriteCompleteCallback writeCompleteCallback_;
SSLErrorCallback sslErrorCallback_;
SocketErrorCallback socketErrorCallback_;
std::atomic_bool retry_; // atomic
std::atomic_bool connect_; // atomic
// always in loop thread
Expand Down
2 changes: 2 additions & 0 deletions trantor/net/callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <functional>
#include <memory>
#include <string>
namespace trantor
{
enum class SSLError
Expand All @@ -34,6 +35,7 @@ using TcpConnectionPtr = std::shared_ptr<TcpConnection>;
using RecvMessageCallback =
std::function<void(const TcpConnectionPtr &, MsgBuffer *)>;
using ConnectionErrorCallback = std::function<void()>;
using SocketErrorCallback = std::function<void(std::string)>;
using ConnectionCallback = std::function<void(const TcpConnectionPtr &)>;
using CloseCallback = std::function<void(const TcpConnectionPtr &)>;
using WriteCompleteCallback = std::function<void(const TcpConnectionPtr &)>;
Expand Down
14 changes: 13 additions & 1 deletion trantor/net/inner/Connector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*
*
*/
#include <sstream>

#include "Connector.h"
#include "Channel.h"
Expand Down Expand Up @@ -78,7 +79,18 @@ void Connector::startInLoop()
void Connector::connect()
{
socketHanded_ = false;
fd_ = Socket::createNonblockingSocketOrDie(serverAddr_.family());
bool flag = (socketErrorCallback_ ? false : true);
fd_ = Socket::createNonblockingSocketOrDie(serverAddr_.family(), flag);
if (fd_ < 0)
{
std::stringstream error;
if (errno != 0)
{
error << strerror_tl(errno) << " (errno=" << errno << ") ";
}
socketErrorCallback_(error.str());
return;
}
if (sockOptCallback_)
sockOptCallback_(fd_);
errno = 0;
Expand Down
12 changes: 12 additions & 0 deletions trantor/net/inner/Connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class Connector : public NonCopyable,
using NewConnectionCallback = std::function<void(int sockfd)>;
using ConnectionErrorCallback = std::function<void()>;
using SockOptCallback = std::function<void(int sockfd)>;
using SocketErrorCallback = std::function<void(std::string error)>;

Connector(EventLoop *loop, const InetAddress &addr, bool retry = true);
Connector(EventLoop *loop, InetAddress &&addr, bool retry = true);
~Connector();
Expand Down Expand Up @@ -56,6 +58,14 @@ class Connector : public NonCopyable,
{
sockOptCallback_ = std::move(cb);
}
void setSockErrorCallback(const SocketErrorCallback &cb)
{
socketErrorCallback_ = cb;
}
void setSockErrorCallback(SocketErrorCallback &&cb)
{
socketErrorCallback_ = std::move(cb);
}
const InetAddress &serverAddress() const
{
return serverAddr_;
Expand All @@ -68,6 +78,8 @@ class Connector : public NonCopyable,
NewConnectionCallback newConnectionCallback_;
ConnectionErrorCallback errorCallback_;
SockOptCallback sockOptCallback_;
SocketErrorCallback socketErrorCallback_;

enum class Status
{
Disconnected,
Expand Down
5 changes: 3 additions & 2 deletions trantor/net/inner/Socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace trantor
class Socket : NonCopyable
{
public:
static int createNonblockingSocketOrDie(int family)
static int createNonblockingSocketOrDie(int family, bool flag = true)
{
#ifdef __linux__
int sock = ::socket(family,
Expand All @@ -41,7 +41,8 @@ class Socket : NonCopyable
if (sock < 0)
{
LOG_SYSERR << "sockets::createNonblockingOrDie";
exit(1);
if (flag)
exit(1);
}
LOG_TRACE << "sock=" << sock;
return sock;
Expand Down
2 changes: 2 additions & 0 deletions trantor/tests/TcpClientTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ int main()
buf->retrieveAll();
conn->shutdown();
});
client[i]->setSocketErrorCallback(
[](std::string error) { LOG_ERROR << error; });
client[i]->connect();
}
loop.loop();
Expand Down