// MySocket.cpp : implementation file
//

#include "stdafx.h"
#include "MySocket.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMySocket

CMySocket::CMySocket()
{
	// Initialize our window handle member
	m_Hwnd = NULL;
}

CMySocket::~CMySocket()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CMySocket member functions

//---------------------------------------------------------------------------
//
// OnAccept
//
// Description:
//
//    Standard CAsyncSocket::OnAccept override setup by ClassWizard.
//    This method is called by the framework to let a listening socket
//    know that it can accept a pending connection request.
//
// Parameters:
//
//    nErrorCode - May indicate a network subsystem failure.  See
//                 CAsyncSocket::OnAccept in MSDN for more details.
//
// Return Value:
//
// Remarks:
//
//    If this objects m_Hwnd handle represents a valid window, the
//    WM_MYSOCKET_ONACCEPT message and nErrorCode are relayed to it.
//
//---------------------------------------------------------------------------
void CMySocket::OnAccept(int nErrorCode) 
{
	TRACE("CMySocket::OnAccept(%d)\n", nErrorCode);
	
	if (::IsWindow(m_Hwnd))
	{
		TRACE("   ::PostMessage(%#08x, WM_MYSOCKET_ONACCEPT, (WPARAM)%d, 0)\n", m_Hwnd, nErrorCode);
		::PostMessage(m_Hwnd, WM_MYSOCKET_ONACCEPT, (WPARAM)nErrorCode, 0);
	}

	CAsyncSocket::OnAccept(nErrorCode);
}

//---------------------------------------------------------------------------
//
// OnConnect
//
// Description:
//
//    Standard CAsyncSocket::OnConnect override setup by ClassWizard.
//    This method is called by the framework to indicate a connection
//    request has completed.
//
// Parameters:
//
//    nErrorCode - Value indicating whether or not this socket operation
//                 succeeded, or resulted in some error condition.  See
//                 CAsyncSocket::OnConnect in MSDN for more details.
//
// Return Value:
//
// Remarks:
//
//    If this objects m_Hwnd handle represents a valid window, the
//    WM_MYSOCKET_ONCONNECT message and nErrorCode are relayed to it.
//
//---------------------------------------------------------------------------
void CMySocket::OnConnect(int nErrorCode) 
{
	TRACE("CMySocket::OnConnect(%d)\n", nErrorCode);

	// Verify the window is valid
	if (::IsWindow(m_Hwnd))
	{
		// Notify the window that a connection request is complete
		TRACE("   ::PostMessage(%#08x, WM_MYSOCKET_ONCONNECT, (WPARAM)%d, 0)\n", m_Hwnd, nErrorCode);
		::PostMessage(m_Hwnd, WM_MYSOCKET_ONCONNECT, (WPARAM)nErrorCode, 0);
	}

	CAsyncSocket::OnConnect(nErrorCode);
}

//---------------------------------------------------------------------------
//
// OnReceive
//
// Description:
//
//    Standard CAsyncSocket::OnReceive override setup by ClassWizard.
//    This method is called by the framework to indicate data has
//    received and maybe read by calling the Received method.
//
// Parameters:
//
//    nErrorCode - Value indicating whether or not this socket operation
//                 succeeded, or resulted in some error condition.  See
//                 CAsyncSocket::OnReceive in MSDN for more details.
//
// Return Value:
//
// Remarks:
//
//    If this objects m_Hwnd handle represents a valid window, the
//    WM_MYSOCKET_ONRECEIVE message and nErrorCode are relayed to it.
//
//---------------------------------------------------------------------------
void CMySocket::OnReceive(int nErrorCode) 
{
	TRACE("CMySocket::OnReceive(%d)\n", nErrorCode);

	// Verify the window is valid
	if (::IsWindow(m_Hwnd))
	{
		// Notify the window that data has been received
		TRACE("   ::PostMessage(%#08x, WM_MYSOCKET_ONRECEIVE, (WPARAM)%d, 0)\n", m_Hwnd, nErrorCode);
		::PostMessage(m_Hwnd, WM_MYSOCKET_ONRECEIVE, (WPARAM)nErrorCode, 0);
	}

	CAsyncSocket::OnReceive(nErrorCode);
}

//---------------------------------------------------------------------------
//
// OnClose
//
// Description:
//
//    Standard CAsyncSocket::OnClose override setup by ClassWizard.
//    This method is called by the framework to notify the socket
//    that its connection has been closed.
//
// Parameters:
//
//    nErrorCode - Value indicating whether or not this socket operation
//                 succeeded, or resulted in some error condition.  See
//                 CAsyncSocket::OnClose in MSDN for more details.
//
// Return Value:
//
// Remarks:
//
//    If this objects m_Hwnd handle represents a valid window, the
//    WM_MYSOCKET_ONCLOSE message and nErrorCode are relayed to it.
//
//---------------------------------------------------------------------------
void CMySocket::OnClose(int nErrorCode) 
{
	TRACE("CMySocket::OnClose(%d)\n", nErrorCode);

	if (::IsWindow(m_Hwnd))
	{
		TRACE("   ::PostMessage(%#08x, WM_MYSOCKET_ONCLOSE, (WPARAM)%d, 0)\n", m_Hwnd, nErrorCode);
		::PostMessage(m_Hwnd, WM_MYSOCKET_ONCLOSE, (WPARAM)nErrorCode, 0);
	}

	CAsyncSocket::OnClose(nErrorCode);
}
