]> granicus.if.org Git - icinga2/blob - base/tcpserver.cpp
a5c3d6dcdcb9339ece80710241bd1e8c735bacce
[icinga2] / base / tcpserver.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "i2-base.h"
21
22 using namespace icinga;
23
24 /**
25  * Constructor for the TcpServer class.
26  */
27 TcpServer::TcpServer(void)
28 {
29         m_ClientFactory = bind(&TcpClientFactory, RoleInbound);
30 }
31
32 /**
33  * Sets the client factory.
34  *
35  * @param clientFactory The client factory function.
36  */
37 void TcpServer::SetClientFactory(function<TcpClient::Ptr()> clientFactory)
38 {
39         m_ClientFactory = clientFactory;
40 }
41
42 /**
43  * Retrieves the client factory.
44  *
45  * @returns The client factory function.
46  */
47 function<TcpClient::Ptr()> TcpServer::GetFactoryFunction(void) const
48 {
49         return m_ClientFactory;
50 }
51
52 /**
53  * Registers the TCP server and starts processing events for it.
54  */
55 void TcpServer::Start(void)
56 {
57         TcpSocket::Start();
58
59         OnReadable += bind_weak(&TcpServer::ReadableEventHandler, shared_from_this());
60 }
61
62 /**
63  * Starts listening for incoming client connections.
64  */
65 void TcpServer::Listen(void)
66 {
67         int rc = listen(GetFD(), SOMAXCONN);
68
69         if (rc < 0) {
70                 HandleSocketError(SocketException(
71                     "listen() failed", GetError()));
72                 return;
73         }
74 }
75
76 /**
77  * Accepts a new client and creates a new client object for it
78  * using the client factory function.
79  *
80  * @param - Event arguments.
81  * @returns 0
82  */
83 int TcpServer::ReadableEventHandler(const EventArgs&)
84 {
85         int fd;
86         sockaddr_storage addr;
87         socklen_t addrlen = sizeof(addr);
88
89         fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
90
91         if (fd < 0) {
92                 HandleSocketError(SocketException(
93                     "accept() failed", GetError()));
94                 return 0;
95         }
96
97         NewClientEventArgs nea;
98         nea.Source = shared_from_this();
99         nea.Client = static_pointer_cast<TcpSocket>(m_ClientFactory());
100         nea.Client->SetFD(fd);
101         nea.Client->Start();
102         OnNewClient(nea);
103
104         return 0;
105 }
106
107 /**
108  * Checks whether the TCP server wants to read (i.e. accept new clients).
109  *
110  * @returns true
111  */
112 bool TcpServer::WantsToRead(void) const
113 {
114         return true;
115 }