/*  $Id: event_trigger_socket.cpp,v 1.8 2003/11/11 10:13:25 grumbel Exp $
**
**  ClanLib Game SDK
**  Copyright (C) 2003  The ClanLib Team
**  For a total list of contributers see the file CREDITS.
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
**  Lesser General Public License for more details.
**
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
**
*/

#ifdef _MSC_VER
#pragma warning (disable:4786)
#endif

#include "event_trigger_socket.h"
#include "socket_generic.h"
#include "API/Core/System/cl_assert.h"

#ifdef WIN32
#include "Core/System/Win32/event_trigger_win32.h"
#endif


/////////////////////////////////////////////////////////////////////////////
// Construction:

CL_EventTrigger_Socket::CL_EventTrigger_Socket(CL_Socket_Generic *sock, Type type)
: sock(sock), type(type)
{
	// Someone forgot to call CL_SetupNetwork::init().
	cl_assert(socket_select != 0);

	start_listen();
}

CL_EventTrigger_Socket::~CL_EventTrigger_Socket()
{
	switch (type)
	{
	case read:      socket_select->remove_read(this); break;
	case write:     socket_select->remove_write(this); break;
	case exception: socket_select->remove_exception(this); break;
	}
}

/////////////////////////////////////////////////////////////////////////////
// Attributes:

/////////////////////////////////////////////////////////////////////////////
// Operations:

/////////////////////////////////////////////////////////////////////////////
// Implementation:

void CL_EventTrigger_Socket::start_listen()
{
	reset();
	switch (type)
	{
	case read:      socket_select->listen_read(this); break;
	case write:     socket_select->listen_write(this); break;
	case exception: socket_select->listen_exception(this); break;
	}
}

CL_SocketSelect *CL_EventTrigger_Socket::socket_select = 0;
