www.gusucode.com > VC++编写的SQL服务端和客户端源码程序 > VC++编写的SQL服务端和客户端源码程序\code\Server\ClientSocket.cpp

    // ClientSocket.cpp : implementation file
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "miniSQL.h"
#include "ServerDoc.h"
#include "ClientSocket.h"
#include "Msg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CClientSocket

CClientSocket::CClientSocket( CServerDoc* pDoc )
{
	m_pDoc = pDoc;
	m_pFile = NULL;
	m_pArchiveIn = NULL;
	m_pArchiveOut = NULL;

	m_name = _T("");
	m_type = 0;
}

CClientSocket::~CClientSocket()
{
	if( m_pArchiveIn )
		delete m_pArchiveIn;
	if( m_pArchiveOut )
		delete m_pArchiveOut;
	if( m_pFile )
		delete m_pFile;
}


// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CClientSocket, CSocket)
	//{{AFX_MSG_MAP(CClientSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CClientSocket member functions

void CClientSocket::Init()
{
	m_pFile = new CSocketFile( this );
	m_pArchiveIn = new CArchive( m_pFile, CArchive::load );
	m_pArchiveOut = new CArchive( m_pFile, CArchive::store );
}

void CClientSocket::Abort()
{
	if( m_pArchiveOut )
	{
		m_pArchiveOut->Abort();
		delete m_pArchiveOut;
		m_pArchiveOut = NULL;
	}
}

void CClientSocket::SendMsg(CMsg &msg)
{
	if( m_pArchiveOut )
	{
		msg.Serialize( *m_pArchiveOut );
		m_pArchiveOut->Flush();
	}
}

void CClientSocket::ReceiveMsg(CMsg &msg)
{
	msg.m_msgList.RemoveAll();
	msg.Serialize( *m_pArchiveIn );
}

/////////////////////////////////////////////////////////////////////////////
// CClientSocket Overridable callbacks

void CClientSocket::OnReceive(int nErrorCode) 
{	
	CSocket::OnReceive(nErrorCode);

	m_pDoc->ProcessPendingRead( this );
}

BOOL CClientSocket::IsAborted()
{
	return m_pArchiveOut == NULL;
}

BOOL CClientSocket::HasConnectionDropped()
{
	BOOL	bConnDropped = FALSE;
	INT		iRet = 0;
	BOOL	bOK = TRUE;
	
	struct	timeval timeout = { 0, 0 };
	fd_set	readSocketSet;
	
	FD_ZERO( &readSocketSet );
	FD_SET( m_hSocket, &readSocketSet );
	
	iRet = ::select( 0, &readSocketSet, NULL, NULL, &timeout );
	bOK = ( iRet > 0 );
	
	if( bOK )
	{
		bOK = FD_ISSET( m_hSocket, &readSocketSet );
	}
	
	if( bOK )
	{
		CHAR szBuffer[1] = "";
		iRet = ::recv( m_hSocket, szBuffer, 1, MSG_PEEK );
		bOK = ( iRet > 0 );
		if( !bOK )
		{
			INT iError = ::WSAGetLastError();
			bConnDropped = ( ( iError == WSAENETRESET ) ||
				( iError == WSAECONNABORTED ) ||
				( iError == WSAECONNRESET ) ||
				( iError == WSAEINVAL ) ||
				( iRet == 0 ) ); //Graceful disconnect from other side.
		}
	}
	
    return( bConnDropped );
}


#ifdef _DEBUG
void CClientSocket::AssertValid() const
{
	CSocket::AssertValid();
}

void CClientSocket::Dump(CDumpContext& dc) const
{
	CSocket::Dump(dc);
}
#endif //_DEBUG

IMPLEMENT_DYNAMIC(CClientSocket, CSocket)