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-05-19 10:27:41 +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.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
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.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
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.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
void TcpServer::Start(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-05-19 10:27:41 +02:00
|
|
|
TcpSocket::Start();
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-05-19 10:27:41 +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.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
void TcpServer::Listen(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-04-04 12:22:46 +02:00
|
|
|
int rc = listen(GetFD(), SOMAXCONN);
|
|
|
|
|
|
|
|
if (rc < 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-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
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
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) {
|
2012-05-26 20:00:03 +02:00
|
|
|
HandleSocketError(SocketException(
|
|
|
|
"accept() failed", GetError()));
|
2012-04-22 16:45:31 +02:00
|
|
|
return 0;
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
NewClientEventArgs nea;
|
|
|
|
nea.Source = shared_from_this();
|
2012-05-19 10:27:41 +02:00
|
|
|
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
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
bool TcpServer::WantsToRead(void) const
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|