Network/TcpClient: Add PollForConnected
This commit is contained in:
@@ -154,59 +154,6 @@ namespace Nz
|
||||
return SocketState_Connected;
|
||||
}
|
||||
|
||||
SocketState SocketImpl::Connect(SocketHandle handle, const IpAddress& address, UInt64 msTimeout, SocketError* error)
|
||||
{
|
||||
SocketState state = Connect(handle, address, error);
|
||||
if (state == SocketState_Connecting)
|
||||
{
|
||||
// http://developerweb.net/viewtopic.php?id=3196
|
||||
fd_set localSet;
|
||||
FD_ZERO(&localSet);
|
||||
FD_SET(handle, &localSet);
|
||||
|
||||
timeval tv;
|
||||
tv.tv_sec = static_cast<long>(msTimeout / 1000ULL);
|
||||
tv.tv_usec = static_cast<long>((msTimeout % 1000ULL) * 1000ULL);
|
||||
|
||||
int ret = select(0, nullptr, &localSet, &localSet, (msTimeout > 0) ? &tv : nullptr);
|
||||
if (ret > 0)
|
||||
{
|
||||
int code = GetLastErrorCode(handle, error);
|
||||
if (code < 0) //< GetLastErrorCode() failed
|
||||
return SocketState_NotConnected;
|
||||
|
||||
if (code)
|
||||
{
|
||||
if (error)
|
||||
*error = TranslateWSAErrorToSocketError(code);
|
||||
|
||||
return SocketState_NotConnected;
|
||||
}
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
if (error)
|
||||
*error = SocketError_TimedOut;
|
||||
|
||||
return SocketState_NotConnected;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (error)
|
||||
*error = TranslateWSAErrorToSocketError(WSAGetLastError());
|
||||
|
||||
return SocketState_NotConnected;
|
||||
}
|
||||
|
||||
if (error)
|
||||
*error = SocketError_NoError;
|
||||
|
||||
state = SocketState_Connected;
|
||||
}
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
bool SocketImpl::Initialize()
|
||||
{
|
||||
int errorCode = WSAStartup(MAKEWORD(2, 2), &s_WSA);
|
||||
@@ -502,6 +449,58 @@ namespace Nz
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
SocketState SocketImpl::PollConnection(SocketHandle handle, const IpAddress& address, UInt64 msTimeout, SocketError* error)
|
||||
{
|
||||
// http://developerweb.net/viewtopic.php?id=3196
|
||||
fd_set localSet;
|
||||
FD_ZERO(&localSet);
|
||||
FD_SET(handle, &localSet);
|
||||
|
||||
timeval tv;
|
||||
tv.tv_sec = static_cast<long>(msTimeout / 1000ULL);
|
||||
tv.tv_usec = static_cast<long>((msTimeout % 1000ULL) * 1000ULL);
|
||||
|
||||
int ret = ::select(0, nullptr, &localSet, &localSet, (msTimeout != std::numeric_limits<UInt64>::max()) ? &tv : nullptr);
|
||||
if (ret > 0)
|
||||
{
|
||||
int code = GetLastErrorCode(handle, error);
|
||||
if (code < 0) //< GetLastErrorCode() failed
|
||||
return SocketState_NotConnected;
|
||||
|
||||
if (code)
|
||||
{
|
||||
if (error)
|
||||
*error = TranslateWSAErrorToSocketError(code);
|
||||
|
||||
return SocketState_NotConnected;
|
||||
}
|
||||
}
|
||||
else if (ret == 0)
|
||||
{
|
||||
if (error)
|
||||
{
|
||||
if (msTimeout > 0)
|
||||
*error = SocketError_TimedOut;
|
||||
else
|
||||
*error = SocketError_NoError;
|
||||
}
|
||||
|
||||
return SocketState_Connecting;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (error)
|
||||
*error = TranslateWSAErrorToSocketError(WSAGetLastError());
|
||||
|
||||
return SocketState_NotConnected;
|
||||
}
|
||||
|
||||
if (error)
|
||||
*error = SocketError_NoError;
|
||||
|
||||
return SocketState_Connected;
|
||||
}
|
||||
|
||||
bool SocketImpl::Receive(SocketHandle handle, void* buffer, int length, int* read, SocketError* error)
|
||||
{
|
||||
|
||||
@@ -40,7 +40,6 @@ namespace Nz
|
||||
static void Close(SocketHandle handle);
|
||||
|
||||
static SocketState Connect(SocketHandle handle, const IpAddress& address, SocketError* error);
|
||||
static SocketState Connect(SocketHandle handle, const IpAddress& address, UInt64 msTimeout, SocketError* error);
|
||||
|
||||
static bool Initialize();
|
||||
|
||||
@@ -61,6 +60,7 @@ namespace Nz
|
||||
static std::size_t QuerySendBufferSize(SocketHandle handle, SocketError* error = nullptr);
|
||||
|
||||
static unsigned int Poll(PollSocket* fdarray, std::size_t nfds, int timeout, SocketError* error);
|
||||
static SocketState PollConnection(SocketHandle handle, const IpAddress& address, UInt64 msTimeout, SocketError* error);
|
||||
|
||||
static bool Receive(SocketHandle handle, void* buffer, int length, int* read, SocketError* error);
|
||||
static bool ReceiveFrom(SocketHandle handle, void* buffer, int length, IpAddress* from, int* read, SocketError* error);
|
||||
|
||||
Reference in New Issue
Block a user