icinga2/base/tcpserver.cpp

90 lines
2.8 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 07:33:57 -04:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2012-03-28 07:24:49 -04:00
#include "i2-base.h"
using namespace icinga;
2012-05-08 09:36:28 -04:00
/**
* Constructor for the TcpServer class.
2012-05-08 09:36:28 -04:00
*/
TcpServer::TcpServer(void)
2012-08-03 09:35:27 -04:00
: m_ClientFactory(boost::bind(&TcpClientFactory, RoleInbound))
{ }
2012-03-28 07:24:49 -04:00
2012-05-08 09:36:28 -04:00
/**
* Sets the client factory.
*
* @param clientFactory The client factory function.
*/
2012-08-03 09:35:27 -04:00
void TcpServer::SetClientFactory(const TcpServer::ClientFactory& clientFactory)
2012-03-28 07:24:49 -04:00
{
m_ClientFactory = clientFactory;
}
2012-05-08 09:36:28 -04:00
/**
* Retrieves the client factory.
*
* @returns The client factory function.
*/
2012-08-03 09:35:27 -04:00
TcpServer::ClientFactory TcpServer::GetFactoryFunction(void) const
2012-03-28 07:24:49 -04:00
{
return m_ClientFactory;
}
2012-05-08 09:36:28 -04:00
/**
* Starts listening for incoming client connections.
*/
void TcpServer::Listen(void)
2012-03-28 07:24:49 -04:00
{
2012-07-17 14:41:06 -04:00
if (listen(GetFD(), SOMAXCONN) < 0)
throw_exception(SocketException("listen() failed", GetError()));
2012-03-28 07:24:49 -04:00
}
2012-06-23 20:56:48 -04:00
/**
* Checks whether the TCP server wants to read (i.e. accept new clients).
*
* @returns true
*/
bool TcpServer::WantsToRead(void) const
{
return true;
}
2012-05-08 09:36:28 -04:00
/**
* Accepts a new client and creates a new client object for it
* using the client factory function.
*/
2012-06-23 20:56:48 -04:00
void TcpServer::HandleReadable(void)
2012-03-28 07:24:49 -04:00
{
int fd;
2012-04-27 03:54:07 -04:00
sockaddr_storage addr;
2012-03-28 07:24:49 -04:00
socklen_t addrlen = sizeof(addr);
fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
2012-07-17 14:41:06 -04:00
if (fd < 0)
throw_exception(SocketException("accept() failed", GetError()));
2012-04-18 09:22:25 -04:00
2012-06-23 20:56:48 -04:00
TcpClient::Ptr client = m_ClientFactory(fd);
2012-03-28 07:24:49 -04:00
2012-07-13 17:33:30 -04:00
Event::Post(boost::bind(boost::ref(OnNewClient), GetSelf(), client));
2012-03-28 07:24:49 -04:00
}
2012-08-03 09:35:27 -04:00