📄 timeoutsocket.cpp
字号:
//////////////////////////////////////////////////////////////////////
// FileFury
// Copyright (c) 2000 Tenebril Incorporated
// All rights reserved.
//
// This source code is governed by the Tenebril open source
// license (http://www.tenebril.com/developers/opensource/license.html)
//
// For more information on this and other open source applications,
// visit the Tenebril OpenSource page:
// http://www.tenebril.com/developers/opensource
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TimeoutSocket.h"
#ifdef TIMEOUTSOCKET_USING
BOOL CTimeoutSocket::ConnectHelper(const SOCKADDR* lpSockAddr, int nSockAddrLen)
{
int lasterror;
if (m_pbBlocking != NULL)
{
WSASetLastError(WSAEINPROGRESS);
return FALSE;
}
m_nConnectError = -1;
if (!CAsyncSocket::ConnectHelper(lpSockAddr, nSockAddrLen))
{
lasterror = GetLastError();
if (lasterror == WSAEWOULDBLOCK)
{
CTime curt, st;
CTimeSpan span(0, 0, 0, Timeout);
st = CTime().GetCurrentTime();
while (PumpMessages(FD_CONNECT))
{
if (m_nConnectError != -1)
{
WSASetLastError(m_nConnectError);
return (m_nConnectError == 0);
}
curt = CTime().GetCurrentTime();
if(curt > (st+span))
return FALSE;
}
}
return FALSE;
}
return TRUE;
}
BOOL CTimeoutSocket::OnMessagePending()
{
MSG msg;
if(::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_NOREMOVE))
{
CTime CurTime = CTime().GetCurrentTime();
/* We must check for time overflows; if our message isn't the one
caught by PeekMessage, we don't want to miss */
if(msg.wParam == (UINT)m_nTimerID || CurTime > (StartTime + TimeSpan))
{
/* Remove the message and break the call */
::PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE);
CancelBlockingCall();
return FALSE;
}
}
return CSocket::OnMessagePending();
}
BOOL CTimeoutSocket::SetTimeout(UINT uTimeOut)
{
KillTimeout();
m_nTimerID = SetTimer(NULL, 0, uTimeOut, NULL);
Timeout = (int)(uTimeOut / 1000);
StartTime = CTime::GetCurrentTime();
TimeSpan = CTimeSpan(0, 0, 0, Timeout);
return (m_nTimerID > 0);
}
BOOL CTimeoutSocket::KillTimeout()
{
BOOL res;
if(m_nTimerID < 1)
return FALSE;
res = KillTimer(NULL, m_nTimerID);
m_nTimerID = 0;
return res;
}
#else
BOOL CTimeoutSocket::SetTimeout(UINT uTimeOut)
{
return FALSE;
}
BOOL CTimeoutSocket::KillTimeout()
{
return FALSE;
}
#endif
int CTimeoutSocket::ReceiveFull(void *lpBuf, int nBufLen, int nFlags)
{
char *bBuf = (char *)lpBuf;
int nReceived = 0, nChunk;
while(nReceived < nBufLen)
{
SetTimeout(5000); // Five seconds.
nChunk = Receive((void *)bBuf, nBufLen - nReceived, nFlags);
// Pull out if the line is silent.
if(nChunk == 0)
return nReceived;
bBuf += nChunk;
nReceived += nChunk;
}
return nReceived;
}
BOOL CTimeoutSocket::Connect(LPCTSTR lpszHostAddress, UINT nHostPort)
{
KillTimeout();
SetTimeout(5000); // Five seconds.
return CSocket::Connect(lpszHostAddress, nHostPort);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -