2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-24 14:02:15 +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-04-03 15:16:11 +02:00
|
|
|
m_SendQueue = make_shared<FIFO>();
|
|
|
|
m_RecvQueue = make_shared<FIFO>();
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
TCPClientRole TCPClient::GetRole(void) const
|
|
|
|
{
|
|
|
|
return m_Role;
|
|
|
|
}
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
void TCPClient::Start(void)
|
|
|
|
{
|
|
|
|
TCPSocket::Start();
|
|
|
|
|
2012-04-03 11:13:17 +02:00
|
|
|
OnReadable += bind_weak(&TCPClient::ReadableEventHandler, shared_from_this());
|
|
|
|
OnWritable += bind_weak(&TCPClient::WritableEventHandler, shared_from_this());
|
2012-03-28 13:24:49 +02:00
|
|
|
}
|
|
|
|
|
2012-04-04 12:22:46 +02:00
|
|
|
void TCPClient::Connect(const string& hostname, unsigned short port)
|
|
|
|
{
|
2012-04-27 09:54:07 +02:00
|
|
|
m_Role = RoleOutbound;
|
|
|
|
|
|
|
|
stringstream s;
|
|
|
|
s << port;
|
|
|
|
string strPort = s.str();
|
|
|
|
|
|
|
|
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-04-27 09:54:07 +02:00
|
|
|
int rc = getaddrinfo(hostname.c_str(), strPort.c_str(), &hints, &result);
|
|
|
|
|
|
|
|
if (rc < 0) {
|
|
|
|
HandleSocketError();
|
|
|
|
|
|
|
|
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-04-27 09:54:07 +02:00
|
|
|
if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
|
2012-04-04 12:22:46 +02:00
|
|
|
#else /* _WIN32 */
|
2012-04-27 09:54:07 +02:00
|
|
|
if (rc < 0 && errno != EINPROGRESS)
|
2012-04-04 12:22:46 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-04-27 09:54:07 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
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
|
|
|
if (fd == INVALID_SOCKET)
|
|
|
|
HandleSocketError();
|
|
|
|
|
|
|
|
freeaddrinfo(result);
|
2012-04-04 12:22:46 +02:00
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
FIFO::Ptr TCPClient::GetSendQueue(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return m_SendQueue;
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
FIFO::Ptr TCPClient::GetRecvQueue(void)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
return m_RecvQueue;
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
int TCPClient::ReadableEventHandler(const EventArgs& ea)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
2012-03-29 20:03:29 +02:00
|
|
|
int rc;
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-03-29 20:03:29 +02:00
|
|
|
size_t bufferSize = FIFO::BlockSize / 2;
|
|
|
|
char *buffer = (char *)m_RecvQueue->GetWriteBuffer(&bufferSize);
|
|
|
|
rc = recv(GetFD(), buffer, bufferSize, 0);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-03-28 14:06:02 +02:00
|
|
|
#ifdef _WIN32
|
2012-03-29 20:03:29 +02:00
|
|
|
if (rc < 0 && WSAGetLastError() == WSAEWOULDBLOCK)
|
2012-03-28 14:06:02 +02:00
|
|
|
#else /* _WIN32 */
|
2012-03-29 20:03:29 +02:00
|
|
|
if (rc < 0 && errno == EAGAIN)
|
2012-03-28 14:06:02 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-03-29 20:03:29 +02:00
|
|
|
return 0;
|
2012-03-28 14:06:02 +02:00
|
|
|
|
2012-03-29 20:03:29 +02:00
|
|
|
if (rc <= 0) {
|
2012-04-22 16:45:31 +02:00
|
|
|
HandleSocketError();
|
2012-03-29 20:03:29 +02:00
|
|
|
return 0;
|
2012-03-28 14:06:02 +02:00
|
|
|
}
|
2012-03-28 13:24:49 +02:00
|
|
|
|
2012-03-29 20:03:29 +02:00
|
|
|
m_RecvQueue->Write(NULL, rc);
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
EventArgs dea;
|
|
|
|
dea.Source = shared_from_this();
|
2012-03-28 13:24:49 +02:00
|
|
|
OnDataAvailable(dea);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
int TCPClient::WritableEventHandler(const EventArgs& ea)
|
2012-03-28 13:24:49 +02:00
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
2012-03-28 19:50:55 +02:00
|
|
|
rc = send(GetFD(), (const char *)m_SendQueue->GetReadBuffer(), m_SendQueue->GetSize(), 0);
|
2012-03-28 13:24:49 +02:00
|
|
|
|
|
|
|
if (rc <= 0) {
|
2012-04-22 16:45:31 +02:00
|
|
|
HandleSocketError();
|
2012-03-28 13:24:49 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_SendQueue->Read(NULL, rc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TCPClient::WantsToRead(void) const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TCPClient::WantsToWrite(void) const
|
|
|
|
{
|
|
|
|
return (m_SendQueue->GetSize() > 0);
|
|
|
|
}
|
2012-04-24 14:02:15 +02:00
|
|
|
|
|
|
|
TCPClient::Ptr icinga::TCPClientFactory(TCPClientRole role)
|
|
|
|
{
|
|
|
|
return make_shared<TCPClient>(role);
|
|
|
|
}
|