2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
TCPClient::TCPClient(void)
|
|
|
|
{
|
2012-04-03 15:16:11 +02:00
|
|
|
m_SendQueue = make_shared<FIFO>();
|
|
|
|
m_RecvQueue = make_shared<FIFO>();
|
2012-04-04 12:22:46 +02:00
|
|
|
|
|
|
|
m_PeerPort = 0;
|
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)
|
|
|
|
{
|
|
|
|
hostent *hent;
|
|
|
|
sockaddr_in sin;
|
|
|
|
|
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
|
|
|
sin.sin_port = htons(port);
|
|
|
|
|
|
|
|
hent = gethostbyname(hostname.c_str());
|
|
|
|
|
|
|
|
if (hent != NULL)
|
|
|
|
sin.sin_addr.s_addr = ((in_addr *)hent->h_addr_list[0])->s_addr;
|
|
|
|
else
|
|
|
|
sin.sin_addr.s_addr = inet_addr(hostname.c_str());
|
|
|
|
|
|
|
|
int rc = connect(GetFD(), (sockaddr *)&sin, sizeof(sin));
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
2012-04-04 16:02:19 +02:00
|
|
|
if (rc < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
|
2012-04-04 12:22:46 +02:00
|
|
|
#else /* _WIN32 */
|
2012-04-04 16:02:19 +02:00
|
|
|
if (rc < 0 && errno != EINPROGRESS) {
|
2012-04-04 12:22:46 +02:00
|
|
|
#endif /* _WIN32 */
|
2012-04-22 16:45:31 +02:00
|
|
|
HandleSocketError();
|
2012-04-04 16:02:19 +02:00
|
|
|
}
|
2012-04-04 12:22:46 +02:00
|
|
|
|
|
|
|
m_PeerHost = hostname;
|
|
|
|
m_PeerPort = port;
|
|
|
|
}
|
|
|
|
|
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-04 12:22:46 +02:00
|
|
|
|
|
|
|
string TCPClient::GetPeerHost(void)
|
|
|
|
{
|
|
|
|
return m_PeerHost;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPClient::GetPeerPort(void)
|
|
|
|
{
|
|
|
|
return m_PeerPort;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|