|
|
@ -10,6 +10,8 @@
|
|
|
|
#include <arpa/inet.h> // inet_ntop
|
|
|
|
#include <arpa/inet.h> // inet_ntop
|
|
|
|
#include <sys/select.h>
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h> // strerror
|
|
|
|
#include <string.h> // strerror
|
|
|
|
#include <errno.h> // errno
|
|
|
|
#include <errno.h> // errno
|
|
|
|
|
|
|
|
|
|
|
@ -63,11 +65,21 @@ bool Socket::closeSocket()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
/// @note are the return values of shutdown and close meaningful?
|
|
|
|
if (shutdown(m_socket, SHUT_RDWR) == -1) {
|
|
|
|
shutdown(m_socket, SHUT_RDWR);
|
|
|
|
LOG_BEGIN(Logger::ERR)
|
|
|
|
close(m_socket);
|
|
|
|
LOG_PROP("Error message", strerror(errno))
|
|
|
|
m_socket = -1;
|
|
|
|
LOG_END("Could not shutdown socket.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (close(m_socket) == -1) {
|
|
|
|
|
|
|
|
LOG_BEGIN(Logger::ERR)
|
|
|
|
|
|
|
|
LOG_PROP("Error message", strerror(errno))
|
|
|
|
|
|
|
|
LOG_END("Could not close socket.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_socket = -1;
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -85,14 +97,6 @@ bool Socket::connect(struct addrinfo *servinfo)
|
|
|
|
LOG_END("Could not connect to peer.");
|
|
|
|
LOG_END("Could not connect to peer.");
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string address, service;
|
|
|
|
|
|
|
|
if ( AddrInfo::convertNameInfo( servinfo, address, service) ) {
|
|
|
|
|
|
|
|
LOG_BEGIN(Logger::INFO)
|
|
|
|
|
|
|
|
LOG_PROP("Host", address)
|
|
|
|
|
|
|
|
LOG_PROP("Port", service)
|
|
|
|
|
|
|
|
LOG_END("Connected to peer.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -110,19 +114,11 @@ bool Socket::bind(struct addrinfo *servinfo )
|
|
|
|
LOG_END("Could not bind name to socket.");
|
|
|
|
LOG_END("Could not bind name to socket.");
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string address, service;
|
|
|
|
|
|
|
|
if ( AddrInfo::convertNameInfo( servinfo, address, service) ) {
|
|
|
|
|
|
|
|
LOG_BEGIN(Logger::INFO)
|
|
|
|
|
|
|
|
LOG_PROP("Host", address)
|
|
|
|
|
|
|
|
LOG_PROP("Port", service)
|
|
|
|
|
|
|
|
LOG_END("Binded to socket.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Socket::listen ( const int maxPendingQueueLen )
|
|
|
|
bool Socket::listen(const int maxPendingQueueLen)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
@ -136,6 +132,28 @@ bool Socket::listen ( const int maxPendingQueueLen )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Socket::accept(int &client_socket)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
sockaddr clientAddr;
|
|
|
|
|
|
|
|
socklen_t clientAddrLen;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// @bug This needs to be investigated ASAP: if the m_socket is not used before accept, it fails.
|
|
|
|
|
|
|
|
LOG_BEGIN(Logger::INFO)
|
|
|
|
|
|
|
|
LOG_SPROP(m_socket)
|
|
|
|
|
|
|
|
LOG_END("Accept mystery.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client_socket = ::accept( m_socket, &clientAddr, &clientAddrLen ) ;
|
|
|
|
|
|
|
|
if ( client_socket == -1 ) {
|
|
|
|
|
|
|
|
LOG_BEGIN(Logger::ERR)
|
|
|
|
|
|
|
|
LOG_PROP("Error message", strerror(errno))
|
|
|
|
|
|
|
|
LOG_END("Could not accept connection on socket.");
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Socket::send ( const void *message, const int length )
|
|
|
|
bool Socket::send ( const void *message, const int length )
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|