// WSTCPS.cpp : implementation file
//

#include "stdafx.h"
#include "GCKMS.h"
#include "GCKMSDlg.h"
#include "WSTCPS.h"

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

/////////////////////////////////////////////////////////////////////////////
// CWSTCPS

CWSTCPS::CWSTCPS()
{
}

CWSTCPS::~CWSTCPS()
{
}


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

/////////////////////////////////////////////////////////////////////////////
// CWSTCPS member functions

void CWSTCPS::OnAccept(int nErrorCode) 
{
	((CGCKMSDlg*)AfxGetMainWnd())->OnTCPS_Accept(nErrorCode);
	CAsyncSocket::OnAccept(nErrorCode);
}

void CWSTCPS::OnReceive(int nErrorCode) 
{
	((CGCKMSDlg*)AfxGetMainWnd())->OnTCPS_Receive(nErrorCode, this);
	CAsyncSocket::OnReceive(nErrorCode);
}

void CWSTCPS::OnClose(int nErrorCode) 
{
	((CGCKMSDlg*)AfxGetMainWnd())->OnTCPS_Close(nErrorCode, this);
	CAsyncSocket::OnClose(nErrorCode);
}

void CWSTCPS::OnSend(int nErrorCode) 
{
	if(nErrorCode == 0)
	{
		if(m_dta.empty() == FALSE)
		{
			int i;
			int ns;
			char *dta;
			dta = new char[m_dta.size()];
			for(i = 0; i < m_dta.size(); i ++)
			{
				dta[i] = m_dta[i];
			}
			ns = m_dta.size();
			m_dta.clear();
			Send(dta, ns);
			delete [] dta;
		}
	}
	CAsyncSocket::OnSend(nErrorCode);
}

int CWSTCPS::Send(const void* lpBuf, int nBufLen, int nFlags) 
{
	int i;
	int ns;
	char *dta;
	dta = new char[nBufLen];
	memcpy(dta, lpBuf, nBufLen);
	for(i = 0; i < nBufLen; i ++)
	{
		m_dta.push_back(dta[i]);
	}
	delete [] dta;
	ns = CAsyncSocket::Send(lpBuf, nBufLen, nFlags);
	if(ns != SOCKET_ERROR)
	{
		if(m_dta.empty() == FALSE)
		{
			m_dta.clear();
		}
	}
	return ns;
}
