|
|
@ -3,19 +3,12 @@
|
|
|
|
#include "Logger.hpp"
|
|
|
|
#include "Logger.hpp"
|
|
|
|
#include "Common.hpp"
|
|
|
|
#include "Common.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h> // inet_ntop
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <poll.h>
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TcpClient::TcpClient( const std::string host,
|
|
|
|
TcpClient::TcpClient( const std::string host,
|
|
|
|
const std::string port )
|
|
|
|
const std::string port )
|
|
|
|
: Socket(AF_INET, SOCK_STREAM)
|
|
|
|
: Socket(AF_INET, SOCK_STREAM)
|
|
|
|
, m_host(host)
|
|
|
|
, m_host(host)
|
|
|
|
, m_port(port)
|
|
|
|
, m_port(port)
|
|
|
|
, m_connected(false)
|
|
|
|
|
|
|
|
, m_watcher(*this)
|
|
|
|
, m_watcher(*this)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
@ -26,8 +19,6 @@ TcpClient::~TcpClient()
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
disconnect();
|
|
|
|
disconnect();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -41,8 +32,7 @@ bool TcpClient::connect()
|
|
|
|
if ( !connectToHost(m_host, m_port) )
|
|
|
|
if ( !connectToHost(m_host, m_port) )
|
|
|
|
return false;
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
m_connected = true;
|
|
|
|
m_watcher.setOwnSocket(m_socket);
|
|
|
|
|
|
|
|
|
|
|
|
m_watcher.start();
|
|
|
|
m_watcher.start();
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -53,9 +43,9 @@ void TcpClient::disconnect()
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
closeSocket();
|
|
|
|
closeSocket();
|
|
|
|
m_connected = false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( m_watcher.isRunning() ) {
|
|
|
|
if ( m_watcher.isRunning() ) {
|
|
|
|
|
|
|
|
m_watcher.stopPolling();
|
|
|
|
m_watcher.stop();
|
|
|
|
m_watcher.stop();
|
|
|
|
m_watcher.join();
|
|
|
|
m_watcher.join();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -66,9 +56,6 @@ bool TcpClient::send(const std::string msg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
if ( !m_connected )
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ssize_t n = write(m_socket, msg.c_str(), msg.length());
|
|
|
|
ssize_t n = write(m_socket, msg.c_str(), msg.length());
|
|
|
|
if (n == -1) {
|
|
|
|
if (n == -1) {
|
|
|
|
LOG( Logger::ERR, errnoToString("ERROR writing to socket. ").c_str() );
|
|
|
|
LOG( Logger::ERR, errnoToString("ERROR writing to socket. ").c_str() );
|
|
|
@ -80,52 +67,36 @@ bool TcpClient::send(const std::string msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TcpClient::WatcherThread::WatcherThread( TcpClient &data )
|
|
|
|
TcpClient::WatcherThread::WatcherThread( TcpClient &data )
|
|
|
|
: m_tcpClient(data)
|
|
|
|
: Poll(data.m_socket, 1)
|
|
|
|
|
|
|
|
, m_tcpClient(data)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void* TcpClient::WatcherThread::run()
|
|
|
|
void TcpClient::WatcherThread::acceptClient()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
struct timespec tm = {0,1000};
|
|
|
|
// not accepting anything
|
|
|
|
while ( m_isRunning ) {
|
|
|
|
receive( m_tcpClient.m_socket );
|
|
|
|
|
|
|
|
}
|
|
|
|
nanosleep(&tm, &tm) ;
|
|
|
|
|
|
|
|
if ( m_tcpClient.m_connected ) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pollfd fds[1];
|
|
|
|
|
|
|
|
fds[0].fd = m_tcpClient.m_socket ;
|
|
|
|
|
|
|
|
fds[0].events = POLLIN | POLLPRI ;
|
|
|
|
|
|
|
|
fds[0].revents = 0 ;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ret = poll( fds , 1, 1000) ;
|
|
|
|
|
|
|
|
if ( ret == -1 ) {
|
|
|
|
|
|
|
|
LOG( Logger::ERR, errnoToString("ERROR polling. ").c_str() );
|
|
|
|
|
|
|
|
m_tcpClient.m_connected = false;
|
|
|
|
|
|
|
|
m_tcpClient.onDisconnect();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( ret != 0 && !receive() ) {
|
|
|
|
|
|
|
|
m_tcpClient.m_connected = false;
|
|
|
|
|
|
|
|
m_tcpClient.onDisconnect();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
void TcpClient::WatcherThread::handleClient( const int fd )
|
|
|
|
}
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
LOG( Logger::DEBUG, "Server closed the connection." );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool TcpClient::WatcherThread::receive()
|
|
|
|
bool TcpClient::WatcherThread::receive( const int fd)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
TRACE;
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
char buffer[256];
|
|
|
|
char buffer[14];
|
|
|
|
int len = recv( m_tcpClient.m_socket, buffer , 256, 0) ;
|
|
|
|
int len = recv( fd, buffer , 14, 0) ;
|
|
|
|
|
|
|
|
|
|
|
|
if (len == -1) {
|
|
|
|
if (len == -1) {
|
|
|
|
LOG( Logger::ERR, errnoToString("ERROR reading from socket. ").c_str() );
|
|
|
|
LOG( Logger::ERR, errnoToString("ERROR reading from socket. ").c_str() );
|
|
|
@ -141,4 +112,13 @@ bool TcpClient::WatcherThread::receive()
|
|
|
|
m_tcpClient.msgArrived(msg);
|
|
|
|
m_tcpClient.msgArrived(msg);
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void* TcpClient::WatcherThread::run()
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
TRACE;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
startPolling();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|