//*****************************************************************************
// RCF - Remote Call Framework
// Copyright (c) 2005. All rights reserved.
// Developed by Jarl Lindrud.
// Contact: jlindrud@hotmail.com .
//*****************************************************************************
#ifndef INCLUDE_RCF_TIMEDBSDSOCKETS_HPP
#define INCLUDE_RCF_TIMEDBSDSOCKETS_HPP
//#include <RCF/Tools.hpp>
#include <RCF/UsingBsdSockets.hpp>
namespace RCF {
//******************************************************
// nonblocking socket routines
int timedConnect(unsigned int timeoutMs, int fd, sockaddr *addr, int addrLen);
/*
{
unsigned int startTimeMs = getCurrentTimeMs();
unsigned int endTimeMs = startTimeMs + timeoutMs;
int ret = Platform::OS::BsdSockets::connect(fd, addr, addrLen);
if ((ret == -1 && Platform::OS::BsdSockets::GetLastError() == Platform::OS::BsdSockets::ERR_EWOULDBLOCK) ||
(ret == -1 && Platform::OS::BsdSockets::GetLastError() == Platform::OS::BsdSockets::ERR_EINPROGRESS))
{
fd_set fdSet;
FD_ZERO(&fdSet);
FD_SET(fd, &fdSet);
unsigned int timeoutMs = generateTimeoutMs(endTimeMs);
timeval timeout;
timeout.tv_sec = timeoutMs/1000;
timeout.tv_usec = 1000*(timeoutMs%1000);
if (timeout.tv_usec < 0)
{
timeout.tv_usec = 0;
}
ret = Platform::OS::BsdSockets::select(fd+1, NULL, &fdSet, NULL, &timeout);
if (ret == 1)
{
return 0;
}
else if (ret == 0)
{
RCF_TRACE("timedConnect - timeout")(fd)(timeout.tv_sec)(timeout.tv_usec);
return -1;
}
else
{
int err = Platform::OS::BsdSockets::GetLastError();
RCF_TRACE("timedConnect - error")(fd)(timeout.tv_sec)(timeout.tv_usec)(err)(Platform::OS::GetErrorString(err));
return -1;
}
}
else if (ret == 0)
{
return 0;
}
else
{
int err = Platform::OS::BsdSockets::GetLastError();
RCF_TRACE("timedConnect - error")(fd)(err)(Platform::OS::GetErrorString(err));
return -1;
}
}*/
// returns -2 for timeout, -1 for error, otherwise number of bytes sent (> 0)
int timedSend(unsigned int timeoutMs, int fd, const char *buffer, std::size_t length, int flags);
/*
{
unsigned int startTimeMs = getCurrentTimeMs();
unsigned int endTimeMs = startTimeMs + timeoutMs;
int ret = 0;
int err = 0;
while (true)
{
ret = Platform::OS::BsdSockets::send(fd, buffer, static_cast<int>(length), flags);
err = Platform::OS::BsdSockets::GetLastError();
RCF_ASSERT(ret != 0);
if (ret > 0)
{
return ret;
}
else if (
ret == -1 &&
err == Platform::OS::BsdSockets::ERR_EWOULDBLOCK)
{
unsigned int timeoutMs = generateTimeoutMs(endTimeMs);
fd_set fdSet;
FD_ZERO(&fdSet);
FD_SET(fd, &fdSet);
timeval timeout;
timeout.tv_sec = timeoutMs/1000;
timeout.tv_usec = 1000*(timeoutMs%1000);
ret = Platform::OS::BsdSockets::select(fd+1, NULL, &fdSet, NULL, &timeout);
RCF_ASSERT(ret == -1 || ret == 0 || ret == 1)(ret);
if (ret == 1)
{
continue;
}
else if (ret == 0)
{
RCF_TRACE("timedSend - select() timeout")(fd)(timeout.tv_sec)(timeout.tv_usec);
return -2;
}
else if (ret == -1)
{
err = Platform::OS::BsdSockets::GetLastError();
RCF_TRACE("timedSend - select() error")(fd)(timeout.tv_sec)(timeout.tv_usec)(ret)(err)(Platform::OS::GetErrorString(err));
return -1;
}
}
else
{
RCF_ASSERT(ret == -1);
RCF_TRACE("timedSend - send() error")(fd)(ret)(err)(Platform::OS::GetErrorString(err));
return -1;
}
}
}*/
// returns -2 for timeout, -1 for error, 0 for peer closure, otherwise size of packet read
int timedRecv(unsigned int timeoutMs, int fd, char *buffer, std::size_t bufferLen, int flags);
/*
{
unsigned int startTimeMs = getCurrentTimeMs();
unsigned int endTimeMs = startTimeMs + timeoutMs;
int ret = 0;
int err = 0;
while (true)
{
ret = Platform::OS::BsdSockets::recv(fd, buffer, static_cast<int>(bufferLen), flags);
err = Platform::OS::BsdSockets::GetLastError();
if (ret >= 0)
{
return ret;
}
else if (
ret == -1 &&
err == Platform::OS::BsdSockets::ERR_EWOULDBLOCK)
{
unsigned int timeoutMs = generateTimeoutMs(endTimeMs);
fd_set fdSet;
FD_ZERO(&fdSet);
FD_SET(fd, &fdSet);
timeval timeout;
timeout.tv_sec = timeoutMs/1000;
timeout.tv_usec = 1000*(timeoutMs%1000);
ret = Platform::OS::BsdSockets::select(fd+1, &fdSet, NULL, NULL, &timeout);
RCF_ASSERT(ret == -1 || ret == 0 || ret == 1)(ret);
if (ret == 1)
{
continue;
}
else if (ret == 0)
{
RCF_TRACE("timedRecv - select() timeout")(fd)(timeout.tv_sec)(timeout.tv_usec);
return -2;
}
else if (ret == -1)
{
err = Platform::OS::BsdSockets::GetLastError();
RCF_TRACE("timedRecv - select() error")(fd)(timeout.tv_sec)(timeout.tv_usec)(ret)(err)(Platform::OS::GetErrorString(err));
return -1;
}
}
else if (ret == -1)
{
err = Platform::OS::BsdSockets::GetLastError();
RCF_TRACE("timedRecv - recv() error")(fd)(ret)(err)(Platform::OS::GetErrorString(err));
return -1;
}
}
}*/
} // namespace RCF
#endif // ! INCLUDE_RCF_TIMEDBSDSOCKETS_HPP