accept moved to StreamConnection

master
Denes Matetelki 13 years ago
parent 6ff4599d25
commit f8457bff9d

@ -1,7 +1,7 @@
#ifndef POLL_HPP
#define POLL_HPP
#include "Connection.hpp"
#include "StreamConnection.hpp"
#include <poll.h>
#include <map>
@ -12,7 +12,7 @@ class Poll
{
public:
Poll( Connection *connection,
Poll( StreamConnection *connection,
const nfds_t maxClient = 10 );
virtual ~Poll();
@ -43,7 +43,7 @@ private:
typedef typename std::map< int, Connection* > ConnectionPool;
Connection *m_connection;
StreamConnection *m_connection;
volatile bool m_polling;
ConnectionPool m_connectionPool;

@ -43,6 +43,7 @@ public:
bool bind();
bool listen( const int maxPendingQueueLen = 64 );
int accept();
int getSocket() const;

@ -19,7 +19,7 @@ public:
virtual bool listen( const int maxPendingQueueLen = 64 ) = 0;
/// @todo move accept and poll here
// virtual bool accept() = 0;
virtual int accept() = 0;
// virtual bool poll() = 0;

@ -36,6 +36,7 @@ public:
bool bind();
bool listen( const int maxPendingQueueLen = 64 );
int accept();
private:

@ -11,6 +11,8 @@
#include <iostream>
#include <string>
#include <signal.h>
class EchoMessage : public Message
{
public:
@ -63,6 +65,20 @@ protected:
};
SocketServer *socketServer;
void signalHandler(int s)
{
LOG_STATIC( Logger::INFO, std::string("Exiting after receiving signal: ").
append(TToStr(s)).c_str() );
socketServer->stop();
delete socketServer;
SslConnection::destroy();
Logger::destroy();
exit(1);
}
int main(int argc, char* argv[] )
{
if ( argc != 3 ) {
@ -70,27 +86,31 @@ int main(int argc, char* argv[] )
return 1;
}
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = signalHandler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
Logger::createInstance();
Logger::init(std::cout);
Logger::setLogLevel(Logger::FINEST);
// Logger::setNoPrefix();
SslConnection::init();
EchoMessage msg;
SslConnection conn(argv[1], StrToT<int>(argv[2]), &msg);
SocketServer socketServer(&conn);
socketServer = new SocketServer(&conn);
if ( !socketServer.start() ) {
LOG( Logger::ERR, "Failed to start TCP server, exiting...");
if ( !socketServer->start() ) {
LOG_STATIC( Logger::ERR, "Failed to start TCP server, exiting...");
delete socketServer;
SslConnection::destroy();
Logger::destroy();
return 1;
}
// never reached
sleep(1);
socketServer.stop();
SslConnection::destroy();
Logger::destroy();
return 0;
}

@ -39,7 +39,7 @@ void Logger::log_pointer( const void* pointer,
<< COLOR_RESET << ":"
<< COLOR( FG_BROWN ) << line << COLOR_RESET << " "
<< COLOR( FG_CYAN ) << function << COLOR_RESET << " "
<< COLOR( FG_BLUE ) << "\"" << pointer << "\""
<< COLOR( FG_BLUE ) << pointer
<< COLOR_RESET << std::endl;
}
@ -67,7 +67,7 @@ void Logger::log_string( const int level,
<< COLOR( FG_BROWN ) << line << COLOR_RESET << " "
<< COLOR( FG_CYAN ) << function << COLOR_RESET << " "
<< color << "\"" << msg << "\" "
<< COLOR( FG_BLUE ) << "\"" << pointer << "\""
<< COLOR( FG_BLUE ) << pointer
<< COLOR_RESET << std::endl;
}

@ -9,7 +9,7 @@
Poll::Poll( Connection *connection,
Poll::Poll( StreamConnection *connection,
const nfds_t maxClient )
: m_connection(connection)
, m_polling(false)
@ -82,12 +82,12 @@ void Poll::acceptClient()
{
TRACE;
sockaddr clientAddr;
socklen_t clientAddrLen;
// sockaddr clientAddr;
// socklen_t clientAddrLen;
// int client_socket = accept( m_connection->getSocket(),
// &clientAddr, &clientAddrLen ) ;
int client_socket = m_connection->accept();
/// @todo put accept into Socket class
int client_socket = accept( m_connection->getSocket(),
&clientAddr, &clientAddrLen ) ;
if ( client_socket == -1 ) {
LOG( Logger::ERR, errnoToString("ERROR accepting. ").c_str() );

@ -15,6 +15,7 @@ void SslConnection::init()
SSL_load_error_strings();
SSL_library_init();
OpenSSL_add_all_algorithms();
}
void SslConnection::destroy()
@ -74,6 +75,8 @@ SslConnection::~SslConnection()
Connection* SslConnection::clone(const int socket)
{
TRACE;
Connection *conn = new SslConnection( socket,
m_message->clone(),
m_bufferLength );
@ -93,7 +96,7 @@ bool SslConnection::connect()
return false;
if ( SSL_connect (m_sslHandle) != 1 ) {
LOG (Logger::ERR, getSslError("Handshake with SSL server failed. ").c_str() );
LOG (Logger::ERR, getSslError("SSL handshake failed. ").c_str() );
return false;
}
@ -123,6 +126,27 @@ bool SslConnection::listen( const int maxPendingQueueLen )
}
int SslConnection::accept()
{
TRACE;
int client_socket = m_tcpConnection.accept();
if ( client_socket == -1)
return client_socket;
if ( SSL_accept(m_sslHandle) == -1 ) {
getSslError("SSL accept failed. ");
return -1;
}
if ( SSL_set_fd(m_sslHandle, client_socket) == 0 ) {
getSslError("SSL set connection socket failed. ");
return -1;
}
return client_socket;
}
/// @todo this function shall be refactored
bool SslConnection::disconnect()
{

@ -81,6 +81,15 @@ bool TcpConnection::listen( const int maxPendingQueueLen )
}
int TcpConnection::accept()
{
sockaddr clientAddr;
socklen_t clientAddrLen;
return ::accept( getSocket(), &clientAddr, &clientAddrLen ) ;
}
bool TcpConnection::disconnect()
{
TRACE;

Loading…
Cancel
Save