2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-base.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
TCPClient::TCPClient(void)
|
|
|
|
{
|
|
|
|
m_SendQueue = new_object<FIFO>();
|
|
|
|
m_RecvQueue = new_object<FIFO>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TCPClient::Start(void)
|
|
|
|
{
|
|
|
|
TCPSocket::Start();
|
|
|
|
|
|
|
|
function<int (EventArgs::RefType)> rd = bind_weak(&TCPClient::ReadableEventHandler, shared_from_this());
|
|
|
|
OnReadable.bind(rd);
|
|
|
|
|
|
|
|
function<int (EventArgs::RefType)> wd = bind_weak(&TCPClient::WritableEventHandler, shared_from_this());
|
|
|
|
OnWritable.bind(wd);
|
|
|
|
}
|
|
|
|
|
|
|
|
FIFO::RefType TCPClient::GetSendQueue(void)
|
|
|
|
{
|
|
|
|
return m_SendQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
FIFO::RefType TCPClient::GetRecvQueue(void)
|
|
|
|
{
|
|
|
|
return m_RecvQueue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPClient::ReadableEventHandler(EventArgs::RefType ea)
|
|
|
|
{
|
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) {
|
|
|
|
Close();
|
|
|
|
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-03-28 13:24:49 +02:00
|
|
|
EventArgs::RefType dea = new_object<EventArgs>();
|
|
|
|
dea->Source = shared_from_this();
|
|
|
|
OnDataAvailable(dea);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TCPClient::WritableEventHandler(EventArgs::RefType ea)
|
|
|
|
{
|
|
|
|
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) {
|
|
|
|
Close();
|
|
|
|
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);
|
|
|
|
}
|