icinga2/base/tcpserver.cpp

114 lines
3.1 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2012-03-28 13:24:49 +02:00
#include "i2-base.h"
using namespace icinga;
2012-05-08 15:36:28 +02:00
/**
* Constructor for the TcpServer class.
2012-05-08 15:36:28 +02:00
*/
TcpServer::TcpServer(void)
2012-03-28 13:24:49 +02:00
{
m_ClientFactory = bind(&TcpClientFactory, RoleInbound);
2012-03-28 13:24:49 +02:00
}
2012-05-08 15:36:28 +02:00
/**
* Sets the client factory.
*
* @param clientFactory The client factory function.
*/
void TcpServer::SetClientFactory(function<TcpClient::Ptr()> clientFactory)
2012-03-28 13:24:49 +02:00
{
m_ClientFactory = clientFactory;
}
2012-05-08 15:36:28 +02:00
/**
* Retrieves the client factory.
*
* @returns The client factory function.
*/
function<TcpClient::Ptr()> TcpServer::GetFactoryFunction(void) const
2012-03-28 13:24:49 +02:00
{
return m_ClientFactory;
}
2012-05-08 15:36:28 +02:00
/**
* Registers the TCP server and starts processing events for it.
*/
void TcpServer::Start(void)
2012-03-28 13:24:49 +02:00
{
TcpSocket::Start();
2012-03-28 13:24:49 +02:00
OnReadable += bind_weak(&TcpServer::ReadableEventHandler, shared_from_this());
2012-03-28 13:24:49 +02:00
}
2012-05-08 15:36:28 +02:00
/**
* Starts listening for incoming client connections.
*/
void TcpServer::Listen(void)
2012-03-28 13:24:49 +02:00
{
int rc = listen(GetFD(), SOMAXCONN);
if (rc < 0) {
2012-05-07 13:48:17 +02:00
HandleSocketError();
return;
}
2012-03-28 13:24:49 +02:00
}
2012-05-08 15:36:28 +02:00
/**
* Accepts a new client and creates a new client object for it
* using the client factory function.
*
2012-05-10 13:46:04 +02:00
* @param - Event arguments.
2012-05-08 15:36:28 +02:00
* @returns 0
*/
int TcpServer::ReadableEventHandler(const EventArgs&)
2012-03-28 13:24:49 +02:00
{
int fd;
2012-04-27 09:54:07 +02:00
sockaddr_storage addr;
2012-03-28 13:24:49 +02:00
socklen_t addrlen = sizeof(addr);
fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
2012-04-22 16:45:31 +02:00
if (fd < 0) {
HandleSocketError();
return 0;
2012-04-18 15:22:25 +02:00
}
NewClientEventArgs nea;
nea.Source = shared_from_this();
nea.Client = static_pointer_cast<TcpSocket>(m_ClientFactory());
2012-04-18 15:22:25 +02:00
nea.Client->SetFD(fd);
nea.Client->Start();
2012-03-28 13:24:49 +02:00
OnNewClient(nea);
return 0;
}
2012-05-08 15:36:28 +02:00
/**
* Checks whether the TCP server wants to read (i.e. accept new clients).
*
* @returns true
*/
bool TcpServer::WantsToRead(void) const
2012-03-28 13:24:49 +02:00
{
return true;
}