pouvez-vous définir les options de socket SO_RCVTIMEO et SO_SNDTIMEO dans boost asio?
-
08-07-2019 - |
Question
pouvez-vous définir les options de socket SO_RCVTIMEO et SO_SNDTIMEO en mode boost asio?
Si oui comment?
Remarque Je sais que vous pouvez utiliser des minuteries à la place, mais j'aimerais en savoir plus sur ces options de socket.
La solution
Absolument! Boost ASIO vous permet d'accéder aux données natives / sous-jacentes, qui dans ce cas sont le SOCKET lui-même. Alors, disons que vous avez:
boost::asio::ip::tcp::socket my_socket;
Et disons que vous avez déjà appelé open
ou bind
ou une fonction membre qui rend réellement my_socket
utilisable. Ensuite, pour obtenir la valeur SOCKET sous-jacente, appelez:
SOCKET native_sock = my_socket.native();
int result = SOCKET_ERROR;
if (INVALID_SOCKET != native_sock)
{
result = setsockopt(native_sock, SOL_SOCKET, <the pertinent params you want to use>);
}
Donc là vous l'avez! ASIO de Boost vous permet de faire beaucoup de choses plus rapidement que vous ne pourriez le faire autrement, mais il y a beaucoup de choses pour lesquelles vous avez encore besoin des appels normaux de la bibliothèque de sockets. C’est l’un d’eux.
Autres conseils
Cela ne semble pas être intégré à Boost.Asio (à partir du SVN Boost actuel), mais si vous êtes prêt à écrire vos propres classes pour simuler le boost :: asio :: detail :: socket_option
, vous pouvez toujours suivre les exemples de boost / asio / socket_base.hpp
et procédez comme suit:
typedef boost::asio::detail::socket_option::timeval<SOL_SOCKET, SO_SNDTIMEO>
send_timeout;
typedef boost::asio::detail::socket_option::timeval<SOL_SOCKET, SO_RCVTIMEO>
receive_timeout;
(Évidemment, je ne suggère pas d'injecter la classe timeval
dans l'espace de noms boost :: asio :: detail :: socket_option
, mais je ne peux pas penser. d'un bon à utiliser pour le moment. :-P)
Edition: Mon exemple d'implémentation de socket_option :: timeval
, basé sur socket_option :: integer
:
// Helper template for implementing timeval options.
template <int Level, int Name>
class timeval
{
public:
// Default constructor.
timeval()
: value_(zero_timeval())
{
}
// Construct with a specific option value.
explicit timeval(::timeval v)
: value_(v)
{
}
// Set the value of the timeval option.
timeval& operator=(::timeval v)
{
value_ = v;
return *this;
}
// Get the current value of the timeval option.
::timeval value() const
{
return value_;
}
// Get the level of the socket option.
template <typename Protocol>
int level(const Protocol&) const
{
return Level;
}
// Get the name of the socket option.
template <typename Protocol>
int name(const Protocol&) const
{
return Name;
}
// Get the address of the timeval data.
template <typename Protocol>
::timeval* data(const Protocol&)
{
return &value_;
}
// Get the address of the timeval data.
template <typename Protocol>
const ::timeval* data(const Protocol&) const
{
return &value_;
}
// Get the size of the timeval data.
template <typename Protocol>
std::size_t size(const Protocol&) const
{
return sizeof(value_);
}
// Set the size of the timeval data.
template <typename Protocol>
void resize(const Protocol&, std::size_t s)
{
if (s != sizeof(value_))
throw std::length_error("timeval socket option resize");
}
private:
static ::timeval zero_timeval()
{
::timeval result = {};
return result;
}
::timeval value_;
};
Une solution facile à ce problème serait d'utiliser les fonctions natives de lecture et d'écriture.
Pour écrire avec un délai d'expiration de 1 seconde:
struct timeval tv = { 1, 0 };
setsockopt(socket.native_handle(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
ssize_t nsent = ::write(socket->native_handle(), buff, size);
if (nsent > 0) {
BOOST_LOG_TRIVIAL(debug) << "Sent " << nsent << " bytes to remote client " << ep;
} else if (nsent == 0) {
BOOST_LOG_TRIVIAL(info) << "Client " << ep << " closed connection";
} else if (errno != EAGAIN) {
BOOST_LOG_TRIVIAL(info) << "Client " << ep << " error: " << strerror(errno);
}
Pour lire avec un délai d'expiration de 1 seconde:
struct timeval tv = { 1, 0 };
setsockopt(socket.native_handle(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
ssize_t nread = ::read(socket.native_handle(), buff, audio_buff_size);
if (nread > 0) {
} else if (nread == 0) {
BOOST_LOG_TRIVIAL(info) << "Source " << source << " server " << host << " closed connection";
break;
} else if (errno != EAGAIN) {
BOOST_LOG_TRIVIAL(info) << "Source " << source << " server " << host << " error: " << strerror(errno);
break;
}
Cela a bien fonctionné pour moi.