2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
2012-05-19 10:27:41 +02:00
|
|
|
* Constructor for the TcpServer class.
|
2012-05-08 15:36:28 +02:00
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
TcpServer::TcpServer(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
m_ClientFactory = boost::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.
|
|
|
|
*/
|
2012-06-24 02:56:48 +02:00
|
|
|
void TcpServer::SetClientFactory(function<TcpClient::Ptr(SOCKET)> 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.
|
|
|
|
*/
|
2012-06-24 02:56:48 +02:00
|
|
|
function<TcpClient::Ptr(SOCKET)> TcpServer::GetFactoryFunction(void) const
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return m_ClientFactory;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Starts listening for incoming client connections.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
void TcpServer::Listen(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-06-16 03:42:54 +02:00
|
|
|
if (listen(GetFD(), SOMAXCONN) < 0) {
|
2012-05-26 20:00:03 +02:00
|
|
|
HandleSocketError(SocketException(
|
|
|
|
"listen() failed", GetError()));
|
2012-04-04 12:22:46 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 02:56:48 +02: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 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Accepts a new client and creates a new client object for it
|
|
|
|
* using the client factory function.
|
|
|
|
*/
|
2012-06-24 02:56:48 +02:00
|
|
|
void TcpServer::HandleReadable(void)
|
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) {
|
2012-05-26 20:00:03 +02:00
|
|
|
HandleSocketError(SocketException(
|
|
|
|
"accept() failed", GetError()));
|
2012-06-16 03:42:54 +02:00
|
|
|
return;
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-24 02:56:48 +02:00
|
|
|
TcpClient::Ptr client = m_ClientFactory(fd);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-07-13 23:33:30 +02:00
|
|
|
Event::Post(boost::bind(boost::ref(OnNewClient), GetSelf(), client));
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|