#include "StdAfx.h"
#include "sock_sv.h"
#include "proceso_cliente.h"
#include "utiles_dll.h"
//********************************************************************
Csock_sv::Csock_sv(BOOL*sal/*=NULL*/, C_error*e/*=NULL*/ )
:Csock_cl(sal,e)
{
	sock_cerrado=FALSE;
	crea_cliente=NULL;
	escucha=NULL;
}
//********************************************************************
Csock_sv::~Csock_sv(void)
{

}
//********************************************************************
void Csock_sv::OnAccept( int nErrorCode )
{
	int k;
	BOOL del_soc;
	SOCKADDR so;
	if(pirate && *pirate)
		goto salir;
	Cproceso_cliente* cli=NULL;
	if (escucha)
		cli=escucha->crea_cliente(pirate);
	else if (crea_cliente)
	{
		cli=crea_cliente(pirate);
	}
	
	if (!cli)
		goto salir;
	del_soc=cli->soc==NULL;
	if(del_soc)
		cli->soc = new Csock_sv(pirate);

	k = sizeof(SOCKADDR);
	if (!Accept( *cli->soc, &so,&k))
	{
		if(del_soc)
			delete cli->soc;
		delete cli;
		goto salir;
	}
	//if (strcmp(ss.logp,"DATOS")== 0 && ss.puerto_escucha==50056)
	//pso->ss.nosalir=1;

	//pso->GetPeerName(cstr, port);
	// proscrito, se hace en thread para no usar tiempo de mainthread
	//strcpy(pso->ss.ipdot,cstr);
	//pso->ss.puerto_llama = port;
	//pso->ss.idu_usr = -1;
	//ss.time = (UINT) time(NULL);
	//pso->ss.time = ss.time;

	// crea thread para atender la consulta
	cli->lanza("Proceso cliente");
salir:
	CAsyncSocket::OnAccept(nErrorCode);
}
//********************************************************************
void Csock_sv::OnClose(int nErrorCode)
{
	sock_cerrado = TRUE;
	CAsyncSocket::OnClose(nErrorCode);
}
//********************************************************************
//********************************************************************
//********************************************************************
//********************************************************************

BOOL Csock_svThread::InitInstance()
{
	if(!CWinThread::InitInstance())
		AfxSocketInit();
	return TRUE;
}
//********************************************************************
int Csock_svThread::Run()
{
	Csock_sv *sc = NULL;
	sc = new Csock_sv();
	if(!sc)
		goto va_mal;
	sc->escucha = escucha;
	sc->crea_cliente = crea_cliente;

	if (!sc->Create(port,
		type,evenFlag, 
		url))
	{
		goto va_mal;
	}
	if(!sc->Listen())
		goto va_mal;
	isLisening = TRUE;
	// Run message loop.
	BOOL bRes = FALSE;
	MSG msg;
	while (!pireateTread)
	{
		while ( PeekMessage (&msg,NULL,NULL,NULL,
			PM_NOREMOVE) )
		{
			PeekMessage (&msg,NULL,NULL,NULL,
				PM_REMOVE);
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		Sleep(100);
	}
va_mal:
	if(sc)
	{
		sc->Close();
		delete sc;
	}
	pireateTread = TRUE;
	isLisening = FALSE;
	
	return (0) ;
}
//********************************************************************
BOOL Csock_svThread::liseningThread( int port, char * url, int type, int evenFlag )
{
	//AFX_MANAGE_STATE(AfxGetStaticModuleState());
	Cutiles_dll dll;
	this->port =port;
	this->type=type;
	this->evenFlag = evenFlag;
	this->url =url;
	isLisening = FALSE;
	pireateTread = FALSE;

	CreateThread(CREATE_SUSPENDED) ;
	m_bAutoDelete = FALSE ; // Let me delete it.
	sName = _T("Test") ; // Initialize something
	ResumeThread() ;
	while(!pireateTread)
	{
		if(isLisening)
			return TRUE;
		Sleep(100);
	}
	return FALSE;
}
//********************************************************************
Csock_svThread::Csock_svThread()
{
	escucha = NULL;
	crea_cliente =NULL;
	isLisening = FALSE;
	pireateTread = TRUE;
}
//********************************************************************
void Csock_svThread::close()
{
	pireateTread = TRUE;
	while(isLisening)
	{
		Sleep(10);
	}
}
//********************************************************************
Csock_svThread::~Csock_svThread()
{
	close();
	WaitForSingleObject(m_hThread, INFINITE);
}
//********************************************************************