Skip to content
Snippets Groups Projects
Commit ace21d9c authored by Adrien Béraud's avatar Adrien Béraud
Browse files

net: add logger support for UdpSocket

parent a02ace2a
No related tags found
No related merge requests found
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "sockaddr.h" #include "sockaddr.h"
#include "utils.h" #include "utils.h"
#include "log_enable.h"
#ifdef _WIN32 #ifdef _WIN32
#include <ws2tcpip.h> #include <ws2tcpip.h>
...@@ -84,8 +85,8 @@ private: ...@@ -84,8 +85,8 @@ private:
class OPENDHT_PUBLIC UdpSocket : public DatagramSocket { class OPENDHT_PUBLIC UdpSocket : public DatagramSocket {
public: public:
UdpSocket(in_port_t port); UdpSocket(in_port_t port, const Logger& l = {});
UdpSocket(const SockAddr& bind4, const SockAddr& bind6); UdpSocket(const SockAddr& bind4, const SockAddr& bind6, const Logger& l = {});
~UdpSocket(); ~UdpSocket();
int sendTo(const SockAddr& dest, const uint8_t* data, size_t size, bool replied) override; int sendTo(const SockAddr& dest, const uint8_t* data, size_t size, bool replied) override;
...@@ -99,6 +100,7 @@ public: ...@@ -99,6 +100,7 @@ public:
void stop() override; void stop() override;
private: private:
Logger logger;
int s4 {-1}; int s4 {-1};
int s6 {-1}; int s6 {-1};
int stopfd {-1}; int stopfd {-1};
......
...@@ -102,7 +102,7 @@ DhtRunner::run(const SockAddr& local4, const SockAddr& local6, const Config& con ...@@ -102,7 +102,7 @@ DhtRunner::run(const SockAddr& local4, const SockAddr& local6, const Config& con
{ {
if (not running) { if (not running) {
if (not context.sock) if (not context.sock)
context.sock.reset(new net::UdpSocket(local4, local6)); context.sock.reset(new net::UdpSocket(local4, local6, context.logger ? *context.logger : Logger{}));
run(config, std::move(context)); run(config, std::move(context));
} }
} }
......
...@@ -105,7 +105,7 @@ void udpPipe(int fds[2]) ...@@ -105,7 +105,7 @@ void udpPipe(int fds[2])
} }
#endif #endif
UdpSocket::UdpSocket(in_port_t port) { UdpSocket::UdpSocket(in_port_t port, const Logger& l) : logger(l) {
SockAddr bind4; SockAddr bind4;
bind4.setFamily(AF_INET); bind4.setFamily(AF_INET);
bind4.setPort(port); bind4.setPort(port);
...@@ -115,7 +115,8 @@ UdpSocket::UdpSocket(in_port_t port) { ...@@ -115,7 +115,8 @@ UdpSocket::UdpSocket(in_port_t port) {
openSockets(bind4, bind6); openSockets(bind4, bind6);
} }
UdpSocket::UdpSocket(const SockAddr& bind4, const SockAddr& bind6) { UdpSocket::UdpSocket(const SockAddr& bind4, const SockAddr& bind6, const Logger& l) : logger(l)
{
openSockets(bind4, bind6); openSockets(bind4, bind6);
} }
...@@ -151,8 +152,8 @@ UdpSocket::sendTo(const SockAddr& dest, const uint8_t* data, size_t size, bool r ...@@ -151,8 +152,8 @@ UdpSocket::sendTo(const SockAddr& dest, const uint8_t* data, size_t size, bool r
if (sendto(s, data, size, flags, dest.get(), dest.getLength()) == -1) { if (sendto(s, data, size, flags, dest.get(), dest.getLength()) == -1) {
int err = errno; int err = errno;
std::cerr << "Can't send message to " << dest.toString() << ": " << strerror(err) << std::endl; logger.d("Can't send message to %s: %s", dest.toString().c_str(), strerror(err));
if (err == EPIPE) { if (err == EPIPE || err == ENOTCONN || err == ECONNRESET) {
auto bind4 = std::move(bound4), bind6 = std::move(bound6); auto bind4 = std::move(bound4), bind6 = std::move(bound6);
openSockets(bind4, bind6); openSockets(bind4, bind6);
return sendTo(dest, data, size, false); return sendTo(dest, data, size, false);
...@@ -189,7 +190,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6) ...@@ -189,7 +190,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
try { try {
s4 = bindSocket(bind4, bound4); s4 = bindSocket(bind4, bound4);
} catch (const DhtException& e) { } catch (const DhtException& e) {
std::cerr << "Can't bind inet socket: " << e.what() << std::endl; logger.e("Can't bind inet socket: %s", e.what());
} }
} }
...@@ -199,7 +200,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6) ...@@ -199,7 +200,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
try { try {
s6 = bindSocket(bind6, bound6); s6 = bindSocket(bind6, bound6);
} catch (const DhtException& e) { } catch (const DhtException& e) {
std::cerr << "Can't bind inet6 socket: " << e.what() << std::endl; logger.e("Can't bind inet6 socket: %s", e.what());
} }
} }
#endif #endif
...@@ -225,7 +226,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6) ...@@ -225,7 +226,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
int rc = select(selectFd, &readfds, nullptr, nullptr, nullptr); int rc = select(selectFd, &readfds, nullptr, nullptr, nullptr);
if (rc < 0) { if (rc < 0) {
if (errno != EINTR) { if (errno != EINTR) {
perror("select"); logger.e("Select error: %s", strerror(errno));
std::this_thread::sleep_for(std::chrono::seconds(1)); std::this_thread::sleep_for(std::chrono::seconds(1));
} }
} }
...@@ -240,7 +241,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6) ...@@ -240,7 +241,7 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
if (FD_ISSET(stop_readfd, &readfds)) { if (FD_ISSET(stop_readfd, &readfds)) {
if (recv(stop_readfd, (char*)buf.data(), buf.size(), 0) < 0) { if (recv(stop_readfd, (char*)buf.data(), buf.size(), 0) < 0) {
std::cerr << "Got stop packet error: " << strerror(errno) << std::endl; logger.e("Got stop packet error: %s", strerror(errno));
break; break;
} }
} }
...@@ -258,12 +259,33 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6) ...@@ -258,12 +259,33 @@ UdpSocket::openSockets(const SockAddr& bind4, const SockAddr& bind6)
pkt->received = clock::now(); pkt->received = clock::now();
onReceived(std::move(pkt)); onReceived(std::move(pkt));
} else if (rc == -1) { } else if (rc == -1) {
std::cerr << "Error receiving packet: " << strerror(errno) << std::endl; logger.e("Error receiving packet: %s", strerror(errno));
int err = errno;
if (err == EPIPE || err == ENOTCONN || err == ECONNRESET) {
if (s4 >= 0) {
close(s4);
try {
s4 = bindSocket(bound4, bound4);
} catch (const DhtException& e) {
logger.e("Can't bind inet socket: %s", e.what());
}
}
if (s6 >= 0) {
close(s6);
try {
s6 = bindSocket(bound6, bound6);
} catch (const DhtException& e) {
logger.e("Can't bind inet6 socket: %s", e.what());
}
}
if (s4 < 0 && s6 < 0)
break;
}
} }
} }
} }
} catch (const std::exception& e) { } catch (const std::exception& e) {
std::cerr << "Error in DHT networking thread: " << e.what() << std::endl; logger.e("Error in UdpSocket rx thread: %s", e.what());
} }
if (s4 >= 0) if (s4 >= 0)
close(s4); close(s4);
...@@ -287,7 +309,7 @@ UdpSocket::stop() ...@@ -287,7 +309,7 @@ UdpSocket::stop()
if (running.exchange(false)) { if (running.exchange(false)) {
auto sfd = stopfd; auto sfd = stopfd;
if (sfd != -1 && write(sfd, "\0", 1) == -1) { if (sfd != -1 && write(sfd, "\0", 1) == -1) {
std::cerr << "can't write to stop fd" << std::endl; logger.e("Can't write to stop fd");
} }
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment