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 TcpClient class.
|
2012-05-08 15:36:28 +02:00
|
|
|
*
|
|
|
|
* @param role The role of the TCP client socket.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
TcpClient::TcpClient(TcpClientRole role)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-04-24 14:02:15 +02:00
|
|
|
m_Role = role;
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
m_SendQueue = boost::make_shared<FIFO>();
|
|
|
|
m_RecvQueue = boost::make_shared<FIFO>();
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the role of the socket.
|
|
|
|
*
|
|
|
|
* @returns The role.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
TcpClientRole TcpClient::GetRole(void) const
|
2012-04-24 14:02:15 +02:00
|
|
|
{
|
|
|
|
return m_Role;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Creates a socket and connects to the specified node and service.
|
|
|
|
*
|
|
|
|
* @param node The node.
|
|
|
|
* @param service The service.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
void TcpClient::Connect(const string& node, const string& service)
|
2012-04-04 12:22:46 +02:00
|
|
|
{
|
2012-04-27 09:54:07 +02:00
|
|
|
m_Role = RoleOutbound;
|
|
|
|
|
|
|
|
addrinfo hints;
|
|
|
|
addrinfo *result;
|
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
|
|
|
hints.ai_protocol = IPPROTO_TCP;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
int rc = getaddrinfo(node.c_str(), service.c_str(), &hints, &result);
|
2012-04-27 09:54:07 +02:00
|
|
|
|
|
|
|
if (rc < 0) {
|
2012-05-26 20:00:03 +02:00
|
|
|
HandleSocketError(SocketException(
|
|
|
|
"getaddrinfo() failed", GetLastSocketError()));
|
2012-04-27 09:54:07 +02:00
|
|
|
return;
|
|
|
|
}
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
int fd = INVALID_SOCKET;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
for (addrinfo *info = result; info != NULL; info = info->ai_next) {
|
|
|
|
fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
if (fd == INVALID_SOCKET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
SetFD(fd);
|
|
|
|
|
|
|
|
rc = connect(fd, info->ai_addr, info->ai_addrlen);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-05-26 20:00:03 +02:00
|
|
|
if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
|
2012-04-04 12:22:46 +02:00
|
|
|
#else /* _WIN32 */
|
2012-05-26 20:00:03 +02:00
|
|
|
if (rc < 0 && errno != EINPROGRESS) {
|
2012-04-04 12:22:46 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-05-26 20:00:03 +02:00
|
|
|
closesocket(fd);
|
|
|
|
SetFD(INVALID_SOCKET);
|
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
continue;
|
2012-05-26 20:00:03 +02:00
|
|
|
}
|
2012-04-27 09:54:07 +02:00
|
|
|
|
|
|
|
break;
|
2012-04-04 16:02:19 +02:00
|
|
|
}
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-27 09:54:07 +02:00
|
|
|
freeaddrinfo(result);
|
2012-05-26 20:00:03 +02:00
|
|
|
|
|
|
|
if (fd == INVALID_SOCKET)
|
2012-05-26 21:30:04 +02:00
|
|
|
HandleSocketError(runtime_error(
|
|
|
|
"Could not create a suitable socket."));
|
2012-04-04 12:22:46 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the send queue for the socket.
|
|
|
|
*
|
|
|
|
* @returns The send queue.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
FIFO::Ptr TcpClient::GetSendQueue(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return m_SendQueue;
|
|
|
|
}
|
|
|
|
|
2012-06-24 02:56:48 +02:00
|
|
|
void TcpClient::HandleWritable(void)
|
2012-06-22 11:47:06 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = send(GetFD(), (const char *)m_SendQueue->GetReadBuffer(), m_SendQueue->GetSize(), 0);
|
|
|
|
|
|
|
|
if (rc <= 0) {
|
|
|
|
HandleSocketError(SocketException("send() failed", GetError()));
|
2012-06-24 02:56:48 +02:00
|
|
|
return;
|
2012-06-22 11:47:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
m_SendQueue->Read(NULL, rc);
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the recv queue for the socket.
|
|
|
|
*
|
|
|
|
* @returns The recv queue.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
FIFO::Ptr TcpClient::GetRecvQueue(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return m_RecvQueue;
|
|
|
|
}
|
|
|
|
|
2012-06-24 02:56:48 +02:00
|
|
|
void TcpClient::HandleReadable(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-06-24 02:56:48 +02:00
|
|
|
for (;;) {
|
|
|
|
size_t bufferSize = FIFO::BlockSize / 2;
|
|
|
|
char *buffer = (char *)m_RecvQueue->GetWriteBuffer(&bufferSize);
|
|
|
|
int rc = recv(GetFD(), buffer, bufferSize, 0);
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
if (rc < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
|
|
|
|
#else /* _WIN32 */
|
|
|
|
if (rc < 0 && errno == EAGAIN)
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (rc <= 0) {
|
|
|
|
HandleSocketError(SocketException("recv() failed", GetError()));
|
|
|
|
return;
|
|
|
|
}
|
2012-03-28 14:06:02 +02:00
|
|
|
|
2012-06-24 02:56:48 +02:00
|
|
|
m_RecvQueue->Write(NULL, rc);
|
2012-03-28 14:06:02 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-07-13 23:33:30 +02:00
|
|
|
Event::Post(boost::bind(boost::ref(OnDataAvailable), GetSelf()));
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Checks whether data should be read for this socket.
|
|
|
|
*
|
|
|
|
* @returns true
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
bool TcpClient::WantsToRead(void) const
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Checks whether data should be written for this socket.
|
|
|
|
*
|
|
|
|
* @returns true if data should be written, false otherwise.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
bool TcpClient::WantsToWrite(void) const
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return (m_SendQueue->GetSize() > 0);
|
|
|
|
}
|
2012-04-24 14:02:15 +02:00
|
|
|
|
2012-05-08 15:36:28 +02:00
|
|
|
/**
|
|
|
|
* Default factory function for TCP clients.
|
|
|
|
*
|
|
|
|
* @param role The role of the new client.
|
|
|
|
* @returns The new client.
|
|
|
|
*/
|
2012-05-19 10:27:41 +02:00
|
|
|
TcpClient::Ptr icinga::TcpClientFactory(TcpClientRole role)
|
2012-04-24 14:02:15 +02:00
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
return boost::make_shared<TcpClient>(role);
|
2012-04-24 14:02:15 +02:00
|
|
|
}
|