#include "StdAfx.h"
#include "ConnectionThread.h"

#define	CLASS_NAME	_U8("ConnectionThread")

CConnectionThread::CConnectionThread(void)
{
}

CConnectionThread::~CConnectionThread(void)
{
	Stop();
}


/////////////////////////////////////////////////////////////////////////////////////
//	操作
/////////////////////////////////////////////////////////////////////////////////////
/*!
	開始
*/
int CConnectionThread::Start(	IServerToolPtr				serverTool,
								IConnectionThreadParentPtr	threadManager,
								IConetextGenePtr			contexGene,
								IClientConnectionAutoPtr	connection,
								LONG						connectionID)
{
	m_serverTool = serverTool;
	m_threadManager = threadManager;
	m_contexGene = contexGene;
	m_connection = connection;
	m_connectionID = connectionID;

	return StartThread();
}

/*!
	停止
*/
void CConnectionThread::Stop()
{
	EndThread();
}

/////////////////////////////////////////////////////////////////////////////////////
//	スレッド関数
/////////////////////////////////////////////////////////////////////////////////////
/*!
	スレッド
*/
void CConnectionThread::ThreadMain()
{
	//	接続初期化
	m_connection->AttacheToThread(m_serverTool, GetIConnectionThreadStatus());
/*
	try
	{
		//	KEEP-ALIVEループ
		int	waitTime = 0;
		do
		{
			//	リクエスト処理
			waitTime = OneRequest();
		}
		while(waitTime > 0 && m_connection->WaitNextRequest(waitTime));
	}
	catch(CServerResponseException &err)
	{
		m_serverTool->WriteSystemLog(CLASS_NAME, SYSTEM_LOG_ERROR, "内部エラー：リクエスト処理で不正な例外が発生しました(CServerResponseException): " + err.GetStatus());
	}
	catch(CServerErrorExceptionBase &err)
	{
		m_serverTool->WriteSystemLog(CLASS_NAME, SYSTEM_LOG_ERROR, "内部エラー：リクエスト処理で不正な例外が発生しました(CServerErrorExceptionBase): " + err.GetErrorInfo());
	}
*/
	TRACE_U8(_U16("New Connection \n"));

	//	接続停止
	m_connection->Close();
	m_connection.Delete();

	//	終了を通知
	m_threadManager->EndConnection(this);
}
