BOOL CBlockingSocket::Accept(CBlockingSocket& sConnect, LPSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
ASSERT(sConnect.m_hSocket == NULL);
int nLengthAddr = sizeof(SOCKADDR);
sConnect.m_hSocket = accept(m_hSocket, psa, &nLengthAddr);
if(sConnect == INVALID_SOCKET) {
// no exception if the listen was canceled
if(WSAGetLastError() != WSAEINTR) {
throw new CBlockingSocketException("Accept");
}
return FALSE;
}
return TRUE;
}
void CBlockingSocket::Close()
{
if (NULL == m_hSocket)
return;
if(closesocket(m_hSocket) == SOCKET_ERROR) {
// should be OK to close if closed already
throw new CBlockingSocketException("Close");
}
m_hSocket = NULL;
}
void CBlockingSocket::Connect(LPCSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
// should timeout by itself
if(connect(m_hSocket, psa, sizeof(SOCKADDR)) == SOCKET_ERROR) {
throw new CBlockingSocketException("Connect");
}
}
int CBlockingSocket::Write(const char* pch, const int nSize, const int nSecs)
{
int nBytesSent = 0;
int nBytesThisTime;
const char* pch1 = pch;
do {
nBytesThisTime = Send(pch1, nSize - nBytesSent, nSecs);
nBytesSent += nBytesThisTime;
pch1 += nBytesThisTime;
} while(nBytesSent < nSize);
return nBytesSent;
}
int CBlockingSocket::Send(const char* pch, const int nSize, const int nSecs)
{
ASSERT(m_hSocket != NULL);
// returned value will be less than nSize if client cancels the reading
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {nSecs, 0};
if(select(0, NULL, &fd, NULL, &tv) == 0) {
throw new CBlockingSocketException("Send timeout");
}
int nBytesSent;
if((nBytesSent = send(m_hSocket, pch, nSize, 0)) == SOCKET_ERROR) {
throw new CBlockingSocketException("Send");
}
return nBytesSent;
}
int CBlockingSocket::Receive(char* pch, const int nSize, const int nSecs)
{
ASSERT(m_hSocket != NULL);
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {nSecs, 0};
if(select(0, &fd, NULL, NULL, &tv) == 0) {
throw new CBlockingSocketException("Receive timeout");
}
int nBytesReceived;
if((nBytesReceived = recv(m_hSocket, pch, nSize, 0)) == SOCKET_ERROR) {
throw new CBlockingSocketException("Receive");
}
return nBytesReceived;
}
int CBlockingSocket::ReceiveDatagram(char* pch, const int nSize, LPSOCKADDR psa, const int nSecs)
{
ASSERT(m_hSocket != NULL);
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {nSecs, 0};
if(select(0, &fd, NULL, NULL, &tv) == 0) {
throw new CBlockingSocketException("Receive timeout");
}
// input buffer should be big enough for the entire datagram
int nFromSize = sizeof(SOCKADDR);
int nBytesReceived = recvfrom(m_hSocket, pch, nSize, 0, psa, &nFromSize);
if(nBytesReceived == SOCKET_ERROR) {
throw new CBlockingSocketException("ReceiveDatagram");
}
return nBytesReceived;
}
int CBlockingSocket::SendDatagram(const char* pch, const int nSize, LPCSOCKADDR psa, const int nSecs)
{
ASSERT(m_hSocket != NULL);
FD_SET fd = {1, m_hSocket};
TIMEVAL tv = {nSecs, 0};
if(select(0, NULL, &fd, NULL, &tv) == 0) {
throw new CBlockingSocketException("Send timeout");
}
int nBytesSent = sendto(m_hSocket, pch, nSize, 0, psa, sizeof(SOCKADDR));
if(nBytesSent == SOCKET_ERROR) {
throw new CBlockingSocketException("SendDatagram");
}
return nBytesSent;
}
void CBlockingSocket::GetPeerAddr(LPSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
// gets the address of the socket at the other end
int nLengthAddr = sizeof(SOCKADDR);
if(getpeername(m_hSocket, psa, &nLengthAddr) == SOCKET_ERROR) {
throw new CBlockingSocketException("GetPeerName");
}
}
void CBlockingSocket::GetSockAddr(LPSOCKADDR psa)
{
ASSERT(m_hSocket != NULL);
// gets the address of the socket at this end
int nLengthAddr = sizeof(SOCKADDR);
if(getsockname(m_hSocket, psa, &nLengthAddr) == SOCKET_ERROR) {
throw new CBlockingSocketException("GetSockName");
}
}
// member functions truly block and must not be used in UI threads
// use this class as an alternative to the MFC CSocket class
class CBlockingSocket : public CObject
{
DECLARE_DYNAMIC(CBlockingSocket)
public:
SOCKET m_hSocket;
CBlockingSocket() { m_hSocket = NULL; }
void Cleanup();
void Create(int nType = SOCK_STREAM);
void Close();
void Bind(LPCSOCKADDR psa);
void Listen();
void Connect(LPCSOCKADDR psa);
BOOL Accept(CBlockingSocket& s, LPSOCKADDR psa);
int Send(const char* pch, const int nSize, const int nSecs);
int Write(const char* pch, const int nSize, const int nSecs);
int Receive(char* pch, const int nSize, const int nSecs);
int SendDatagram(const char* pch, const int nSize, LPCSOCKADDR psa,
const int nSecs);
int ReceiveDatagram(char* pch, const int nSize, LPSOCKADDR psa,
const int nSecs);
void GetPeerAddr(LPSOCKADDR psa);
void GetSockAddr(LPSOCKADDR psa);
static CSockAddr GetHostByName(const char* pchName,
const USHORT ushPort = 0);
static const char* GetHostByAddr(LPCSOCKADDR psa);
operator SOCKET()
{ return m_hSocket; }
};